Help with Indicator changes value

 

Hi Everyone,

I wrote an indicator to filter undesirable condition. So it use a lot of iCustom.

When I do backtest, it show different value from real trade. On the visual chart after backtest, the indicator show up (automatically). Then I attach the indicator to that visual chart to make comparison, the value are not the same!

On the same live chart, I attach this indicator few minutes apart and the value are not the same.

The discrepancy is in recent and past bars.

My indicator use D1 iCustome value on H4, and iCustom H4 on H4 as below.

Can you help me with that problem?

Is this caused by the for(I=numberbars-1; I>=1; I--)?

I appreciate your help.

SCFX

      int    counted_bars=IndicatorCounted();
      int    i;
      int    numberbars=Bars - counted_bars;
      double d1bar_0,d1bar_1,d1bar_2,d1bar_3,d1bar_4,h4bar_0,h4bar_1,h4bar_2,h4bar_3,h1bar_0,h1bar_1,h1bar_2,h1bar_3;
      ;
      if(counted_bars<5) counted_bars=5;

//----

for(i=numberbars-1; i>=1;i--)
   {     
         d1bar_0 = iBarShift(Symbol(), D1, Time[i]);          //this one is for checking purpose on D1 only
         d1bar_1 = iBarShift(Symbol(), D1, Time[i]) + 1;      // D1 Bars of H4  
         d1bar_2 = iBarShift(Symbol(), D1, Time[i]) + 2;      // D1_1 Bars see the first condition
         d1bar_3 = iBarShift(Symbol(), D1, Time[i]) + 3;
         d1bar_4 = iBarShift(Symbol(), D1, Time[i]) + 4;

         h4bar_0 = iBarShift(Symbol(), H4, Time[i]);  
         h4bar_1 = iBarShift(Symbol(), H4, Time[i]) + 1;    
         h4bar_2 = iBarShift(Symbol(), H4, Time[i]) + 2;    
         h4bar_3 = iBarShift(Symbol(), H4, Time[i]) + 3; 
            
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

if(  

   //set3-start ------------------------------------------------------------------------------------------------------------------------------;      
   //D1Hist_M_change_D_color_0=-1       trend6_3MA=-3   D1MA15_50=1
      (  iMACD(NULL,D1,20,50,11,0,0,d1bar_2)>iMACD(NULL,D1,20,50,11,0,0,d1bar_3) && iMACD(NULL,D1,20,50,11,0,0,d1bar_2)>iMACD(NULL,D1,20,50,11,0,0,d1bar_1)       
      && iCustom(NULL,0,"H_trend_on3MA",5,15,50,0,i)==-3 
      && iMA(NULL,D1,15,0,MODE_EMA,0,d1bar_1)>iMA(NULL,D1,50,0,MODE_EMA,0,d1bar_1)  ) 
          
   ||  
   //D1ma3_2dtrend_1=3  adx_cut_D_1=1   adx_main_level=2
      (  iCustom(NULL,D1,"H_trend_ontrend_3MA",5,15,50,1,d1bar_2)==3      
      && iADX(NULL,0,14,0,1,i+2)<iADX(NULL,0,14,0,2,i+2) && iADX(NULL,0,14,0,1,i+1) > iADX(NULL,0,14,0,2,i+1)   
      && 20<iADX(NULL,0,14,0,0,i)&& iADX(NULL,0,14,0,0,i)<=27  )
      
   ||   
   //D1ma3_1dtrend=3 and adxcode=4 and macd3_2dtrend_2=2 
      (  iCustom(NULL,D1,"H_trend_ontrend_3MA",5,15,50,0,d1bar_1)==3 
      && iADX(NULL,0,14,0,1,i)>iADX(NULL,0,14,0,2,i) && iADX(NULL,0,14,0,0,i)>iADX(NULL,0,14,0,1,i) 
      && iCustom(NULL,0,"H_trend_ontrend_3macd",5,15,50,1,i+2)==2    )
...
...
)
filter[i]=1;
 

Your code as posted above is too wide 

This is more readable

   for(i=numberbars-1; i>=1;i--)
     {
      d1bar_0 = iBarShift(Symbol(), H1, Time[i]);
      d1bar_1 = iBarShift(Symbol(), H1, Time[i]) + 1;
      d1bar_2 = iBarShift(Symbol(), H1, Time[i]) + 2;
      d1bar_3 = iBarShift(Symbol(), H1, Time[i]) + 3;
      d1bar_4 = iBarShift(Symbol(), H1, Time[i]) + 4;
      //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

      if(
         (iMACD(NULL,D1,20,50,11,0,0,d1bar_2)>iMACD(NULL,D1,20,50,11,0,0,d1bar_3)
         && iMACD(NULL,D1,20,50,11,0,0,d1bar_2)>iMACD(NULL,D1,20,50,11,0,0,d1bar_1)
         && iCustom(NULL,0,"H_trend_on3MA",5,15,50,0,i)==-3
         && iMA(NULL,D1,15,0,MODE_EMA,0,d1bar_1)>iMA(NULL,D1,50,0,MODE_EMA,0,d1bar_1))
         || 
         (iCustom(NULL,D1,"H_trend_ontrend_3MA",5,15,50,1,d1bar_2)==3
         && iADX(NULL,0,14,0,1,i+2)<iADX(NULL,0,14,0,2,i+2) && iADX(NULL,0,14,0,1,i+1)>iADX(NULL,0,14,0,2,i+1)
         && 20<iADX(NULL,0,14,0,0,i) && iADX(NULL,0,14,0,0,i)<=27)
         )
         }
        

 

 I don't understand the point of this

      d1bar_0 = iBarShift(Symbol(), H1, Time[i]);
      d1bar_1 = iBarShift(Symbol(), H1, Time[i]) + 1;
      d1bar_2 = iBarShift(Symbol(), H1, Time[i]) + 2;
      d1bar_3 = iBarShift(Symbol(), H1, Time[i]) + 3;
      d1bar_4 = iBarShift(Symbol(), H1, Time[i]) + 4;

 If on an H4 chart, depending on the time, d1bar_0 could be 0,1,2 or 3 so 4 possible different sets of values

 

Sorry, let me clean the code a bit.

Right now I use this indicator on H4 chart. So the D1 of the previous day will be used to make sure that this D1 is totally closed.


      int    counted_bars=IndicatorCounted();
      int    i;
      int    numberbars=Bars - counted_bars;
      double d1bar_0,d1bar_1,d1bar_2,d1bar_3,d1bar_4,h4bar_0,h4bar_1,h4bar_2,h4bar_3,h1bar_0,h1bar_1,h1bar_2,h1bar_3;
      ;
      if(counted_bars<5) counted_bars=5;

//----

for(i=numberbars-1; i>=1;i--)
   {     
         d1bar_0 = iBarShift(Symbol(), D1, Time[i]);          //this one is for checking purpose on D1 only
         d1bar_1 = iBarShift(Symbol(), D1, Time[i]) + 1;      // D1 Bars of H4  
         d1bar_2 = iBarShift(Symbol(), D1, Time[i]) + 2;      // D1_1 Bars see the first condition
         d1bar_3 = iBarShift(Symbol(), D1, Time[i]) + 3;
         d1bar_4 = iBarShift(Symbol(), D1, Time[i]) + 4;

         h4bar_0 = iBarShift(Symbol(), H4, Time[i]);  
         h4bar_1 = iBarShift(Symbol(), H4, Time[i]) + 1;    
         h4bar_2 = iBarShift(Symbol(), H4, Time[i]) + 2;    
         h4bar_3 = iBarShift(Symbol(), H4, Time[i]) + 3; 
            
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

if(  

   //set3-start ------------------------------------------------------------------------------------------------------------------------------;      
   //D1Hist_M_change_D_color_0=-1       trend6_3MA=-3   D1MA15_50=1
      (  iMACD(NULL,D1,20,50,11,0,0,d1bar_2)>iMACD(NULL,D1,20,50,11,0,0,d1bar_3) && iMACD(NULL,D1,20,50,11,0,0,d1bar_2)>iMACD(NULL,D1,20,50,11,0,0,d1bar_1)       
      && iCustom(NULL,0,"H_trend_on3MA",5,15,50,0,i)==-3 
      && iMA(NULL,D1,15,0,MODE_EMA,0,d1bar_1)>iMA(NULL,D1,50,0,MODE_EMA,0,d1bar_1)  ) 
          
   ||  
   //D1ma3_2dtrend_1=3  adx_cut_D_1=1   adx_main_level=2
      (  iCustom(NULL,D1,"H_trend_ontrend_3MA",5,15,50,1,d1bar_2)==3      
      && iADX(NULL,0,14,0,1,i+2)<iADX(NULL,0,14,0,2,i+2) && iADX(NULL,0,14,0,1,i+1) > iADX(NULL,0,14,0,2,i+1)   
      && 20<iADX(NULL,0,14,0,0,i)&& iADX(NULL,0,14,0,0,i)<=27  )
      
   ||   
   //D1ma3_1dtrend=3 and adxcode=4 and macd3_2dtrend_2=2 
      (  iCustom(NULL,D1,"H_trend_ontrend_3MA",5,15,50,0,d1bar_1)==3 
      && iADX(NULL,0,14,0,1,i)>iADX(NULL,0,14,0,2,i) && iADX(NULL,0,14,0,0,i)>iADX(NULL,0,14,0,1,i) 
      && iCustom(NULL,0,"H_trend_ontrend_3macd",5,15,50,1,i+2)==2    )
...
...
)
filter[i]=1;
 

I still don't get it

Why

         d1bar_0 = iBarShift(Symbol(), D1, Time[i]);          //this one is for checking purpose on D1 only
         d1bar_1 = iBarShift(Symbol(), D1, Time[i]) + 1;      // D1 Bars of H4  
         d1bar_2 = iBarShift(Symbol(), D1, Time[i]) + 2;      // D1_1 Bars see the first condition
         d1bar_3 = iBarShift(Symbol(), D1, Time[i]) + 3;
         d1bar_4 = iBarShift(Symbol(), D1, Time[i]) + 4;

 Why not

         d1bar_0 = iBarShift(Symbol(), D1, Time[i]);          //this one is for checking purpose on D1 only
         d1bar_1 = d1bar_0 + 1;                               // D1 Bars of H4  
         d1bar_2 = d1bar_0 + 2;                            // D1_1 Bars see the first condition
         d1bar_3 = d1bar_0 + 3;
         d1bar_4 = d1bar_0 + 4;

 Seems silly to keep calculating the same function when you have the result stored as a variable

Same goes for H4 calculations 

 
scfx:
int    counted_bars=IndicatorCounted();
int    numberbars=Bars - counted_bars;
if(counted_bars<5) counted_bars=5; <<< This line does nothing. Therefor:
for(i=numberbars-1; i>=1;i--) 
   iADX(NULL,0,14,0,2,i+2)         <<< This looks back before start of chart.
  1. int    counted_bars=IndicatorCounted();
    if(counted_bars<2) counted_bars=2;          // Handle look back of 2
    for(i=Bars - 1 - counted_bars; i>=1;i--) 
        iADX(NULL,0,14,0,2,i+2)

  2. GumRai: Seems silly to keep calculating the same function when you have the result stored as a variable
    Agree. Also silly to use variables named varX where it contains var0 + X
  3. Your if statement is unreadable. You need to organize so you can read it.
    UnreadableSelf documenting
    if(

    //set3-start --------------------------------------------------------------
    //D1Hist_M_change_D_color_0=-1       trend6_3MA=-3   D1MA15_50=1
    (  iMACD(NULL,D1,20,50,11,0,0,d1bar_2) > iMACD(NULL,D1,20,50,11,0,0,d1bar_3) && iMACD(NULL,D1,20,50,11,0,0,d1bar_2) > iMACD(NULL,D1,20,50,11,0,0,d1bar_1)
    && iCustom(NULL,0,"H_trend_on3MA",5,15,50,0,i)==-3
    && iMA(NULL,D1,15,0,MODE_EMA,0,d1bar_1)>iMA(NULL,D1,50,0,MODE_EMA,0,d1bar_1)  )

    ||                                                   
    //D1ma3_2dtrend_1=3  adx_cut_D_1=1   adx_main_level=2

    double   d1Macd[4],
             trendOn     = iCustom(NULL,0,"H_trend_on3MA",5,15,50,0,i),
             D1EmaFast   = iMA(NULL,D1,15,0,MODE_EMA,0,d1bar_1),
             D1EmaSlow   = iMA(NULL,D1,50,0,MODE_EMA,0,d1bar_1);
             d1Mad[3]    = iMACD(NULL,D1,20,50,11,0,0,d1bar_3);
             d1Mad[2]    = iMACD(NULL,D1,20,50,11,0,0,d1bar_2);
             d1Mad[1]    = iMACD(NULL,D1,20,50,11,0,0,d1bar_1);
    bool
     isD1ColorChange  = d1Macd[3]) < d1Macd[2] && d1Macd[2] > d1Macd[1],
     isTrendOn        = trendOn == -3 ,
     isD1UpTrend      = D1EmaFast > D1EmaSlow,
     isCondition1     = isD1ColorChange && isTrendOn && isD1UpTrend
    if(  
       isCondition1
       ||                                                   
       //D1ma3_2dtrend_1=3  adx_cut_D_1=1   adx_main_level=2
    

 

First, thank you both for pointing these silly problem.

I don't know how to 'import' the D1 value on to H4/H1, so someone tell me the trick and I just follow it. "Unfortunately" it works, so I just follow until today you point out the better way.

That counted_bar=5, I think there are condition looking back 5 bar. So I put it there.

That self-documenting is so great, I didn't know that and I will do it that way.

Many thanks,

SCFX