You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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?
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 string?
I tried both with a string and as in the previously posted code.
I tried it both with a string and as in the previously posted code.
in his code.
that's why it's not quite clear if there's false
and if so:
?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 might be relevant for working with algorithms that work on current time.
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?
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
false - for standardisation, doesn't affect anything :) To be honest, I don't know why this false in some functions is needed at all?
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.
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.
-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
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!