Discussion of article "Universal Expert Advisor: the Event Model and Trading Strategy Prototype (Part 2)"

 

New article Universal Expert Advisor: the Event Model and Trading Strategy Prototype (Part 2) has been published:

This article continues the series of publications on a universal Expert Advisor model. This part describes in detail the original event model based on centralized data processing, and considers the structure of the CStrategy base class of the engine.

The article contains further description of the universal trading engine CStrategy. In the first article Universal Expert Advisor: Trading Modes of Strategies (Part 1), we have discussed in detail trading modes and functions that allow implementing them. We have analyzed a universal Expert Advisor scheme consisting of four methods, two of which open new positions and the other two methods close them. Different combinations of method calls define a particular trading mode. For example, an Expert Advisor can be allowed only to Sell or to Buy, can manage previously opened positions or wait. Using these modes, an Expert Advisor operation can be flexibly set up depending on the trading time or day of the week.

However, the trading modes are not the only thing that an Expert Advisor may need. In the second part we will discuss the event model of the CStrategy trading mode based on the centralized event handling. The proposed event handling scheme differs from the system events in that all the events are gathered in one place. The advantages of such an implementation will be considered later.

Also, this article describes two important classes of the trading engine — CStrategy and CPosition. The first one is the core of the whole EA trading logic, it unites events and modes into a single flexible framework that the custom EA inherits directly. The second class is the basis of universal trading operations. It contains actions applied to an open position (like closing of a position or modification of its Stop Loss or Take Profit). This allows formalizing all trading actions and making them platform-independent.

Access to Events Occurring on Other Instruments, the MarketEvent Structure

When designing a trading system which analyzes multiple symbols, you need to create a mechanism that can track changes in the prices of multiple instruments. However, the standard OnTick function is only called for a new tick of the instrument the Expert Advisor is running on. On the other hand, trading system developers may use the OnBookEvent function that responds to changes in the order book (Depth of Market). Unlike OnTick, OnBookEvent is called for any change in the order book of the instrument, to which you subscribed using the MarketBookAdd function.

Changes in the order book happen very often, that is why monitoring this event is a resource-intensive procedure. As a rule, monitoring changes in the tick stream of the required symbol is enough for Expert Advisors. On the other hand, the event of the order book change also includes the arrival of a new tick. Apart from OnBookEvent, you can set up calls of OnTimer at specified intervals and analyze price changes of multiple symbols in this function.

