Coding

 
//+------------------------------------------------------------------+
//|                                                   Indicators.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---

   datetime time1D1=iTime(Symbol(),PERIOD_D1,1);
   
   double highprice1D1=iHigh(Symbol(),PERIOD_D1,1);
   
   double lowprice1D1=iLow(Symbol(),PERIOD_D1,1);
   
   Print("time=",time1D1);
   
   Print("high=",highprice1D1);
   
   Print("low=",lowprice1D1);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

When I load the above indicator code into MT5, whenever I close the terminal then open it again it prints the wrong data. When I change time frames it returns the correct data. What is wrong with the code?  

 
smithy234:

When I load the above indicator code into MT5, whenever I close the terminal then open it again it prints the wrong data. When I change time frames it returns the correct data. What is wrong with the code?  

iTime etc can be unreliable especially with the first call after you load the indicator/EA as it has not had time to update and will return out of date times.

I find it better to use CopyTime or CopyRates and return and wait for the next tick if  CopyTime or CopyRates returns -1.

 
   datetime time1D1=iTime(Symbol(),PERIOD_D1,1);
Keith Watford #:

iTime etc can be unreliable especially with the first 

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)

Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
 
Thanks for the replies.
Reason: