Multi symbol expert advisor change result depending on symbol selected in backtesting setting

 

Hi everyone, like the title I have this weird problem


The onInit function is the following. The symbols are stored in the input variable Symbols separated by ",". I have some array for checking new bar in different timefrimes  (such as TimeLastCheckTickProcessed)  for each symbol and then I have an array of OBDealer a custom class that use a custom indicator to manage the EA. 

int OnInit()
  {
   trade.SetExpertMagicNumber(MagicNumber);
   
   initialBalance=AccountInfoDouble(ACCOUNT_BALANCE);
   string OrderBlockPluginName="Orderblock Generator";
  
   StringSplit(Symbols,StringGetCharacter(",",0),SymbolList);

   ArrayResize(TimeLastCheckTickProcessed, ArraySize(SymbolList));
   ArrayInitialize(TimeLastCheckTickProcessed, D'1971.01.01 00:00');
   ArrayResize(TimeLastShortProcessed, ArraySize(SymbolList));
   ArrayInitialize(TimeLastShortProcessed, D'1971.01.01 00:00');
   ArrayResize(TimeLastMedProcessed, ArraySize(SymbolList));
   ArrayInitialize(TimeLastMedProcessed, D'1971.01.01 00:00'); 
   ArrayResize(TimeLastLongProcessed, ArraySize(SymbolList));
   ArrayInitialize(TimeLastLongProcessed, D'1971.01.01 00:00');

   ArrayResize(obDealer,ArraySize(SymbolList));

   for(int i=0; i<ArraySize(SymbolList); i++)
    obDealer[i]= new OBDealer(SymbolList[i],iCustom(SymbolList[i], ShortestPeriod, OrderBlockPluginName, countingbar,rigidity,maxCandleSize,minCandleSize,checkOtherBars),iCustom(SymbolList[i], MediumPeriod, OrderBlockPluginName, countingbar,rigidity,maxCandleSize,minCandleSize,checkOtherBars),iCustom(SymbolList[i], LongestPeriod, OrderBlockPluginName, countingbar,rigidity,maxCandleSize,minCandleSize,checkOtherBars));
 
   return(INIT_SUCCEEDED);
  }

I provide you the code of OnInit because somewhere I read that MQL5 download history basing on the OnInit function.. but I don't know if it makes any sense.

Have you got some idea? Thanks

 
Simone Alessandrì: Hi everyone, like the title I have this weird problem. The onInit function is the following. The symbols are stored in the input variable Symbols separated by ",". I have some array for checking new bar in different timefrimes  (such as TimeLastCheckTickProcessed)  for each symbol and then I have an array of OBDealer a custom class that use a custom indicator to manage the EA. I provide you the code of OnInit because somewhere I read that MQL5 download history basing on the OnInit function.. but I don't know if it makes any sense. Have you got some idea? Thanks

You should not be doing anything related to history data or trade data in the OnInit event. It should be used strictly for code initialisation (including Indicator handles) and parameter verification and nothing else.

All the data related activity should be handled in the other event handlers, not in the OnInit. Please read the following for how to synchronise and organise the data — Organizing Data Access

Also, please note that the OnTick() event handler is only called for new ticks for the current symbol. It is not suitable for multi-currency.

For multi-currency, you need to adopt a different approach of event handling, such as using OnTimer() or using custom chart events from specialised indicators for this purpose.

Articles

The Implementation of a Multi-currency Mode in MetaTrader 5

Konstantin Gruzdev, 2011.02.18 17:58

For a long time multi-currency analysis and multi-currency trading has been of interest to people. The opportunity to implement a full fledged multi-currency regime became possible only with the public release of MetaTrader 5 and the MQL5 programming language. In this article we propose a way to analyze and process all incoming ticks for several symbols. As an illustration, let's consider a multi-currency RSI indicator of the USDx dollar index.
 
Fernando Carreiro #:

You should not be doing anything related to history data or trade data in the OnInit event. It should be used strictly for code initialisation (including Indicator handles) and parameter verification and nothing else.

All the data related activity should be handled in the other event handlers, not in the OnInit. Please read the following for how to synchronise and organise the data — Organizing Data Access

