How does your EA's algorithm work?

In this chapter we will discuss how the algorithm interacts with the other modular parts of your EA and how it will behave depending on its state.

EA overview
How ticks/quotes are processed

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 flexible EA's. The modules are divided into three categories:

  1. Prevent Open
  2. Open
  3. Close

Read more about Modules. Depending on the algorithm's state it 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. No open orders
  2. One or more open orders

No open orders

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

Algorithm - no orders opened
Tick/quote flow when no orders are open

Setup: You've got one Prevent Open module: Maximum spread. The module is set so that it allows 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 Prevent Open module that it can not open any orders at the time, 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 becomes active 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. When SL/TP is hit, or a Close signal becomes active, 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 flow when no orders are opened is evaluated. If an order would 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 becomes active, after being inactive.

Algorithm - no orders opened
Tick/quote flow 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 potentially 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 is currently not sent to the broker, which is why you don't see the SL/TP in MetaTrader, but the SL/TP is there.
  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 and 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 becomes active 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.
A new quote is processed: The Algorithm will read that the order should be closed because the TP was hit. Before closing, it will evaluate if it should close the order, or if a new order would be opened directly after closing, but because the Time gap between orders (prevent open) module would block opening an order after closing the order, the Open Signal is discarded and the 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 yet. 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