Problem with indicator

 

I have an issue with this indicator... well, i think actually there are more issues about it, but the thing it really stresses me up is that it doesn't work after I put it on a chart. I have to always recompile it after I place it over chart.

//+------------------------------------------------------------------+
//|                                                    BBoverSTD.mq4 |
//|                                                 Bogdan Caramalac |
//|                                     mailto:fxeconomist@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Bogdan Caramalac"
#property link      "mailto:fxeconomist@yahoo.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 LightSeaGreen //STDDEV
#property indicator_color2 Blue          //MA
#property indicator_color3 Yellow        //Upper BB
#property indicator_color4 Yellow        //Lower BB

//---- input parameters
extern int       STDLEN=20;
extern int       BBLEN=10;
extern int       BBMULT=2;
extern string    MATYPE="EMA";


double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE, 0, 1, indicator_color1);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE, 0, 1, indicator_color2);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE, 0, 1, indicator_color3);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_LINE, 0, 1, indicator_color4);
   SetIndexBuffer(3, ExtMapBuffer4);
//----
   SetIndexDrawBegin(0,STDLEN);
   
   SetIndexDrawBegin(1,STDLEN+BBLEN-1);
   SetIndexDrawBegin(2,STDLEN+BBLEN-1);
   SetIndexDrawBegin(3,STDLEN+BBLEN-1);
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int ExtCountedBars,spos;
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   //if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   //if (ExtCountedBars>0) ExtCountedBars--;
   int pos=Bars-1;
   spos=pos;
   ArraySetAsSeries(ExtMapBuffer1,true);
   while(pos>=0)
     {      
      ExtMapBuffer3[pos]=EMPTY_VALUE;
      ExtMapBuffer4[pos]=EMPTY_VALUE;
      if (MATYPE=="SMA")
        {
         ExtMapBuffer1[pos]=iStdDev(Symbol(),Period(),STDLEN,0,MODE_SMA,MODE_OPEN,pos);
         ExtMapBuffer2[pos]=iMAOnArray(ExtMapBuffer1,0,BBLEN,0,MODE_SMA,pos);
        }
      if (MATYPE=="EMA")
        {
         ExtMapBuffer1[pos]=iStdDev(Symbol(),Period(),STDLEN,0,MODE_EMA,MODE_OPEN,pos);
         ExtMapBuffer2[pos]=iMAOnArray(ExtMapBuffer1,0,BBLEN,0,MODE_EMA,pos);
        }
      if (MATYPE=="SMMA")
        {
         ExtMapBuffer1[pos]=iStdDev(Symbol(),Period(),STDLEN,0,MODE_SMMA,MODE_OPEN,pos);
         ExtMapBuffer2[pos]=iMAOnArray(ExtMapBuffer1,0,BBLEN,0,MODE_SMMA,pos);
        }
      if (MATYPE=="LWMA")
        {
         ExtMapBuffer1[pos]=iStdDev(Symbol(),Period(),STDLEN,0,MODE_LWMA,MODE_OPEN,pos);      
         ExtMapBuffer2[pos]=iMAOnArray(ExtMapBuffer1,0,BBLEN,0,MODE_LWMA,pos);
        }
      //if (spos-pos>BBLEN)
      //  {
         ExtMapBuffer3[pos]=iBandsOnArray(ExtMapBuffer1,0,BBLEN,BBMULT,0,MODE_HIGH,pos);
         ExtMapBuffer4[pos]=iBandsOnArray(ExtMapBuffer1,0,BBLEN,BBMULT,0,MODE_LOW,pos); 
      //  }
      //else
      //  {
      //   ExtMapBuffer3[pos]=EMPTY_VALUE;
      //   ExtMapBuffer4[pos]=EMPTY_VALUE;
      //  }
       //Print(pos,ExtMapBuffer1[pos]);
       pos--;       
     }
   return(0);
  }
//+------------------------------------------------------------------+

plus that the values are not retrieved correctly with iCustom for ExtMapBuffer1 that's set as series...

 
TheEconomist:

I have an issue with this indicator... well, i think actually there are more issues about it, but the thing it really stresses me up is that it doesn't work after I put it on a chart. I have to always recompile it after I place it over chart.

plus that the values are not retrieved correctly with iCustom for ExtMapBuffer1 that's set as series...

iMaOnArray() cannot work correctly with the buffers that are calculated in the same loop. Move all i..OnArray calls into a separate loop that runs after the loop that calculates initial 'OnArray' array.

 

I was curios so I did what Irtron suggested. Is this what it is meant to do?

Files:
bboverstd.mq4  5 kb
 

WOW! Thanks! I thought a while what did Irtron meant with that loop thingy, because I never saw an indicator with more than one loop. You did it well!

What I like about the indicator is that it has a tendency to work well, in the current setup, over H4 charts. I am considering entry when the stddev crosses upper band and exit when stddev crosses moving average back.

However, even if a signal of volatility, it needs a secondary indicator for direction...

This is why I believe the indicator could be good in future, for vola scalping when we'll have options...