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:
@@ -14,65 +14,69 @@
|
||||
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 AddOptionContractFromUniverseRegressionAlgorithm(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, 9)
|
||||
self.set_start_date(2014, 6, 5)
|
||||
self.set_end_date(2014, 6, 9)
|
||||
|
||||
self._expiration = datetime(2014, 6, 21)
|
||||
self._securityChanges = None
|
||||
self._security_changes = None
|
||||
self._option = None
|
||||
self._traded = False
|
||||
|
||||
self._twx = Symbol.Create("TWX", SecurityType.Equity, Market.USA)
|
||||
self._aapl = Symbol.Create("AAPL", SecurityType.Equity, Market.USA)
|
||||
self.UniverseSettings.Resolution = Resolution.Minute
|
||||
self.UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw
|
||||
self._twx = Symbol.create("TWX", SecurityType.EQUITY, Market.USA)
|
||||
self._aapl = Symbol.create("AAPL", SecurityType.EQUITY, Market.USA)
|
||||
self.universe_settings.resolution = Resolution.MINUTE
|
||||
self.universe_settings.data_normalization_mode = DataNormalizationMode.RAW
|
||||
|
||||
self.AddUniverse(self.Selector, self.Selector)
|
||||
self.add_universe(self.selector, self.selector)
|
||||
|
||||
def Selector(self, fundamental):
|
||||
if self.Time <= datetime(2014, 6, 5):
|
||||
def selector(self, fundamental):
|
||||
if self.time <= datetime(2014, 6, 5):
|
||||
return [ self._twx ]
|
||||
return [ self._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 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 == datetime(2014, 6, 6, 14, 0, 0):
|
||||
if self.time == datetime(2014, 6, 6, 14, 0, 0):
|
||||
# liquidate & remove the option
|
||||
self.RemoveOptionContract(self._option)
|
||||
self.remove_option_contract(self._option)
|
||||
|
||||
def OnSecuritiesChanged(self, changes):
|
||||
def on_securities_changed(self, changes):
|
||||
# keep track of all removed and added securities
|
||||
if self._securityChanges == None:
|
||||
self._securityChanges = changes
|
||||
if self._security_changes == None:
|
||||
self._security_changes = changes
|
||||
else:
|
||||
self._securityChanges.op_Addition(self._securityChanges, changes)
|
||||
self._security_changes += changes
|
||||
|
||||
if any(security.Symbol.SecurityType == SecurityType.Option for security in changes.AddedSecurities):
|
||||
if any(security.symbol.security_type == SecurityType.OPTION for security in changes.added_securities):
|
||||
return
|
||||
|
||||
for addedSecurity in changes.AddedSecurities:
|
||||
options = self.OptionChainProvider.GetOptionContractList(addedSecurity.Symbol, self.Time)
|
||||
options = sorted(options, key=lambda x: x.ID.Symbol)
|
||||
for addedSecurity in changes.added_securities:
|
||||
options = self.option_chain_provider.get_option_contract_list(addedSecurity.symbol, 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)
|
||||
|
||||
self.AddOptionContract(option)
|
||||
self.add_option_contract(option)
|
||||
|
||||
# just keep the first we got
|
||||
if self._option == None:
|
||||
|
||||
Reference in New Issue
Block a user