Analogue to iBarShift - page 2

 
Here are two variants of the iBarShift() analogue: post #5 and post #7
 
Denis:

The Bars function returns the number of bars. When we need to get the index of a bar with index 9 (sorry for the tautology), it will return 10, because the index of the first bar is 0.
Right, thanks. All in all, it's a good addition-notice. True, I do not really bother, I just know that it is the number of bars. And if the program needs an index, only then I subtract one.
 
Vasiliy Pushkaryov:
Here are two variants of the iBarShift() analogue: post #5 and post #7
Simple variant:
Bars(NULL, 0, t, 32000000000)-1;

the shortest and fastest solution for iBarShift analog for current symbol and timeframe and requires no additional functions. However, for non-current ones too, you just need to fill in the first two parameters instead of null ones.
 
Nikolai Semko:
Just a variant:

is the shortest and fastest solution to counterpart iBarShift for the current symbol and current timeframe and requires no additional functions.

Only it won't work correctly in all cases. GetBarShift() from fxsaber is more versatile.

Try to put TimeCurrent() instead of t in your function, you'll see the result.

 
Vasiliy Pushkaryov:

Only it won't work correctly in all cases. GetBarShift() by fxsaber is more universal.

Try putting TimeCurrent() instead of t in your function, you'll see the result.

The result will be the same.
 
Nikolai Semko:
The result will be the same.
You haven't even checked
 
Vasiliy Pushkaryov:
You haven't even checked.
I've checked before many times, so I just know. But you haven't even tried it yourself.
 
Nikolai Semko:
I've tested it many times before, so I just know. But you haven't even tried it yourself.

Here's the script.

void OnStart()
{
  int idShort = Bars(NULL, 0, TimeCurrent(), 32000000000)-1;
  int idUni = GetBarShift(_Symbol, 0, TimeCurrent());
  Print("idShort = ", idShort, " idUni=", idUni);
}


//+------------------------------------------------------------------------------------------------------------------+
//| 
//+------------------------------------------------------------------------------------------------------------------+
int GetBarShift(const string symbol_name, const ENUM_TIMEFRAMES timeframe, const datetime time) 
{
  int res=-1;
  datetime last_bar;
  
   // --- время открытия последнего бара по символу, периоду
  if(SeriesInfoInteger(symbol_name, timeframe, SERIES_LASTBAR_DATE, last_bar)) 
  {
    if(time > last_bar) res = 0;
    else 
    {
      const int shift = Bars(symbol_name, timeframe, time, last_bar);
      if(shift > 0) res = shift-1;
    }
  }
  return res;
}

It just hangs. Forcibly removing it from the chart gets the result.


So I tried it, that's why I advised you too. But since you used it and it suits you, I won't change your mind.

 

That's fine, too

int iBarShift(string symbol, ENUM_TIMEFRAMES timeframe, datetime tm)
   {
        datetime tm0[1];      
        CopyTime(symbol,timeframe,0,1,tm0);
        int res=Bars(symbol,timeframe,tm0[0],tm)-1;
        return(res);
   }
 
iBarShift
iBarShift
  • votes: 42
  • 2013.10.25
  • Alain Verleyen
  • www.mql5.com
Многие ищут функцию iBarShift, которая была в языке MQL4. В языке MQL5 ее нет, но есть все возможности для ее реализации в виде библиотеки.