Last Hour of the Day

ptunneyptunney Posts: 246
edited November 2019 in Alpha Sources

Came across some info on the internet so I thought I would do a quick CloudQuant test...

"Heading into Monday, the S&P 500 had been up in the final hour of trading for seven straight days, and over the prior five weeks, the last hour of trading saw positive returns 90% of the time! That kind of late day strength doesn’t occur all that often."

The script uses timers so is for CloudQuant Elite. Because return was positive from 10am the timer for entry triggers at 10am. The commented out line is still there so you can switch to Entry 1 hour before the close instead.

from cloudquant.interfaces import Strategy

class lev_to_for(Strategy):

    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol in ["SPY","XLY","XLF","XLK","XLV","XLB","XLE","XLV","XLP","XLI","XLRE"] 
#        return symbol in ["AMZN","FB","AAPL","GOOG","GOOGL","NFLX"] 
#        return symbol in ["SPY","XLY","XLF","XLK","XLV","XLB","XLE","XLV","XLP","XLI","XLRE","AMZN","FB","AAPL","GOOG","GOOGL","NFLX"] 

    def on_start(self, md, order, service, account):
        closetime = md.market_close_time 
#        service.add_time_trigger(closetime-service.time_interval(1), timer_id = 'enter') # enter 1 hour before close
        service.add_time_trigger(service.time(10), timer_id = 'enter') # enter at 10am
        service.add_time_trigger(closetime-service.time_interval(0,15), timer_id = 'marketonclose')

    def on_timer(self, event, md, order, service, account):
        if event.timer_id == "enter":
            mopen =
            mlast = md.L1.last
            mclose = md.stat.prev_close
            mcltoop = (mopen-mclose)/mclose
            moptola = (mlast-mopen)/mclose
            mcltola = (mlast-mclose)/mclose
            print "{} {:<6} close{:8.2f}  open{:8.2f}  last{:8.2f}  open-close{:8.2f} {:8.2f}%  last-open{:8.2f} {:8.2f}%  last-close{:8.2f} {:8.2f}%".format(service.time_to_string(event.timestamp),self.symbol,mclose,mopen,mlast,mopen-mclose,mcltoop,mlast-mopen,moptola,mlast-mclose,mcltola)
            mshares = int(10000/mclose)
            order.send(self.symbol, 'buy', mshares, type='MKT') # simple market order
#            order.vwap(self.symbol, mshares, "buy", time_frame=60, num_slices = 10, order_aggression=3) # VWAP order over 60 minutes in 10 slices

        if event.timer_id == "marketonclose":
            print self.symbol,mshares
            if mshares>0:
                order.send(self.symbol, 'sell', mshares, type='MOC') 
            elif mshares<0:
                order.send(self.symbol, 'buy', mshares, type='MOC') 

Sign In or Register to comment.