pep8 conversion of python algos #13 (#7955)

* t status
pep8 conversion

* Minor tweaks and rebase

* Various minor fixes

---------

Co-authored-by: Martin Molinero <martin.molinero1@gmail.com>
This commit is contained in:
Louis Szeto
2024-04-20 03:14:12 +08:00
committed by GitHub
parent d2669fb0c2
commit 08a3de9e2d
33 changed files with 312 additions and 312 deletions
@@ -17,24 +17,24 @@ import torch.nn.functional as F
class PytorchNeuralNetworkAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2013, 10, 7) # Set Start Date
self.SetEndDate(2013, 10, 8) # Set End Date
def initialize(self):
self.set_start_date(2013, 10, 7) # Set Start Date
self.set_end_date(2013, 10, 8) # Set End Date
self.SetCash(100000) # Set Strategy Cash
self.set_cash(100000) # Set Strategy Cash
# add symbol
spy = self.AddEquity("SPY", Resolution.Minute)
self.symbols = [spy.Symbol] # using a list can extend to condition for multiple symbols
spy = self.add_equity("SPY", Resolution.MINUTE)
self._symbols = [spy.symbol] # using a list can extend to condition for multiple symbols
self.lookback = 30 # days of historical data (look back)
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 28), self.NetTrain) # train the NN
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 30), self.Trade)
self.schedule.on(self.date_rules.every_day("SPY"), self.time_rules.after_market_open("SPY", 28), self.net_train) # train the NN
self.schedule.on(self.date_rules.every_day("SPY"), self.time_rules.after_market_open("SPY", 30), self.trade)
def NetTrain(self):
def net_train(self):
# Daily historical data is used to train the machine learning model
history = self.History(self.symbols, self.lookback + 1, Resolution.Daily)
history = self.history(self._symbols, self.lookback + 1, Resolution.DAILY)
# dicts that store prices for training
self.prices_x = {}
@@ -44,13 +44,13 @@ class PytorchNeuralNetworkAlgorithm(QCAlgorithm):
self.sell_prices = {}
self.buy_prices = {}
for symbol in self.symbols:
for symbol in self._symbols:
if not history.empty:
# x: preditors; y: response
self.prices_x[symbol] = list(history.loc[symbol.Value]['open'])[:-1]
self.prices_y[symbol] = list(history.loc[symbol.Value]['open'])[1:]
self.prices_x[symbol] = list(history.loc[symbol.value]['open'])[:-1]
self.prices_y[symbol] = list(history.loc[symbol.value]['open'])[1:]
for symbol in self.symbols:
for symbol in self._symbols:
# if this symbol has historical data
if symbol in self.prices_x:
@@ -79,17 +79,17 @@ class PytorchNeuralNetworkAlgorithm(QCAlgorithm):
self.buy_prices[symbol] = net(y)[-1] + np.std(y.data.numpy())
self.sell_prices[symbol] = net(y)[-1] - np.std(y.data.numpy())
def Trade(self):
def trade(self):
'''
Enter or exit positions based on relationship of the open price of the current bar and the prices defined by the machine learning model.
Liquidate if the open price is below the sell price and buy if the open price is above the buy price
'''
for holding in self.Portfolio.Values:
if self.CurrentSlice[holding.Symbol].Open < self.sell_prices[holding.Symbol] and holding.Invested:
self.Liquidate(holding.Symbol)
for holding in self.portfolio.values():
if self.current_slice[holding.symbol].open < self.sell_prices[holding.symbol] and holding.invested:
self.liquidate(holding.symbol)
if self.CurrentSlice[holding.Symbol].Open > self.buy_prices[holding.Symbol] and not holding.Invested:
self.SetHoldings(holding.Symbol, 1 / len(self.symbols))
if self.current_slice[holding.symbol].open > self.buy_prices[holding.symbol] and not holding.invested:
self.set_holdings(holding.symbol, 1 / len(self._symbols))