Accessing multisymbols data from custom indicator MQL5

 

Hello guys,

I'm trying to code some ideas in mql5 rather than mql4 in order to better test my multicurrency indicators and experts but I've big issues accessing indicator values (ex. MA) of symbols. I read all documentation deeply and looked also on the forum I'm getting very confused.

This is my attempt, theorically I used all functions correctly, but I can't get right values, sometimes I get "+inf" value, sometimes 0, sometimes wrong value, it's very randomic.

PS: SYMBOLS[] is an array in which I splitted my calcSymbols string, countSymbols is the results of split (aka array size).

string calcSymbols  = "AUDUSD;EURUSD;GBPUSD;NZDUSD;USDCAD;USDCHF;USDJPY";

int OnInit() {
   GetIndicatorHandles(FastPeriod,"Fast");
   GetIndicatorHandles(SlowPeriod,"Slow");
   return(0);
}

void GetIndicatorHandles(int period, string type="") {
   for( int i=0; i<countSymbols; i++ ) {
      if( type=="Slow" ) MA_Slow_Handle[i] = iMA(SYMBOLS[i],0,period,0,MODE_LWMA,PRICE_CLOSE);
      if( type=="Fast" ) MA_Fast_Handle[i] = iMA(SYMBOLS[i],0,period,0,MODE_LWMA,PRICE_CLOSE);
   }
}

