Index out of range error with Bars.IntradayBarNumber
Author: sedelstein
Creation Date: 5/12/2016 2:04 AM
profile picture

sedelstein

#1
Related to the post https://www.wealth-lab.com/Forum/Posts/How-to-generate-orders-in-real-time-during-the-trading-day-38279 Cone suggested (in post #30) to get the Close of previous day by looking at Close[bar - Bars.IntradayBarNumber(bar) - 1] . The bar prior to the first bar of the day.

This generally has worked flawlessly but I was getting some index out of range errors when I ran the code below and selected minute = 31 (the first intraday bar). What is strange is that there were no errors when I chose a data range of 100,000 bars (a little over a year) but got an error when selecting a data range of 1 year (rather than bars).

In some cases Bars.IntradayBarNumber(bar) returned the bar number instead of the intraday bar number (which I would have assumed was 0 for 09:31) This led to index out of range when asking for a Close[-1]

This was tested by clicking on a symbol within a Dataset comprised of 1minute bars for the Nasdaq 100
CODE:
Please log in to see this code.


The code prints to the debug window if bar - Bars.IntradayBarNumber(bar) - 1 is less than zero

ADBE 5/12/2015 9:31:00 AM bar = 780 ybar -1 intraday bar # = 780
AVGO 5/12/2015 9:31:00 AM bar = 780 ybar -1 intraday bar # = 780
ESRX 5/12/2015 9:31:00 AM bar = 780 ybar -1 intraday bar # = 780
FISV 5/12/2015 9:31:00 AM bar = 780 ybar -1 intraday bar # = 780
KHC 5/12/2015 9:31:00 AM bar = 780 ybar -1 intraday bar # = 780
PYPL 5/12/2015 9:31:00 AM bar = 780 ybar -1 intraday bar # = 780
profile picture

Eugene

#2
You might have noticed that synchronization is enabled in your script:
CODE:
Please log in to see this code.

What you're seeing is expected and is synchronization in action. For example, PYPL only started trading in July 2016 which is well after your starting date set to 5/12/2015. Now try selecting a different starting date.
profile picture

sedelstein

#3
Hello Eugene

Thanks for clarifying though I'm still unfortunately confused about some things. I'm not sure how the choice of 100k bars or 1 year made a difference. In addition, if I asked to find 09:32, one minute later there was no problem.

I'd like to ensure that when I do my loop through all the symbols in a dataset, looking for a particular date *and* time of day that I line up correctly and if for example in the case of PYPL that if the data does not exist that I can catch it and ensure it doesnt make my list of candidates that I choose from to trade at that time.

Could you offer a suggestion (perhaps a code snippet) as to how I might accomplish this correctly? It would be very helpful

Rgds.
profile picture

Eugene

#4
Hi Steve,

For your intraday rotation script that generates orders in real time based on last trading session's OHLC, I wonder if it's really necessary to choose a data range like 100,000 bars?
profile picture

sedelstein

#5
This isn't for the trade generation part which is a separate issue but for backtesting.portion. That is, backtesting an intraday strategy over the last year. It's very important that the bars line up properly and I'm not trading a symbol that really isnt available

With synchronize set to "true" and before the start of PYPL (when clicking on AAPL) the intraday bar number returned for the bars of PYPL prior to the start of PYPL is the bar of the series, clearly outside of the range of 0 to 390 for an intraday bar.

It looks like I will need to check for a non-zero price and a correct time stamp to the bar. Example of 7/6/16 there was no bar at 10:46. the 10:46 bar shows a time stamp of the previous close e.g. 10:45 but then the bar is the bar number for the larger time series, AAPL, is large and then Close[bar - Bars.IntradayBarNumber(bar) - 1] makes no sense


sync symbol Bars.Count bar Date[bar] time Close IntradayBarNumber ybar = bar - Bars.IntradayBarNumber(bar) - 1
TRUE PYPL 7020 2337 6/25/2015 9:31:00 0 2337 -1
TRUE PYPL 7020 2338 6/25/2015 9:31:00 0 2338 -1
TRUE PYPL 7020 2339 6/25/2015 9:31:00 0 2339 -1
TRUE PYPL 7020 2340 7/6/2015 9:31:00 39 0 2339
TRUE PYPL 7020 2341 7/6/2015 9:32:00 39 1 2339
profile picture

Eugene

#6
Got it. Then use Bars.FirstActualBar to not start trading before the first real bar of the secondary series.

CODE:
Please log in to see this code.
profile picture

sedelstein

#7
Will do. Thanks
Will still need to check the time stamp due to the fact that the "true" setting rolls the last price forward.
Appreciate the help
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).