I have this options strategy for which I would like to implement the following:
Close the position if 10% profit target is met. If the profit target is not met by 12pm ET, close the position.
Does anyone know how to fix the below code?
from datetime import timedelta
from AlgorithmImports import *
from QuantConnect.DataSource import *
class VirtualYellowGiraffe(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 5, 1)
self.SetEndDate(2023, 5, 1)
self.SetCash(100000)
self.equity = self.AddEquity("SPY", Resolution.Minute)
self.symbol = self.equity.Symbol
self.InitOptionsAndGreeks(self.equity)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(9, 35), self.OpenPosition)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(12, 0), self.ClosePosition)
self.targetProfitPercent = 0.10
self.positionOpen = False
self.putContract = None
self.callContract = None
def InitOptionsAndGreeks(self, theEquity):
theEquity.SetDataNormalizationMode(DataNormalizationMode.Raw)
self.SetWarmup(30, Resolution.Daily)
self.SetSecurityInitializer(lambda x: x.SetMarketPrice(self.GetLastKnownPrice(x)))
theOptionSubscription = self.AddOption(theEquity.Symbol)
theOptionSubscription.PriceModel = OptionPriceModels.CrankNicolsonFD()
theOptionSubscription.SetFilter(self.OptionsFilterFunction)
def OpenPosition(self):
if not self.IsWarmingUp and not self.positionOpen:
if self.equity.Symbol in self.CurrentSlice.Keys:
putContract = self.SellAnOTMPut(0.5, 0)
callContract = self.SellAnOTMCall(0.5, 0)
if putContract is not None and callContract is not None:
self.putContract = putContract
self.callContract = callContract
self.positionOpen = True
else:
# Handle the case when suitable contracts are not found
self.Debug("No suitable contracts found for the specified symbol and parameters.")
else:
# Handle the case when data for the symbol is not available in the current slice
self.Debug("Data for the specified symbol is not available in the current slice.")
def ClosePosition(self):
if self.positionOpen:
currentProfit = self.CalculateProfit()
if currentProfit >= self.targetProfitPercent:
self.ClosePositions()
self.positionOpen = False # Set positionOpen to False after closing the position
else:
self.ClosePositions()
self.positionOpen = False # Set positionOpen to False if target profit is not met
def CalculateProfit(self):
putPrice = self.Securities[self.putContract.Symbol].Price
callPrice = self.Securities[self.callContract.Symbol].Price
putCost = self.putContract.AskPrice + self.putContract.BidPrice
callCost = self.callContract.AskPrice + self.callContract.BidPrice
totalCost = putCost + callCost
currentProfit = (totalCost - (putPrice + callPrice)) / totalCost
return currentProfit
def ClosePositions(self):
self.Liquidate(self.putContract.Symbol)
self.Liquidate(self.callContract.Symbol)
def SellAnOTMPut(self, target_DELTA, target_DTE):
putContract = self.SelectContractByDelta(self.equity.Symbol, target_DELTA, target_DTE, OptionRight.Put)
orderMessage = f"Stock @ ${self.CurrentSlice[self.equity.Symbol].Close} |" + \
f"STO {putContract.Symbol} " + \
f"({round(putContract.Greeks.Delta,2)} Delta)"
self.Debug(f"{self.Time} {orderMessage}")
self.MarketOrder(putContract.Symbol, -1, False, orderMessage)
return putContract
def SellAnOTMCall(self, target_DELTA, target_DTE):
callContract = self.SelectContractByDelta(self.equity.Symbol, target_DELTA, target_DTE, OptionRight.Call)
orderMessage = f"Stock @ ${self.CurrentSlice[self.equity.Symbol].Close} |" + \
f"STC {callContract.Symbol} " + \
f"({round(callContract.Greeks.Delta,2)} Delta)"
self.Debug(f"{self.Time} {orderMessage}")
self.MarketOrder(callContract.Symbol, -1, False, orderMessage)
return callContract
def SelectContractByDelta(self, symbolArg, strikeDeltaArg, expiryDTE, optionRightArg=OptionRight.Call):
canonicalSymbol = self.AddOption(symbolArg)
if self.CurrentSlice.OptionChains.ContainsKey(canonicalSymbol.Symbol):
theOptionChain = self.CurrentSlice.OptionChains[canonicalSymbol.Symbol]
theExpiryDate = self.Time + timedelta(days=expiryDTE)
filteredContracts = [x for x in theOptionChain if x.Right == optionRightArg]
contractsSortedByExpiration = sorted(filteredContracts, key=lambda p: abs(p.Expiry - theExpiryDate), reverse=False)
if len(contractsSortedByExpiration) > 0:
closestExpirationDate = contractsSortedByExpiration[0].Expiry
contractsMatchingExpiryDTE = [contract for contract in contractsSortedByExpiration if contract.Expiry == closestExpirationDate]
if len(contractsMatchingExpiryDTE) > 0:
closestContract = min(contractsMatchingExpiryDTE, key=lambda x: abs(abs(x.Greeks.Delta) - strikeDeltaArg))
return closestContract
return None
def OptionsFilterFunction(self, optionsContractsChain):
strikeCount = 10
min_Expiry_DTE = 0
max_Expiry_DTE = 45
return optionsContractsChain.IncludeWeeklys() \
.Strikes(-strikeCount, strikeCount) \
.Expiration(timedelta(min_Expiry_DTE), timedelta(max_Expiry_DTE))
Peter
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!