Persisting State Information Using On_Restore

I'm trying to create a state dictionary that will persist across the daily simulations. There is an example in the API documentation on how to do this (APIs/CloudQuant/Strategy/on_restore):

https://app.cloudquant.com/#/glossary/81

I've made a copy of this code and run a test with the multi-day flag set, but I am not getting the output shown in the example. Specifically, I am not seeing the saving / restoring print messages. I wonder if anyone else has had success with this script?

Comments

  • ptunneyptunney Posts: 246
    edited February 2018

    I suspect that feature has been deprecated, I will confirm and replace the documentation with an alternative as soon as I get confirmation from the devs.

    In the mean time I would suggest you set up the variables you wish to persist in the class.
    I recommend using a default dict which returns a default value when you query for an entry that does not exist...

    See example code below..

    # "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=="DLPH"
    
        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.
            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
    

    Paul

  • Thanks Paul. I've taken this approach and it is working fine.

Sign In or Register to comment.