Minute Bars Thin - I just want the 5 most recent minute bars and I do not care about their timing

ptunneyptunney Posts: 246
edited October 2018 in Quick Question

Then you would use the md.bar.minute_by_index command...

from cloudquant.interfaces import Strategy

class ThinMinuteBars(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol =="VVPR" # Very Thin Normal Symbol trades all hours

    def on_minute_bar(self, event, md, order, service, account, bar):
        myminbar = md.bar.minute_by_index(start=-5, today_only=False, include_extended=True) 
        for i in xrange(-len(myminbar.timestamp),0):
            print "{:6}, bar number {:3}, BarDate {}, BarTime {}, Close {:8.2f} bar_volume {}".format(self.symbol, i, service.time_to_string(myminbar.timestamp[i],"%Y-%m-%d"),service.time_to_string(myminbar.timestamp[i],"%H:%M:%S.%f"),myminbar.close[i],myminbar.volume[i])
        print "{}  How many bars did I get = {}. Bar timestamp {}  {:5} mins ago. Close : {:8.2f}  acc_volume : {}\n".format(service.time_to_string(event.timestamp,"%Y-%m-%d %H:%M:%S"),len(myminbar.close),service.time_to_string(myminbar.timestamp[-1],"%Y-%m-%d %H:%M:%S"), int((event.timestamp-myminbar.timestamp[-1])/60000000), myminbar.close[-1],md.L1.acc_volume)

Output of VVPR run on 10/22/2018

VVPR  , bar number  -5, BarDate 2018-10-22, BarTime 09:35:00.000000, Close     1.77 bar_volume  9951
VVPR  , bar number  -4, BarDate 2018-10-22, BarTime 09:36:00.000000, Close     1.75 bar_volume  3287
VVPR  , bar number  -3, BarDate 2018-10-22, BarTime 09:37:00.000000, Close     1.60 bar_volume  15038
VVPR  , bar number  -2, BarDate 2018-10-22, BarTime 09:38:00.000000, Close     1.51 bar_volume  1000
VVPR  , bar number  -1, BarDate 2018-10-22, BarTime 09:39:00.000000, Close     1.58 bar_volume  50
09:40:01  How many bars did I get = 5. Bar timestamp 09:39:00 1 mins ago. Close : 1.58  acc_volume : 129509
VVPR  , bar number  -5, BarDate 2018-10-22, BarTime 09:36:00.000000, Close     1.75 bar_volume  3287
VVPR  , bar number  -4, BarDate 2018-10-22, BarTime 09:37:00.000000, Close     1.60 bar_volume  15038
VVPR  , bar number  -3, BarDate 2018-10-22, BarTime 09:38:00.000000, Close     1.51 bar_volume  1000
VVPR  , bar number  -2, BarDate 2018-10-22, BarTime 09:39:00.000000, Close     1.58 bar_volume  50
VVPR  , bar number  -1, BarDate 2018-10-22, BarTime 09:40:00.000000, Close     1.58 bar_volume  1004
09:41:05  How many bars did I get = 5. Bar timestamp 09:40:00 1 mins ago. Close : 1.58  acc_volume : 130513
VVPR  , bar number  -5, BarDate 2018-10-22, BarTime 09:36:00.000000, Close     1.75 bar_volume  3287
VVPR  , bar number  -4, BarDate 2018-10-22, BarTime 09:37:00.000000, Close     1.60 bar_volume  15038
VVPR  , bar number  -3, BarDate 2018-10-22, BarTime 09:38:00.000000, Close     1.51 bar_volume  1000
VVPR  , bar number  -2, BarDate 2018-10-22, BarTime 09:39:00.000000, Close     1.58 bar_volume  50
VVPR  , bar number  -1, BarDate 2018-10-22, BarTime 09:40:00.000000, Close     1.58 bar_volume  1004
09:42:05  How many bars did I get = 5. Bar timestamp 09:40:00 2 mins ago. Close : 1.58  acc_volume : 130513
VVPR  , bar number  -5, BarDate 2018-10-22, BarTime 09:37:00.000000, Close     1.60 bar_volume  15038
VVPR  , bar number  -4, BarDate 2018-10-22, BarTime 09:38:00.000000, Close     1.51 bar_volume  1000
VVPR  , bar number  -3, BarDate 2018-10-22, BarTime 09:39:00.000000, Close     1.58 bar_volume  50
VVPR  , bar number  -2, BarDate 2018-10-22, BarTime 09:40:00.000000, Close     1.58 bar_volume  1004
VVPR  , bar number  -1, BarDate 2018-10-22, BarTime 09:42:00.000000, Close     1.59 bar_volume  100
09:43:05  How many bars did I get = 5. Bar timestamp 09:42:00 1 mins ago. Close : 1.59  acc_volume : 130813
VVPR  , bar number  -5, BarDate 2018-10-22, BarTime 09:38:00.000000, Close     1.51 bar_volume  1000
VVPR  , bar number  -4, BarDate 2018-10-22, BarTime 09:39:00.000000, Close     1.58 bar_volume  50
VVPR  , bar number  -3, BarDate 2018-10-22, BarTime 09:40:00.000000, Close     1.58 bar_volume  1004
VVPR  , bar number  -2, BarDate 2018-10-22, BarTime 09:42:00.000000, Close     1.59 bar_volume  100
VVPR  , bar number  -1, BarDate 2018-10-22, BarTime 09:43:00.000000, Close     1.40 bar_volume  24618
09:44:05  How many bars did I get = 5. Bar timestamp 09:43:00 1 mins ago. Close : 1.40  acc_volume : 155431

As you can see, there was nothing traded in the 9:41 minute bar but at 9:42 you had 5 bars returned.

Sign In or Register to comment.