- 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
Creating Expert Advisors
In this chapter, we begin to study the MQL5 trading API used to implement Expert Advisors. This type of program is perhaps the most complex and demanding in terms of error-free coding and the number and variety of technologies involved. In particular, we will need to utilize many of the skills acquired from the previous chapters, ranging from OOP to the applied aspects of working with graphical objects, indicators, symbols, and software environment settings.
Depending on the chosen trading strategy, the Expert Advisor developer may need to pay special attention to the following:
- Decision-making and order-sending speed (for HFT, High-Frequency Trading)
- Selecting the optimal portfolio of instruments based on their correlations and volatility (for cluster trading)
- Dynamically calculating lots and distance between orders (for martingale and grid strategies)
- Analysis of news or external data sources (this will be discussed in the 7th part of the book)
All such features should be optimally applied by the developer to the described trading mechanisms provided by the MQL5 API.
Next, we will consider in detail built-in functions for managing trading activity, the Expert Advisor event model, and specific data structures, and recall the basic principles of interaction between the terminal and the server, as well as the basic concepts for algorithmic trading in MetaTrader 5: order, deal, and position.
At the same time, due to the versatility of the material, many important nuances of Expert Advisor development, such as testing and optimization, are highlighted in the next chapter.
We have previously considered the Design of MQL programs of various types, including Expert Advisors, as well as started Features of starting and stopping programs. Despite the fact that an Expert Advisor is launched on a specific chart, for which a working symbol is defined, there are no obstacles to centrally manage trading of an arbitrary set of financial instruments. Such Expert Advisors are traditionally referred to as multicurrency, although in fact, their portfolio may include CFDs, stocks, commodities, and tickers of other markets.
In Expert Advisors, as well as in indicators, there are Key events OnInit and OnDeinit. They are not mandatory, but, as a rule, they are present in the code for the preparation and regular completion of the program: we used them and will continue using them in the examples. In a separate section, we provided an Overview of all event handling functions: we have already studied some of them in detail by now (for example, OnCalculate indicator events and the OnTimer timer). Expert Advisor-specific events (OnTick, ontrade, OnTradeTransaction) will be described in this chapter.
Expert Advisors can use the widest range of source data as trading signals: quotes, tics, depth of market, trading account history, or indicator readings. In the latter case, the principles of creating indicator instances and reading values from their buffers are no different from those discussed in the chapter Using ready-made indicators from MQL programs. In the Expert Advisor examples in the following sections, we will demonstrate most of these tricks.
It should be noted that trading functions can be used not only in Expert Advisors but also in scripts. We will see examples for both options.