I write some code but don't know how to make it work

 

It is called Bar Direction. My idea is that if the current bar has C higher than Midpoint, it will be equal to 1 and vice versa. But if that bar is a perfect Doji (C = O and C = Midpoint), it will be equal to Direction of the Previous Bar. That's when the problem arose and I don't know how to solve it.

//+-IBS--------------------------------------------------------------------------------------------------------+
  double BarRange = High[n] - Low[n];
  if (BarRange != 0) double IBS = (Close[n] - Low[n]) / BarRange * 100;
  if (Close[n] > Open[n] && IBS >= 50) BarDir[n] = 1;
  else if (Close[n] < Open[n] && IBS <= 50) BarDir[n] = -1;
  else if (Close[n] == Open[n] && IBS > 50) BarDir[n] = 1;
  else if (Close[n] == Open[n] && IBS < 50) BarDir[n] = -1;
  else if (Close[n] == Open[n] && IBS == 50) BarDir[n] = BarDir[n+1]; <------------------ Problem here
  else if (Close[n] < Open[n] && IBS > 50) BarDir[n] = 1;
  else if (Close[n] > Open[n] && IBS < 50) BarDir[n] = -1;
  else if (IBS == 50) BarDir[n] = BarDir[n+1];
  else BarDir[n] = BarDir[n+1];

I know that I cannot put [n] behind BarDir. I just want to show that I want it to work like that so...

Can you guys show me the way to make it work?

Thank you in advance!

 
IITJV: I know that I cannot put [n] behind BarDir.
double BarRange = High[n] - Low[n];
Since you are computing bar n, you must. Make BarDir a buffer.
 
William Roeder #:
Since you are computing bar n, you must. Make BarDir a buffer.

Thank you so much! I really did it. You made my day William.

Thank you again,

Have a nice day!