How do I tell my icustom indicator to read past historical bars in the EA?

 

Hi,


I've made a custom indicator similar to bolinger bands.


My EA is supposed to open and close orders when the price crosses the bands, but for about the first 200 bars, it opens and closes orders about 50 pips above the bands.


I believe the problem is because the custom indicator needs to read through some historical bars to start accurately calculating, but I have no idea how to tell my EA to let my indicator to do that.


I'm including the code for the indicator, hopefully it's an easy fix.


#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 White
#property indicator_color2 Blue
#property indicator_color3 Yellow
    
       int    average=3;
extern double till_breakeven_rate=5;  
extern double breakeven_buffer1=25;       
extern string SetOne="**********************Set_One_Settings***************";  
extern double space1=1;                         
extern double expand_rate1=35;
extern string SetTwo="**********************Set_Two_Settings***************"; 
extern double start_at_pips_profit2=50;
extern double space2=1;
extern double expand_rate2=50;
extern string SetThree="**********************Set_Three_Settings***********";
extern double start_at_pips_profit3=100;
extern double space3=1;
extern double expand_rate3=65;


double AverageFX[];
double Top[];
double Bottom[];

int max,order;
double bed,tal,atr,step,deviation,bu,sl,maxlow,maxhigh,isranging,istime,cat,dif,breakeven_bufferX,spaceX,expand_rateX,profitX=0,till_breakeven_rateX;
bool ranging;

int init()
  {
   max=50;  
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,AverageFX);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Top);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,Bottom);
   
   SetIndexDrawBegin(0,max);
   SetIndexDrawBegin(1,max);
   SetIndexDrawBegin(2,max);
   
   if(Digits==5 || Digits==3)  { dif=Point*10; } else dif=Point; 
 
   return(0);
  }

int start()
  {
   int    i,k,x,counted_bars=IndicatorCounted();
   double buffer,sum,newres,BandsDeviation,distance,sub,expandingx=till_breakeven_rate,mh;
   bool condition1=false;
   
   if(Bars<=max) { return(0); }
   if(counted_bars<1)
      for(i=1;i<=max;i++)
        {
         AverageFX[Bars-i]=EMPTY_VALUE;
         Top[Bars-i]=EMPTY_VALUE;
         Bottom[Bars-i]=EMPTY_VALUE;
         }

   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   for(i=0; i<limit; i++)
      AverageFX[i]=iMA(NULL,0,average,0,MODE_EMA,PRICE_CLOSE,i);

   i=Bars-max+1;
   if(counted_bars>max-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
     if(order==0) { profitX=1; }
     if(profitX<start_at_pips_profit2) {
         cat=1;
         breakeven_bufferX=breakeven_buffer1*dif;
         spaceX=space1*dif;
         expand_rateX=expand_rate1*dif; }
     if(profitX>=start_at_pips_profit2 && profitX<=start_at_pips_profit3) {
         cat=2; 
         breakeven_bufferX=breakeven_buffer1*dif;
         spaceX=space2*dif;
         expand_rateX=expand_rate2*dif; } 
     if(profitX>start_at_pips_profit3) { 
         cat=3;
         breakeven_bufferX=breakeven_buffer1*dif;
         spaceX=space3*dif;
         expand_rateX=expand_rate3*dif; } 
         
      buffer=6*spaceX;
      till_breakeven_rateX=till_breakeven_rate*dif;

      if((order==1) && (step<(bu+breakeven_bufferX)))   { expandingx=till_breakeven_rateX; }
      if((order==1) && (step>(bu+breakeven_bufferX)))   { expandingx=expand_rateX;  }
      if((order==2) && (step>(sl-breakeven_bufferX)))   { expandingx=till_breakeven_rateX; }
      if((order==2) && (step<(sl-breakeven_bufferX)))   { expandingx=expand_rateX;  }
     
      if(order==0) { step=AverageFX[i]-till_breakeven_rateX; }
      if(order==1) { // Buy
         if((AverageFX[i]>step) && MathAbs(AverageFX[i]-step)>expandingx)  { step=AverageFX[i]-expandingx;  }}
      if(order==2) { // Sell
         if((AverageFX[i]<step) && MathAbs(AverageFX[i]-step)>expandingx)  { step=AverageFX[i]+expandingx;  }}
 
      if(order==1) { 
      Top[i]=step;
      Bottom[i]=step-buffer; }
      if(order==2) { 
      Top[i]=step+buffer;
      Bottom[i]=step; }

      if(AverageFX[i]>Top[i])    { order=1; if(bu==0) { bu=AverageFX[i]; sl=0; profitX=0; }}
      if(AverageFX[i]<Bottom[i]) { order=2; if(sl==0) { sl=AverageFX[i]; bu=0; profitX=0; }}
      
      if(order==1) { if(NormalizeDouble(MathAbs(AverageFX[i]-bu)*10000,0)>profitX) { profitX=NormalizeDouble(MathAbs(AverageFX[i]-bu)*10000,0); }}
      if(order==2) { if(NormalizeDouble(MathAbs(AverageFX[i]-sl)*10000,0)>profitX) { profitX=NormalizeDouble(MathAbs(AverageFX[i]-sl)*10000,0); }}

      i--; }
 
   return(0);
  }
  
  
 

Also I've tried loading the past 4000 values for the indicator in init() but that doesn't change anything.

int init()  {
    ArraySetAsSeries(Top,true); ArraySetAsSeries(Bottom,true); 
      int i;      
         for(i=1;i<4000;i++) {
         Top[i]=iCustom(NULL,TimeFrame,"carmens_eyes_v2",till_breakeven_rate,breakeven_buffer1,space1,expand_rate1,start_at_pips_profit2,space2,expand_rate2,
                start_at_pips_profit3,space3,expand_rate3,1,i); 
         Bottom[i]=iCustom(NULL,TimeFrame,"carmens_eyes_v2",till_breakeven_rate,breakeven_buffer1,space1,expand_rate1,start_at_pips_profit2,space2,expand_rate2,
                start_at_pips_profit3,space3,expand_rate3,2,i); }}
Reason: