>> The IV for the underlying is calculated by mathematically combining the ATM IV of the options. As far as how to do this, it depends. Some combine the ATM IV of options across several expirations. The "combining" algorithm is not standardized. This sounds like something that I am looking for. Then however, it still comes down to how to calculate the implied volatility ATM? I think I look for a real mathematical example where we can input values for this? >> Why do you want to do the calculations yourself? I am creating a C# program to backtest for how implied volatility can affect a strategy.
Something is off here! When trying that "study" are you sure you have your chart set to Daily bars, and are using a minimum of 1Year time frame? When I use it on GOOG, I see : which is identical to the TOS value: I believe (this is what someone at TOS responded once) this IV is based on TOS's derivation of the 30 Day IV which is based on the same formula in the CBOE white paper: on VIX. (except it is on the specified underlying instead of SPX options). So, to calculate it yourself, you will need the prices of all ATM and OTM strikes of the two expiry's on either side of 30 DTE. You will need to implement the algorithms detailed in the CBOE's VIX White Paper. This may be more effort than you desire, since accessing all the option prices necessary, is non-trivial, unless you subscribe to a service for the data. (You can get it from TOS via RTD, but unless this is critical to you, you may tire prior to completion. ) Note: that white paper is detailed and specific, so is very good if you really want to implement the algorithm. I am curious why the requirement to compute this value for yourself, instead of using the available data. Perhaps you are implementing your own BackTest with the IV percentile as a trade qualifier? If so, can you possibly merely use TOS for that, or is your backtest too complex for ThinkScript? OOPS! I missed the last line of your response. Coding in C# for backtesting. Then you may wish to implement the algorithm for the 30Day IV specified in the CBOE white paper: "https://www.cboe.com/micro/vix/vixwhite.pdf" BTW: FYI: This IV calculation does not use BSM. All data derived from mid prices of the specified options (and DTE). To test the "Theory" that TOS's "ImpVolatility" relates to the VIX white paper derivation, you may shortcut the difficult task to merely average the two expiry's as specified in that white paper, where each individual expiry should MATCH the values displayed in TOS to the right of the representative expiration: See below for example:
Have you looked at Livevol Pro? You can buy the data and it will be accurate. Why do calculations for past events? I can see if you you want to do the calculations live, but that is not as easy as you think. You need accurate interest and dividend flows. Hard to borrow stocks and stocks with irregular dividends are difficult to update.
>> Are you sure you have your chart set to Daily bars, and are using a minimum of 1Year time frame. Yes, we use the same chart there with daily bars. It is the "ImplVolatility" indicator that I have added to the chart. It would be the "18.76%" that is shown in your image there. Where to find the "Current IV Percentile" in TOS? Thank you for the .pdf to vixwhite. However, it seems a little to complicated for me to exactly follow how to implement it. I keep wondering if there is something more "easy-to-follow example". I think the implied volatility perheps doesn't need to be clinically exact for my backtest. I look at this video where someone finds the implied volatility to be 30.82% but there is no formulas shown behind the cells. He iterates the volatility until finding the correct option price which I could do in C# if I know some formulas to go after? I think for example ATM and Midpoint CALL/PUT prices for the option would be a good estimate to use. youtube.com/watch?v=XHTSYxgMCPY Yes, my backtest will be to complicated for the ThinkScript. I really need to get some formula down to get the implied volatility so it is possible to use for a backtest.
I think I have come up with a C# solution to calculate the implied volatility. I post the C# code below. However when I input the current values for GOOG where impliedvolatility is known: 0.187 by looking in TOS and using the strikeprice 785. This function returns a callprice(BlackScholesCallPrice) of 17.13 which is not correct. The TOS shows a callprice at: 13.5. I wonder what can skew it so much? Another test with below code is if I change the impliedvolatility to: 0.147, then this returns about the same current call price. So 0.147 vs 0.187(TOS) That might be a correct estimate as calculation is not exactly the same as TOS? private void Form1_Load(object sender, EventArgs e) { double stockprice = 786.90; double exerciseprice = 785; double impliedvolatility = 0.187; //Assumed implied volatility double riskfreerate = 0.02; double dividendyield = 0.0020; double daystoexpiration = 26; double BlackScholesCallPrice = 0; //We know that the price is: 13.5 (Adjust impliedvolatility until the price is correct) double PutPricePutCallParity = 0; double PutOptionDelta = 0; calculateimpliedvolatility(stockprice, exerciseprice, impliedvolatility, riskfreerate, dividendyield, daystoexpiration, out BlackScholesCallPrice, out PutPricePutCallParity, out PutOptionDelta); MessageBox.Show(BlackScholesCallPrice.ToString() + "," + PutPricePutCallParity.ToString() + "," + PutOptionDelta); } void calculateimpliedvolatility(double stockprice, double exerciseprice, double impliedvolatility, double riskfreerate, double dividendyield, double daystoexpiration, out double BlackScholesCallPrice, out double PutPricePutCallParity, out double PutOptionDelta) { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); //(LN($B$1/$B$5) + ($B$3 - $B$4 + 0,5*($B$2^2))*($B$6/365))/($B$2*($B$6/365)^0,5) double d1 = (Math.Log(stockprice / exerciseprice) + (riskfreerate - dividendyield + 0.5 * (Math.Pow(impliedvolatility, 2))) * (daystoexpiration / 365)) / ((impliedvolatility * Math.Pow((daystoexpiration / 365), 0.5))); //E1 - $B$2*($B$6/365)^0,5 double d2 = d1 - impliedvolatility * Math.Pow((daystoexpiration / 365), 0.5); //NORMSDIST(E1) double Nd1 = NORMSDIST(d1); //NORMSDIST(E2) double Nd2 = NORMSDIST(d2); //$B$1*EXP(-$B$4*($B$6/365))*E5 - EXP(-$B$3*($B$6/365))*$B$5*E6 BlackScholesCallPrice = stockprice * Math.Exp(-dividendyield * (daystoexpiration / 365)) * Nd1 - Math.Exp(-riskfreerate * (daystoexpiration / 365)) * exerciseprice * Nd2; //$E$9 - EXP(-$B$4*($B$6/365))*$B$1 + $B$5 * EXP(-$B$3*($B$6/365)) PutPricePutCallParity = BlackScholesCallPrice - Math.Exp(-dividendyield * (daystoexpiration / 365)) * stockprice + exerciseprice * Math.Exp(-riskfreerate * (daystoexpiration / 365)); //-NORMSDIST(-E1) PutOptionDelta = -NORMSDIST(-d1); } private static double erf(double x) { //A&S formula 7.1.26 double a1 = 0.254829592; double a2 = -0.284496736; double a3 = 1.421413741; double a4 = -1.453152027; double a5 = 1.061405429; double p = 0.3275911; x = Math.Abs(x); double t = 1 / (1 + p * x); //Direct calculation using formula 7.1.26 is absolutely correct //But calculation of nth order polynomial takes O(n^2) operations //return 1 - (a1 * t + a2 * t * t + a3 * t * t * t + a4 * t * t * t * t + a5 * t * t * t * t * t) * Math.Exp(-1 * x * x); //Horner's method, takes O(n) operations for nth order polynomial return 1 - ((((((a5 * t + a4) * t) + a3) * t + a2) * t) + a1) * t * Math.Exp(-1 * x * x); } public static double NORMSDIST(double z) { double sign = 1; if (z < 0) sign = -1; return 0.5 * (1.0 + sign * erf(Math.Abs(z) / Math.Sqrt(2))); }
assuming your code is correct, check your daycount convention. It could be the source of such a wide discrepancy.
Hm.... Not intending to be negative, but seems you may not have a good sense for what you are trying to do, and/or the amount of effort required to accomplish it. Use of an IV Rank or Percentile for qualifying a trade, typically relates to a fairly standardized metric. To convince yourself of the basis of the TOS "impVolatility();" derivation, pull up SPX, and throw on that study, and compare it to VIX. It is the same (on SPX). {This metric is derived from a set of ATM and OTM options near 30 DTE (cboe white paper), not just a single option contract.} Where to find the "Current IV Percentile" in TOS? ->"Trade"->"Today's Option Statistics" near the bottom of the window.-- See screen shot provided earlier. " I think the implied volatility perhaps doesn't need to be clinically exact for my backtest." <-- perhaps you need to determine precisely what you require! How much error do you need to tolerate, then find a match if there is one. Your reference to that simple youtube reference to IV is a simplistic reference to a specific option contract, and it's individual implied volatility, which does NOT appear suitable to your backtest criteria for IV percentile. I think the code you posted may implement this, but I have not checked your code. I'm more concerned that this may NOT meet your goal (whether coded correctly or not). Regarding the code you posted: It is not clear why you expect the 26 DTE 785 CALL Strike Individual IV to match that of the standardized 30DTE IV! You are not comparing apples to apples as you reference IV. Each Option has its own individual Implied Volatility, not to be confused with the pseudo-standardized impVolatility() function. If you have not done so already, please Add "Impl Vol" to your Trade->Option Chain layout to observe the individual implied volatility values. Remember, however to set the "Volatility calculation mode:" to your required setting, since the option chain layout is based on that setting. Note that these are unique for each option and expiration, and are not identical to the pseudo standardized Implied Volatility value commonly used for evaluating IV Percentile.
I am not a C programmer but it looks to me like you are trying to program using the BS equation? If so, there is no closed form solution for IV as a function of call premium, time, interest rate and dividend rate. You need an iterative program and make sure your units are correct, especially the unit/definition of IV and time. Martinghoul gave you the correct answer, if you want a quick and dirty answer without doing an iterative programming, there are approximation equations you can use (see his post).
If you merely want the IV of a specific option, I use the following in Perl, which seems to be adequate for my needs: # Solve for IV using B&S sub ResolveIV { my ($call_flag, $S, $X, $A, $T, $r, $iv, $q) = @_; # S: Stock Price # X: Strike price # A: Actual Option Price # T: Time to expiration in years # r: Risk-free interest rate # v: Volitility (ignore the value received, we will find proper value) # q: dividend yield # my $high=5; my $low=0; my $precision=0.0001; # Amount of precision (error) required#. my $IVTrial; my $TheoPrice; while (($high - $low) > $precision) { $IVTrial=($high + $low)/2; if ($IVTrial < 0.00001) { return ($high+$low)/2; # Do not call function with zero IV value. } $TheoPrice = &BlackScholes($call_flag, $S, $X, $T, $r, $IVTrial, $q); if ($TheoPrice > $A) { $high=($high + $low)/2; } else { $low=($high + $low)/2; } } return ($high+$low)/2; } # end sub ResolveIV
That is interesting! When you say "near 30 DTE", I think of that 1 option contract only has 30 DTE one day of its life. So I wonder there, do you mean that if combining the current month and next month contract. For example if it is 15 days left in current months and 45 days left in next month contract: (15 + 45) / 2 = 30 days. Then take the average implied volatility for those to get a better picture for each days that pass? My idéa is to take the nearest ITM and OTM option and calculate the average implied volatility between those to come as close as possible to ATM implied volatility to get an estimate overall where the implied volatility is. I wonder if that is a good way of thinking of this? Perheps it is even better as you say to take a SET of ITM and OTM options and create an average of those to get an even more "precise" picture?