I am trying to know the index of a bar, from a datetime data.

 
Hello,

I am trying to know the index of a bar, from a datetime data.

for example

 datetime dtBarDatetime=D'2018.02.08 13:05';


 
May be this helps you: https://www.mql5.com/en/code/1864
iBarShift
iBarShift
  • votes: 45
  • 2013.10.25
  • Alain Verleyen
  • www.mql5.com
But this function is not included in MQL5, mainly because MQL5 provides all necessary basic, low-level, functions to process such operation in a library. What I discovered, remarkably, is that all four versions are bugged, in the sense they don't reproduce exactly the MQL4 iBarShift(). Indeed, when the datetime given as input parameter, don't...
 
Carl Schreiber:
May be this helps you: https://www.mql5.com/en/code/1864

Thanks Carl.

Here my code.
Can probably be improved.


//+------------------------------------------------------------------+
//|                                       scriptIndexBarResearch.mq5 |
//|                                   Copyright 2018, Pierre Rougier |
//|                           https://www.mql5.com/en/users/pierre8r |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, Pierre Rougier"
#property link      "https://www.mql5.com/en/users/pierre8r"
#property version   "1.00"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int index_bar_research(datetime dt_bar_research)
  {
   datetime dt_start_time=D'1970.01.01 00:00';
   datetime time_array[];

   int res=CopyTime(NULL,NULL,dt_start_time,dt_bar_research,time_array);
   if(res<=0)
     {
      Print("Getting index bar research Failed! Error",GetLastError());
      return(0);
     }
   return(res-1);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {

   datetime dt_index_bar_research=D'2018.02.08 13:05';
   int i_index_bar_research=index_bar_research(dt_index_bar_research);
   Print("dt_index_bar_research :"+TimeToString(dt_index_bar_research)
         +" - i_index_bar_research :"+IntegerToString(i_index_bar_research));

   dt_index_bar_research=D'2017.09.14 13:05';
   i_index_bar_research=index_bar_research(dt_index_bar_research);
   Print("dt_index_bar_research :"+TimeToString(dt_index_bar_research)
         +" - i_index_bar_research :"+IntegerToString(i_index_bar_research));

   dt_index_bar_research=D'2017.09.06 10:35';
   i_index_bar_research=index_bar_research(dt_index_bar_research);
   Print("dt_index_bar_research :"+TimeToString(dt_index_bar_research)
         +" - i_index_bar_research :"+IntegerToString(i_index_bar_research));

//---
  }
//+------------------------------------------------------------------+
Reason: