Delta-hedged straddle

Below an example of a delta-hedged straddle on the SPY. 

Strategy in a nutshell: 

  • sell 1-month At-The-Money call and put options on the SPY, 
  • delta-hedge once a day (if delta above a threshold), 
  • close other option in case one is assigned (or both on their last trading day),
  • Repeat.
Update Backtest

Alex - Thank you for sharing this!! Excellent work


You're welcome ;-)


Hi Alex, thanks for posting this excellent straddle algorithm. 

LEAN has this method to get all American public holidays.

self.TradingCalendar.GetDaysByType(TradingDayType.PublicHoliday, startDate, EndDate)

Here is an example to demonstrate this method

Newbie here. I cloned this algo to understand the code (as I am learning). Why does backtest take so long on this? It has been running (successfully) for last 10+ minutes. Is it because the total time period for the backtest is 1 year and it is using minute resolution?


Alex Muci 

I've converted your code to C#, and added Jing Wu's suggestion of US Holidays:


if (TradingCalendar
.GetDaysByType(TradingDayType.PublicHoliday, expiry, expiry)
.ToList().Count != 0)
Debug("Expiry on Holiday");
return expiry.AddDays(-1);


Helped me get into writing something in C# as I'm better with it than Python- just need to buy some data so I can actually test locally (don't like the web-IDE that much).

Still not 100% sure I've got it selling both the call / put or that if my days are "off by 1".


My first comment is excellent work!  I am new to QuantConnect and have been using this project to learn.  I have a question however: What is the utility of the variable _assignedOption?  

It is being used only 3 times.

1x in the class declaration:

private bool _assignedOption = false;

And 2x in the OnData function:

if (_assignedOption)
// can't figure out Linq Portfolio.Where(x => x.Value.Invested).Foreach(x => {}) ??
foreach (Securities.SecurityHolding holding in Portfolio.Values)
if (holding.Invested)
_assignedOption = false;

So, my question is, how is the code segment in OnData that is executed when _assignedOption is true ever triggered?

It seems this is a bug. And what is happening is that instead of liquidating the symbol here, all of the stock position is being liquidated in the close_options:

if (Portfolio[this.equity_symbol].Invested)

I am not sure of the effect, but I don't think this is the behavior that is expected.


Hi Josh, look at my Python code.

self._assignedOption is true in case one of the two options gets assigned (event fire up automatically in the OnAssignmentOrderEvent function - such assignment is simulated by QuantConnect to recreate live trading situations). My choice was to closing all legs of the trade once one of the options got assigned (NB: buyers can exercise their American options before expiry).

Hope this helps.


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