Get value from iBarShift().

 

Hello, 

How do you get a value from iBarShift() function for a timeframe other than the current at the start of the terminal (that is the first run)? It is always returning -1(or no bar) no matter what I have tried. I am creating a Multi-time Frame indicator that loops from the start date to the present, analyzing for the said pattern.

datetime startdate = D'2022.01.01';                              // Start Date
ENUM_TIMEFRAME timeframe1 = PERIOD_D1;

m_startday1 = iBarShift(_Symbol, timeframe1, startdate);         
// return -1 (when the chart is in a different TF (e.g 4H) from timeframe1(D1) at the start of the terminal)

        

if the iBarShift() is called for the current timeframe, you get a value.

int CurrentTFBar = iBarShift(_Symbol, _Period, startdate);

meaning, the user would have to change chart, or something that would re-initialize the chart for the indicator buffers to be plotted. Thanks.

Note: I wrote a test program, outputting the values of iBarShift() inside the onCalculate() function for both the current timeframe and a TF different from the current.

***

Test Code:

#property copyright ""
#property link      ""
#property version   "1.00"
#property indicator_chart_window

datetime startday = D'2022.01.04';           // current start date
ENUM_TIMEFRAMES timeframe1 = PERIOD_D1;
//+------------------------------------------------------------------+
//| 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[])
  {
//--- Print the Current and Inputted TF
      Print("Filename: "__FILE__, " Function: ",__FUNCTION__);
      int CTF = iBarShift(_Symbol, _Period, startday);
      Print(__LINE__, " TimeFrame: ", EnumToString(_Period), " index: ", CTF);
      
      int tf = iBarShift(_Symbol, timeframe1, startday);
      Print(__LINE__, " TimeFrame: ", EnumToString(timeframe1)," index: ", tf);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
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
Files:
iBarShift_.png  132 kb
 
Make the handler in Oncalculate, not in OnInit.
 
Vladimir Karputov #:
Make the handler in Oncalculate, not in OnInit.

Thanks, I have done that but it seems not to be the cause.

 
Thank-god Avwerosuoghene Odukudu # :

Thanks, I have done that but it seems not to be the cause.

Do as I advised you: handle all errors in OnCalculate!. And finally post the MQL5 code - otherwise my telepathic abilities are weak :)

 
Vladimir Karputov #:

Do as I advised you: handle all errors in OnCalculate!. And finally post the MQL5 code - otherwise my telepathic abilities are weak :)

I did, in the screenshot I added, thanks.

 
Thank-god Avwerosuoghene Odukudu # :

*** I did, in the screenshot I added, thanks.

The screenshot has been removed. Please insert the code using the button Code

 
Also check out the Documentation: Organizing Data Access
Documentation on MQL5: Timeseries and Indicators Access / Organizing Data Access
Documentation on MQL5: Timeseries and Indicators Access / Organizing Data Access
  • www.mql5.com
Organizing Data Access - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov #:
Also check out the Documentation: Organizing Data Access
Thanks, the file attachment I added was not deleted, I  don't know why the image attachment was.
 
Thank-god Avwerosuoghene Odukudu # :
Thanks, the file attachment I added was not deleted, I  don't know why the image attachment was.

Study this Documentation, after that you will know that when requesting data from someone else's timeframe, you should always check the error code (just in case: suddenly the timeseries has not been built yet).

 
Vladimir Karputov #:

Study this Documentation, after that you will know that when requesting data from someone else's timeframe, you should always check the error code (just in case: suddenly the timeseries has not been built yet).

I can check to ensure there is value, but I am wondering what would happen before I can get a value at the start. I have been reading the documentation and searching the forum. It might be something I overlooked, please, point me in that direction, thanks.
 
Thank-god Avwerosuoghene Odukudu # :
I can check to ensure there is value, but I am wondering what would happen before I can get a value at the start. I have been reading the documentation and searching the forum. It might be something I overlooked, please, point me in that direction, thanks.

I do not have telepathic abilities - insert your code correctly and I can help you.