when does the info in md.stat become available?

kc975943kc975943 Posts: 55
edited September 2019 in Under The Hood

I am trying to extract a list of market betas for the universe and to store it in a class variable by doing this inside on_strategy_start:

@classmethod
def on_strategy_start(cls, md, service, account):
        print("Beginning the backtest at " + service.time_to_string(service.system_time))        
        cls.sp500_handle='9a802d98-a2d7-4326-af64-cea18f8b5d61'
        cls.sec_list = service.symbol_list.get_handle(cls.sp500_handle)        
        cls.universe = []
        for md_info in md:
            if service.symbol_list.in_list(cls.sec_list,md_info.symbol):
                cls.universe.append(md_info.symbol)
        betas = [md[s].stat.beta for s in cls.universe]
        ....

However, I am getting Key error for every ticker in the universe.
At the same time, if I comment this out, then later inside the same function I have no problem accessing md[s].bar.daily() functions for each ticker.

How can this happen, and how can I get betas here?

Tagged:

Comments

  • ptunneyptunney Posts: 246

    I can only assume bad data on a particular date? Do you have a date that you ran this on and a start time as I cannot reproduce.

    Also, i cut your code down a little...

                cls.universe = service.symbol_list.get_symbols("S&P 500")
                betas = [md[s].stat.beta for s in cls.universe]
                print betas
    
  • Jan. 6, 2016, for example. start time 15:30:00, end time 15:45:00

  • kc975943kc975943 Posts: 55
    edited September 2019

    using the cls.universe = service.symbol_list.get_symbols("S&P 500") also gives me an error on 10/2/2015
    ValueError: Service.symbol_list.get_symbols(9a802d98-a2d7-4326-af64-cea18f8b5d61)failed: guid not found in Service.symbol_list._guids_to_syms
    so, i changed the code to the original and added a print statement
    print('Universe size = '+str(len(cls.universe)))
    On any business day between 10/1/2015 and 11/16/2015, inclusively, I am getting
    Universe size = 0
    Perhaps, there is a gap in the database? The only reason I just noticed this is while working on slower trading strategies, I have them rebalancing once a month, and if the data is missing, it simply waits for another month. But I am now wondering what implication does this 45 day gap in the data means for calculating daily PnL during this period for positions that are being held.
    Anyway, it would be great if the universe integrity could be restored.

Sign In or Register to comment.