MQL4 Code DEBUG WillyR

 

Hello I am a new in MQL and using MQL4 I have a code that gives index out of range error. Even though I understand the error I could not figure out how to fix it. 

Actually, it is a part of code which I want to take the moving average of willy r for 5 and 10 days then get the difference and just started to write. 

I hope you can help me.


#property copyright ""

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_plots   1

//--- plot Willy510

#property indicator_label1  "Willy510"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrYellow

#property indicator_style1  STYLE_SOLID

#property indicator_width1  3



//--- indicator buffers

double         Willy510Buffer[];

double willy5[], willy10[];



int OnInit()

  {

   SetIndexBuffer(0,Willy510Buffer);

   SetIndexBuffer(0,willy5);

   SetIndexBuffer(0,willy10);



   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[])

  {





      int counted_bars=IndicatorCounted();

      int limit = Bars-counted_bars-1;



/* in here i will create the willy r for 5 and 10 days */

      for(int i = 0; i<= limit; i++)

      {

         willy5[i] =  iWPR(NULL,0,5,i);

         willy10[i] = iWPR(NULL,0,10,i);

      }



/*

Here i will create the graph by differenting them like willy5-willy10  

The code bellow works just fine.  

The exception comes from the loop above. But even when I try to 

plot  iWPR(NULL,0,5,i); it does not work and gives out of range error again*/ 

      for(int i = 0; i<= limit; i++)

      {

         Willy510Buffer[i] = iMA(NULL,0,13,8,MODE_SMMA,PRICE_MEDIAN,i);  

      

      }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+
 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
In future please post in the correct section
I will move this topic to the MQL4 and Metatrader 4 section.
 
int OnInit()
{
   IndicatorBuffers(3);
   SetIndexBuffer(0,Willy510Buffer);
   SetIndexBuffer(1,willy5);
   SetIndexBuffer(2,willy10);

   return(INIT_SUCCEEDED);
}

Use "rates _ total" instead of Bars, and "prev _ calculated" instead of counted _ bars.

Calculate the limit at program startup (i.e. prev _ calculated == 0) and at other times.

//int counted_bars=IndicatorCounted();
//int limit = Bars-counted_bars-1;
int limit;

if (prev_calculated == 0)
   limit = rates_total - 10;
else
   limit = rates_total - prev_calculated;
//Willy510Buffer[i] = iMA(NULL,0,13,8,MODE_SMMA,PRICE_MEDIAN,i); 
Willy510Buffer[i] = willy5[i] - willy10[i];

I don't know whether it's useful.

Reason: