Download MetaTrader 5

Indicator not drawing forward

To add comments, please log in or register
Savoiardo
79
Savoiardo  

Hello everybody,

I've been coding for some time with mql4 and now shifting to mql5, and I'm having a really hard time with a very simple code.

For the purpose of training and understanding, i'm building a test indicator that connects the outputs of the IFractals handle with DRAW_SECTION plot.

I've read the docs on CopyBuffer again and again, and still I can't make this work. Indicator draws the two lines when I put it on the chart the first time, but when I go forward in the tester it draws nothing more. Really I'm not understanding the reason.

Here's my code, can anybody make light on why this doesn't plot forward??

#property indicator_buffers 2
#property indicator_plots 2
#property indicator_label1 "TESTONE!"
#property indicator_type1 DRAW_SECTION
#property indicator_color1 clrWhiteSmoke
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_label2 "TESTONE2"
#property indicator_type2 DRAW_SECTION
#property indicator_color2 clrWhiteSmoke
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1

double testBuffer[];
double testBuffer2[];
int HDLFractals;
int start;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

   HDLFractals=iFractals(NULL,0);

//--- indicator buffers mapping
   SetIndexBuffer(0,testBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,testBuffer2,INDICATOR_DATA);
   IndicatorSetInteger(INDICATOR_DIGITS,Digits());
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| 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 toCopy;
   if(prev_calculated<1)
     {
      start=0;
      toCopy=rates_total;
     }
   else
     {
      start=prev_calculated-1;
      toCopy=rates_total-prev_calculated;
      if(prev_calculated>0) toCopy++;
     }
   CopyBuffer(HDLFractals,0,0,toCopy,testBuffer);
   CopyBuffer(HDLFractals,1,0,toCopy,testBuffer2);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
36081
Alain Verleyen  
Savoiardo:

Hello everybody,

I've been coding for some time with mql4 and now shifting to mql5, and I'm having a really hard time with a very simple code.

For the purpose of training and understanding, i'm building a test indicator that connects the outputs of the IFractals handle with DRAW_SECTION plot.

I've read the docs on CopyBuffer again and again, and still I can't make this work. Indicator draws the two lines when I put it on the chart the first time, but when I go forward in the tester it draws nothing more. Really I'm not understanding the reason.

Here's my code, can anybody make light on why this doesn't plot forward??

Probably because you don't know how the Fractals indicator is working, you are copying the candle 0 only (current candle) :

   else
     {
      start=prev_calculated-1;
      toCopy=rates_total-prev_calculated;
      if(prev_calculated>0) toCopy++;
     }
   CopyBuffer(HDLFractals,0,0,toCopy,testBuffer);
   CopyBuffer(HDLFractals,1,0,toCopy,testBuffer2);

toCopy is zero most of the time, except when there is a new candle. So you are copying  a value which is never set by the Fractals indicator.

You need to copy at least 3 more candles, as the Fractals check a new high low with at least 2 closed candles after it.

      toCopy=rates_total-prev_calculated+3;
Savoiardo
79
Savoiardo  

Thanks, I got it now.

So in the fractals case, we have to check the last 3 values because it could repaint the last 3 candles?

And if we CopyBuffer an indicator that does no repainting, we could just watch for the last value emitted, right?

To add comments, please log in or register