indicator updating question

 

Hello Forum, I have the following question which has shown up a BIG gap in my base understanding of how and when indicators update.

Why is comparing indicator values for bars 0 to 1, different to comparing indicator values bars i to (i+1), in the context below?

Context:

I put together an indicator which I hoped would simplistically show ( as a discrete arrow on the main chart, see attached picture) the change in direction of the MACD line.

The arrow on the main chart simply changes from either upwards, downwards or sideways as the direction of the MACD line changes.

All went well until I noticed some synchronisation issues, that is, the chart arrow seemed to lag when changing direction relative to the MACD line visible on the chart.

Anyway with a lot of exploring, I found the problem lay in whether I defined the change in my MACD line slope as:

1. Shown by Black Arrow

MACD_LineAngle =(iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,0)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,0))-
                        ((iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,1)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,1)));

or 2. Shown by Blue Arrow

MACD_LineAngle =(iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i))-
                        ((iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i+1)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i+1)));

Now I am only interested in what is happening in the current active bar, versus the previous closed bar and I thought the above 2 methods would give the same result.

The black arrow is updating as planned (changing directions sometimes intra bar), the blue is not working the way I had hoped

Would someone mind telling me why they are behaving differently in this context?.....

thanks in advance (indicator code also attached)



#property  copyright "dpdp"
//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 0

//---- indicator parameters


extern int    FastEMA                            = 12;
extern int    SlowEMA                            = 26;
extern int    SignalSMA                          = 9;
extern string chart_corner_Help                  = "RightTop =1, LeftTop=4";
extern double AngleMin                           = 0.005;  // change to 0.000005 if applying to forex
double        MACD_LineAngle;

//-------------MACD_LineAngleArrow1----------------------------------------------------------------------
extern int    chart_corner                       = 1;
extern int    arrow_size                         = 50;
extern int    x_distance                         = 100;
extern int    y_distance                         = 0;
extern int    applied_price                      = 0;

double        wingding_code;
extern bool   Show_MACD_LineAngleArrow1          = true;   // set true to display arrow
//--------------MACD LineAngleArrow2--------------------------------------------------------------------
double        macdlineanglearrowwingding2, c1;
extern bool   Show_MACD_LineAngleArrow2          = true;   // set false to hide
extern int    macdlineanglearrow2_x_distance     = 50;
extern int    macdlineanglearrow2_y_distance     = 0;
extern int    macdlineanglearrow2size            = 50;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
   int init()
   {
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
   
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD_LINE _ANGLE ("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
   return(0);
  }
//+-------------------------------------------------------------------------------------------------------+
//---- Custom indicator deinitialization function                                                         |
//+-------------------------------------------------------------------------------------------------------+
   int deinit()
   {
   ObjectDelete("MACD_LineAngleArrow1");
   ObjectDelete("MACD_LineAngleArrow2");
       
   return(0);
   }
//+-------------------------------------------------------------------------------------------------------+  
//| The angle for MACD_LineAngle                                                |
//+------------------------------------------------------------------+
int start()
  {
   if (applied_price>=7) applied_price=0;
   int limit;
   int counted_bars=IndicatorCounted();
  //---- check for possible errors
   if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
  //---- main loop
   for(int i=0; i<limit; i++)
//------------------------------------------------------------------------------------------------------------------------------------------------------------
//-- MACD LINE ANGLE BLACK ARROW
//------------------------------------------------------------------------------------------------------------------------------------------------------------
      MACD_LineAngle=(iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,0)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,0))-
                       ((iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,1)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,1)));
              
      if(MACD_LineAngle>AngleMin)
         wingding_code=233;
      else if (MACD_LineAngle<-AngleMin)
         wingding_code=234;
      else 
         wingding_code=232;
     
      if (Show_MACD_LineAngleArrow1)
      ObjectCreate    ("MACD_LineAngleArrow1", OBJ_LABEL,0,0,0);                                          // Creating Object
      ObjectSet       ("MACD_LineAngleArrow1", OBJPROP_CORNER,   chart_corner);                           // Reference Corner
      ObjectSet       ("MACD_LineAngleArrow1", OBJPROP_XDISTANCE,x_distance);                             // X corodinate
      ObjectSet       ("MACD_LineAngleArrow1", OBJPROP_YDISTANCE,y_distance);                             // Y coordinate
      ObjectSetText   ("MACD_LineAngleArrow1", CharToStr(wingding_code), arrow_size, "Wingdings", Black);
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
//-- MACD LINE ANGLE BLUE ARROW
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
                  c1=(iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i))-
                       ((iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i+1)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i+1)));
           
      if (c1>AngleMin)
         macdlineanglearrowwingding2=233;
      else if (c1<-AngleMin)
         macdlineanglearrowwingding2=234;
      else 
         macdlineanglearrowwingding2=232;
         
      if (Show_MACD_LineAngleArrow2) // draw if true
      ObjectCreate    ("MACD_LineAngleArrow2", OBJ_LABEL,0,0,0);                                     // Creating Object
      ObjectSet       ("MACD_LineAngleArrow2", OBJPROP_CORNER,   chart_corner);       // Reference Corner
      ObjectSet       ("MACD_LineAngleArrow2", OBJPROP_XDISTANCE,macdlineanglearrow2_x_distance);    // X corodinate
      ObjectSet       ("MACD_LineAngleArrow2", OBJPROP_YDISTANCE,macdlineanglearrow2_y_distance);    // Y coordinate
      ObjectSetText   ("MACD_LineAngleArrow2", CharToStr(macdlineanglearrowwingding2),macdlineanglearrow2size, "Wingdings", Blue);
      //--
     return(0);
   }


 
pullend:

Hello Forum, I have the following question which has shown up a BIG gap in my base understanding of how and when indicators update.

Why is comparing indicator values for bars 0 to 1, different to comparing indicator values bars i to (i+1), in the context below?

Well that depends on what the final value of  i  is when the for loop exits,  what is it's value ?  if it's 0 then there is no difference.
Reason: