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
@@ -17,36 +17,36 @@ from AlgorithmImports import *
### Regression algorithm to test the OptionChainedUniverseSelectionModel class
### </summary>
class OptionChainedUniverseSelectionModelRegressionAlgorithm(QCAlgorithm):
def Initialize(self):
self.UniverseSettings.Resolution = Resolution.Minute
self.SetStartDate(2014, 6, 6)
self.SetEndDate(2014, 6, 6)
self.SetCash(100000)
def initialize(self):
self.universe_settings.resolution = Resolution.MINUTE
self.set_start_date(2014, 6, 6)
self.set_end_date(2014, 6, 6)
self.set_cash(100000)
universe = self.AddUniverse("my-minute-universe-name", lambda time: [ "AAPL", "TWX" ])
self.AddUniverseSelection(
universe = self.add_universe("my-minute-universe-name", lambda time: [ "AAPL", "TWX" ])
self.add_universe_selection(
OptionChainedUniverseSelectionModel(
universe,
lambda u: (u.Strikes(-2, +2)
lambda u: (u.strikes(-2, +2)
# Expiration method accepts TimeSpan objects or integer for days.
# The following statements yield the same filtering criteria
.Expiration(0, 180))
.expiration(0, 180))
)
)
def OnData(self, slice):
if self.Portfolio.Invested or not (self.IsMarketOpen("AAPL") and self.IsMarketOpen("AAPL")): return
values = list(map(lambda x: x.Value, filter(lambda x: x.Key == "?AAPL" or x.Key == "?TWX", slice.OptionChains)))
def on_data(self, slice):
if self.portfolio.invested or not (self.is_market_open("AAPL") and self.is_market_open("AAPL")): return
values = list(map(lambda x: x.value, filter(lambda x: x.key == "?AAPL" or x.key == "?TWX", slice.option_chains)))
for chain in values:
# we sort the contracts to find at the money (ATM) contract with farthest expiration
contracts = sorted(sorted(sorted(chain, \
key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
key = lambda x: x.Expiry, reverse=True), \
key = lambda x: x.Right, reverse=True)
key = lambda x: abs(chain.underlying.price - x.strike)), \
key = lambda x: x.expiry, reverse=True), \
key = lambda x: x.right, reverse=True)
# if found, trade it
if len(contracts) == 0: return
symbol = contracts[0].Symbol
self.MarketOrder(symbol, 1)
self.MarketOnCloseOrder(symbol, -1)
symbol = contracts[0].symbol
self.market_order(symbol, 1)
self.market_on_close_order(symbol, -1)