Indicator draws with incorrect shift

 

Hello, 

I am trying to draw an indicator when MACD turns direction from up to down. Per my reasoning, at the start of a new bar, if MACD at bar[1] is less than the MACD[2] and MACD[2] is greater than MACD[3], indicator should draw above the the current new bar.

However the indicator draws above the bar when the formation completes not the one after....Can someone help me with what I am doing wrong?

int start()
{
ExtCountedBars=IndicatorCounted();
pos=Bars-ExtCountedBars-1;
int LongSignal, ShortSignal;
bool MACDTrendChangeDown, MACDTrendChangeUp;
while(pos>=0)
{
MACDTrendChangeDown = false;
MACDTrendChangeUp = false;

if (iMACD(Symbol(),PERIOD_H4,5,13,1,PRICE_CLOSE,MODE_MAIN, pos+1)<iMACD(Symbol(), PERIOD_H4,5,13,1,PRICE_CLOSE,MODE_MAIN,pos+2)
&&iMACD(Symbol(),PERIOD_H4, 5,13,1, PRICE_CLOSE, MODE_MAIN, pos+2)>iMACD(Symbol(), PERIOD_H4,5,13,1,PRICE_CLOSE, MODE_MAIN, pos+3))
MACDTrendChangeDown=true;

if (MACDTrendChangeDown==true){ExtMapBuffer1[pos]=Open[pos]+100*Point;}
pos--;
}
return(0);
}

 

Any help will be appreciated.

Reason: