Is it possible to get an option's implied volatility in Python?
For example BasicTemplateOptionsHistoryAlgorithm.cs works fine in C#, printing out option Greeks and IV, but BasicTemplateOptionsHistoryAlgorithm.py is giving the following: Runtime Error: Python.Runtime.PythonException: KeyError : 'Level symbol must be same as name (None)'
It also looks like option.PriceModel cannot be set in the Python code.
Jared Broad
Thanks for the message Turbo. We'll look into it.
Bugs reported to the forums get lost :) Please send support tickets for this.
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.
Jing Wu
Hi TurboDzyl, you can set the PriceModel by import the library
from QuantConnect.Securities.Option import OptionPriceModels
The updated backtest code attached
TurboDZyl
Thanks Jing, Jared! Now it works as it should.
TurboDZyl
However the Greeks delta, gamma and theta are still showing 0, is there an equally simple solution for that?
TurboDZyl
Never mind, seems to be working now.
Am I correct that option data is only available in minute resolution? If so, is there a way to access the slice object on a larger resulotion (day or hour)? I've looked into consolidators but they seem to provide a bar object only.
Lorrrr
Hi jared_broadl,
im having trouble getting the implied volatility of the next expiration ( it say the iv value is the same of the front, meaning in my code down self.first and self.second have the same value even though the should be slightly different
also it would be nice if option could have resolution of 1 hour to make backtest faster
Â
Â
equity = self.AddEquity("SPY", Resolution.Minute) option = self.AddOption("SPY", Resolution.Minute) self.symbol = option.Symbol option.PriceModel = OptionPriceModels.CrankNicolsonFD() option.SetFilter(self.UniverseFunc) #option.SetFilter(-2,2, timedelta(0), timedelta(180)) self.exp = 0 self.strike = 0 self.lors = None self.calls = None def UniverseFunc(self, universe): # include weekly contracts return universe.IncludeWeeklys().Expiration(TimeSpan.FromDays(2), TimeSpan.FromDays(40)).Strikes(-30,30) def OnData(self, data): #if not data.ContainsKey(self.vx1): # return if self.IsWarmingUp: return if self.day == self.Time.day: return if(data.HasData) : for kvp in data.OptionChains.Values: chains = kvp if kvp.Contracts.Count < 1: continue calls = list(filter(lambda x: x.Right == OptionRight.Call, chains)) puts = list(filter(lambda x: x.Right == OptionRight.Put, chains)) #self.underlying_price = self.Securities[self.spy].Price self.underlying_price = self.Securities[self.symbol].Price expiries = [i.Expiry for i in puts] strikes = [i.Strike for i in puts] if len(expiries) < 2: return strike = min(strikes, key=lambda x: abs(x-self.underlying_price * float(0.97) )) self.atm_call = [i for i in chains if i.Expiry == expiries[0] and i.Strike == strike] self.atm_put = [i for i in chains if i.Expiry ==expiries[0] and i.Strike ==strike] calls = [i for i in chains if i.Expiry == expiries[1] and i.Strike == strike] puts = [i for i in chains if i.Expiry ==expiries[1] and i.Strike ==strike] if len(self.atm_call) <1 or len(calls) < 1: return if calls[0].ImpliedVolatility == 0.0: return self.first = self.atm_call[0].ImpliedVolatility self.second = calls[0].ImpliedVolatility
Â
Derek Melchin
Hi Lorrrr,
The IV of the two contracts are equal because the two contracts selected are actually the same contract. See the attached backtest for reference.
Best,
Derek Melchin
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.
T C
I got all 0 implied volatility and Greeks from the above code attached by Jing Wu. Any ideas? Thanks!
Vladimir
Vladimir
T C
Try this.
T C
Thank you very much Vladimir! It works.Â
TurboDZyl
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!