How to get access to the Open/High/Close/Low candles of Other Timeframes from Inside the OnCalculate() method of indicator

 

Imagine the input timeframe for the indicator is PERIOD_H4 and indicator is placed on the M15 timeframe.

Inside the OnCalculate() I'd need to access the Open/High/Close/Low candles of the input timeframe which is H4 but apparently inside OnCalculate() we only have access to the current timeframe so the iOpen(), iClose(), iLow(), iHigh() of other timeframes don't work.

How can we access to buffer accessing these values from other timeframes and how to refresh/update the offer?


ENUM_TIMEFRAMES inputTimeframe = PERIOD_H4;
double         OpenBuffer[];

int OnInit()
 {
  OpenBuffer[0] = iOpen(NULL, inputTimeframe  , 0);
  OpenBuffer[1] = iOpen(NULL, inputTimeframe  , 1);
  OpenBuffer[2] = iOpen(NULL, inputTimeframe  , 2);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
      // how to update the buffer? we only have access to current timeframe? is there any easier way apart from shifting and dividing etc?

}
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
  • www.mql5.com
Chart Timeframes - Chart Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Create a Buffer (dynamic) with the open of the PERIOD_H4 timeframe 
 
Pooya Khamooshi: apparently inside OnCalculate() we only have access to the current timeframe so the iOpen(), iClose(), iLow(), iHigh() of other timeframes don't work.
  1. They work fine.

    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 - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20

    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.12.15
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum 2019.05.31
              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.07.17
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum 2019.09.03
              OnCalculate during weekend MT5 - General - MQL5 programming forum

  2. int OnInit()
     {
      OpenBuffer[0] = iOpen(NULL, inputTimeframe  , 0);
      OpenBuffer[1] = iOpen(NULL, inputTimeframe  , 1);
      OpenBuffer[2] = iOpen(NULL, inputTimeframe  , 2);
    }

    Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
Reason: