MACD histogram arrow display movement changes direction

Below indicator displays up/ down arrows when MACD histogram changes direction. I am trying to get it to change the arrow to display on the current bar (0) as opposed to bar 1. i.e. when MACD changes direction and candle closes I would like arrow to be displayed on the following candle as opposed to that one. I have tried changing last two if statements ‘i’ but have had no luck… I feel like the solution should be an easy one but am unable to find an answer.

Any help or advice much appreciated.

//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrLawnGreen
#property indicator_color2 clrRed
#property strict
//------------------------------------------------------------------
extern int                Fast_EMA = 12;          // Fast ema period
extern int                Slow_EMA = 26;          // Slow ema period
extern ENUM_APPLIED_PRICE price    = PRICE_CLOSE; // Price

double CrossUp[],CrossDown[],direction[],macd[];
int OnInit()
{
  IndicatorBuffers(4);
  SetIndexBuffer(0, CrossUp);   SetIndexStyle(0, DRAW_ARROW); SetIndexArrow(0, 225);
  SetIndexBuffer(1, CrossDown); SetIndexStyle(1, DRAW_ARROW); SetIndexArrow(1, 226);
  SetIndexBuffer(2, direction); 
  SetIndexBuffer(3, macd); 
 return(0);
}
int OnCalculate (const int       rates_total,
                const int       prev_calculated,
                const datetime& btime[],
                const double&   open[],
                const double&   high[],
                const double&   low[],
                const double&   close[],
                const long&     tick_volume[],
                const long&     volume[],
                const int&      spread[] )
{
  int counted_bars = prev_calculated;
     if(counted_bars < 0) return(-1);
     if(counted_bars > 0) counted_bars--;
           int limit=MathMin(rates_total-counted_bars,rates_total-1);

     for(int i=limit; i>-0; i--)
     {
        macd[i]      = iMA(NULL,0,Fast_EMA,0,MODE_EMA,price,i)-iMA(NULL,0,Slow_EMA,0,MODE_EMA,price,i);
        direction[i] = (i<Bars-1) ? (macd[i]>macd[i+1]) ? 1 : (macd[i]<macd[i+1]) ? -1 : 0 : 0;
        CrossUp[i]   = EMPTY_VALUE;
        CrossDown[i] = EMPTY_VALUE;
        if (i<Bars-1 && direction[i]!=direction[i+1])
        {
              if (direction[i]== 1) CrossUp[i]   = Low[i]  - iATR(NULL,0,10,i);
              if (direction[i]==-1) CrossDown[i] = High[i] + iATR(NULL,0,10,i);
        }
 }
 return(rates_total);
}

I have tried changing

The main loop ends at 1.

for(int i=limit; i>=0; i--)
111

Hi

I have tried changing to what you suggested but arrows stay displayed in the same place… any other suggestions?

Thanks

for(int i=limit; i>0; i--)
      if (direction[i]== 1) CrossUp[i-1]   = Low[i-1]  - iATR(NULL,0,10,i-1);
Try something like that.

When I do this arrows no longer appear at all on chart…

{
   int j,limit = prev_calculated<1 ? rates_total-2 : rates_total-prev_calculated; 

   for(int i=limit; i>=0; i--)
     {
      j=i+1;
      macd[j]      = iMA(NULL,0,Fast_EMA,0,MODE_EMA,price,j)-iMA(NULL,0,Slow_EMA,0,MODE_EMA,price,j);
      direction[j] = (j<rates_total-1) ? (macd[j]>macd[j+1]) ? 1 : (macd[j]<macd[j+1]) ? -1 : 0 : 0;
      CrossUp[i]   = EMPTY_VALUE;
      CrossDown[i] = EMPTY_VALUE;
      if(j<rates_total-1 && direction[j]!=direction[j+1])
        {
         if(direction[j]== 1) CrossUp[i]   = low[j]  - iATR(NULL,0,10,j);
         if(direction[j]==-1) CrossDown[i] = high[j] + iATR(NULL,0,10,j);
        }
     }
   return(rates_total);
  }

Wow! nice thank you so much! that works great!!