Analogue to iBarShift - page 6

 
Vasiliy Pushkaryov:

I gotTimeCurrent() just as a special case.

I now read this note to the Bars() function more carefully:

"When requesting the number of bars in a given date range , only those bars whose opening time falls within that range are taken into account. For example, if the current day of the week is Saturday, when requesting the number of weekly bars with start_time=lastTuesday and stop_time=lastFriday, the function returns 0 because the opening time of the weekly timeframe always falls on Sunday and no weekly bar falls into the specified range".

Since TimeCurrent() is almost all time later than the opening time of the current bar, so the Bars() function returns 0. So, if we pass the time corresponding to 02:05 on the hourly timeframe as the start_time parameter, and we want the bar that started at 2 o'clock to be validated, then we must get the bar open time (02:00:00) through CopyTime() . Otherwise, function Bars() will ignore this bar.

I.e., if the time is 3:30, I understand that the time 2:05 in the hourly timeframe refers to the bar with index 1. None of the functions on the 2nd page will return this index. With this correction Renat Akhtyamov's function returned what I expected.

I'm attaching a script, with 4 options for index lookup functions, which I used as a test.

Tried the presented function, it turns out that it lies by one bar, if it asks for a bar with a time that is not on the chart. I.e. if we want to know the first bar of the day, and ask for the time "28.03.2018 00:00" but there is no bar with that time, we will get the index of the last bar of the previous day.

Or was it meant to be like that?

 
Aleksey Vyazmikin:

I tried the presented function and it turns out that it lies by one bar, if a bar is requested with a time that is not on the chart. I.e. if we want to know the first bar of the day, and ask for the time "28.03.2018 00:00". but there is no bar with that time, we will get the index of the last bar of the previous day.

Or was it meant to be like that?

How do you pass the time, by a string?
 
Renat Akhtyamov:
How do you pass the time, by string?

I tried both with a string and as in the previously posted code.

int teset_01=iBarShift(_Symbol,PERIOD_CURRENT,StringToTime("30.03.2018 00:00"),false);
Print ("teset_01=",teset_01);
 
Aleksey Vyazmikin:

I tried it both with a string and as in the previously posted code.

in his code.

int iBarShift2(string symbol, ENUM_TIMEFRAMES timeframe, datetime time)

that's why it's not quite clear if there's false

and if so:

int teset_01=iBarShift 2(_Symbol,PERIOD_CURRENT,StringToTime("30.03.2018 00:00"));
Print ("teset_01=",teset_01);
?
 

So far I've settled on this code, it seems to work quickly:

//+------------------------------------------------------------------+ 
//| Получим iBarShift для заданного номера бара                      | 
//+------------------------------------------------------------------+    
int iBarShift3(const string Symb,const ENUM_TIMEFRAMES TimeFrame,datetime time,const bool Exact=false)
  {
   static int Res=-1;
   static string LastSymb=NULL;
   static ENUM_TIMEFRAMES LastTimeFrame=0;
   static datetime LastTime=0;
   static bool LastExact=false;
   static int PerSec=::PeriodSeconds(LastTimeFrame);
   
   if (LastTimeFrame!=TimeFrame) PerSec=::PeriodSeconds(TimeFrame);
   time-=time%PerSec;

   if((time!=LastTime) || (Symb!=LastSymb) || (TimeFrame!=LastTimeFrame) || (Exact!=LastExact))
     {
      Res=::Bars(Symb,TimeFrame,time,UINT_MAX)-1;
      if(Res<0) Res=0;

      LastTime = time;
      LastSymb = Symb;
      LastTimeFrame=TimeFrame;
      LastExact=Exact;
     }

   return(Res);
  }  

Has anyone identified any disadvantages in this code, or is there a faster option?

Ah, well, the only drawback is if you make a request for a bar that is not yet on the chart, i.e. with today's time e.g..... maybe there's a solution to that as well? Here it would just make sense to return the last known bar. This might be relevant for working with algorithms that work on current time.

 
Renat Akhtyamov:

he has the same.

so the presence of false is not quite clear

false - for standardization, it doesn't affect anything :) To be honest, I don't know why this false in some functions is needed at all?

 
Aleksey Vyazmikin:

So far I've settled on this code, it seems to work quickly:

Has anyone identified any disadvantages in this code, or is there a faster option?

Ah, well, the only drawback is if you make a request for a bar that is not yet on the chart, i.e. with today's time e.g..... maybe there's a solution to that as well? Here it would just make sense to return the last known bar. This could be relevant for working with algorithms that work on current time.

returning a bar number that does not exist is not good

it's easier to return -1

 
Aleksey Vyazmikin:

false - for standardisation, doesn't affect anything :) To be honest, I don't know why this false in some functions is needed at all?

I'm lost there and I'm a follower of simple codes, no colons
 
Renat Akhtyamov:

returning a bar number that doesn't exist is not good

it is easier to return -1

No, it should just return the last known bar, i.e. with index 0, but now it takes a long time to figure out what to return there.

Here's the code, which is wrong by one bar, returns the correct bar - zero.

Or do you mean when the history asks for a bar by time that doesn't exist? Then I think that most often you need to get the bar closest to our time of those that are available, and the code returns in this case bar with offset -1 on history, but it works correctly, if there is no further history - it quickly returns zero bar.

 
Aleksey Vyazmikin:

No, it should just return the last known bar, i.e. with index 0, but now it takes a long time to figure out what to return there.

The code, which is wrong by one bar, returns the correct bar - zero.

Or do you mean when the history asks for a bar by time that doesn't exist? Then I think that most often we should get the bar which is closest to our time, and the code returns in this case bar with offset -1 on history, but it works correctly, if there is no further history - it quickly returns zero bar.

Yes (highlighted)

-1 is one minus (I clarify) and the error returned by the function is that there is no such bar

That is, my function.

Forum on trading, automated trading systems and strategy testing

Analog of iBarShift

Renat Akhtyamov, 2017.06.08 01:19

This is also possible

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);
   }

also requires improvement

although...

Documentation:

"Note.

If data for the timeseries with the specified parameters when calling the Bars() function have not yet been generated in the terminal, or data of the timeseries are not synchronized with the trade server at the moment of the function call, then function will return zero value. "

====

If res==0, we will catch -1 from the function as it is.

===

So it all works, use it to your advantage!