250 Daily Bars

ptunneyptunney Posts: 246
edited March 2018 in How To

User asking how to get 250 daily bars.. was running November 2012 and sometimes only getting 249 bars.

For each backtest date, in order to keep the speed as fast as possible, we have a pre-built set of daily bars for all symbols. These bars include all corporate actions so that the data looks as it would have looked on that date in history. We observed that the vast majority of users pulled a small number of daily bars with the requests tailing off around the year mark and very few looking back more than a year on a particular date. So again, to keep the speed up we limited to it 1 year worth of bars or 250 trading days. However, we later discovered that, at certain times of the year, when you pull 250 bars you land in a weekend and the 250th day is the Friday and exists in the previous year.. ie..

Tuesday 20121106, request 250 days goes back to Monday 20111107 250 bars
Monday 20121105 request 250 days goes back to Monday 20111107 249 bars
Friday 20121102 request 250 days goes back to Friday 20111104 250 bars
Thursday 20121101 request 250 days goes back to Thursday 20111103 250 bars

So we recreated all the daily bars for all the symbols back as far as October 30th 2013 with 252 days, this way you should always be able to get 250 bars.

Prior to October 30th 2013 the years contain 250 bars so some dates will only have 249.

See the code below that I used to pin the data down...

from cloudquant.interfaces import Strategy
import pandas as pd

class BAR_HISTORY(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == 'SPY' 

    def on_start(self, md, order, service, account):
        bars = md.bar.daily(start=-300, include_empty=True) 
        date_list = list(bars.timestamp)
        df = pd.DataFrame(date_list,index=[service.time_to_string(t) for t in bars.timestamp])
        print(df.shape)
        pd.set_option('display.max_rows',300)
        print(df)
        service.terminate()

If you run this for 20121101 to 20121106 you will see that on the monday you only get 249 bars.
If you run this for dates after the end of October 2013 you should always be able to get 250 bars.

Sign In or Register to comment.