How to deal with empty minute bar data?

Just wondering how you might deal with empty bar data on CQ. If you run the script below, you'll find there are no minute bars from 11:28 to 11:46. So, if you are in the middle of a trade and doing calculations based on price, say you are finding the maximum range of the last 5 minutes, you'll get a failed run because the list becomes empty. Do you write code to keep the last known price and keep doing calculations? Do you just get out of the trade and exit because the model doesn't have any data and that's the most conservative thing to do? In order to trap the error, would you use the math library? (if not math.isnan(bar_data_minute):)

def is_symbol_qualified(cls, symbol, md, service, account):
    return (symbol=='CBI')
def on_minute_bar(self, event, md, order, service, account, bar):
    if (1 
        and event.timestamp>=service.time(9,35)
        ):
        bar_data_minute=md.bar.minute(start=-1,include_extended=False)
        print bar_data_minute.high
        print service.time_to_string(service.system_time,format="%Y-%m-%d %H:%M:%S.%f")

Comments

  • ptunneyptunney Posts: 246
    edited January 2020

    No date provided, Assume you are talking about the NYSE CBI stock that was delisted 2018/05/11.

    The question you should ask is, what would YOU do in such a circumstance if manually trading, then you program that behaviour.

    If you are in a minute bar callback and there is nothing in the previous minute bar then the symbol is not trading, it is a thin symbol, use extreme caution when auto trading symbols that are thin, I would generally caution against it!

    Try running this to get an idea of the different kinds of bar fetch you can do.


    from cloudquant.interfaces import Strategy class minutebars(Strategy): @classmethod def is_symbol_qualified(cls, symbol, md, service, account): return (symbol=='EWY') def on_minute_bar(self, event, md, order, service, account, bar): print service.time_to_string(md.L1.minute_start_timestamp,format="%H:%M"), "Minute Bar Call" my_bars=md.bar.minute(start=-5) print "we have {} bars of 1 min data from a request for 5 bars".format(len(my_bars.close)) if len(my_bars.close)>0: for i in range(len(my_bars.close)): print service.time_to_string(my_bars.timestamp[i],format="%H:%M"),my_bars.high[0] my_bars = md.bar.minute(start=md.L1.minute_start_timestamp-service.time_interval(0,5), end=md.L1.minute_start_timestamp) # current bar time to 5 mins ago. print "we have {} bars of 1 min data from a request for bars from the last 5 minutes".format(len(my_bars.close)) if len(my_bars.close)>0: for i in range(len(my_bars.close)): print service.time_to_string(my_bars.timestamp[i],format="%H:%M"),my_bars.high[0] my_bars=md.bar.minute_by_index(-5,include_extended=True) print "we have {} bars of 1 min data from a request for 5 bars no time limit (by_index) including out of hours".format(len(my_bars.close)) if len(my_bars.close)>0: for i in range(len(my_bars.close)): print service.time_to_string(my_bars.timestamp[i],format="%H:%M"),my_bars.high[0] my_bars = md.bar.minute(start=-12, bar_size=5) # will attempt to create 12 x 5 minute bars print "we have {} bars of 5 min data from a request for 12 bars".format(len(my_bars.close)) if len(my_bars.close)>0: for i in range(len(my_bars.close)): print service.time_to_string(my_bars.timestamp[i],format="%H:%M"),my_bars.high[i] print

    I chose a symbol that you can use on recent dates, that has empty bars and that has pre-market activity. For the md.bar.minute_by_index I added include_extended=True (you could also add today_only = False if you were willing to go back into yesterdays bars to get a full set of bars.

    Hope this helps!

  • My apologies, the date is an important piece of information here... 2017-06-30. Thank you for the response.

Sign In or Register to comment.