Back

How to save a status object in objectstore

Hello

my stategy need to store status in an object to guarantee persistance across redeployments and potential crashes

class statusPair:
    def __init__(self):
        self.positionDay = ''
        self.block_long = False
        self.block_short = False

 

I've tryed to store it in object store using SaveJson method

algorithm.ObjectStore.SaveJson(self.statusKey, self.statusObj)

where self.statusObj is a statusPair object, according to the documentation usage example:

self.ObjectStore.SaveJson("key", objectValue)

but I get the following error:

 Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the SaveJson method. Please checkout the API documentation. do I need to "prepare" my object before using the saveJson Method?

could you provide an example of best practice of how to do it ?

what would be the best options among the various methods available for storing objects in objectstore?

a more detailed guide ont this topic would be of great help!

thanks   
Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


>  do I need to "prepare" my object before using the saveJson Method?

Yes you need to first serialize the object into a compatible format. JSON works only on certain data types in Python, and (besides hacky ways of encoding your custom object statusPair into JSON-compatible) you are probably better off just using pickle.

This is done by:

self.statusObj = statusPair() # Your custom object

# Serialization into bytes
import pickle
serializedString = pickle.dumps(self.statusObj)

# Save into ObjectStore
self.ObjectStore.Save("MyObject", serializedString)

or

self.ObjectStore.SaveBytes("MyObject", serializedString)

##

# Load from ObjectStore
deserializedString = self.ObjectStore.Read("MyObject")
loadStatusObj = pickle.loads(self.statusObj)

assert loadStatusObj == self.statusObj

 

1

Typo:

pickle.loads(self.statusObj) # Ignore this

pickle.loads(deserializedString) # This one

 

0

Hello Adam,

I've tryed to create a script that uses pickle for encoding as per your suggestion

but unfortunately doesn't seem to work for me with  both Save and Savebytes 

here's the code

from dateutil import parser
import pickle

class statusObj:
def __init__(self):
self.invested = False
self.positionDay = '2020-01-01'

class ResistanceUncoupledThrustAssembly(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2020, 8, 13) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.spy = self.AddEquity('SPY', Resolution.Daily).Symbol
if self.ObjectStore.ContainsKey("MyObject"):
self.ObjectStore.Delete("MyObject")
self.status=statusObj()

def OnData(self, data):
if not self.Portfolio.Invested:
self.SetHoldings(self.spy, 1)
self.status.invested=True
self.status.positionDay=str(self.Time.day)
self.Log(str(self.status.positionDay))
serialized=pickle.dumps(self.status)
self.ObjectStore.SaveBytes("MyObject", serialized)

else:
if self.ObjectStore.ContainsKey("MyObject"):
deserialized = self.ObjectStore.ReadBytes("MyObject")
storedStatus = pickle.loads(deserialized)
assert storedStatus==self.status
self.Log(str(storedStatus.positionDay))
date = parser.parse(storedStatus.PositionDay)
self.Liquidate(self.spy)
self.status.invested=False
self.positionDay = '2020-01-01'

 

0

the Save method  does not accept the encoded argument, while saveBytes accept the argoument but when I try to retreive the object from store It gets a Byte[] and it seems it's not what is expected to restore the object back..

0

Shile l've tried to use the save method in the posted script instead of SaveBytes but it doesn't work. How should I modify it in order to make it work? Could you provide an example?

0

From what I gather, you are saying that the file saves correctly but you are unable to get python to read the returned byte object (that is, self.ObjectStore.ReadBytes("MyObject") is of form Byte[])? I'd try it right now if my nodes weren't all busy, but give the following a read:

https://stackoverflow.com/questions/37789281/passing-bytes-as-parameter-to-c. Basically, your stored item is coming back as a .NET object (System.Byte, unlike python's bytes object).  Now, translate into something python can understand. I'd guess deserialized=bytes(self.ObjectStore.ReadBytes("MyObject")).

 

0

Great spot Aaron,

it finally  does the job!

ps: the assert test still fails, but values get stored  correctly

 

2


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed