pep8 conversion of python algos #12 (#7954)
Python Virtual Environments / build (push) Has been cancelled
API Tests / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Report Generator Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled

* pep8 conversion

* PEP8 updates/fixes

* Update pythonnet version to 2.0.35

---------

Co-authored-by: Jhonathan Abreu <jdabreu25@gmail.com>
This commit is contained in:
Louis Szeto
2024-04-20 04:55:48 +08:00
committed by GitHub
parent b64ad5f68a
commit fa886eba79
27 changed files with 272 additions and 273 deletions
@@ -21,50 +21,49 @@ from AlgorithmImports import *
### <meta name="tag" content="options" />
class OptionSplitRegressionAlgorithm(QCAlgorithm):
def Initialize(self):
def initialize(self):
# this test opens position in the first day of trading, lives through stock split (7 for 1),
# and closes adjusted position on the second day
self.SetCash(1000000)
self.SetStartDate(2014,6,6)
self.SetEndDate(2014,6,9)
self.set_cash(1000000)
self.set_start_date(2014,6,6)
self.set_end_date(2014,6,9)
option = self.AddOption("AAPL")
option = self.add_option("AAPL")
# set our strike/expiry filter for this option chain
option.SetFilter(self.UniverseFunc)
option.set_filter(self.universe_func)
self.SetBenchmark("AAPL")
self.set_benchmark("AAPL")
self.contract = None
def OnData(self, slice):
if not self.Portfolio.Invested:
if self.Time.hour > 9 and self.Time.minute > 0:
for kvp in slice.OptionChains:
chain = kvp.Value
contracts = filter(lambda x: x.Strike == 650 and x.Right == OptionRight.Call, chain)
sorted_contracts = sorted(contracts, key = lambda x: x.Expiry)
def on_data(self, slice):
if not self.portfolio.invested:
if self.time.hour > 9 and self.time.minute > 0:
for kvp in slice.option_chains:
chain = kvp.value
contracts = filter(lambda x: x.strike == 650 and x.right == OptionRight.CALL, chain)
sorted_contracts = sorted(contracts, key = lambda x: x.expiry)
if len(sorted_contracts) > 1:
self.contract = sorted_contracts[1]
self.Buy(self.contract.Symbol, 1)
self.buy(self.contract.symbol, 1)
elif self.Time.day > 6 and self.Time.hour > 14 and self.Time.minute > 0:
self.Liquidate()
elif self.time.day > 6 and self.time.hour > 14 and self.time.minute > 0:
self.liquidate()
if self.Portfolio.Invested:
options_hold = [x for x in self.Portfolio.Securities if x.Value.Holdings.AbsoluteQuantity != 0]
holdings = options_hold[0].Value.Holdings.AbsoluteQuantity
if self.Time.day == 6 and holdings != 1:
self.Log("Expected position quantity of 1 but was {0}".format(holdings))
if self.Time.day == 9 and holdings != 7:
self.Log("Expected position quantity of 7 but was {0}".format(holdings))
if self.portfolio.invested:
options_hold = [x for x in self.portfolio.securities if x.value.holdings.absolute_quantity != 0]
holdings = options_hold[0].value.holdings.absolute_quantity
if self.time.day == 6 and holdings != 1:
self.log("Expected position quantity of 1 but was {0}".format(holdings))
if self.time.day == 9 and holdings != 7:
self.log("Expected position quantity of 7 but was {0}".format(holdings))
# set our strike/expiry filter for this option chain
def UniverseFunc(self, universe):
return universe.IncludeWeeklys().Strikes(-2, 2).Expiration(timedelta(0), timedelta(365*2))
def universe_func(self, universe):
return universe.include_weeklys().strikes(-2, 2).expiration(timedelta(0), timedelta(365*2))
def OnOrderEvent(self, orderEvent):
self.Log(str(orderEvent))
def on_order_event(self, order_event):
self.log(str(order_event))