Pre Market/1st Min vol

Hey all,

f rom cloudquant.interfaces import Strategy

class Furdong(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return md.stat.avol > 1000

    def on_start(self, md, order, service, account):
        if md.stat.prev_close == 0 or md.L1.last < 1:
            return
        service.add_time_trigger(md.market_close_time)

    def on_finish(self, md, order, service, account):
        if md.L1.acc_volume < 5000000 or md.L1.last < 1:
            return
        gap = round(((md.L1.open - md.stat.prev_close) / md.stat.prev_close) * 100, 3)
        if gap > 30.0:
            s = service.time_to_string(service.system_time, format="%Y-%m-%d") + ',' + self.symbol + ',' + str(gap) + ',' 
            service.write_file('results', s)  

**Heres my script so far, its just to collect data, how would I get it to also print the pre market volume, and after hours volume on the previous day? A long side the date/symbol/gap %.

This is how it looks so far as an example: 2020-07-31,APDN,49.828, and I would like it to be: 2020-07-31,APDN,49.828, (PM Vol), (AH vol (prev working day)

Any help is appreciated!

Thanks!**

Comments

  • ptunneyptunney Posts: 246

    From docs
    https://app.cloudquant.com/#/glossary/133

    start
    May be a negative integer as a reference to now (e.g. -10 ... 10 bars ago), or a specific timestamp.
    Negative integer. This represents 'bars back from now'.
    Timestamp. Usually computed by service.time() or service.time_interval().

    So you can give it a timestamp.

    https://app.cloudquant.com/#/glossary/146
    service.get_market_hours(-1)
    returns a tuple of the open and close timestamp for the previous trading day.

    service.time_interval(h,m,s,ms) # converts hours mins seconds millis into millis for time mathematics.

    So you can take the close time and add a few hours, take the open time and subtract a few hours...

    So something like this can get you closer to what you want to do...

    # pre market volume, and after hours volume on the previous day? A long side the date/symbol/gap %.
    # 2020-07-31,APDN,49.828, and I would like it to be: 2020-07-31,APDN,49.828, (PM Vol), (AH vol (prev working day)
    # You only need to run this backtest for 1 minute, say around 9:50am
    
    from cloudquant.interfaces import Strategy
    
    class Furdong1(Strategy):
    
        @classmethod
        def is_symbol_qualified(cls, symbol, md, service, account):
            return md.stat.avol > 50000 and md.stat.prev_close > 1 
    
        def on_finish(self, md, order, service, account):
            gap = round(((md.L1.open - md.stat.prev_close) / md.stat.prev_close) * 100, 3)
            if gap > 30.0:
                s = service.time_to_string(service.system_time, format="%Y-%m-%d") + ',' + self.symbol + ',' + str(gap) + ',' 
                print(s)
    #            service.write_file('results', s)
    
                yestClTime = service.get_market_hours(-1)[1]
                todayOpTime = service.get_market_hours(0)[0]
                todayEarly = todayOpTime-service.time_interval(5)
                # add 8 hours to the close time to get end of day, will be right most days (half days will be off slightly!
                yestLate = yestClTime+service.time_interval(8) 
                print(service.time_to_string(yestClTime),service.time_to_string(yestLate),service.time_to_string(todayEarly),service.time_to_string(todayOpTime))
    
                yestBars = md.bar.minute(start=yestClTime,end=yestLate,today_only=False,include_extended=True)
                print(self.symbol,service.time_to_string(yestBars.timestamp[0]),service.time_to_string(yestBars.timestamp[-1]),len(yestBars.close))
                print(max(yestBars.high),min(yestBars.low))
    
                todayBars = md.bar.minute(start=todayEarly,end=todayOpTime,today_only=False,include_extended=True)
                print(self.symbol,service.time_to_string(todayBars.timestamp[0]),service.time_to_string(todayBars.timestamp[-1]),len(todayBars.close))
                print(max(todayBars.high),min(todayBars.low))
    
                # What happens if you give it no times?? It goes back to the previous close...
                whatBars = md.bar.minute(today_only=False,include_extended=True)
                print(self.symbol,service.time_to_string(whatBars.timestamp[0]),service.time_to_string(whatBars.timestamp[-1]),len(todayBars.close))
                print(max(whatBars.high),min(whatBars.low))
    
Sign In or Register to comment.