pep8 conversion on python algorithms #2 (#7933)

* pep8 conversion

* more

* Minor fix

* Fix related regression algorithm

---------

Co-authored-by: Martin Molinero <martin.molinero1@gmail.com>
This commit is contained in:
Louis Szeto
2024-04-18 04:36:53 +08:00
committed by GitHub
parent c2bea3c173
commit 784e497691
25 changed files with 466 additions and 466 deletions
@@ -1,4 +1,4 @@
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,45 +20,45 @@ from AlgorithmImports import *
### <meta name="tag" content="benchmarks" />
### <meta name="tag" content="futures" />
class BasicTemplateFuturesDailyAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2013, 10, 8)
self.SetEndDate(2014, 10, 10)
self.SetCash(1000000)
def initialize(self):
self.set_start_date(2013, 10, 8)
self.set_end_date(2014, 10, 10)
self.set_cash(1000000)
resolution = self.GetResolution()
extendedMarketHours = self.GetExtendedMarketHours()
resolution = self.get_resolution()
extended_market_hours = self.get_extended_market_hours()
# Subscribe and set our expiry filter for the futures chain
self.futureSP500 = self.AddFuture(Futures.Indices.SP500EMini, resolution, extendedMarketHours=extendedMarketHours)
self.futureGold = self.AddFuture(Futures.Metals.Gold, resolution, extendedMarketHours=extendedMarketHours)
self.future_sp500 = self.add_future(Futures.Indices.SP_500_E_MINI, resolution, extended_market_hours=extended_market_hours)
self.future_gold = self.add_future(Futures.Metals.GOLD, resolution, extended_market_hours=extended_market_hours)
# set our expiry filter for this futures chain
# SetFilter method accepts timedelta objects or integer for days.
# The following statements yield the same filtering criteria
self.futureSP500.SetFilter(timedelta(0), timedelta(182))
self.futureGold.SetFilter(0, 182)
self.future_sp500.set_filter(timedelta(0), timedelta(182))
self.future_gold.set_filter(0, 182)
def OnData(self,slice):
if not self.Portfolio.Invested:
for chain in slice.FutureChains:
def on_data(self,slice):
if not self.portfolio.invested:
for chain in slice.future_chains:
# Get contracts expiring no earlier than in 90 days
contracts = list(filter(lambda x: x.Expiry > self.Time + timedelta(90), chain.Value))
contracts = list(filter(lambda x: x.expiry > self.time + timedelta(90), chain.value))
# if there is any contract, trade the front contract
if len(contracts) == 0: continue
contract = sorted(contracts, key = lambda x: x.Expiry)[0]
contract = sorted(contracts, key = lambda x: x.expiry)[0]
# if found, trade it.
# Also check if exchange is open for regular or extended hours. Since daily data comes at 8PM, this allows us prevent the
# algorithm from trading on friday when there is not after-market.
if self.Securities[contract.Symbol].Exchange.Hours.IsOpen(self.Time, True):
self.MarketOrder(contract.Symbol, 1)
if self.securities[contract.symbol].exchange.hours.is_open(self.time, True):
self.market_order(contract.symbol, 1)
# Same as above, check for cases like trading on a friday night.
elif all(x.Exchange.Hours.IsOpen(self.Time, True) for x in self.Securities.Values if x.Invested):
self.Liquidate()
elif all(x.exchange.hours.is_open(self.time, True) for x in self.securities.values() if x.invested):
self.liquidate()
def GetResolution(self):
return Resolution.Daily
def get_resolution(self):
return Resolution.DAILY
def GetExtendedMarketHours(self):
def get_extended_market_hours(self):
return False