Can someone help me please. When I run the back test my code makes one trade and that is all. How do I make it so that when my initial trade closes. Then it starts to scan for another trade to meet the criteria
from datetime import timedelta
from AlgorithmImports import *
class BullPutSpreadStrategy(QCAlgorithm):
def initialize(self) -> None:
self.set_start_date(2020, 1, 1)
self.set_end_date(2023, 12, 1)
self.set_cash(50000)
option = self.add_option("SPY", Resolution.MINUTE)
self.symbol = option.symbol
option.set_filter(self.universe_func)
self.credit_received = 0.0
self.current_option_strategy = None
self.spy = self.add_equity("SPY", Resolution.HOUR).symbol
self.sma = self.sma(self.spy, 200, Resolution.HOUR)
def universe_func(self, universe: OptionFilterUniverse) -> OptionFilterUniverse:
return universe.include_weeklys().strikes(-1, 1).expiration(timedelta(days=1), timedelta(days=7))
def on_data(self, slice: Slice) -> None:
if not self.sma.is_ready:
return
if self.current_option_strategy is not None:
self.check_stop_loss()
return
if self.securities[self.spy].price <= self.sma.current.value:
return
chain = slice.option_chains.get(self.symbol, None)
if not chain:
return
expiry = min([x.Expiry for x in chain])
puts = [i for i in chain if i.Expiry == expiry and i.right == OptionRight.PUT]
if len(puts) == 0:
return
puts = sorted(puts, key=lambda x: abs(x.greeks.delta + 0.35))
short_put = puts[0]
lower_strike_puts = [p for p in puts if p.strike < short_put.strike]
if not lower_strike_puts:
return
long_put = min(lower_strike_puts, key=lambda x: x.strike)
option_strategy = OptionStrategies.bull_put_spread(self.symbol, short_put.strike, long_put.strike, expiry)
self.current_option_strategy = option_strategy
self.credit_received = short_put.bid_price - long_put.ask_price
self.buy(option_strategy, 1)
def check_stop_loss(self):
current_float_loss = self.securities[self.symbol].holdings.unrealized_profit - self.credit_received
if current_float_loss <= -1.5 * self.credit_received:
self.liquidate(self.current_option_strategy)
self.credit_received = 0.0
self.current_option_strategy = None
Mia Alissi
Sources: Index Option Bull Put Spread Algorithm, Index Option Bull Call Spread Algorithm, Index Option Bear Put Spread Algorithm, Bull Put Spread - QuantConnect.com, Index Option Bear Call Spread Algorithm
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Warren Chapman
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!