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
@@ -14,46 +14,50 @@
from AlgorithmImports import *
### <summary>
### We add an option contract using 'QCAlgorithm.AddOptionContract' and place a trade, the underlying
### We add an option contract using 'QCAlgorithm.add_option_contract' and place a trade, the underlying
### gets deselected from the universe selection but should still be present since we manually added the option contract.
### Later we call 'QCAlgorithm.RemoveOptionContract' and expect both option and underlying to be removed.
### Later we call 'QCAlgorithm.remove_option_contract' and expect both option and underlying to be removed.
### </summary>
class AddOptionContractExpiresRegressionAlgorithm(QCAlgorithm):
def Initialize(self):
def initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2014, 6, 5)
self.SetEndDate(2014, 6, 30)
self.set_start_date(2014, 6, 5)
self.set_end_date(2014, 6, 30)
self._expiration = datetime(2014, 6, 21)
self._option = None
self._traded = False
self._twx = Symbol.Create("TWX", SecurityType.Equity, Market.USA)
self._twx = Symbol.create("TWX", SecurityType.EQUITY, Market.USA)
self.AddUniverse("my-daily-universe-name", self.Selector)
self.add_universe("my-daily-universe-name", self.selector)
def Selector(self, time):
def selector(self, time):
return [ "AAPL" ]
def OnData(self, data):
def on_data(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
if self._option == None:
options = self.OptionChainProvider.GetOptionContractList(self._twx, self.Time)
options = sorted(options, key=lambda x: x.ID.Symbol)
options = self.option_chain_provider.get_option_contract_list(self._twx, self.time)
options = sorted(options, key=lambda x: x.id.symbol)
option = next((option for option in options if option.ID.Date == self._expiration and option.ID.OptionRight == OptionRight.Call and option.ID.OptionStyle == OptionStyle.American), None)
option = next((option
for option in options
if option.id.date == self._expiration and
option.id.option_right == OptionRight.CALL and
option.id.option_style == OptionStyle.AMERICAN), None)
if option != None:
self._option = self.AddOptionContract(option).Symbol
self._option = self.add_option_contract(option).symbol
if self._option != None and self.Securities[self._option].Price != 0 and not self._traded:
if self._option != None and self.securities[self._option].price != 0 and not self._traded:
self._traded = True
self.Buy(self._option, 1)
self.buy(self._option, 1)
if self.Time > self._expiration and self.Securities[self._twx].Invested:
if self.time > self._expiration and self.securities[self._twx].invested:
# we liquidate the option exercised position
self.Liquidate(self._twx)
self.liquidate(self._twx)