* pep8 conversion * PEP8 updates/fixes * Minor update --------- Co-authored-by: Jhonathan Abreu <jdabreu25@gmail.com>
This commit is contained in:
@@ -19,57 +19,57 @@ from QuantConnect.Securities.Positions import IPositionGroup
|
||||
### </summary>
|
||||
class OptionStrategyFactoryMethodsBaseAlgorithm(QCAlgorithm):
|
||||
|
||||
def Initialize(self):
|
||||
self.SetStartDate(2015, 12, 24)
|
||||
self.SetEndDate(2015, 12, 24)
|
||||
self.SetCash(1000000)
|
||||
def initialize(self):
|
||||
self.set_start_date(2015, 12, 24)
|
||||
self.set_end_date(2015, 12, 24)
|
||||
self.set_cash(1000000)
|
||||
|
||||
option = self.AddOption("GOOG")
|
||||
self._option_symbol = option.Symbol
|
||||
option = self.add_option("GOOG")
|
||||
self._option_symbol = option.symbol
|
||||
|
||||
option.SetFilter(-2, +2, 0, 180)
|
||||
option.set_filter(-2, +2, 0, 180)
|
||||
|
||||
self.SetBenchmark("GOOG")
|
||||
self.set_benchmark("GOOG")
|
||||
|
||||
def OnData(self, slice):
|
||||
if not self.Portfolio.Invested:
|
||||
chain = slice.OptionChains.get(self._option_symbol)
|
||||
def on_data(self, slice):
|
||||
if not self.portfolio.invested:
|
||||
chain = slice.option_chains.get(self._option_symbol)
|
||||
if chain is not None:
|
||||
self.TradeStrategy(chain, self._option_symbol)
|
||||
self.trade_strategy(chain, self._option_symbol)
|
||||
else:
|
||||
# Verify that the strategy was traded
|
||||
positionGroup = list(self.Portfolio.Positions.Groups)[0]
|
||||
position_group = list(self.portfolio.positions.groups)[0]
|
||||
|
||||
buyingPowerModel = positionGroup.BuyingPowerModel
|
||||
if not isinstance(buyingPowerModel, OptionStrategyPositionGroupBuyingPowerModel):
|
||||
buying_power_model = position_group.buying_power_model
|
||||
if not isinstance(buying_power_model, OptionStrategyPositionGroupBuyingPowerModel):
|
||||
raise Exception("Expected position group buying power model type: OptionStrategyPositionGroupBuyingPowerModel. "
|
||||
f"Actual: {type(positionGroup.BuyingPowerModel).__name__}")
|
||||
f"Actual: {type(position_group.buying_power_model).__name__}")
|
||||
|
||||
self.AssertStrategyPositionGroup(positionGroup, self._option_symbol)
|
||||
self.assert_strategy_position_group(position_group, self._option_symbol)
|
||||
|
||||
# Now we should be able to close the position
|
||||
self.LiquidateStrategy()
|
||||
self.liquidate_strategy()
|
||||
|
||||
# We can quit now, no more testing required
|
||||
self.Quit()
|
||||
self.quit()
|
||||
|
||||
def OnEndOfAlgorithm(self):
|
||||
if self.Portfolio.Invested:
|
||||
def on_end_of_algorithm(self):
|
||||
if self.portfolio.invested:
|
||||
raise Exception("Expected no holdings at end of algorithm")
|
||||
|
||||
orders_count = len(list(self.Transactions.GetOrders(lambda order: order.Status == OrderStatus.Filled)))
|
||||
if orders_count != self.ExpectedOrdersCount():
|
||||
raise Exception(f"Expected {self.ExpectedOrdersCount()} orders to have been submitted and filled, "
|
||||
orders_count = len(list(self.transactions.get_orders(lambda order: order.status == OrderStatus.FILLED)))
|
||||
if orders_count != self.expected_orders_count():
|
||||
raise Exception(f"Expected {self.expected_orders_count()} orders to have been submitted and filled, "
|
||||
f"half for buying the strategy and the other half for the liquidation. Actual {orders_count}")
|
||||
|
||||
def ExpectedOrdersCount(self) -> int:
|
||||
def expected_orders_count(self) -> int:
|
||||
raise NotImplementedError("ExpectedOrdersCount method is not implemented")
|
||||
|
||||
def TradeStrategy(self, chain: OptionChain, option_symbol: Symbol) -> None:
|
||||
def trade_strategy(self, chain: OptionChain, option_symbol: Symbol) -> None:
|
||||
raise NotImplementedError("TradeStrategy method is not implemented")
|
||||
|
||||
def AssertStrategyPositionGroup(self, positionGroup: IPositionGroup, option_symbol: Symbol) -> None:
|
||||
def assert_strategy_position_group(self, position_group: IPositionGroup, option_symbol: Symbol) -> None:
|
||||
raise NotImplementedError("AssertStrategyPositionGroup method is not implemented")
|
||||
|
||||
def LiquidateStrategy(self) -> None:
|
||||
def liquidate_strategy(self) -> None:
|
||||
raise NotImplementedError("LiquidateStrategy method is not implemented")
|
||||
|
||||
Reference in New Issue
Block a user