- Expert Advisors main event: OnTick
- Basic principles and concepts: order, deal, and position
- Types of trading operations
- Order types
- Order execution modes by price and volume
- Pending order expiration dates
- Margin calculation for a future order: OrderCalcMargin
- Estimating the profit of a trading operation: OrderCalcProfit
- MqlTradeRequest structure
- MqlTradeCheckResult structure
- Request validation: OrderCheck
- Request sending result: MqlTradeResult structure
- Sending a trade request: OrderSend and OrderSendAsync
- Buying and selling operations
- Modying Stop Loss and/or Take Profit levels of a position
- Trailing stop
- Closing a position: full and partial
- Closing opposite positions: fill and partial
- Placing a pending order
- Modifying a pending order
- Deleting a pending order
- Getting a list of active orders
- Order properties (active and historical)
- Functions for reading properties of active orders
- Selecting orders by properties
- Getting the list of positions
- Position properties
- Functions for reading position properties
- Deal properties
- Selecting orders and deals from history
- Functions for reading order properties from history
- Functions for reading deal properties from history
- Types of trading transactions
- OnTradeTransaction event
- Synchronous and asynchronous requests
- OnTrade event
- Monitoring trading environment changes
- Creating multi-symbol Expert Advisors
- Limitations and benefits of Expert Advisors
- Creating Expert Advisors in the MQL Wizard
Expert Advisors main event: OnTick
The OnTick event is generated by the terminal for Expert Advisors when a new tick appears containing the price of the current chart's working symbol on which the Expert Advisor is running. To handle this event, the OnTick function must be defined in the Expert Advisor code. It has the following prototype.
void OnTick(void)
As you can see, the function has no parameters. If necessary, the very value of the new price and other tick characteristics should be requested by calling SymbolInfoTick.
From the point of view of the reaction to the new tick event, this handler is similar to OnCalculate in indicators. However, OnCalculate can only be defined in indicators, and OnTick only in Expert Advisors (to be more precise, the OnTick function in the code of an indicator, script, or service will be simply ignored).
At the same time, the Expert Advisor does not have to contain the OnTick handler. In addition to this event, Expert Advisors can process the OnTimer, OnBookEvent, and OnChartEvent events and perform all necessary trading operations from them.
All events in Expert Advisors are processed one after the other in the order they arrive, since Expert Advisors, like all other MQL programs, are single-threaded. If there is already an OnTick event in the queue or such an event is being processed, then new OnTick events are not queued.
An OnTick event is generated regardless of whether automatic trading is disabled or enabled (Algo trading button in the terminal interface). Disabled automatic trading means only restriction on sending trade requests from the Expert Advisors but does not prevent the Expert Advisor from running.
It should be remembered that tick events are generated only for one symbol, which is the symbol of the current chart. If the Expert Advisor is multicurrency, then getting ticks from other symbols should be organized in some alternative way, for example, using a spy indicator EventTickSpy.mq5 or subscription to market book events, as in MarketBookQuasiTicks.mq5.
As a simple example, consider the Expert Advisor ExpertEvents.mq5. It defines handlers for all events that are usually used to launch trading algorithms. We will study some other events (OnTrade, OnTradeTransaction, as well as tester events) later.
All handlers call the display helper function which outputs the current time (millisecond system counter label) and handler name in a multi-line comment.
#define N_LINES 25
|
The OnTick event will be called automatically upon the arrival of new ticks. For timer and order book events, you need to activate the corresponding handlers using EventSetTimer and MarketBookAdd calls from OnInit.
void OnInit()
|
The chart change event is also available: it can be used to trade on markup based on graphical objects, by pressing buttons or hotkeys, as well as upon the arrival of custom events from other programs, for example, indicators like EventTickSpy.mq5.
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
|
The following screenshot shows the result of the Expert Advisor operation on the chart.
Comments with events of various types in the Expert Advisor
Please note that the OnBookEvent event (if it is broadcast for a symbol) arrives more often than OnTick.