Does MetaTrader allow Multiple symbols for an Expert?

 

Hello, I'm trying to think in a way  (If possible) of having an Expert which operates in multiple Symbols at a time, but I'd like to get your opinion on the best approach.

I have seen this post https://www.mql5.com/en/forum/212858, which shed some light into the problem, but I would like to use the class `CExpert`. 


Is it in any way possible to extend the class `CExpert` in such a way that I'm allow to change the symbol at run time without receiving an error

> Change of symbol is forbidden

If it's not possible at all, how do people manage large portfolios of experts? Do they have +50 open charts with experts attached ?


Update


I've been trying for a while now without success, either having an array of symbols and an Expert and trying to change the symbol, or having an array of Expect to map 1 to 1 to an array of Symbols. Either way, seems I can think of a way of achieving this behavior.


Example:

#include <Expert\Expert.mqh>

CExpert* expert[] = {new CExpert, new CExpert};
string symbols[] = {"USDCAD", "EURUSD"};
int OnInit()
{
//---

   for(uint i = 0; i < symbols.Size(); i++) {
      if(!expert[i].Init(symbols[i], Period(), false, 9999999)) {
         printf(__FUNCTION__ + ": error initializing expert");
         expert[i].Deinit();
         return(INIT_FAILED);
      }
   }
//---
   return(INIT_SUCCEEDED);
}
void OnTick()
{
   for(uint i = 0; i < symbols.Size(); i++) {
      Print("Processing symbol ", expert[i].Low(0));
      expert[i].OnTick();
   }
}

This fails always with:

>  CExpert::Init: wrong symbol or timeframe (must be SYMBOL:PERIOD)

Where Symbol:PERIOD is the one selected in the tester 

Best regards

Multi-symbol Expert some code for sharing - MT5
Multi-symbol Expert some code for sharing - MT5
  • 2017.08.07
  • www.mql5.com
Hello MT5 programmers...
 
algui91 :


You need to write your own code for a multi-symbol Expert Advisor, since the CExpert class was created specifically for the MQL5 Wizard (for the 'Expert Advisor (generate)' mode).

 
Vladimir Karputov #:

You need to write your own code for a multi-symbol Expert Advisor, since the CExpert class was created specifically for the MQL5 Wizard (for the 'Expert Advisor (generate)' mode).

Do I need to start all from scratch? I can't reuse anything from those classes? (CExpert and CExpertBase?)

Thanks for your answer.  

 
algui91 # :

Do I need to start all from scratch? I can't reuse anything from those classes? (CExpert and CExpertBase?)

Thanks for your answer.  

Yes, you need to start all over again, from scratch.

 
Vladimir Karputov #:

Yes, you need to start all over again, from scratch.

Ok, thank you very much :-)

 

I've found this great example as an starting point




But  I wonder, when testing this, 0 ticks are generated, don't know why. But even if ticks were generated, wouldn't they correspond to the selected symbol in the tester, independent of the symbols in which the expert is working?

 
algui91 #:

I've found this great example as an starting point




But  I wonder, when testing this, 0 ticks are generated, don't know why. But even if ticks were generated, wouldn't they correspond to the selected symbol in the tester, independent of the symbols in which the expert is working?

Yes, if you use the OnTick() function (I suppose that's what you're asking about), the EA will use ticks from the selected symbol in the tester (or from the chart you placed the EA on). If the selected symbol (or chart) is GBPUSD, GBPUSD ticks will be used even if your EA trades 10 symbols none of which are GBPUSD. I.e. the EA will make calculations on each tick of GBPUSD symbol.
That's also the reason why it's better to test and use a multi-symbol EA on GBPUSD|EURUSD instead of other Forex pairs. The more ticks you get, the more actual is the data used in your OnTick() calculations.

 
Alexandru Casian #:

Yes, if you use the OnTick() function (I suppose that's what you're asking about), the EA will use ticks from the selected symbol in the tester (or from the chart you placed the EA on). If the selected symbol (or chart) is GBPUSD, GBPUSD ticks will be used even if your EA trades 10 symbols none of which are GBPUSD. I.e. the EA will make calculations on each tick of GBPUSD symbol.
That's also the reason why it's better to test and use a multi-symbol EA on GBPUSD|EURUSD instead of other Forex pairs. The more ticks you get, the more actual is the data used in your OnTick() calculations.

 
Seems it really is possible to process each tick for all symbols, see https://www.mql5.com/en/forum/413770

That would solve my problem
 
algui91 #:

Do I need to start all from scratch? I can't reuse anything from those classes? (CExpert and CExpertBase?)

Thanks for your answer.  

No, you don't need to start from the scratch, but you need object oriented understanding. You create your custom Expert Class and Inherit from the CExpert Class like shown in the example. Afterwards you simply overwrite the functions, yout want to change. 

class MyExpertCExpert : public CExpert{
...
}