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