The direction of for recycle in an indicator main loop changed,result changed

 

Hi Gentleman,

I am confused when I change the direction of for recycle from {i=0;i<limit ;i++} to {i=limit;i>=0;i--} or {i=limit-1;i>=0;i--}in a indicator main loop, the result are not the same, while the last indicator show more late than the previous one.

Here are the codes,

The  previous one source code can be found here:https://www.mql5.com/en/code/viewcode/8633/46617/sidus_v_2.mq4

The last one which I changed is,

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Magenta
//---
extern int timeframe  =0;
int       EMA_fast=50;
int       EMA_slow=85;
int       RSI_parameter=17;
//---- buffers
double buf1[];
double buf2[];
double buf3[];
double buf4[];
double buf5[];
double buf6[];
//---- variables
int sg_now=0;
int sg_last=0;
double diff=0.0;
int start()
 {
   int counted_bars=IndicatorCounted();
   double RSI_signal=0.0;
   //---- check for possible errors
   if (Bars<3) return (0); 
   if(counted_bars<0) return(-1);
   //---- last counted bar will be recounted 
   int limit=Bars-counted_bars; 
   if(counted_bars==0)limit--;
   if(counted_bars>0) limit++;
   //---- main loop
   for(int i=limit; i>=0; i--)
   {
     buf5[i] = buf5[i+1];
     buf6[i] = buf6[i+1];
     //---- ma_shift set to 0 because SetIndexShift called above
     buf1[i]=iMA(NULL,timeframe,EMA_fast,0,MODE_EMA,PRICE_CLOSE,i);
     buf2[i]=iMA(NULL,timeframe,EMA_slow,0,MODE_EMA,PRICE_CLOSE,i);
     RSI_signal = iRSI(NULL,timeframe,RSI_parameter,PRICE_CLOSE, i);
     diff=(buf1[i]-buf2[i])*1000;
     if (diff>0.0 && RSI_signal>50.0) sg_now = 1;  //Up
     else if (diff<0.0 && RSI_signal<50.0)sg_now = 2;  //Down
     
     if (sg_now==1 && sg_last==2)//UP
     {
        buf3[i] = Low[i]-15*Point;
        buf5[i] = Time[i];
     } 
     else if (sg_now==2 && sg_last==1)//DOWN
     {
        buf4[i] = High[i]+15*Point;
        buf6[i] = Time[i];
     }

     if(NewBar() || i>0)sg_last=sg_now;
   }
   return(0);
}
//+------------------------------------------------------------------+
bool NewBar() 
{
   static datetime lasttime;
   datetime CurTime = Time[0];
   if (lasttime != CurTime) 
   {
      lasttime = CurTime;
      return (true);
   }
   else return (false);
}

The result are the attached pictures,

We can see the right one are more late than the left one.

Could you know the reasion? And Could you help me to modify and complete this indicator?

Thank you very much in advance!

 

When counting up, sq-last is based on i-1

when counting down, sq_last is based on i+1 

 
kitgain: the result are not the same,
  1. buf1[i]=iMA(NULL,timeframe,EMA_fast,0,MODE_EMA,PRICE_CLOSE,i);
    buf2[i]=iMA(NULL,timeframe,EMA_slow,0,MODE_EMA,PRICE_CLOSE,i);
    RSI_signal = iRSI(NULL,timeframe,RSI_parameter,PRICE_CLOSE, i);
    You look back is the maximum of EMA_fast, EMA_slow, RSI_parameters, and 1 (buff5[i+1]).
    Handle it properly No need for the decrement.
       int limit=Bars-counted_bars; 
       if(counted_bars==0)limit--;
       if(counted_bars>0) limit++;
       //---- main loop
       for(int i=limit; i>=0; i--)
    
    int lookback = MathMax( MathMax(EMA_fast, EMA_slow),
                            MathMax(RSI_parameter, 1) );
    for(int i=Bars - 1 - MathMax(lookback, counted_bars); i>=0; i--)
    Contradictory information on IndicatorCounted() - MQL4 forum

  2.    if(NewBar() || i>0)sg_last=sg_now;
    You can't do this. NewBar is not indicating a change but i is changing in your loop and so must sg_last. When a new bar does occur, your loop recalculates bar one, sg_last must not be changed there but must be for bar zero.  sg_now must be saved in a buffer so you can set sg_last=buffer[i+1]
  3. Of course the results are not the same. If you are counting up, you are using future bars values (sig_last) to determine previous bar arrows. That's called repainting and is useless for trading.
 
WHRoeder:
  1. You look back is the maximum of EMA_fast, EMA_slow, RSI_parameters, and 1 (buff5[i+1]).
    Handle it properly No need for the decrement.
    Contradictory information on IndicatorCounted() - MQL4 forum

  2. You can't do this. NewBar is not indicating a change but i is changing in your loop and so must sg_last. When a new bar does occur, your loop recalculates bar one, sg_last must not be changed there but must be for bar zero.  sg_now must be saved in a buffer so you can set sg_last=buffer[i+1]
  3. Of course the results are not the same. If you are counting up, you are using future bars values (sig_last) to determine previous bar arrows. That's called repainting and is useless for trading.

Dear Uncle William,

Thank you very much for your answer.I will study it.

I have sent a private message to you but I'm not sure whether it is done ok.

If not, could you PM me or give me your email?

Thank you again!

wlonghai@sohu.com 

Reason: