I could only find static symbol examples for options trading and wanted to share how I implemented AddUniverse in an options contract writing example. I hope someone finds it useful.
I took my alpha out before I shared, so I don't think this example will make you money - this example trades short strangles indescriminately.
As is, this thing is slow, scanning a massive options dataset. Definitely need to filter down to expand on the timeframe that you can reasonably backtest on. Backtesting 1 year can take 30 min to an hour. enjoy!
Alexandre Catarino
Hi Stephan Vanwoezik ,
Thank you for your contribution. Allow me to make some considerations. :-)
When the algorithm subscribes to options with
option = self.AddOption(security.Symbol.Value)
it creates an Options Universe that will select and subscribe options contracts in the following iteration.
Therefore
chain = slice.OptionChains.GetValue(option.Symbol)
will be None/null/empty. I would recomment subscribing to options data in the OnSecuritiesChanged method.
At the moment, subscribe to options using AddOption in Universe Selection algorithms is not recommended because the algorithm cannot remove it when the underlying is removed by the Universe Selection. Instead, we advise using OptionChainProvider+AddOptionContract.
Stephan Vanwoezik
Hi Alexandre, I appreciate your feedback. I'll update this post when I get a chance to implement your suggestions. thanks!
Stephan Vanwoezik
maybe somebody else will take up the challenge to implement your suggestions - I had difficulty .... Alexandre could you offer pros and cons. Is it purely performance concerns? Also why does warmup happen? I'm new here and the framework puzzles me. Haven't been able to spend much time but I'm lost at times and just run a lot of code that seems 'magical', if you know what I mean.
DanRock
I've been working on something with similar issues and am a bit stuck currently as well. I think options functionality in the framework is a nice idea, but there are some issues to work through.
Per the comments above on using the OptionChainProvider, it is unfortunately not sufficient when weekly options are desired or the greeks are needed on the chain prior to selection. I've been trying to work a solution based from Alexandre's comment in the link below (which is from 2017). By controlling it in the OnSecuritiesChanged. However, a runtime error has been persistant with this method and is associated with adding the options but only well after they are added and it never gets to a point to remove them to even test that part of the code. I'm currious if there are any better ways that one might employ when weekly options are needed at a minimum within the framework? If get a chance to make a minimum reproducible example, I attach one.
Stephan,
Your warmup issue of AddOption that Alexandre referenced was that you added the option chain in the same data slice that you try to access it. Data doesn't get piped in until the next slice. So it would be better to use the event handler where it adds it when securities change. It could be done in the OnData, but would have to only use it at the next slice and so you would have to keep track of things with more complicated logic.
Alexandre,
Is the RemoveSecurity function not a recommended method to take an option contract out of the universe currently, or better will it still work (granted it is not likely recommended manually doing anything). The documentation page (below) is not working these days and I haven't been able to look it up without making assumptions from the github page.
https://www.quantconnect.com/lean/docs#topic27616.html
https://www.quantconnect.com/forum/discussion/2928/how-to-keep-an-updated-option-universe/p1
Stephan Vanwoezik
Thanks DanRock , I've been away from this but your feedback is noted. I hope to get back to implementation soon...
Stephan Vanwoezik
Alexandre, I was running a similar version just for fun but it threw up:
Runtime Error: The DataQueueHandler does not support Options. Stack Trace: System.NotSupportedException: The DataQueueHandler does not support Options. at QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed.CreateUniverseSubscription (QuantConnect.Data.UniverseSelection.SubscriptionRequest request) [0x003a5] in <3840913a1b2f4c648b4cce63d6dee8d5>:0 at QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed.CreateSubscription (QuantConnect.Data.UniverseSelection.SubscriptionRequest request) [0x00011] in <3840913a1b2f4c648b4cce63d6dee8d5>:0 at QuantConnect.Lean.Engine.DataFeeds.DataManager.AddSubscription (QuantConnect.Data.UniverseSelection.SubscriptionRequest request) [0x0001d] in <3840913a1b2f4c648b4cce63d6dee8d5>:0 at QuantConnect.Lean.Engine.DataFeeds.DataManager+<>c__DisplayClass13_0.<.ctor>b__0 (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x0010a] in <3840913a1b2f4c648b4cce63d6dee8d5>:0 at QuantConnect.Securities.UniverseManager.OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000a] in <33281a5b01c8489eadf30c9da1927150>:0 at QuantConnect.Securities.UniverseManager.Add (QuantConnect.Symbol key, QuantConnect.Data.UniverseSelection.Universe universe) [0x00017] in <33281a5b01c8489eadf30c9da1927150>:0 at QuantConnect.Algorithm.QCAlgorithm.OnEndOfTimeStep () [0x00400] in <735a786b160343f7ba8a6947640abf5b>:0 at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnEndOfTimeStep () [0x00001] in <1c45cc3f99394650bdded68f37b16fad>:0 at QuantConnect.Lean.Engine.AlgorithmManager.Run (QuantConnect.Packets.AlgorithmNodePacket job, QuantConnect.Interfaces.IAlgorithm algorithm, QuantConnect.Lean.Engine.DataFeeds.ISynchronizer synchronizer, QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions, QuantConnect.Lean.Engine.Results.IResultHandler results, QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime, QuantConnect.Lean.Engine.Server.ILeanManager leanManager, QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x01219] in <3840913a1b2f4c648b4cce63d6dee8d5>:0 at QuantConnect.Lean.Engine.Engine+<>c__DisplayClass9_2.<Run>b__9 () [0x000c8] in <3840913a1b2f4c648b4cce63d6dee8d5>:0
DataQueueHandler? Is this related to your recommendation or another issue? I still haven't had time to looik into your suggestions.
Jared Broad
"Runtime Error: The DataQueueHandler does not support Options"
Guessing you're working with LEAN? The data source you've set does not support options.
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.
Stephan Vanwoezik
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!