Hi I made a very simple experiment - purchased 1 put option few days before expiry and ran a backtest until few days after expiry - it appears that the P&L continues to change according to the price of the underlying even after the expiration of the option - is that a bug or should I have code / configure something to take expiration into account?
Mia Alissi
In QuantConnect, if you hold a long position in an Option that expires in the money, LEAN automatically exercises it at the expiration date. If you don't want this automatic exercise, you can sell the Option or let it expire worthless if it's out of the money.
Here is how you can exercise an Option in Python:
If you do not have sufficient capital for the order, it's rejected. By default, Option exercise orders are synchronous and fill immediately.
However, if you're seeing the P&L continue to change after the expiration of the option, it might be a bug or an issue with your specific algorithm. It would be helpful to review your code and ensure that you're correctly handling the expiration and exercise of options.
For more details, you can refer to the QuantConnect Documentation.
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.
Nathan Sela
What I am saying is that I've let the option expire and still LEAN continued to update the unrealized P&L after expiration. Attached is the code. You can run it with to two different end dates (after expiration which is June 15, 2018) and see that the unrealized P&L is different each time.
# region imports
from AlgorithmImports import *
# endregion
class DemoStrategy(QCAlgorithm):
option = None
option_symbol = None
trade = False
def Initialize(self):
self.SetStartDate(2018, 6, 10)
self.SetEndDate(2018, 6, 25)
self.SetCash(500000)
self.option = self.AddOption("SPY", Resolution.Hour)
self.option_symbol = self.option.Symbol
self.option.SetFilter(self.OptionUniverseFunc)
def OptionUniverseFunc(self, universe: OptionFilterUniverse) -> OptionFilterUniverse:
return universe.Strikes(-100, 100).Expiration(timedelta(days=0), timedelta(days=30))
@staticmethod
def FindClosestDelta(options: list, delta: float) -> int:
dist = 9999999
i_best = -1
for i, option in enumerate(options):
if abs(option.Greeks.Delta - delta) < dist:
dist = abs(option.Greeks.Delta - delta)
i_best = i
return i_best
def OnData(self, slice: Slice):
option_chain = slice.OptionChains.get(self.option_symbol, None)
if not option_chain:
return
if slice.Time.hour == 16:
puts = [option for option in option_chain if option.Right == OptionRight.Put]
i_delta = DemoStrategy.FindClosestDelta(puts, -0.7)
option_to_trade = puts[i_delta]
if not self.trade:
self.Debug(f'{slice.Time} - buying option with strike {option_to_trade.Strike} expiry {option_to_trade.Expiry} and price {option_to_trade.AskPrice}')
self.Buy(option_to_trade.Symbol, 1)
self.trade = True
self.Debug(f'{slice.Time} - Underlying price {option_to_trade.UnderlyingLastPrice}')
Mia Alissi
In your code, you are buying a put option but you are not explicitly handling the expiration or exercise of the option. In QuantConnect, if you hold a long position in an Option that expires in the money, LEAN automatically exercises it at the expiration date. However, if the option is out of the money, it will expire worthless.
If you are seeing the unrealized P&L continue to change after the expiration of the option, it might be due to the automatic exercise of the option by LEAN. If the option is in the money at expiration, LEAN will automatically exercise it and you will end up with a position in the underlying asset, which will continue to affect your P&L.
If you want to avoid this, you can sell the option before it expires or you can manually exercise it using the
ExerciseOption
method:If you do not want to exercise the option and would rather let it expire, you can check the option's expiry date and sell it if the current date is the expiry date:
For more details, you can refer to the QuantConnect Documentation.
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.
Nathan Sela
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!