# Getting incorrect results on Heiken Ashi calculation.

I was trying to find out open, high, close, low prices of a candle with the Heiken Ashi formula. Using a daily chart, heiken ashi close and low prices calculation gives correct results but from open and high prices calculation getting incorrect results. Have I applied the wrong formula? Could someone please help me with what I have missed? Thanks in advance.

```for(int i=0; i<10; i++) {
datetime time  =  iTime(Symbol(), Period(), i);

double HA_Open    =  (iOpen(Symbol(), Period(), i+1) + iClose(Symbol(), Period(), i+1))/2;
double HA_Close   =  (iOpen(Symbol(), Period(), i) +
iHigh(Symbol(), Period(), i) +
iLow(Symbol(), Period(), i) +
iClose(Symbol(), Period(), i))/4;

double HA_High    =  MathMax(iHigh(Symbol(), Period(), i), MathMax(HA_Open, HA_Close));
double HA_Low     =  MathMin(iLow(Symbol(), Period(), i), MathMin(HA_Open, HA_Close));

Print("Candle No:", i, ", Time: ", time, ", HA_Open: ", HA_Open, ", HA_Close: ", HA_Close, ", HA_High: ", HA_High, ", HA_Low: ", HA_Low);
}  ```

Using this formula:

Open of candle: (open of previous bar + close of previous bar) / 2
Close of candle: (open + high + low + close) / 4
High of candle: the maximum value from the high, open, or even close of the current period
Low of candle: the lowest value from the low, open, or close of the current period​

Please see the attached files for comparing log and chart prices.

Files:

Md Atikur Rahman: Have I applied the wrong formula
1. `for(int i=0; i<10; i++) {`

Yes. You are applying future bars to generate earlier ones.

2. Why are you not just reading the indicator?
take candle color hekin ashi - MQL4 and MetaTrader 4 #8-10 or #1 (2018)

William Roeder #:
1. Yes. You are applying future bars to generate earlier ones.

2. Why are you not just reading the indicator?
take candle color hekin ashi - MQL4 and MetaTrader 4 #8-10 or #1 (2018)

Thank you very much William for your help!

Actually I don't want to use the iCustom function for getting Heiken Ashi data.
Would you please tell me If I reverse the loop will it give me the correct result of open and high prices of a candle? Like
```for(int i=10; i>=0; i--) {
datetime time  =  iTime(Symbol(), Period(), i);

double HA_Open    =  (iOpen(Symbol(), Period(), i-1) + iClose(Symbol(), Period(), i-1))/2;
double HA_Close   =  (iOpen(Symbol(), Period(), i) +
iHigh(Symbol(), Period(), i) +
iLow(Symbol(), Period(), i) +
iClose(Symbol(), Period(), i))/4;

double HA_High    =  MathMax(iHigh(Symbol(), Period(), i), MathMax(HA_Open, HA_Close));
double HA_Low     =  MathMin(iLow(Symbol(), Period(), i), MathMin(HA_Open, HA_Close));

Print("Candle No:", i, ", Time: ", time, ", HA_Open: ", HA_Open, ", HA_Close: ", HA_Close, ", HA_High: ", HA_High, ", HA_Low: ", HA_Low);
}```

If I make a mistake again, could you please explain it to me. Because the calculation seems easy but somehow I stuck on the logic.

Md Atikur Rahman #: Would you please tell me If I reverse the loop will it give me the correct result of open and high prices of a candle?
`double HA_Open    =  (iOpen(Symbol(), Period(), i-1) + iClose(Symbol(), Period(), i-1))/2;`

No, because you are still reading future prices.

What about check the documentation and see the description of the parameters?

shift

[in]  The index of the received value from the timeseries (backward shift by specified number of bars relative to the current bar).

Documentation on MQL5: Timeseries and Indicators Access / iHigh
• www.mql5.com
iHigh - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Documentation on MQL5: Timeseries and Indicators Access / Indexing Direction in Arrays, Buffers and Timeseries
• www.mql5.com
Indexing Direction in Arrays, Buffers and Timeseries - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

William Roeder #:

No, because you are still reading future prices.

Now I have understood I need to read past data to calculate future prices. And I fixed my code, it is working now.

Thanks William! Really appreciate your help.

Samuel Manoel De Souza #:

What about check the documentation and see the description of the parameters?

shift

[in]  The index of the received value from the timeseries (backward shift by specified number of bars relative to the current bar).

Thank you very much Samuel!