can timer cross several days

Can I set a time that across days or weeks?

Comments

  • ptunneyptunney Posts: 246
    edited March 2018

    No, but if you are using defaultdict you could add an extra couple of keys.. date and time.. then each morning check if the date matches.. if it does set a timer up for the time value.

    A default dict returns a default value if the dictionary does not contain that entry...
    We set up the default dict above the model, at the class level, and it is available throughout the backtest.

    This is a very simple default dict based model,
    The first time past the symbol is not in the dictionary so it returns the default of 0.
    If it is zero we enter a position and pop a one in there (day one).
    Each day, if we are in a position it adds one to 'inPos'
    When 'inPos' reaches 5 it exits and puts 999 in there.

    # "RUN AS A SINGLE MULTIDAY JOB"
    # If you are on CQ Elite you can "ENABLE FAST SIMULATION" as script does not use any ELITE functions
    
    from cloudquant.interfaces import Strategy
    from collections import defaultdict
    
    class multidayModel(Strategy):
    
        dataStore=defaultdict(lambda: defaultdict(lambda: 0))  #lets call our DefaultDict "dataStore"
    
        @classmethod
        def is_symbol_qualified(cls, symbol, md, service, account):
            return symbol=="SPY"
    
        def on_start(self, md, order, service, account):
            print "\n",self.symbol," Shares : ",account[self.symbol].position.shares,"  Days : ",multidayModel.dataStore[self.symbol]['inPos']
    
            # if we are in a position, add one to the day. We check the account so that if a symbol splits we can catch it.
            if account[self.symbol].position.shares!=0:
                multidayModel.dataStore[self.symbol]['inPos']=multidayModel.dataStore[self.symbol]['inPos']+1
                print self.symbol,"Day",multidayModel.dataStore[self.symbol]['inPos']
    
            # if nothing in datastore, then we are not in a position so we enter a position.            
            if multidayModel.dataStore[self.symbol]['inPos'] == 0:     
                order.algo_buy(self.symbol,"market","init",order_quantity=100)
                multidayModel.dataStore[self.symbol]['inPos'] = 1
                print "\nEntering Long ",self.symbol
    
            # if there is something in the datastore, is it a 5.. if so, lets exit and set the datastore to 999
            elif multidayModel.dataStore[self.symbol]['inPos'] == 5 :
                order.algo_sell(self.symbol,"market","exit")
                print "\nDay ",multidayModel.dataStore[self.symbol]['inPos']," Exiting Long ",self.symbol
                multidayModel.dataStore[self.symbol]['inPos'] = 999
    
Sign In or Register to comment.