Arrows not updating/refreshing in real time (mql5)

 

Hello friends,

I wrote a simple mt5 indicator to display arrow/dot, when we get a bullish candle after a bearish/series of bearish candle and vice versa.

It works fine.But the problem is it is not updating in real time.I need to switch time frames or restart mt5 in order to get it updated every time.

Please help me.

//+------------------------------------------------------------------+
//|                                            Up and Down arrow.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Label2
#property indicator_label2  "Label2"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- indicator buffers
double         Label1Buffer[];
double         Label2Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,Label2Buffer,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   
//---
   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[])
  {

 
   
   for( int i =0; i<=rates_total; i++)
   {
   
     if( (open[i]< close[i]) && (open[i+1]> close[i+1]) )
        {Label1Buffer[i+1]= high[i+1]; }
     
     
     if( (open[i]> close[i]) && (open[i+1]< close[i+1]) )
        { Label2Buffer[i+1]= low[i+1]; }
     
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
 
 //for( int i =0; i<=rates_total; i++) 
 for ( int i = 0 ; i<=rates_total- 2 ; i++)

Your program has an "array out of range" error, because maximum size of buffer is rates_total - 1.

 
Don't recalculate all bars each tick.
          How to do your lookbacks correctly.
 
Naguisa Unada:

Your program has an "array out of range" error, because maximum size of buffer is rates_total - 1.

Thank you so much.
 
whroeder1:
Don't recalculate all bars each tick.
          How to do your lookbacks correctly.
Thank you so much.
Reason: