DRAW_HISTOGRAM2 To Mark Candles not working. Seems like an Issue with the time series access method.

 

Hi guys, I am trying to convert some code from mql4, and I can't get the HISTROGRAM2 drawing to work properly.

I wrote this simple test indicator, but I am getting the completely wrong prices in the print statement, almost like an error resulting in accessing a time series data in a non-series array. 

But I did set the buffer arrays as series to true for good measure. Did try with and without, no difference (I believe SetIndexBuffer() automattically sets your buffer arrays to series).

I even followed the example here, but I could not find why my code has a corrupted output.

Any keen eyes out there see what I am doing wrong?


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1

#property indicator_type1 DRAW_HISTOGRAM2
#property indicator_color1 clrYellow
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1

double candlehigh[], candlelow[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
   
   ArraySetAsSeries(candlehigh, true);
   ArraySetAsSeries(candlelow, true);
   
   SetIndexBuffer(0, candlehigh,INDICATOR_DATA);
   SetIndexBuffer(1, candlelow,INDICATOR_DATA);
   
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
   
   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[]
) {
   
   int limit = MathMin(   
      rates_total-prev_calculated,              //onCalculate counter
      100                                       //user Setting
   );

   IndicatorLoop(limit, high, low);  
   return(rates_total);
}


void IndicatorLoop( const int startAtIndex, const double &h[], const double &l[] ) {

   for(int i=startAtIndex; i>=0; i--) {
      printf("setting candle %i  to  %f", i, h[i]); 
      candlehigh[i] = h[i];
      candlelow[i] = l[i];
   
   }
}
 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1

#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 clrYellow
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1

double candlehigh[], candlelow[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   ArraySetAsSeries(candlehigh, true);
   ArraySetAsSeries(candlelow, true);
   
   SetIndexBuffer(0, candlehigh,INDICATOR_DATA);
   SetIndexBuffer(1, candlelow,INDICATOR_DATA);
   
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
   
   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[]
)
{
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   
   int limit = MathMin(   
      rates_total-prev_calculated,              //onCalculate counter
      100                                       //user Setting
   );

   IndicatorLoop(limit, high, low);  
   return(rates_total);
}


void IndicatorLoop( const int startAtIndex, const double &h[], const double &l[] ) {

   for(int i=startAtIndex; i>=0; i--) {
      printf("setting candle %i  to  %f", i, h[i]); 
      candlehigh[i] = h[i];
      candlelow[i] = l[i];
   
   }
}
 
Konstantin Nikitin:

Ok something wrong with my mt5 then?


same code produces unstable outputs.

 
dazamate:

Ok something wrong with my mt5 then?



Oh I see what you did.

ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);

The arrays in OnCalculate are not passed as time series? That's so stupid.

 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1

#ifdef __MQL5__
  #property indicator_type1 DRAW_HISTOGRAM2
#else
  #property indicator_type1 DRAW_HISTOGRAM
#endif
#property indicator_color1 clrYellow
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1

double candlehigh[], candlelow[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   ArraySetAsSeries(candlehigh, true);
   ArraySetAsSeries(candlelow, true);
   
   SetIndexBuffer(0, candlehigh,INDICATOR_DATA);
   SetIndexBuffer(1, candlelow,INDICATOR_DATA);
   
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
   
   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[]
)
{
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   
   int limit = MathMin(   
      rates_total-prev_calculated,              //onCalculate counter
      100                                       //user Setting
   );

   IndicatorLoop(limit, high, low);  
   return(rates_total);
}


void IndicatorLoop( const int startAtIndex, const double &h[], const double &l[] ) {

   for(int i=startAtIndex; i>=0; i--) {
      printf("setting candle %i  to  %f", i, h[i]); 
      candlehigh[i] = h[i];
      candlelow[i] = l[i];
   
   }
}

There was MT4, this is MT5. New code MT4/5

Reason: