Error getting iBarShift when opening MT5

 

Hi,

the first time the indicator is executed when opening MT5, it fails to get iBarShift function, the second or third time the event OnCalculate is executed, it works OK


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[])
  {
//---
   int dayIndex = iBarShift( _Symbol, PERIOD_D1, iTime(_Symbol,PERIOD_CURRENT,0), true);
   Print ("OnCalculate: ", iTime(_Symbol,PERIOD_CURRENT,0), " - ", dayIndex);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }



Thank you

 
israelhernando :


1. Documentation recommended for study: Timeseries and Indicators Access

2. Always check the result of all functions (in this case you didn't check the result of ' iTime ')

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

1. Documentation recommended for study: Timeseries and Indicators Access

2. Always check the result of all functions (in this case you didn't check the result of ' iTime ')

Hi,


the result of iTime is correct:

The problem is the result of iBarShift (-1 the two first times).


Thank you.

 
israelhernando #:

Hi,


the result of iTime is correct:

The problem is the result of iBarShift (-1 the two first times).


Thank you.

Forum on trading, automated trading systems and testing trading strategies

Error getting iBarShift when opening MT5

Vladimir Karputov, 2022.06.02 15:09

1. Documentation recommended for study: Timeseries and Indicators Access

2. Always check the result of all functions (in this case you didn't check the result of ' iTime ')


 
//+------------------------------------------------------------------+
//|                                               Temp Indicator.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.000"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0
//--- input parameters
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   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 i_time_current=iTime(Symbol(),PERIOD_CURRENT,0);
   int i_bar_shift_D1=iBarShift(Symbol(),PERIOD_D1,i_time_current,true);
   Print("OnCalculate: ",TimeToString(i_time_current,TIME_DATE|TIME_MINUTES)," - ",i_bar_shift_D1);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Item 1. Documentation recommended for study:   Timeseries and Indicators Access is required reading and study.

And now an example: I closed the terminal and deleted the history and ticks for the 'EURUSD' symbol. Terminal launch result:

2022.06.02 16:30:38.868 Temp Indicator (EURUSD,H1)      OnCalculate: 2022.06.02 16:00 - -1
2022.06.02 16:30:39.015 Temp Indicator (EURUSD,H1)      OnCalculate: 2022.06.02 16:00 - -1
2022.06.02 16:30:39.016 Temp Indicator (EURUSD,H1)      OnCalculate: 2022.06.02 16:00 - -1
2022.06.02 16:30:39.219 Temp Indicator (EURUSD,H1)      OnCalculate: 2022.06.02 16:00 - -1
2022.06.02 16:30:39.220 Temp Indicator (EURUSD,H1)      OnCalculate: 2022.06.02 16:00 - -1
2022.06.02 16:30:39.276 Temp Indicator (EURUSD,H1)      OnCalculate: 2022.06.02 16:00 - 0
2022.06.02 16:30:39.377 Temp Indicator (EURUSD,H1)      OnCalculate: 2022.06.02 16:00 - 0
2022.06.02 16:30:39.874 Temp Indicator (EURUSD,H1)      OnCalculate: 2022.06.02 16:00 - 0

That is, someone else's timeframe was built in five ticks. In general, the advice is the same: read and study the documentation:  Timeseries and Indicators Access 

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

Hi,

the first time the indicator is executed when opening MT5, it fails to get iBarShift function, the second or third time the event OnCalculate is executed, it works OK




Thank 

If(dayIndex <0) return 0;
BarShift() does not give the correct value from the start, it takes some time to initialize.

Place the check before the print statement.
Reason: