How to code so that based on my account value I only make an open credit spread options using 2% of my account value. Use my code below as reference …
from AlgorithmImports import *
class BullCreditSpread(QCAlgorithm):
def initialize(self):
self.set_start_date(2024, 1, 1)
self.set_end_date(2024, 11, 1)
self.set_cash(10000)
self.spy = self.add_equity("SPY", Resolution.HOUR).symbol
self.spy_option = self.add_option("SPY")
self.spy_option.set_filter(self.weekly_filter)
self.sma = self.SMA(self.spy, 50, Resolution.HOUR)
self.has_position = False
self.spy_price = None
self.tickets = []
self.open_symbols = []
self.schedule.on(self.date_rules.every(DayOfWeek.MONDAY), self.time_rules.at(10, 0), self.trade_options)
def weekly_filter(self, universe):
return universe.include_weeklys().strikes(-5, 5).expiration(0, 7)
def on_data(self, slice: Slice) -> None:
if not self.sma.is_ready:
return
if self.securities.contains_key(self.spy):
self.spy_price = self.securities[self.spy].price
if any([self.portfolio[symbol].invested for symbol in self.open_symbols]):
return
chain = slice.option_chains.get(self.spy_option.symbol)
if not chain:
return
expiry = min([x.expiry for x in chain])
puts = sorted([i for i in chain if i.expiry == expiry and i.right == OptionRight.PUT],
key=lambda x: x.strike)
if len(puts) < 2:
return
credit_per_spread = puts[-1].bid_price - puts[0].ask_price
if credit_per_spread <= 0:
return
target_gain = self.portfolio.total_portfolio_value * 0.1
num_spreads = int(target_gain / credit_per_spread)
bull_put_spread = OptionStrategies.bull_put_spread(self.spy_option.symbol, puts[-1].strike, puts[0].strike, expiry)
def trade_options(self):
if self.spy_price is None:
return
if self.spy_price > self.sma.current.value:
chain = self.current_slice.option_chains.get(self.spy_option.symbol)
if chain is None:
return
contracts = sorted(chain, key=lambda x: abs(x.greeks.delta - 0.35))
if len(contracts) == 0:
return
short_option = contracts[0]
long_options = [x for x in chain if x.strike < short_option.strike and x.expiry == short_option.expiry]
if not long_options:
return
long_option = sorted(long_options, key=lambda x: x.strike, reverse=True)[0]
if not self.has_position:
spread = OptionStrategies.bull_put_spread(self.spy_option.symbol, short_option.strike, long_option.strike, short_option.expiry)
ticket = self.buy(spread, 1)
self.tickets.append(ticket)
self.open_symbols.append(self.spy_option.symbol)
self.has_position = True
def check_stop_loss(self):
current_float_loss = self.securities[self.spy].holdings.unrealized_profit - self.credit_received
if current_float_loss <= -1.25 * self.credit_received:
self.liquidate(self.current_option_strategy)
self.credit_received = 0.0
def check_take_profit(self):
current_float_profit = self.securities[self.spy].holdings.unrealized_profit
if current_float_profit >= .75 * self.credit_received:
self.liquidate(self.current_option_strategy)
self.credit_received = 0.0
def on_order_event(self, orderEvent):
if orderEvent.status == OrderStatus.FILLED:
self.has_position = False
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, Bull Call Spread - QuantConnect.com
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!