Hello Team Quantconnect & all the other users
I'm using your platform with several accounts and I've noticed that the backtesting with the options greek is incredibly slow. I really would like to include them in my strategies but a single backtesting takes me around 30-45 min. I'm using "AddOption" to select the greeks. Simultaneously, I'm using a similar strategy without the greeks via "OptionChainProvider" without having long waiting time for backtests.
Is there a possibility to use greeks via the "OptionChainProvider"? If not, is there way to accelerate the backtests when using the greeks via "AddOption"?
Thank you for your efforts in advance!
Rahul Chowdhury
Hey Nicola,
Your backtests shouldn't be taking that long when using greeks,
Do you mind posting a snippet of your code? Otherwise it's impossible to attempt to debug.
Best
Rahul
Nate Miller
Hi Rahul,
I'm not the original poster but was noticed the same thing, so thought I would post here. I'm rather new to the platform so appologies in advance if the error is obvious.
from datetime import timedelta import math from QuantConnect.Securities.Option import OptionPriceModels SYMBOL = "SPY" class CoveredCallAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2019, 1, 1) self.SetEndDate(2019, 1, 5) self.SetCash(100000) equity = self.AddEquity(SYMBOL, Resolution.Minute) option = self.AddOption(SYMBOL, Resolution.Minute) self.symbol = option.Symbol self.UniverseSettings.Resolution = Resolution.Daily # set strike/expiry filter for this option chain option.SetFilter(-60, -30, timedelta(30), timedelta(90)) option.PriceModel = OptionPriceModels.CrankNicolsonFD() # set the warm-up period for the pricing model self.SetWarmUp(TimeSpan.FromDays(10)) # use the underlying equity as the benchmark self.SetBenchmark(equity.Symbol) def OnData(self, slice): """Handle each data slice.""" if self.IsWarmingUp: return self.TradeOptions(slice) def TradeOptions(self,slice): """Does not actually trade. Just an example of how reading greeks can slow algorithm.""" for i in slice.OptionChains: if i.Key != self.symbol: continue chain = i.Value call = [x for x in chain if x.Right == OptionRight.Call] contracts = call # Uncommenting the line below requests the greeks and causes the algorithm to run very slow. Estimated 5 minutes # to process each day. With the line commented, the algorithm runs 5 days in just a few seconds. [contract.Greeks.Delta for contract in contracts] return
Any insights you have would be much appreicated, and thanks so much for the great platform!
Nate
Cole S
Looking at the Lean code I can see that the Greeks are lazy loaded (i.e. on demand). This would explain why calling the Greeks slows everything down. They also use a third party lib for the greek calculations so I'm assuming that they don't have much control over how long it takes and is probably why they are lazy loaded in the first place.
I'm very interested in this question though as I've written a few Option algorithms, but due to the slow backtesting I have left them alone. Even with a single option chain it runs very slowly when using the Greeks/ImpliedVolatility.
Jared Broad
Jason Bohne did some interesting work here he presented in the recent meet up. I think ultimately kctrader we need to preprocess these greeks with the top 3 libraries and pull them from cached values instead of doing the math in backtesting. No matter what we do it'll still be heavy and slow, so probably we'll need to add hourly or daily resolution data to see a real speed up :\ Fingers cross Jason shares his code :)
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.
Nate Miller
Thanks all for the info. I ended up adjusting my `OnData` method to pull in greeks 1x per day and cache them. It worked out to a pretty substantial speed up.
Nicola Crimi
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!