int OnCalculate(const int rates_total,     // price[] array size 
                const int prev_calculated, // number of previously handled bars
                const int begin,           // where significant data start from 
                const double &price[])     // value array for handling
   {
   Print("EURUSD MA Fast: "+MA(rates_total,"EURUSD", FastPeriod, "Fast", MODE_LWMA, PRICE_WEIGHTED, 0);
   Print("GBPUSD MA Slow: "+MA(rates_total,"GBPUSD", SlowPeriod, "Slow", MODE_LWMA, PRICE_WEIGHTED, 0);

}

double MA(int n, string sym, int per, string type, ENUM_MA_METHOD Mode, int Price, int i) {
   double res = 0;
   
   CopyIndicatorBuffers(SymbolNumber(sym),n);
   
   if( type=="Slow" ) Print(sym+" MA Slow: "+DoubleToString(MA_Slow[i],8));
   if( type=="Fast" ) Print(sym+" MA Fast: "+DoubleToString(MA_Fast[i],8));

   switch(Period())
     {
       case PERIOD_H1   : if( type=="Slow" ) res += MA_Slow[i]; if( type=="Fast" ) res += MA_Fast[i]; 
     } 
   return(res);
}

void CopyIndicatorBuffers(int i, int n) {
   int count_slow = CopyBuffer(MA_Slow_Handle[i],0,0,n,MA_Slow);
   if( count_slow<0 ) Print("Failed to copy buffer of "+SYMBOLS[i]+" Slow MA");
   int count_fast = CopyBuffer(MA_Fast_Handle[i],0,0,n,MA_Fast);
   if( count_fast<0 ) Print("Failed to copy buffer of "+SYMBOLS[i]+" Fast MA");
}

int SymbolNumber(string symbol) {
   if( symbol=="AUDUSD" ) return(0);
   if( symbol=="EURUSD" ) return(1);
   if( symbol=="GBPUSD" ) return(2);
   if( symbol=="NZDUSD" ) return(3);
   if( symbol=="USDCAD" ) return(4);
   if( symbol=="USDCHF" ) return(5);
   if( symbol=="USDJPY" ) return(6);
   return(-1);
}

It's funny that MQL5 becomes more difficult and intricate compared to MQL4. Usually newer languages becomes more easy and understandable, but it's not our case. I hope I'll wrong when I learn MQL5 better.

Thanks for everyone that want to spend some time to helping me! :-)

 

This is a draft.

It works ONLY ONLINE. On the story DOES NOT WORK. When pumping more than one bar - DOES NOT WORK.

 

Vladimir, thanks a lot for your draft! I really appreciate it!

I created a for cycle to print all values of the graph, and it obtains right values for all graph bars.

   int limit = prev_calculated -1;
   if( limit < 0 ) limit = 0;
   for( int pos=limit; pos<rates_total; pos++ ) {
      double amount_fast=0.0;
      double amount_slow=0.0;
      for(int i=0; i<k; i++)
        {
         double array_temp[];       // array temp of Moving Average values
         int values_to_copy=rates_total;      // number of values copied from the iMA indicator
         //--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation
         if(!FillArrayFromBuffer(array_temp,Inp_MA_ma_shift,handles_iMA_fast[i],values_to_copy))
            return(0);
         amount_fast+=array_temp[pos];
         //--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation
         if(!FillArrayFromBuffer(array_temp,Inp_MA_ma_shift,handles_iMA_slow[i],values_to_copy))
            return(0);
         amount_slow+=array_temp[pos];
        }
   //---
      FastBuffer[pos]=amount_fast;
      SlowBuffer[pos]=amount_slow;
   }

Anyway, it works well only if I put as input symbol only the current chart symbol name, if I try to work on multiple symbols I obtain error code 4806 on BarsCalculated of other symbols handles.

It occurs both on your original code (that plot values only of rates_total-1 candle) and on my change with a for cycle.

It seems absurd to me that in MQL5 it's impossible to have a multicurrency indicator... In MQL4 it can be done in a couple of minutes with few rows of code....

 
Fabio Cavalloni :

***

It occurs both on your original code (that plot values only of rates_total-1 candle) and on my change with a for cycle.

It seems absurd to me that in MQL5 it's impossible to have a multicurrency indicator... In MQL4 it can be done in a couple of minutes with few rows of code....

There is no problem - there is a problem in you: you do not want to handle elementary errors. For example, you probably don’t even understand that for some characters there are BARS BARS - for example, on the symbol # 1 we have 1000 bars, and on the symbol # 2 - only 980.

The old language (especially WITHOUT #property strict) forgave all your mistakes - as a result, you began to work carelessly.

 

version "1.001" Now works with history.

 

I always try to handle and manage error in the code, I never worked without property strict in mql4.

I found the mql5 language more complicated than mql4 and all my knowledge in mql4 is pretty useless, it changed a lot of things.

Thanks again for your help and your new version, I'll study it deeply to better understand.

 

It remains to overcome these values (they are on the LEFT bars)


 
Fabio Cavalloni:

Hello guys,

I'm trying to code some ideas in mql5 rather than mql4 in order to better test my multicurrency indicators and experts but I've big issues accessing indicator values (ex. MA) of symbols. I read all documentation deeply and looked also on the forum I'm getting very confused.

This is my attempt, theorically I used all functions correctly, but I can't get right values, sometimes I get "+inf" value, sometimes 0, sometimes wrong value, it's very randomic.

PS: SYMBOLS[] is an array in which I splitted my calcSymbols string, countSymbols is the results of split (aka array size).

It's funny that MQL5 becomes more difficult and intricate compared to MQL4. Usually newer languages becomes more easy and understandable, but it's not our case. I hope I'll wrong when I learn MQL5 better.

Thanks for everyone that want to spend some time to helping me! :-)

Ciao, hai risolto? Anch'io ho lo stesso problema... fatto tutto nella 4 m qualche problema con la 5. Io carico i 28 handles su un array in OnInit

 if(f > 3)

      for(uchar x = 0; x < sizePairs; x++)

        {

         switch(f)

           {

            case 4:

               h[x] = iCustom(pairsT[x], PERIOD_CURRENT, " indicator1", 1);

               break;

            case 5:

               h[x] = iCustom(pairsT[x], PERIOD_CURRENT, "indicator2", r1);

               break;

            case 6:

               h[x] = iCustom(pairsT[x], PERIOD_CURRENT, "indicator3", r1);

               break;

}


poi faccio i copyBuffer all'occorrenza prima di leggere gli array... ma da problemi...

Reason: