EA create multiple instances of Custom Indicators

 

When I use the custom indicator, it shows the graph as follows 

When I use the Expert Advisor with the custom indicator, it shows the following 

It also repeated opened a lot of custom indicator when I am doing backtesting.

How should I change the EA such that

  1. It only opened a custom indicator;

  2. It plot the same graph as the custom indicator?

Below is the code for custom indicator (HitBollingerBand1.mq4)

#property indicator_chart_window 
#property indicator_buffers 2 

sinput string s_  = " Bolinger Bands ";   //  ind#1
input int      InpBandsPeriod    = 20;    //period
input double   InpBandsDeviation = 2.0;   //deviation

double CrossedUp[];
double CrossedDown[];

int limit, x;
double POINT;

int init()
  {

   SetIndexStyle(0, DRAW_ARROW, 1, 2,clrRed);
   SetIndexBuffer(0,CrossedUp);
   SetIndexStyle(1, DRAW_ARROW, 1, 2,clrRed);
   SetIndexBuffer(1,CrossedDown);
   POINT=0.0001;
   return(0);
  }

int start()
  {

   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1); 
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars; 


   for(int i=limit; i>=0; i--) { 
      isHitBollingerBand(i);     
   }
   return(0);
  }


void isHitBollingerBand(int candleIdx){        
        int dir[]={1,-1};
         bool setUp=false;
         bool setDown=false;
        for (int j=0;j<ArraySize(dir);j++){
            double bands = iBands(_Symbol,0,InpBandsPeriod,InpBandsDeviation,0,PRICE_CLOSE,dir[j]<0?MODE_LOWER:MODE_UPPER,candleIdx);
            double _peak = dir[j]<0 ? iLow(_Symbol,0,candleIdx): iHigh(_Symbol,0,candleIdx);  
            if(dir[j] * (_peak-NormalizeDouble(bands,Digits) ) >= 0){                         
              double result=_peak+ dir[j]*3*POINT;
              if(dir[j]==1)
               CrossedUp[candleIdx]=result;
              else
               CrossedDown[candleIdx]=result;                  
      }            
    }

}

Below is the code of EA makes use of the custom indicator (only the essential parts - the code is runnable) (HitBollingerBandEA.mq4)

int init()  {     
   start();
   return(0);
  }
sinput string s_  = " Bolinger Bands ";   //  ind#1
input int      InpBandsPeriod    = 20;    //period
input double   InpBandsDeviation = 2.0;   //deviation
int start()
  {
         double HitBollingerBandUp=iCustom(NULL, 0, "HitBollingerBand1", InpBandsPeriod,InpBandsDeviation,0,1);
         double HitBollingerBandDown=iCustom(NULL, 0, "HitBollingerBand1", InpBandsPeriod,InpBandsDeviation,1,1);
         if(HitBollingerBandUp!=EMPTY_VALUE){
            printf("%i%s [%s]",__LINE__,__FUNCTION__,TimeToStr(Time[1],TIME_DATE|TIME_MINUTES));                 
        }
   return(0);
  }
 
jackee1234:

When I use the custom indicator, it shows the graph as follows 

When I use the Expert Advisor with the custom indicator, it shows the following 

It also repeated opened a lot of custom indicator when I am doing backtesting.

How should I change the EA such that

  1. It only opened a custom indicator;

  2. It plot the same graph as the custom indicator?

Below is the code for custom indicator (HitBollingerBand1.mq4)

Below is the code of EA makes use of the custom indicator (only the essential parts - the code is runnable) (HitBollingerBandEA.mq4)

You are not cleaning up (setting to empty value) the CrossedUP and CrossedDown buffers before assigning values to them in the indicator. That can cause you a classical case of repainting and that, in return, can cause the issues you have from the ea

	          
 
Thanks, how do I make sure that it only opens an instance of indicator rather than opened huge amount instances of indicators after each call of start() in EA?
 
jackee1234 how do I make sure that it only opens an instance of indicator rather than opened huge amount instances of indicators after each call of start() in EA?
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. double HitBollingerBandUp=iCustom(NULL, 0, "HitBollingerBand1", InpBandsPeriod,InpBandsDeviation,0,1);
    double HitBollingerBandDown=iCustom(NULL, 0, "HitBollingerBand1", InpBandsPeriod,InpBandsDeviation,1,1);
    Don't open a "huge amount" iCustom calls with different parameters. What you posted is one iCustom call.
  3. When I use the Expert Advisor with the custom indicator, it shows the following ||
    Icustom does not show anything. Attached indicators has nothing to do with iCustom. You should write a self documenting function instead of calling iCustom directly, see Detailed explanation of iCustom - MQL4 forum