So in the system functions that react to NewTick, BookEvent and Timer events, you can add a call of some intermediate module (let's call it EventProcessor), which would simultaneously analyze changes in prices of multiple instruments and generate an appropriate event. Each event would have a unified description in the form of a structure and would be sent by the control methods of the strategy. Having received an appropriate event as a structure, the strategy would either react to it or ignore. In this case, the system function which actually initiated the event for the final Expert Advisor would be unknown.

Indeed, if an Expert Advisor receives a notification of a new incoming tick, it does not matter whether the information is received through OnTick, OnTimer or OnBookEvent. The only thing that matters is that there is a new tick for the specified symbol. One event handler can be used for many strategies. For example, if each strategy is represented as a custom class, multiple instances of these classes can be stored in the special list of strategies. In this case, any strategy from the list will be able to receive a new event generated by EventProcessor. The following diagram shows how events are generated and sent:

Fig. 1. Diagram of event generation and sending

Author: Vasiliy Sokolov

 

thanks

***** 

 

Hi,

I tried to compile your code (Agent.mq5) and I got the following error. Build 1274

Internal compiler error    Agent.mq5    1    1

There is also a little typo on line 388 of Dictionary.mqh

/| Returns previous object.       The current object becomes previous|
 

On build 1241, the code is compiling well, so I tried to run a backtest. It doesn't take any trades.

After digging a bit  I found it's due to filling mode. The mode allowed on the broker/symbol I am using is ORDER_FILLING_IOC. Your TradeCustom class set the filling mode by default to ORDER_FILLING_FOK. And I am stick there, how can change this filling mode for the Agent.mq5 EA to take trade ? I could search, it will take me a lot of time.

That's the problem with such tools, very similar to MQL5 wizard EA from Metaquotes, it's almost unusable for anyone who doesn't know all the details of the classes. Once you face an issue which was not provided by the author it becomes a real pain to fix it or modify/add to it. I don't see any real difference between your solution and the one from Metaquotes (wizard).

Anyway, congratulations for the huge work. It is a great programming work.

 
This is a very good article. I have learned a lot. Good contribution, thanks.
 
Alain Verleyen:

On build 1241, the code is compiling well, so I tried to run a backtest. It doesn't take any trades.

After digging a bit  I found it's due to filling mode. The mode allowed on the broker/symbol I am using is ORDER_FILLING_IOC. Your TradeCustom class set the filling mode by default to ORDER_FILLING_FOK. And I am stick there, how can change this filling mode for the Agent.mq5 EA to take trade ? I could search, it will take me a lot of time.

That's the problem with such tools, very similar to MQL5 wizard EA from Metaquotes, it's almost unusable for anyone who doesn't know all the details of the classes. Once you face an issue which was not provided by the author it becomes a real pain to fix it or modify/add to it. I don't see any real difference between your solution and the one from Metaquotes (wizard).

Anyway, congratulations for the huge work. It is a great programming work.

There are some classes in MetaEditor5 include folder. If some one can give us a deep explain of Expert folder classes, it will be very helpful, because they looks very complicated. People like to write standard EA based on formal base classes and not always re-create wheels.... Anyway, this article helps a lots for not re-create wheels.
 
Amy Liu:
There are some classes in MetaEditor5 include folder. If some one can give us a deep explain of Expert folder classes, it will be very helpful, because they looks very complicated. People like to write standard EA based on formal base classes and not always re-create wheels.... Anyway, this article helps a lots for not re-create wheels.
I do agree. The problem is when there is a bug in the framework. Before using a framework we should either decide to learn it deeply or to be sure it's well supported by the author.
 
Alain Verleyen:
I do agree. The problem is when there is a bug in the framework. Before using a framework we should either decide to learn it deeply or to be sure it's well supported by the author.
This is why I want to learn classes in MetaEditor5, they're from "government". ;)
 
Amy Liu:
This is why I want to learn classes in MetaEditor5, they're from "government". ;)
And bugged and not well supported :-D
 
thanks.Very good article.
 

Hi Vasiliy,

Please pardon me for asking a question on your article this far down after you wrote it. I'm only going through the articles properly now in search of alternatives to a framework. Something has struck me as odd, very likely due to my misunderstanding.

With regards to New Tick and New Bar event handlers. You loop through the list of added ticks, then build the event structure, passing it to Init and Support event handlers, e.g. new tick event below:

//+------------------------------------------------------------------+
//| Detects the arrival of new ticks of multi-instruments.           |
//+------------------------------------------------------------------+
void CStrategy::NewTickDetect(void)
  {
   if(m_ticks_detectors.Total()==0)
      AddTickEvent(ExpertSymbol());
   for(int i=0; i<m_ticks_detectors.Total(); i++)
     {
      CTickDetector *tick=m_ticks_detectors.At(i);
      if(tick.IsNewTick())
        {
         m_event.period=PERIOD_CURRENT;
         m_event.type=MARKET_EVENT_TICK;
         m_event.symbol=tick.Symbol();
         CallSupport(m_event);
         CallInit(m_event);
        }
     }
  }

In one of your examples, e.g. moving average clip below;

bool CMovingAverage::IsTrackEvents(const MarketEvent &event)
  {
//--- We handle only opening of a new bar on the working symbol and timeframe
   if(event.type != MARKET_EVENT_BAR_OPEN)return false;
   if(event.period != Timeframe())return false;
   if(event.symbol != ExpertSymbol())return false;
   return true;
  }

This IsTrackEvents function seems to nullify the purpose of NewTickDetect function above! So the Moving Average example above should be able to trade on multiple instruments based on its ability to check multiple symbols as in the NewTickDetect, but the IsTrackEvents allows trading only for the Strategy timeframe & Symbol (symbol being key here). Does this not mean that the NewTickDetect loop is not really required, as the strategy can only trade on its symbol? In effect the new tick detect should only check if the received tick is of the strategy symbol - without looping. Which in effect is similar to having a strategy object for each symbol of interest, which the CStragyList loops over?

I sure hope that I'm making sense, and hope that you can clarify this for me.

I love your work. I have learn't a lot from your articles, so a great many thanks.

Kind regards,

Shep

Reason: