0
$\begingroup$

I am trying to calculate implied volatility using javascript , I have following code

function pdf_stdgauss(x) {
    return Math.exp(-x * x / 2.0) / Math.sqrt(2.0 * Math.PI);
}

function cdf_stdgauss(x) {
    var t = 1.0 / (1.0 + 0.2316419 * (x < 0 ? -x : x));
    var b1 = 0.319381530;
    var b2 = -0.356563782;
    var b3 = 1.781477937;
    var b4 = -1.821255978;
    var b5 = 1.330274429;
    var a = t * (b1 + t * (b2 + t * (b3 + t * (b4 + t * b5))));
    return (x < 0 ? a * pdf_stdgauss(x) : (1.0 - a * pdf_stdgauss(x)));
}



function ecp(s, x, rfi, dvd, sigma, t) {
    var sst = sigma * Math.sqrt(t);
    var d1 = (Math.log(s / x) + (rfi - dvd + sigma * sigma / 2.0) * t) / sst;
    var d2 = d1 - sst;
    var Nd1 = cdf_stdgauss(d1);
    var Nd2 = cdf_stdgauss(d2);
    var pd1 = pdf_stdgauss(d1);
    var pd2 = pdf_stdgauss(d2);
    var erfi = Math.exp(-rfi * t);
    var edvd = Math.exp(-dvd * t);
    var c = s * edvd * Nd1 - x * erfi * Nd2;
    var p = c + x * erfi - s * edvd;
    var cdelta = edvd * Nd1;
    var pdelta = cdelta - edvd;
    var gamma = edvd * pd1 / (s * sst);
    var ctheta = dvd * s * edvd * Nd1 - rfi * x * erfi * Nd2 - 0.5 * sigma * sigma * s * s * gamma;
    var ptheta = ctheta + rfi * x * erfi - dvd * s * edvd;
    var vega = s * edvd * pd1 * Math.sqrt(t);
    var crho = x * erfi * Nd2 * t;
    var prho = x * erfi * (Nd2 - 1.0) * t;
    var cdvd = -s * edvd * Nd1 * t;
    var pdvd = s * edvd * (1.0 - Nd1) * t;
    return [c, cdelta, gamma, ctheta, vega, crho, cdvd, p, pdelta, gamma, ptheta, vega, prho, pdvd];
}

function implied_volatility(i, p, s, x, rfi, dvd, t) {
    var cv = function(sigma) {
        var sst = sigma * Math.sqrt(t);
        var d1 = (Math.log(s / x) + (rfi - dvd + sigma * sigma / 2.0) * t) / sst;
        var d2 = d1 - sst;
        var Nd1 = cdf_stdgauss(d1);
        var Nd2 = cdf_stdgauss(d2);



        if (i == 7) {
            Nd1 = Nd1 - 1.0;
            Nd2 = Nd2 - 1.0;
        }
        return s * Math.exp(-dvd * t) * Nd1 - x * Math.exp(-rfi * t) * Nd2 - p;
    };
    var cvp = function(sigma) {
        var sst = sigma * Math.sqrt(t);
        var d1 = (Math.log(s / x) + (rfi - dvd + sigma * sigma / 2.0) * t) / sst;
        return s * Math.exp(-dvd * t) * pdf_stdgauss(d1) * Math.sqrt(t);
    };
    return newt_root(0.2, cv, cvp, 0.000001);
}

function newt_root(x, f, fp, tol) {
    var x0;
    for (x0 = x; Math.abs(f(x0)) > tol; x0 -= f(x0) / fp(x0));
    return x0;
}

var dayselect = 23;
var monthselect = 1;
var yearselect = 2020;


function calculate_time2expire() {
    var today = new Date();
    var eday = parseInt(dayselect);
    var emonth = parseInt(monthselect);
    var edate = new Date(yearselect, emonth - 1, eday);
    var days = Math.ceil((edate.getTime() - today.getTime()) / 86400000);
    return days / 365.0;
}

It is working most of the times, but sometimes I get Infinity or - Infinity as output.

When I run

var ceiv = 100.0* implied_volatility(0, 624.65, 12352.35, 11750, 0.069, 0, 0.03287671232876712)

It is returning infinity

But others strike prices are giving correct IV , For example If i run

var ceiv = 100.0* implied_volatility(0, 1521.75,31590, 30100, 0.069, 0, 0.0136986301369863)

It gives 19.08

Here is parameter

implied_volatility(callput, optionprice,spotprice, strikeprice, riskfreeinterest/100, dividend, daytoexpireinyear)
$\endgroup$
  • 1
    $\begingroup$ It looks like the first one you have the option price below the intrinsic value of the option. $\endgroup$ – will Jan 18 at 10:04
  • $\begingroup$ You should check your inputs for problematic situations before going ahead with the calculation. For IV to exist you need S>0, T>0, C>MIN(S-X,0). $\endgroup$ – noob2 Jan 18 at 11:40
  • $\begingroup$ Hi @noob2, please check my edited question , in this case option price is more than intrinsic value. $\endgroup$ – Gracie williams Jan 18 at 11:49
  • $\begingroup$ 624.65 > (12352.35-11750) $\endgroup$ – Gracie williams Jan 18 at 11:58
0
$\begingroup$

Assume we are in the Black Scholes for call option settings, and let’s ignore the dividend. For the implied vol, we can treat all other variables as constant, and focus on the price of the call option as a function of implied vol.

$C\left( \sigma\right)=SN\left(d_1\right)-Xe^{-rT}N\left(d_2\right)$

Where:

$d_1=\frac{ln \frac{F}{X}}{\sigma \sqrt{T}}+\frac{1}{2}\sigma\sqrt{T} $

$d_2=\frac{ln \frac{F}{X}}{\sigma \sqrt{T}}-\frac{1}{2}\sigma\sqrt{T}$

The domain, range of implied volatility values, is $(0, \infty)$ - in practice the domain is much narrower but that’s a different point. What is important is the domain, and the fact that vol appears in the formula through the d’s.

It is easy to check that as implied volatility goes to zero, both d’s go to plus/minus infinity depending on whether F is greater than X:

$\lim_{\sigma \to 0} d_1=\mathrm{sign} \left(F-X\right) \infty$

$\lim_{\sigma \to 0} d_2=\mathrm{sign} \left(F-X\right) \infty$

And then using the fact that $N\left(\infty\right)=1$ and $N\left(-\infty\right)=0$, we conclude that if F>X, the lowest point of the range of the call option price is:

$\lim_{\sigma \to 0}C\left( \sigma\right)=SN\left(\infty\right)-Xe^{-rT}N\left(\infty\right)$

$=S-Xe^{-rT}$

And for F less than X:

$\lim_{\sigma \to 0}C\left( \sigma\right)=SN\left(-\infty\right)-Xe^{-rT}N\left(-\infty\right)=0$

The other end is easy- as implied vol goes to infinity:

$\lim_{\sigma \to \infty} d_1=\infty$

$\lim_{\sigma \to \infty} d_2=-\infty$

So the call option price goes to S, the current value of the underlying.

You can restrict the range of option prices as per above to alert the users to potential issues in the inputs.

| improve this answer | |
$\endgroup$
-1
$\begingroup$

The option price should be superior than the intrinsic value of the option. In your case:
31590-29800=1790>1768.05.
if you want to test the IV given by your algorithm you can use my website [https://www.valometrics.com]. it is a web platform coded using javascript that contains an IV calculator. please let me know for more information.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Hi , please check my edited question , in this case option price is more than intrinsic value. $\endgroup$ – Gracie williams Jan 18 at 11:49
  • $\begingroup$ Actually the premium also should be superior than Max(forward-strike,0.0). in your case, the forward is equal to 12352.35*exp(0.069*0.032876712)=12380.40303. $\endgroup$ – Valometrics.com Jan 18 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.