pep8 conversion of python algos #1-25 (#7926)

* pep8 conversion of python algos

* address peer-review

* PEP8 updates/fixes

* More fixes

---------

Co-authored-by: Jhonathan Abreu <jdabreu25@gmail.com>
This commit is contained in:
Louis Szeto
2024-04-18 05:02:32 +08:00
committed by GitHub
parent 784e497691
commit ed7f3ebbbf
35 changed files with 735 additions and 727 deletions
@@ -18,77 +18,77 @@ from AlgorithmImports import *
### we add future option contracts individually using <see cref="AddFutureOptionContract"/>
### </summary>
class AddFutureOptionContractDataStreamingRegressionAlgorithm(QCAlgorithm):
def Initialize(self):
self.onDataReached = False
def initialize(self):
self.on_data_reached = False
self.invested = False
self.symbolsReceived = []
self.expectedSymbolsReceived = []
self.dataReceived = {}
self.symbols_received = []
self.expected_symbols_received = []
self.data_received = {}
self.SetStartDate(2020, 1, 4)
self.SetEndDate(2020, 1, 8)
self.set_start_date(2020, 1, 4)
self.set_end_date(2020, 1, 8)
self.es20h20 = self.AddFutureContract(
Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, datetime(2020, 3, 20)),
Resolution.Minute).Symbol
self.es20h20 = self.add_future_contract(
Symbol.create_future(Futures.Indices.SP_500_E_MINI, Market.CME, datetime(2020, 3, 20)),
Resolution.MINUTE).symbol
self.es19m20 = self.AddFutureContract(
Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, datetime(2020, 6, 19)),
Resolution.Minute).Symbol
self.es19m20 = self.add_future_contract(
Symbol.create_future(Futures.Indices.SP_500_E_MINI, Market.CME, datetime(2020, 6, 19)),
Resolution.MINUTE).symbol
# Get option contract lists for 2020/01/05 (timedelta(days=1)) because Lean has local data for that date
optionChains = self.OptionChainProvider.GetOptionContractList(self.es20h20, self.Time + timedelta(days=1))
optionChains += self.OptionChainProvider.GetOptionContractList(self.es19m20, self.Time + timedelta(days=1))
option_chains = self.option_chain_provider.get_option_contract_list(self.es20h20, self.time + timedelta(days=1))
option_chains += self.option_chain_provider.get_option_contract_list(self.es19m20, self.time + timedelta(days=1))
for optionContract in optionChains:
self.expectedSymbolsReceived.append(self.AddFutureOptionContract(optionContract, Resolution.Minute).Symbol)
for option_contract in option_chains:
self.expected_symbols_received.append(self.add_future_option_contract(option_contract, Resolution.MINUTE).symbol)
def OnData(self, data: Slice):
if not data.HasData:
def on_data(self, data: Slice):
if not data.has_data:
return
self.onDataReached = True
hasOptionQuoteBars = False
self.on_data_reached = True
has_option_quote_bars = False
for qb in data.QuoteBars.Values:
if qb.Symbol.SecurityType != SecurityType.FutureOption:
for qb in data.quote_bars.values():
if qb.symbol.security_type != SecurityType.FUTURE_OPTION:
continue
hasOptionQuoteBars = True
has_option_quote_bars = True
self.symbolsReceived.append(qb.Symbol)
if qb.Symbol not in self.dataReceived:
self.dataReceived[qb.Symbol] = []
self.symbols_received.append(qb.symbol)
if qb.symbol not in self.data_received:
self.data_received[qb.symbol] = []
self.dataReceived[qb.Symbol].append(qb)
self.data_received[qb.symbol].append(qb)
if self.invested or not hasOptionQuoteBars:
if self.invested or not has_option_quote_bars:
return
if data.ContainsKey(self.es20h20) and data.ContainsKey(self.es19m20):
self.SetHoldings(self.es20h20, 0.2)
self.SetHoldings(self.es19m20, 0.2)
if data.contains_key(self.es20h20) and data.contains_key(self.es19m20):
self.set_holdings(self.es20h20, 0.2)
self.set_holdings(self.es19m20, 0.2)
self.invested = True
def OnEndOfAlgorithm(self):
self.symbolsReceived = list(set(self.symbolsReceived))
self.expectedSymbolsReceived = list(set(self.expectedSymbolsReceived))
def on_end_of_algorithm(self):
self.symbols_received = list(set(self.symbols_received))
self.expected_symbols_received = list(set(self.expected_symbols_received))
if not self.onDataReached:
if not self.on_data_reached:
raise AssertionError("OnData() was never called.")
if len(self.symbolsReceived) != len(self.expectedSymbolsReceived):
raise AssertionError(f"Expected {len(self.expectedSymbolsReceived)} option contracts Symbols, found {len(self.symbolsReceived)}")
if len(self.symbols_received) != len(self.expected_symbols_received):
raise AssertionError(f"Expected {len(self.expected_symbols_received)} option contracts Symbols, found {len(self.symbols_received)}")
missingSymbols = [expectedSymbol for expectedSymbol in self.expectedSymbolsReceived if expectedSymbol not in self.symbolsReceived]
if any(missingSymbols):
raise AssertionError(f'Symbols: "{", ".join(missingSymbols)}" were not found in OnData')
missing_symbols = [expected_symbol for expected_symbol in self.expected_symbols_received if expected_symbol not in self.symbols_received]
if any(missing_symbols):
raise AssertionError(f'Symbols: "{", ".join(missing_symbols)}" were not found in OnData')
for expectedSymbol in self.expectedSymbolsReceived:
data = self.dataReceived[expectedSymbol]
for dataPoint in data:
dataPoint.EndTime = datetime(1970, 1, 1)
for expected_symbol in self.expected_symbols_received:
data = self.data_received[expected_symbol]
for data_point in data:
data_point.end_time = datetime(1970, 1, 1)
nonDupeDataCount = len(set(data))
if nonDupeDataCount < 1000:
raise AssertionError(f"Received too few data points. Expected >=1000, found {nonDupeDataCount} for {expectedSymbol}")
non_dupe_data_count = len(set(data))
if non_dupe_data_count < 1000:
raise AssertionError(f"Received too few data points. Expected >=1000, found {non_dupe_data_count} for {expected_symbol}")