Multi Currency Indicator Array out of range

 

Hi, im developing a multi currency indicator, but always give me array out of range

So my problem is where i put double calc = eurusdBuffer[i] + eurjpyBuffer[i].close * 100000; -> In the chart always says that is out of range, but when i put only eurusd doesnt show the error

I have always the currency in observation market and both currency chart open

# Indicator to plot

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1

#property indicator_label1  "plotbuffer"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

input int      period=20;

double plotBuffer[];
double eurusdBuffer[];
double eurgbpBuffer[];
double eurcadBuffer[];
double euraudBuffer[];
double eurchfBuffer[];
double eurnzdBuffer[];
MqlRates eurjpyBuffer[];

int eurusd = INVALID_HANDLE;
int eurgbp = INVALID_HANDLE;
int euraud = INVALID_HANDLE;
int eurcad = INVALID_HANDLE;

int limit;

int OnInit()
  {

   SetIndexBuffer(0,plotBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,eurusdBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,eurgbpBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,eurcadBuffer,INDICATOR_CALCULATIONS);
   
   SymbolSelect("EURUSD",true);
   SymbolSelect("EURCAD",true);
   SymbolSelect("EURAUD",true);
   SymbolSelect("EURGBP",true);
   
   eurusd = iCustom("EURUSD",PERIOD_CURRENT,"overbs.ex5",period);
   if(eurusd == INVALID_HANDLE)
      return INIT_FAILED;
   eurgbp = iCustom("EURGBP",PERIOD_CURRENT,"overbs.ex5",period);
   if(eurgbp == INVALID_HANDLE)
      return INIT_FAILED;
   euraud = iCustom("EURAUD",PERIOD_CURRENT,"overbs.ex5",period);
   if(euraud == INVALID_HANDLE)
      return INIT_FAILED;
   eurcad = iCustom("EURCAD",PERIOD_CURRENT,"overbs.ex5",period);
   if(eurcad == INVALID_HANDLE)
      return INIT_FAILED;
      
   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   
   if(CopyBuffer(eurusd,0,0,rates_total,eurusdBuffer) <= 0) return 0;
   if(CopyBuffer(eurgbp,0,0,rates_total,eurgbpBuffer) <= 0) return 0;
   if(CopyBuffer(euraud,0,0,rates_total,euraudBuffer) <= 0) return 0;
   if(CopyBuffer(eurcad,0,0,rates_total,eurcadBuffer) <= 0) return 0;
   
   CopyRates("EURJPY",PERIOD_CURRENT,0,rates_total,eurjpyBuffer);
   
   
   if(prev_calculated == 0) {
      limit = 0;
   } else {
      limit = rates_total - 3;
   }
   
   for(int i = limit; i < rates_total; i++)
     {
      plotBuffer[i] = 0.0;

      double calc = eurusdBuffer[i] + eurjpyBuffer[i].close * 100000;
      plotBuffer[i] = calc;
      Comment(plotBuffer[i]);

     }
   return(rates_total);
  }


# Indicator that gives the data

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot buffer
#property indicator_label1  "buffer"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      period=20;
//--- indicator buffers
double         bufferBuffer[];

int limit;
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,bufferBuffer,INDICATOR_DATA);

   SymbolSelect("EURUSD",true);
   SymbolSelect("EURCAD",true);
   SymbolSelect("EURAUD",true);
   SymbolSelect("EURGBP",true);

//---
   return(INIT_SUCCEEDED);
  }
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   if(prev_calculated == 0) {
      limit = 0;
   } else {
      limit = rates_total - 3;
   }
   
   for(int i = limit; i < rates_total; i++)
     {
      
      bufferBuffer[i] = 0.0;
      
      double calc1 = 0.0;
      double calc2 = 0.0;
      
      if(i < period)
        {
         continue;
        }
      
      for(int n=0;n<period;n++)
        {
         calc1 = close[i-n] - open[i-n];
         calc2 += calc1;
        }
        
      bufferBuffer[i] = calc2;      
        
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
The "rates total" is the number of bars for each individual chart, it is out of range cause one chart has more bars than the other ... Print the value of "i" before "calc", then the rates total for the individual charts
 

Get the symbol with the least amount of bars and only copy what is needed.

//--- input parameters
input int      Period=14;
//--- indicator buffers
double         PlotBuffer[];
//---
double         EURUSD[];
double         EURGBP[];
double         EURJPY[];
//---
int handleEURUSD;
int handleEURGBP;
int handleEURJPY;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!SymbolSelect("EURUSD",true) ||
      !SymbolSelect("EURGBP",true) ||
      !SymbolSelect("EURJPY",true))
      return(INIT_FAILED);
//---
   if((handleEURUSD=iCustom("EURUSD",_Period,"overbs.ex5",Period))==INVALID_HANDLE ||
      (handleEURGBP=iCustom("EURGBP",_Period,"overbs.ex5",Period))==INVALID_HANDLE ||
      (handleEURJPY=iCustom("EURJPY",_Period,"overbs.ex5",Period))==INVALID_HANDLE)
      return(INIT_PARAMETERS_INCORRECT);
//--- indicator buffers mapping
   SetIndexBuffer(0,PlotBuffer,INDICATOR_DATA);
   ArraySetAsSeries(PlotBuffer,true);
//---
   ArraySetAsSeries(EURUSD,true);
   ArraySetAsSeries(EURGBP,true);
   ArraySetAsSeries(EURJPY,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
   int totalEURUSD=iBars("EURUSD",_Period);
   int totalEURGBP=iBars("EURGBP",_Period);
   int totalEURJPY=iBars("EURJPY",_Period);
   static int calcEURUSD=0,calcEURGBP=0,calcEURJPY=0;
   if(BarsCalculated(handleEURUSD)<totalEURUSD ||
      BarsCalculated(handleEURGBP)<totalEURGBP ||
      BarsCalculated(handleEURJPY)<totalEURJPY)
      return(calcEURUSD=calcEURGBP=calcEURJPY=0);
//---
   const int toCopy=(prev_calculated<1)?MathMin(rates_total-Period,
                                        MathMin(totalEURUSD-Period,
                                        MathMin(totalEURGBP-Period,totalEURJPY-Period))):
                    (rates_total!=prev_calculated)?MathMax(rates_total-prev_calculated,
                                                   MathMax(totalEURUSD-calcEURUSD,
                                                   MathMax(totalEURGBP-calcEURGBP,totalEURJPY-calcEURJPY))):1;
//---
   if(CopyBuffer(handleEURUSD,0,0,toCopy,EURUSD)!=toCopy ||
      CopyBuffer(handleEURGBP,0,0,toCopy,EURGBP)!=toCopy ||
      CopyBuffer(handleEURJPY,0,0,toCopy,EURJPY)!=toCopy)
      return(calcEURUSD=calcEURGBP=calcEURJPY=0);
//---
   int limit=toCopy-1;
//---
   for(int i=limit;i>=0 && !IsStopped();i--)
      PlotBuffer[i]=(EURUSD[i]+EURGBP[i]+EURJPY[i]);
//---
   calcEURUSD=totalEURUSD;
   calcEURGBP=totalEURGBP;
   calcEURJPY=totalEURJPY;      
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Custom Indicator deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(handleEURUSD);
   IndicatorRelease(handleEURGBP);
   IndicatorRelease(handleEURJPY);
//---
  }
//+------------------------------------------------------------------+

//--- input parameters
input int      Period=14;
//--- indicator buffers
double         PlotBuffer[];
int            divider=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,PlotBuffer,INDICATOR_DATA);
   divider=StringFind(_Symbol,"JPY")!=-1?100:1;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   const int begin=(prev_calculated<1)?Period:(rates_total!=prev_calculated)?prev_calculated:prev_calculated-1;
//---
   double calc;
   for(int i=begin,n;i<rates_total && !IsStopped();i++)
     {
      calc=0.;
      for(n=0;n<Period;n++)
         calc+=close[i-n]-open[i-n];
//---
      PlotBuffer[i]=calc/divider;
     }   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Trapheal:
The "rates total" is the number of bars for each individual chart, it is out of range cause one chart has more bars than the other ... Print the value of "i" before "calc", then the rates total for the individual charts

Thanks, it worked.


Trapheal:
The "rates total" is the number of bars for each individual chart, it is out of range cause one chart has more bars than the other ... Print the value of "i" before "calc", then the rates total for the individual charts

Thank you!