array out of range - indicator buffer

 

hi guys . I'm new in indicator and English language too.I've tried to convert an indicator from MT4 to MT5 . i downloaded an include file ("<mql4_2_mql5.mqh>") to make my work easier .

this is my code that give me "array out of range in Line ( 171:18 )" error

will you help me to solve my problem?

please give me some articles link about learning how to convert indicator from MT4 to MT5

my code :

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property  indicator_chart_window

#include <mql4_2_mql5.mqh>

#property indicator_buffers 4
#property indicator_plots 4
#property indicator_color1 RoyalBlue//wicks
#property indicator_color2 Red//wicks
#property indicator_color3 RoyalBlue
#property indicator_color4 Red

#property indicator_type1 DRAW_HISTOGRAM
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_type4 DRAW_HISTOGRAM

#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3
input int    Length=20;      // Bollinger Bands Period
input int    Deviation=2;    // Deviation was 2
input double MoneyRisk=1.00; // Offset Factor
input int    Signal=1;       // Display signals mode: 1-Signals & Stops; 0-only Stops; 2-only Signals;
input int    Line=1;         // Display line mode: 0-no,1-yes
input int    Nbars=1000;

input string IIIIIIIIIIIIIIIIIIIIIIIII=">>> Candle/Wick Display Settings >>>>>>>>>>>>>>>>>";
input int  BarWidth   = 1,
           CandleWidth  = 3;

double Bar1[],
       Bar2[],
       Candle1[],
       Candle2[],
       Candle3[],
       Candle4[],
       Candle5[],
       Candle6[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorSetString(INDICATOR_SHORTNAME,"TopTrendCandle");

   SetIndexBuffer(0,Bar1,INDICATOR_DATA);
   SetIndexBuffer(1,Bar2,INDICATOR_DATA);
   SetIndexBuffer(2,Candle1,INDICATOR_DATA);
   SetIndexBuffer(3,Candle2,INDICATOR_DATA);
   SetIndexBuffer(4,Candle3,INDICATOR_DATA);
   SetIndexBuffer(5,Candle4,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,Candle5,INDICATOR_CALCULATIONS);
   SetIndexBuffer(7,Candle6,INDICATOR_CALCULATIONS);

   //PlotIndexSetInteger(0,PLOT_LINE_WIDTH,BarWidth);
   //PlotIndexSetInteger(1,PLOT_LINE_WIDTH,BarWidth);
   //PlotIndexSetInteger(2,PLOT_LINE_WIDTH,CandleWidth);
   //PlotIndexSetInteger(3,PLOT_LINE_WIDTH,CandleWidth);

   //PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   //PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   //PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   //PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   //PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   //PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   //PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   //PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   InitMql4();
   ArraySetAsSeries(Bar1,true);
   ArraySetAsSeries(Bar2,true);
   ArraySetAsSeries(Candle1,true);
   ArraySetAsSeries(Candle2,true);
   ArraySetAsSeries(Candle3,true);
   ArraySetAsSeries(Candle4,true);
   ArraySetAsSeries(Candle5,true);
   ArraySetAsSeries(Candle6,true);

//---
   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[])
  {
//---
   Print(ArraySize(Candle3)," ---------------------------------------------------------------- ",__LINE__);

   int bars=MQL4Run(rates_total,prev_calculated);
// bars - количество баров, доступных mql4-программам
   start(bars, CountedMQL4,Bar1,Bar2,Candle1,Candle2,Candle3,Candle4,Candle5,Candle6);

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
double Open(int ie) {return iOpen(_Symbol,0,ie);}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Close(int ie) {return iClose(_Symbol,0,ie);}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(int rates_total,int prev_calculated,
          double &Bar1[],
          double &Bar2[],
          double &Candle1[],
          double &Candle2[],
          double &Candle3[],
          double &Candle4[],
          double &Candle5[],
          double &Candle6[]
         )
  {
   int handle1=iCustom(NULL,0,"TOPTREND",Length,Deviation, MoneyRisk,Signal,Line,Nbars);
   double MA_1[], MA_2[];
   for(int i = 0; i<=MathMax(rates_total-1-prev_calculated,1); i++)
     {
      CopyBuffer(handle1,0,i,1,MA_1);
      CopyBuffer(handle1,1,i,1,MA_2);
      //Print(ArraySize(MA_1));
      double   Ma1   = MA_1[0];
      double   Ma2   = MA_2[0];


      //Print(i,"  ",__LINE__,"  ",Ma1,"  ",Ma2,"  ",ArraySize(Candle3));

      double high,low,bodyHigh,bodyLow;



      bodyHigh = MathMax(Open(i),Close(i));
      bodyLow  = MathMin(Open(i),Close(i));
      high     = iHigh(_Symbol,0,i);
      low      = iLow(_Symbol,0,i);

      Ma2=-1;
      Print(ArraySize(Candle3),"  ",i,"  ",rates_total,"  ",prev_calculated,"  ",GetLastError(),"  ",handle1);

      if(Ma2==-1)
        {
         Bar1[i] = low;
         Candle1[i] = bodyLow;
         Bar2[i] = low;
         Candle2[i] = bodyLow;
         Bar1[i] = low;
         Candle3[i] = bodyLow;
         Bar2[i] = low;
         Candle4[i] = bodyLow;
         Bar1[i] = low;
         Candle5[i] = bodyLow;
         Bar2[i] = low;
         Candle6[i] = bodyLow;
         Bar1[i] = high;
         Candle1[i] = bodyHigh;
        }
      else
         if(Ma1 == -1)
           {
            Bar1[i] = low;
            Candle1[i] = bodyLow;
            Bar2[i] = low;
            Candle2[i] = bodyLow;
            Bar1[i] = low;
            Candle3[i] = bodyLow;
            Bar2[i] = low;
            Candle4[i] = bodyLow;
            Bar1[i] = low;
            Candle5[i] = bodyLow;
            Bar2[i] = low;
            Candle6[i] = bodyLow;
            Bar2[i] = high;
            Candle2[i] = bodyHigh;
           }



     }

   return(0);
  }
//+------------------------------------------------------------------+
 
   int handle1=iCustom(NULL,0,"TOPTREND",Length,Deviation, MoneyRisk,Signal,Line,Nbars);
   ⋮
      CopyBuffer(handle1,0,i,1,MA_1);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate/OnStart (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)