Discussion of article "Universal Expert Advisor: Custom Strategies and Auxiliary Trade Classes (Part 3)"

 

New article Universal Expert Advisor: Custom Strategies and Auxiliary Trade Classes (Part 3) has been published:

In this article, we will continue analyzing the algorithms of the CStrategy trading engine. The third part of the series contains the detailed analysis of examples of how to develop specific trading strategies using this approach. Special attention is paid to auxiliary algorithms — Expert Advisor logging system and data access using a conventional indexer (Close[1], Open[0] etc.)

In this part, we continue to discuss the CStrategy trading engine. Here are the brief contents of the previous two parts. In the first article Universal Expert Advisor: Trading Modes of Strategies, we have discussed in detail trading modes that allow configuring Expert Advisor logic in accordance with time and day of the week. In the seconds article Universal Expert Advisors: the Event Model and Trading Strategy Prototype, we have analyzed the event model based on centralized event handling, as well as the main algorithms of the basic CStrategy class underlying custom Expert Advisors.

In the third part of the series, we will describe in detail the examples of Expert Advisors based on the CStrategy trading engine and some auxiliary algorithms that may be needed for the EA development. Particular attention is paid to the logging procedure. Despite its purely supporting role, logging is a very important element of any complex system. A good logger allows you to quickly understand the cause of the problem and find the place where it occurred. This logger is written using a special programming technique called Singleton pattern. Information about it will be interesting not only for those who organize the trading process, but also for those who create algorithms for performing non-standard tasks.

Also, the article describes the algorithms that allow you to access market data through convenient and intuitive indexes. Indeed, data access through indexes like Close[1] and High[0] is a popular feature of MetaTrader 4. So why avoid it, if it can also be used in MetaTrader 5? This article explains how to do this, and describes in detail the algorithms that implement the above idea.

Methods to Be Overridden by the Custom Expert Advisor

In the first article Universal Expert Advisor: Trading Modes of Strategies (Part 1), we have considered in detail trading modes of a strategy and its main methods that need to be overridden. Now, it is time to do some practicing.

Each Expert Advisor created using the CStrategy engine must override virtual methods that are responsible for some properties and behavior of the Expert Advisor. We will list all the methods to be overridden as a table of three columns. The first one contains the name of the virtual method, the second one shows the event or action to be tracked or performed. The third column contains the description of the purpose of using the method. So here is the table:

Virtual method Event/Action Purpose
OnSymbolChanged Called when the name of a trading symbol changes When you change the trading instrument, the EA's indicators should be re-initialized. The event allows performing re-initialization of indicators of the Expert Advisor.
OnTimeframeChanged Change of the working timframe When you change the working timeframe, the EA's indicators should be re-initialized. The event allows performing re-initialization of indicators of the Expert Advisor.
ParseXmlParams Parsing custom parameters of the strategy loaded via n XML file The strategy should recognize XML parameters passed to the method and configure its settings accordingly.
ExpertNameFull Returns the full name of the Expert Advisor The full name of the Expert Advisor consists of the strategy name and, as a rule, a unique set of the strategy parameters. A strategy instance must independently determine its full name. This name is also used in the visual panel, in the drop-down Agent list.
OnTradeTransaction Occurs in case of a trade event Some strategies need to analyze trade events for proper operation. This event allows passing a trade event to the Expert Advisor and analyze it.
InitBuy Initiates a Buy operation One of the basic methods that must be overridden. In this method you should execute a Buy operation if suitable trading conditions have formed.
InitSell Initiates a Sell operation One of the basic methods that must be overridden. In this method you should execute a Sell operation if suitable trading conditions have formed.
SupportBuy Manages a previously opened long position An open long position needs to be managed. For example, you should set Stop Loss or close the position at a position exit signal. All these steps must be performed in this method.
SupportSell Manages a previously opened short position An open short position needs to be managed. For example, you should set Stop Loss or close the position at a position exit signal. All these steps must be performed in this method.

 Table 1. Virtual methods and their purposes

The most important methods that you must override are InitBuy, InitSell, SupportBuy, and SupportSell. They are shown in bold in the table. If you forget to override, for example, InitBuy, the custom strategy will not buy. If you do not override one of the Support methods, an open position can be left open forever. Therefore, when creating an Expert Advisor, be careful overriding these methods.

Author: Vasiliy Sokolov

 
Quite interesting. It is indeed possible to speed up and simplify the development process. You use methods from the standard library to open a position, but what about other standard classes - money management, trailing stop. Is it possible to add them to your engine?
 
hftech:
Quite interesting. It is indeed possible to speed up and simplify the development process. You use methods from the standard library to open a position, but what about other standard classes - money management, trailing stop. Is it possible to add them to your engine?

You can use other standard classes through special adapters. However, the difficulty here is that these classes were designed for MetaTrader 5 and are platform-dependent, while the CStrategy engine is not. The attentive reader has probably already noticed that the engine wraps everything in OOP-layers on purpose, and it is done on purpose.

Now we have a super task - to port CStrategy to MetaTrader 4. Then the strategy written with the help of the engine will be compiled and run in both platforms at the same time.

P.S. In fact, the engine does not use the standard CTrade trading class, but opens positions using a certain CTradeControl, which resembles CTrade very much and even inherits from it, but is not CTrade. In the future it will help to port it to MT4, which will make CStrategy really platform-independent.

 
Vasiliy Sokolov:

You can also use other standard classes through special adapters. However, the difficulty here is that these classes were designed for MetaTrader 5 and are platform-dependent, while the CStrategy engine is not. The attentive reader has probably already noticed that the engine wraps everything in OOP-layers on purpose, and it is done on purpose.

Now we have a super task - to port CStrategy to MetaTrader 4. Then the strategy written with the help of the engine will be compiled and run in both platforms at the same time.

P.S. In fact, the engine does not use the standard CTrade trading class, but opens positions using a certain CTradeControl, which resembles CTrade very much and even inherits from it, but is not CTrade. In the future it will help to port it to MT4, making CStrategy truly platform independent.

You say in the future, how will the updates happen? Will you be posting new versions of the libraries on a regular basis? How sure can you be that today's code won't change, otherwise you download a new version and something will fall off in the current developments.


You can use other standard classes through special adapters.

It would be interesting to read about an example of such an adapter.

P.S. In fact, the engine does not use the standard CTrade trading class, but opens positions using some CTradeControl, which resembles CTrade very much and even inherits from it, but is not it. In the future it will help to port it to MT4, which will make CStrategy truly platform independent.

Yes, I see similar CTrade methods in CTradeCustom, but the standard libraries are still used:

#include <Trade\SymbolInfo.mqh>
#include <Trade\OrderInfo.mqh>
#include <Trade\HistoryOrderInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\DealInfo.mqh>
 

As for platform independence, the concept is certainly interesting, but who really needs it in practice. In my opinion, everyone has long ago made their choice. MT5 is primarily interesting for those who work on stock exchanges. MT4 is perfect for forex and CFD. Usually, those who work on Forex do not work on stock markets and vice versa. Who needs to write one strategy and implement it on both platforms? Only robot sellers. But this is my private opinion, maybe I am wrong.

As someone who works exclusively on MT5, I would be interested in expanding the functionality of the same CTradeControl. For example, to open a position only through limit orders and using interesting approaches, taking into account, for example, current volatility and supply/demand in the stack.

 
hftech:

You say in the future, but how will the updates happen? Will you post new versions of libraries regularly? How can you be sure that today's code will not change, or you will download a new version and something will fall off in the current developments.

There are no guarantees unfortunately. If it is necessary to make a change that significantly improves simplicity/reliability/infrastructure of the engine - such a change will be made, at least at the first stages of forming its code base.

However, you should not worry much about these potential changes, because firstly, most likely, if they will be made, they will be made in the internal structure of the engine, and users will not feel these changes. Secondly, the algorithms that are given are tested by many years of practice and in other platforms. They really work. And so far, I have not invented anything better, and I have tried more than a few: models based on finite automata and other templates, like the one offered by Wealth-Lab.

As for new versions - we'll see. There are a few more aspects that have not been covered (platform independence and work with pending orders). I will find forces to finish the engine, - then there will be a new extended version, and the corresponding documentation for these features in the form of articles.

 
hftech:

As for platform independence, the concept is certainly interesting, but who really needs it in practice. In my opinion, everyone has long ago made their choice. MT5 is primarily interesting for those who work on stock exchanges. MT4 is perfect for forex and CFD. Usually, those who work on Forex do not work on stock markets and vice versa. Who needs to write one strategy and implement it on both platforms? Only robot sellers. But this is my private opinion, maybe I am wrong.

