How to read/write from/to ObjectStore. Is this below methods correct ?
def load_items(self):
if self.algorithm.ObjectStore.ContainsKey(self.trade_planning_key):
items_json = self.algorithm.ObjectStore.ReadBytes(self.trade_planning_key)
return json.loads(items_json.decode("utf-8"), object_hook=self.deserialize_date)
return []
def store_items(self):
items_json = json.dumps(self.items, default=self.serialize_date).encode("utf-8")
self.algorithm.ObjectStore.SaveBytes(self.trade_planning_key, items_json)
self.algorithm.Debug(f"Stored {len(self.items)} items in ObjectStore under tag '{self.trade_planning_key}'.")
Mia Alissi
Yes, the methods you provided are correct for reading and writing data to the ObjectStore in QuantConnect.
The
load_items
method checks if the key exists in the ObjectStore usingself.algorithm.ObjectStore.ContainsKey(self.trade_planning_key)
. If the key exists, it reads the bytes from the ObjectStore usingself.algorithm.ObjectStore.ReadBytes(self.trade_planning_key)
, decodes the bytes into a string, and then loads the string as a JSON object.The
store_items
method converts the items to a JSON string usingjson.dumps(self.items, default=self.serialize_date).encode("utf-8")
, and then saves the encoded string to the ObjectStore usingself.algorithm.ObjectStore.SaveBytes(self.trade_planning_key, items_json)
.Here are the relevant knowledge sources for your reference:
Please note that the ObjectStore is shared across the entire organization. Using the same key, you can access data across all projects in an organization. The ObjectStore has helper methods to store strings, JSON objects, XML objects, and bytes. It also has helper methods to retrieve the stored objects.
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.
Dharmesh Khalasi
How to test objectStore using Mock Class?
Dharmesh Khalasi
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!