How can I get the current day high and low on an expert advisor?


I am building an expert advisor for the 5m timeframe. However, this expert advisor needs to identify the low and the high of the current day since it will only buy if the price is at the low of the current day or sell if the price is at the high of the current day.

What is the code for getting the low or high of the current day?

This codes works correctly when I use it on a custom indicator:
(iHigh(_Symbol, PERIOD_D1, idx)
(iLow(_Symbol, PERIOD_D1, idx)

But the code doesn't work when I use it on an expert advisor. Apparently, it works well with the custom indicator because indicators use historical data.

However, when I use this code on an expert advisor it is not working as in the indicator, maybe because the expert advisor does not uses historical data, maybe it uses tick value of the daily candle which hasn't closed yet (because the current day has not finished yet). I really don't know what is happening but I need the expert advisor to give me the same signals as the custom indicator.

Any help would be appreciated. 


There is no high or low of the current day until the market is closed.
iHigh(_Symbol, PERIOD_D1, idx)

Is fine, but what is idx?

For the current day you should use

iHigh(_Symbol, PERIOD_D1, 0)

Just make sure that D1 data is up to date.

William William #:
There is no high or low of the current day until the market is closed.

Of course there is.

It is the high or low of the current day at the time.

I noticed that I was confused by the indicator, it looked so perfect because it already knew the final low / high of every day. But an expert advisor won't know that until the day is finished so its signals won't be as perfect as the indicator's.

My expert advisor is correctly coded. It was my mistake. Sorry

Thank you for your help
  1. You can use the D1 timeframe as Keith shown, but then you have to deal with 4066 / synchronization issue.

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

    On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
              Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

  2. Or just find the beginning of the day and then the current high.
              date/time (2017)
              Find bar of the same time one day ago - MQL4 programming forum (2017)

    datetime  bod=date();
    int      iBod=iBarShift(_Symbol, _Period, bod);
    int      iHod=iHighest(_Symbol, _Period, MODE_HIGH, iBod+1, 0);
    double   hod =iHigh(_Symbol, _Period, iHod);