How does your EA's algorithm work?

In this chapter we will discuss how the algorithm works with the modules of your EA and how it will act.

EA overview
How ticks/quotes are processed by your EA

Your EA's modular system

Your EA consists out of modules the algorithm interacts with. It's because of the modulairity that you are able to create any EA. The modules are divided into three categories:

  1. Modules which prevent opening
  2. Modules for opening
  3. Modules for closing

Read more about Modules. As you've seen in the image above, the algorithm will interact with one or more modules. In the next chapter we will go into detail about this.

Your EA's behaviour

Your EA's algorithm will behave differently depending on its state:

  1. There are no open orders
  2. There are one or more open orders

No open orders

When no orders are opened, all the EA does is wait for an Open Signal. When the signal activates, it will open an order, unless a Prevent Open module blocks it.

Algorithm - no orders opened
Tick/quote processing when no orders are open.

Setup: You've got one Prevent Open module: Maximum spread. The module is set to only allow a maximum of 1.3 pips of spread.
Situation: There are no open orders.
A new quote is processed: The current spread on the Pair (f.e. AUDCAD) is 1.5 pips, the algorithm will now read from the Maximum spread module that it is not allowed to open any orders, so it will not evaluate it's Open Signals. Once the spread drops to 1.3 or lower, the Open Signal is evaluated. If the Open Signal activates an order will be opened.

One or more open orders

When orders are open, your EA will evaluate if the open orders should be closed, based on if a SL/TP was hit or a Close signal has activated. When SL/TP is hit, or a Close signal actives, it will first check if a new order would be (hypothetically) opened after closing the current order. This is done by evaluating Open signals (taking into account Modules which prevent opening) and Close signals. So, the same process, as when there are no open orders, is evaluated. If an order would (hypothetically) not be opened, the order will be closed. Also, the EA will evaluate if additional orders have to be opened. Additional orders will only be opened when an Open signal activates, thus after being inactive.

Algorithm - no orders opened
Tick/quote processing when orders are open
  1. An order is closed when its SL or TP is hit, or by a Close Signal. All Modules for closing can influence the orders' SL/TP. SL is set below the order (from a Buy perspective) and can only be moved up by the same, or another, module. The opposite is true for the TP: it can only potentially be moved down. SL/ TP values are currently not sent to the broker.
  2. An order is only closed if the order wouldn't be opened again directly after closing, because if the algorithm would close the order, another order would be opened immediately and you would lose another round-trip of commission/ spread. To validate if this is the case, the Open Signal is re-evaluated before closing, also taking into account Prevent Open modules. F.e., when an order has to be closed seconds after opening, it's very likely the Open Signal is still active. This can be countered by using a Prevent Open module, like the Time gap between orders module. When doing so, the Open Signal will be blocked, after closing the order, so the order can be closed.
  3. When using the Multiple order Open module additional orders are opened when the Open Signal has switched from active (which opened the previous order) to inactive and activates again, now opening an additional order.

Setup: You've got one Take Profit module and one Stop Loss module, both set at 10 pips. You've got one Prevent Open module: Time gap between orders. The module is set so that it allows an order to be opened 60 minutes after a previous order was closed.
Situation: There is one open order. The Open Signal is currently active. The open order's TP is hit.
The Algorithm will read that the order should be closed because the TP was hit. Before closing, it will evaluate if it should indeed close the order, or if a new order would be opened directly after closing. Since the Time gap between orders module would block opening an order after closing the current order, the Open Signal is discarded and the current order will be closed.

Setup: You configured your EA for allowing multiple open orders. You've got one Take Profit module and one Stop Loss module, both set at 10 pips. You've got one Prevent Open module: Time gap between orders. The module is set so that it allows an additional order to be opened 60 minutes after a previous order was opened.
Situation: There is one open order which was opened 30 minutes ago. The Open Signal is currently active.
A new quote is processed: The algorithm will read that the currently opened order should not be closed yet. The Open Signal is active, but it has been active since opening the previous order. Since this is still the same active signal, an additional order will not be opened. The Open Signal deactivates and reactivates, the algorithm will now check if a Prevent Open module prevents opening an additional order. This is the case, because the previous order was opened 30 minutes ago, so an additional order will not be opened yet. When the 60 minutes has passed, the additional order is still not opened, because the current Open signal is considered to be an old Signal. Only when the Signal deactivates and reactivates an addition order will be opened.

Next: Indicators