Price in OnCalculate() not showing correctly

 

I modified a sample code from MQL5 documentation to look like the following

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//---- plot Line
#property indicator_label1  "Line"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDarkBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         LineBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,LineBuffer,INDICATOR_DATA);
//---
   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[])
  {
//--- Get the number of bars available for the current symbol and chart period
   int bars=Bars(Symbol(),0);
   Print("Bars = ",bars,", rates_total = ",rates_total,",  prev_calculated = ",prev_calculated);
   Print("time[0] = ",time[0]," time[rates_total-1] = ",time[rates_total-1]);
   Print("OHLC: ", open[0], ", ", high[0], ", ", low[0], ", ", close[0]);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


The output print something like this

2019.11.01 17:32:03.037 TestIndicator (USDJPY,M1) OHLC: 357.73, 357.73, 357.73, 357.73


How come the OHLC price looks like this? I tried it on different symbol and time frame, did I do something wrong? Your help is appreciated. Thanks.



 
Terry Zhang

2019.11.01 17:32:03.037 TestIndicator (USDJPY,M1) OHLC: 357.73, 357.73, 357.73, 357.73


How come the OHLC price looks like this? I tried it on different symbol and time frame, did I do something wrong? Your help is appreciated. Thanks.



Maybe it's the first tick of a new bar.

 
Keith Watford:

Maybe it's the first tick of a new bar.

I did try to run it for couple minutes, and every tick output something like this, although the numbers are not exactly the same, but it's definitely not the price for USDJPY.

 
Terry Zhang:


Use MQL5 code:

//--- Get the number of bars available for the current symbol and chart period
//int bars=Bars(Symbol(),0); // you don’t need to do this - use "rates_total" in the indicator
   Print("rates_total = ",IntegerToString(rates_total),
         ", prev_calculated = ",IntegerToString(prev_calculated));
   Print("time[rates_total-1] = ",TimeToString(time[rates_total-1],TIME_DATE|TIME_SECONDS));
   Print("OHLC: ",
         DoubleToString(open[rates_total-1],Digits()), ", ",
         DoubleToString(high[rates_total-1],Digits()), ", ",
         DoubleToString(low[rates_total-1],Digits()), ", ",
         DoubleToString(close[rates_total-1],Digits()));
//--- return value of prev_calculated for next call
   return(rates_total);
 
Vladimir Karputov:

Use MQL5 code:

Thanks, I will try it out.