I have problems with a buffer

 

Hey guys,

I made this indicator showing me candles where the body size is <0.5 x candle size. These candles are indicated by a dot in the middle of the body.

My problem occurs when I am changing the timeframe. There are always some dots somewhere in the chart and I don't know how to delete them. I have to switch timeframes 3-4 times and then they disappear.

Can someone help me please?

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2 
//--- plot Arrows 
#property indicator_type1   DRAW_ARROW 
#property indicator_color1  clrBlack 
#property indicator_width1  1
#property indicator_type2   DRAW_ARROW 
#property indicator_color2  clrWhite 
#property indicator_width2  1
double bullCandleBuffer[], bearCandleBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,bullCandleBuffer,INDICATOR_DATA); 
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
   
   SetIndexBuffer(1,bearCandleBuffer,INDICATOR_DATA); 
   PlotIndexSetInteger(1,PLOT_ARROW,159); 
   PlotIndexSetDouble(1,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=prev_calculated-1;
   if (prev_calculated==0) limit=0;

   for (int i=limit;i<rates_total;i++) {
      double bodySize=MathAbs(close[i]-open[i]);
      double candleSize=high[i]-low[i];
      if (bodySize<0.5*candleSize) {
         if (close[i]>open[i]) bullCandleBuffer[i]=MathAbs(close[i]+open[i])/2;
         else if (close[i]<open[i]) bearCandleBuffer[i]=MathAbs(close[i]+open[i])/2;
      }
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Files:
EURUSD.aH12.png  43 kb
 
Marbo: There are always some dots somewhere in the chart and I don't know how to delete them.
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

At the top of your loop, set both buffer elements to zero.

 
Marbo :

Hey guys,

I made this indicator showing me candles where the body size is <0.5 x candle size. These candles are indicated by a dot in the middle of the body.

My problem occurs when I am changing the timeframe. There are always some dots somewhere in the chart and I don't know how to delete them. I have to switch timeframes 3-4 times and then they disappear.

Can someone help me please?

You should always ZERO the indicator buffer:

   int limit=prev_calculated-1;
   if(prev_calculated==0)
      limit=0;
   bullCandleBuffer[i]=0.0;
   bearCandleBuffer[i]=0.0;
   for(int i=limit; i<rates_total; i++)
     {
      double bodySize=MathAbs(close[i]-open[i]);
      double candleSize=high[i]-low[i];
      if(bodySize<0.5*candleSize)
        {
         if(close[i]>open[i])
            bullCandleBuffer[i]=MathAbs(close[i]+open[i])/2;
         else
            if(close[i]<open[i])
               bearCandleBuffer[i]=MathAbs(close[i]+open[i])/2;
        }
     }
 
Vladimir Karputov #:

You should always ZERO the indicator buffer:

I included this into the loop and now it works perfect. Thank you!

for (int i=limit;i<rates_total;i++) {
      bullCandleBuffer[i]=0.0;
      bearCandleBuffer[i]=0.0;
      double bodySize=MathAbs(close[i]-open[i]);
      double candleSize=high[i]-low[i];
      if (bodySize<0.5*candleSize) {
         if (close[i]>open[i]) bullCandleBuffer[i]=MathAbs(close[i]+open[i])/2;
         else if (close[i]<open[i]) bearCandleBuffer[i]=MathAbs(close[i]+open[i])/2;
      }
   }
 
Marbo #: I included this into the loop and now it works perfect. Thank you!

As I said: “at the top of your loop.” Not above the loop.

 
William Roeder #:

As I said: “at the top of your loop.” Not above the loop.

Thank you!!

Reason: