How your EA works
This page explains what your exported EA does in MetaTrader: how its parts work together, and what happens on every quote (tick). It describes the code that EA Builder Pro generates, for MT4 and MT5 alike.
In short:
- On every quote, the EA first handles the open orders: the modules for closing set the SL / TP, and an order closes when its SL / TP is hit or a Close signal is true.
- Then it looks at opening: an Open signal opens an order on the quote it becomes true, unless a module which prevents opening blocks it or the maximum number of orders is reached.
- A signal that stays true opens no extra orders. A next order needs the signal to become false and then true again.
Your EA’s modular system
Section titled “Your EA’s modular system”On every quote that MetaTrader sends, your EA runs through two chains, as in the picture:
- Closing (only with open orders): an order closes when its Close Buy or Close Sell signal is true (Close Buy closes the buy orders, Close Sell the sell orders), or when a stop loss or take profit of the modules for closing is hit.
- Opening: the Open Buy or Open Sell signal is the trigger. An order opens on the quote that signal becomes true. The Open order module checks whether the order may open (Maximum open orders, Hedging), the modules for closing set its stop loss and take profit, and the money manager sets its lot size. Then the EA sends the order to MetaTrader.
- Modules which prevent opening can block an Open signal, for example outside a trading session or with a high spread. A blocked signal is not remembered.
You build the four signals from conditions on your indicators: see Signals and conditions. Because all these parts are separate, you can create almost any EA.
Read more about Modules.Your EA’s behaviour
Section titled “Your EA’s behaviour”On every quote, in this order:
- Pending orders: a pending order whose Expire when or Expire after is reached is deleted. See Order type.
- Modules which prevent opening decide whether a new buy order, a new sell order, or no order at all is allowed on this quote.
- Open orders (if there are any): see One or more open orders.
- Opening: see No open orders.
With the input First quote/tick per bar processed only set to true, the EA only does this on the first quote of
each bar.
No open orders
Section titled “No open orders”When no orders are open, all the EA does is wait for an Open signal.
- An Open signal opens an order on the quote it changes from false to true: the first quote on which its conditions are met.
- No order opens when a module which prevents opening blocks it on that quote. The signal is not remembered: the EA waits until the signal has been false for at least one quote and becomes true again.
- For the new order, the modules for closing calculate its SL and TP, and then the money manager calculates its lot size (some money managers, such as Fixed risk, need the SL).
One or more open orders
Section titled “One or more open orders”When orders are open, the EA first checks whether they should be closed:
- Modules for closing set the SL and TP of each order. Each module keeps its own SL and/or TP; the first one that is hit closes the order. A module that closes only a percentage of the order closes that part.
- Close signals: when Close Buy is true, all buy orders close; when Close Sell is true, all sell orders close.
- Where SL / TP are managed: with Expert Advisor, the EA closes the order when the price reaches the SL or TP. With Broker, the SL and TP are sent to the broker, who closes the order; a Close signal is still executed by the EA. Closing a part of an order only works when the EA manages the SL / TP. See Settings.
Then the EA checks whether to open an additional order, the same way as without open orders, plus:
- Maximum open orders caps the number of orders: open and pending orders together.
- Without Hedging, only the direction of the open order is evaluated: with an open buy order, only Open Buy (for an additional buy order). With Hedging, both Open Buy and Open Sell are evaluated. See Open order V3.
- An Open signal that stays true doesn’t open additional orders. An additional order needs the signal to become false and then true again.
Open order V2: closing is blocked by an Open signal
Section titled “Open order V2: closing is blocked by an Open signal”EAs with the legacy Open order V2 module behave differently when an order would close:
- Before closing (by a Close signal or an SL / TP the EA manages), the EA checks whether the Open signal of the same direction is true, taking the modules which prevent opening into account.
- If it is, the order is not closed: closing would open a new order of the same type right away and cost another round of spread and commission. The EA shows the error “Order not closing! Close Signal activated at the same time as Open Signal was activated.”
- The remedy is a module which prevents opening, such as Time gap between orders: it blocks the Open signal after an order opened, so the order can close.
With Open order V3, an order always closes when its SL / TP is hit or a Close signal is true. See Changes compared to the V2 module.
Related
Section titled “Related”- Strategy Modules: all modules and their settings
- Signals and conditions: the Open and Close signals
- Money Management: the lot size of each order
MetaTrader is a registered trademark of MetaQuotes Ltd. EA Builder Pro is not affiliated with MetaQuotes.