As someone who works exclusively on MT5, I would be interested in expanding the functionality of the same CTradeControl. For example, to open a position only through limit orders and using interesting approaches that take into account, say, current volatility and supply/demand in the stack.

Many in practice have to work with two platforms at once. For example, in MT4 they test on their own data, and in MT5 they trade (Until MT5 has its own data feeds, the situation will not change). And here it is very important to have the same algorithm implementation for both platforms at the same time. Otherwise unpleasant surprises are possible.

It is also very convenient for vendors and freelancers to write for two platforms at the same time without worrying about the notorious incompatibility. By the way, you are wrong to relegate them to the background. Freelancers write a very large amount of code and are very busy. They have no time to develop different concepts and engines. That is why it is important to give them a tool and a clear description of it. For many people, even those who are not familiar with programming, it will make life much easier and help to formulate the TOR more clearly (in the rules of the engine).

 
Vasiliy Sokolov:

Many in practice have to work with two platforms at once. For example, in MT4 they test on their own data, and in MT5 they trade (Until MT5 has its own data feeds, the situation will not change). And here it is very important to have the same algorithm implementation for both platforms at the same time. Otherwise unpleasant surprises are possible.

It is also very convenient for vendors and freelancers to write for two platforms at the same time without worrying about the notorious incompatibility. By the way, you are wrong to relegate them to the background. Freelancers write a very large amount of code and are very busy. They have no time to develop different concepts and engines. That is why it is important to give them a tool and a clear description of it. To many people, even not familiar with programming, it will make life much easier and will help to formulate the TOR (in the rules of the engine) more clearly.

Thanks for the articles, great idea! We are waiting for the continuation.

I think you will soon write additional shell classes for porting Cstrategy to mt4 and a simple line like Trade.Buy(MM.GetLotFixed(), ExpertSymbol(), "");

will compile equally in both platforms.

But so far it turns out that the universal Expert Advisor is platform independent only at the compilation stage?

1) Will it be possible to achieve the same position opening in mt4 tester and mt5 real/demo account? Let it be with the use of your data in mt4 and of course your engine. OHLC between the tester and the real account may be different, so the indicator readings will be different, and therefore the availability of trades.

2) You also need to think about the divergence of the concepts of orders and transactions between platforms. I.e. here it is necessary to exclude the possibility of opening multidirectional positions in mt4 adhering to the logic of mt5.

How will you solve these two issues? Or the end user can cope by himself)?

Thanks

 
Arnold Bobrinskiy:

2) We also need to think about the divergence of the concepts of orders and trades between platforms. I.e. it is necessary to exclude the possibility of opening multidirectional positions in mt4, sticking to the logic of mt5.

I can answer the second question at once: the engine has no restrictions on opening multidirectional positions. On the contrary, it was originally written to manage multidirectional positions. That is, everything will be no less standard in MT4 than in MT5.
 
Vasiliy Sokolov:
I can answer the second question at once: the engine has no restrictions on opening multidirectional positions. On the contrary, it was originally written to manage multidirectional positions. I.e. everything will be no less standard in MT4 than in MT5.
But then how will the Expert Advisor repeat the logic in the same way? For example, in mt5 a buy position is opened with 0.2 lot, after the sell 0.2 command the position should be closed. In mt4 in this case there will be 2 multidirectional positions, while in mt5 there will be no more open positions.
 
Arnold Bobrinskiy:
Then how does the Expert Advisor repeat the logic in the same way? For example, in mt5 a buy position is opened with 0.2 lot, after the sell 0.2 command the position should be closed. In mt4 in this case there will be 2 multidirectional positions, while in mt5 there will be no more open positions.

Simply in MT5 the Expert Advisor will have one and only one position to manage. In MT4 the Expert Advisor will receive several positions sequentially. If you perform a conversion operation in MT5 without checking the current position (buy when there is a sell position and sell when there is a buy position), the position will be closed and in the next call the Expert Advisor will simply not call the position maintenance module.

Top-down compatibility is also obvious: Expert Advisors written for MT5 and trading only one position in one direction will work normally in MT4. Expert Advisors trading several positions at once in MT4 will not be able to work out the same logic in MT5. But if a position is traded only one way, there should be no problems in MT5.