How are options assigments computed?


I've coded a monthly short strangle on the S&P500, however I'm having a hard time understanding how are options assignments computed.

By looking at the "trades" tab, it appears that all options that are "against me" (strike value < underlying value on calls, the opposite on puts) are assigned at expiration.

I would like options to be assigned at the worst possible case scenario. For example, if I sell a monthly call with strike 100, and during the month the underlying goes from 100 to 110 and goes back to 105 when the option expire, I'd like the backtester to assume that the option gets assigned when the underlying is at 110, not 105.

Is this possible?

Thanks a lot!

Update Backtest

Hi Joseph, if you don't liquidate the contract, the algorithm will hold the contract until it expires. If you want to liquidate the position before it expires, you should save the contract symbol and compare the contract strike price with the current underlying price and send the liquidation order. For example

if self.Portfolio[self.atm_call[0].Symbol].Invested:
if self.Securities[self.atm_call[0].Symbol].AskPrice > self.atm_call[0].Strike:

Using Ask or Bid price depends on your algorithm.


Hello Jing,

Thank you very much for your time, code example and expertise: this is very much appreciated.

You explained to me how to liquidate an option contract before expiration and how to verify its strike price relative to the underlying price.

What I'd ideally want to achieve now is to liquidate the option contract at the worst possible moment: out of all the prices the underlying will touch, I'd like to identify the one which is further away from the option strike and liquidate it that day. Can you kindly confirm to me there is no way to do this automatically? I think I can code it myself if there's not: maybe it'll be useful to other people as well (LEAN engine's assumptions that american options are always liquidated at expiry might show some strategies to be performing better than what they'd do in the real market, where options might be assigned, for the better or for the worse, before they expire).

The only problem in coding something like this is that the OnData() function only looks at a single tick values: we can't possibly check the future or the past in there (nor we want to - we'd be searching every tick for something we only need once: the liquidation date according to the "worst possible" idea).

If I might ask: what do you think would be the best way to code something like this? I'm not asking for specific code examples, just a general "best practice" idea. I was thinking about a function that gets called every time we sell an option: the function would need the option strike and the underlying prices for the period before expiration, and would return either nothing or a flag that indicates when the market will assign the option. The result of this function should be stored in a global variable and OnData will always check this variable and call .Liquidate based on what's stored in it (we could use a date, or the underlying price).

Thanks again for your input!


If your liquidation condition is complicated you can create a function with arguments with the underlying price. if not, "if else" statement would be easier to implement.  "identify the one which is further away from the option strike and liquidate it that day" This "further away" is hard to define as you only have the history price without the future price. If you need the history price of underlying, you can use the rolling window to save the history bar.

Update Backtest


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.


This discussion is closed