Also, please note that the OnTick() event handler is only called for new ticks for the current symbol. It is not suitable for multi-currency.

For multi-currency, you need to adopt a different approach of event handling, such as using OnTimer() or using custom chart events from specialised indicators for this purpose.

Thank you, Fernando! I really appreciate your help. I will read the documentation  the documentation and work on resolving this issue.

I'm not into MQL5 but searching on internet i found a lot of person suggesting using a loop over symbol inside OnTick to obtain a multi currency expert advisor. Are you indicating that solutions like this one might not be the best approach?  

advanced-mql-programming/episode003/control-bar-opening-multi-symbol2.mq5 at master · darwinex/advanced-mql-programming
advanced-mql-programming/episode003/control-bar-opening-multi-symbol2.mq5 at master · darwinex/advanced-mql-programming
  • darwinex
  • github.com
Contribute to darwinex/advanced-mql-programming development by creating an account on GitHub.
 
Simone Alessandrì #: I'm not into MQL5 but searching on internet i found a lot of person suggesting using a loop over symbol inside OnTick to obtain a multi currency expert advisor. Are you indicating that solutions like this one might not be the best approach?  

Imagine some of the symbols you were trading were cryptos (e.g BTCUSD), but the current chart on which the EA is running is EURUSD.

So, during the week end, no ticks arrive for EURUSD, and so the OnTick() event handler is never called.

How will you process the data for BTCUSD (which is 24/7), if there are no incoming ticks on EURUSD (which is only 24/5)?

You can still use OnTick() but you need to combine it with other event handlers so that processing can be carried out.

Examples are using OnTimer(), or using OnChartEvent() with custom events being processed for specialised indicators that are open for the required symbols.

But there can be various ways of tackling the issue depending on the strategy's requirements.

 
Fernando Carreiro #:

Imagine some of the symbols you were trading were cryptos (e.g BTCUSD), but the current chart on which the EA is running is EURUSD.

So, during the week end, no ticks arrive for EURUSD, and so the OnTick() event handler is never called.

How will you process the data for BTCUSD (which is 24/7), if there are no incoming ticks on EURUSD (which is only 24/5)?

You can still use OnTick() but you need to combine it with other event handlers so that processing can be carried out.

Examples are using OnTimer(), or using OnChartEvent() with custom events being processed for specialised indicators that are open for the required symbols.

But there can be various ways of tackling the issue depending on the strategy's requirements.

Thank you very much! Wishing you a wonderful day
 
 

Thank you both for your answer!

After delving into the documentation, it seems that employing an indicator to trigger OnChartEvent is considered the most effective solution. However I read that it doesn't work for backtesting. This poses a bit of a challenge for me, as optimizing the EA relies heavily on the results derived from backtesting.

I'm wondering if anyone has come across a workaround for this particular issue. Any suggestions or alternative approaches would be greatly appreciated!

 
Simone Alessandrì #: After delving into the documentation, it seems that employing an indicator to trigger OnChartEvent is considered the most effective solution. However I read that it doesn't work for backtesting. This poses a bit of a challenge for me, as optimizing the EA relies heavily on the results derived from backtesting.I'm wondering if anyone has come across a workaround for this particular issue. Any suggestions or alternative approaches would be greatly appreciated!

It does work in the Strategy Tester as long as it is the EA processing the custom chart events — "When testing in an EA, we can handle custom events using the OnChartEvent() function"

     
    Simone Alessandrì #:

    Thank you both for your answer!

    After delving into the documentation, it seems that employing an indicator to trigger OnChartEvent is considered the most effective solution. However I read that it doesn't work for backtesting. This poses a bit of a challenge for me, as optimizing the EA relies heavily on the results derived from backtesting.

    I'm wondering if anyone has come across a workaround for this particular issue. Any suggestions or alternative approaches would be greatly appreciated!

    You can use this ready-made wrapper to immediately write code without being distracted by the features of the tester.

    OnTickMulti
    OnTickMulti
    • www.mql5.com
    Мультисимвольный OnTick.
     
    Really thanks! Have a good day
    Reason: