pep8 conversion of python algos #11 (#7952)

* pep8 conversion

* Minor tweak

---------

Co-authored-by: Martin Molinero <martin.molinero1@gmail.com>
This commit is contained in:
Louis Szeto
2024-04-19 23:54:50 +08:00
committed by GitHub
parent 3c30e255fe
commit 979bd9baef
18 changed files with 369 additions and 368 deletions
+36 -36
View File
@@ -21,72 +21,72 @@ class ObjectStoreExampleAlgorithm(QCAlgorithm):
history call. This pattern can be equally applied to a machine learning model being
trained and then saving the model weights in the object store.
'''
SPY_Close_ObjectStore_Key = "spy_close"
SPY_Close_History = RollingWindow[IndicatorDataPoint](252)
SPY_Close_EMA10_History = RollingWindow[IndicatorDataPoint](252)
SPY_Close_EMA50_History = RollingWindow[IndicatorDataPoint](252)
spy_close_object_store_key = "spy_close"
spy_close_history = RollingWindow[IndicatorDataPoint](252)
spy_close_ema10_history = RollingWindow[IndicatorDataPoint](252)
spy_close_ema50_history = RollingWindow[IndicatorDataPoint](252)
def Initialize(self):
self.SetStartDate(2013, 10, 7)
self.SetEndDate(2013, 10, 11)
def initialize(self):
self.set_start_date(2013, 10, 7)
self.set_end_date(2013, 10, 11)
self.SPY = self.AddEquity("SPY", Resolution.Minute).Symbol
self.SPY = self.add_equity("SPY", Resolution.MINUTE).symbol
self.SPY_Close = self.Identity(self.SPY, Resolution.Daily)
self.SPY_Close_EMA10 = IndicatorExtensions.EMA(self.SPY_Close, 10)
self.SPY_Close_EMA50 = IndicatorExtensions.EMA(self.SPY_Close, 50)
self.spy_close = self.identity(self.SPY, Resolution.DAILY)
self.spy_close_ema10 = IndicatorExtensions.ema(self.spy_close, 10)
self.spy_close_ema50 = IndicatorExtensions.ema(self.spy_close, 50)
# track last year of close and EMA10/EMA50
self.SPY_Close.Updated += lambda _, args: self.SPY_Close_History.Add(args)
self.SPY_Close_EMA10.Updated += lambda _, args: self.SPY_Close_EMA10_History.Add(args)
self.SPY_Close_EMA50.Updated += lambda _, args: self.SPY_Close_EMA50_History.Add(args)
self.spy_close.updated += lambda _, args: self.spy_close_history.add(args)
self.spy_close_ema10.updated += lambda _, args: self.spy_close_ema10_history.add(args)
self.spy_close_ema50.updated += lambda _, args: self.spy_close_ema50_history.add(args)
if self.ObjectStore.ContainsKey(self.SPY_Close_ObjectStore_Key):
if self.object_store.contains_key(self.spy_close_object_store_key):
# our object store has our historical data saved, read the data
# and push it through the indicators to warm everything up
values = self.ObjectStore.Read(self.SPY_Close_ObjectStore_Key)
self.Debug(f'{self.SPY_Close_ObjectStore_Key} key exists in object store.')
values = self.object_store.read(self.spy_close_object_store_key)
self.debug(f'{self.spy_close_object_store_key} key exists in object store.')
history = pd.read_csv(StringIO(values), header=None, index_col=0, squeeze=True)
history.index = pd.to_datetime(history.index)
for time, close in history.items():
self.SPY_Close.Update(time, close)
self.spy_close.update(time, close)
else:
self.Debug(f'{self.SPY_Close_ObjectStore_Key} key does not exist in object store. Fetching history...')
self.debug(f'{self.spy_close_object_store_key} key does not exist in object store. Fetching history...')
# if our object store doesn't have our data, fetch the history to initialize
# we're pulling the last year's worth of SPY daily trade bars to fee into our indicators
history = self.History(self.SPY, timedelta(365), Resolution.Daily).close.unstack(0).squeeze()
history = self.history(self.SPY, timedelta(365), Resolution.DAILY).close.unstack(0).squeeze()
for time, close in history.items():
self.SPY_Close.Update(time, close)
self.spy_close.update(time, close)
# save our warm up data so next time we don't need to issue the history request
self.ObjectStore.Save(self.SPY_Close_ObjectStore_Key,
'\n'.join(reversed([f'{x.EndTime},{x.Value}' for x in self.SPY_Close_History])))
self.object_store.save(self.spy_close_object_store_key,
'\n'.join(reversed([f'{x.end_time},{x.value}' for x in self.spy_close_history])))
# Can also use ObjectStore.SaveBytes(key, byte[])
# and to read ObjectStore.ReadBytes(key) => byte[]
# Can also use ObjectStore.save_bytes(key, byte[])
# and to read ObjectStore.read_bytes(key) => byte[]
# we can also get a file path for our data. some ML libraries require model
# weights to be loaded directly from a file path. The object store can provide
# a file path for any key by: ObjectStore.GetFilePath(key) => string (file path)
# a file path for any key by: ObjectStore.get_file_path(key) => string (file path)
def OnData(self, slice):
def on_data(self, slice):
close = self.SPY_Close
ema10 = self.SPY_Close_EMA10
ema50 = self.SPY_Close_EMA50
close = self.spy_close
ema10 = self.spy_close_ema10
ema50 = self.spy_close_ema50
if ema10 > close and ema10 > ema50:
self.SetHoldings(self.SPY, 1)
self.set_holdings(self.SPY, 1)
elif ema10 < close and ema10 < ema50:
self.SetHoldings(self.SPY, -1)
self.set_holdings(self.SPY, -1)
elif ema10 < ema50 and self.Portfolio[self.SPY].IsLong:
self.Liquidate(self.SPY)
elif ema10 < ema50 and self.portfolio[self.SPY].is_long:
self.liquidate(self.SPY)
elif ema10 > ema50 and self.Portfolio[self.SPY].IsShort:
self.Liquidate(self.SPY)
elif ema10 > ema50 and self.portfolio[self.SPY].is_short:
self.liquidate(self.SPY)