OPEN-SOURCE SCRIPT

Generalized Black-Scholes-Merton Option Pricing Formula [Loxx]

Generalized Black-Scholes-Merton Option Pricing Formula [Loxx] is an adaptation of the Black-Scholes-Merton Option Pricing Model including Numerical Greeks aka "Option Sensitivities" and implied volatility calculations. The following information is an excerpt from Espen Gaarder Haug's book "Option Pricing Formulas".

Black-Scholes-Merton Option Pricing
The BSM formula and its binomial counterpart may easily be the most used "probability model/tool" in everyday use — even if we con- sider all other scientific disciplines. Literally tens of thousands of people, including traders, market makers, and salespeople, use option formulas several times a day. Hardly any other area has seen such dramatic growth as the options and derivatives businesses. In this chapter we look at the various versions of the basic option formula. In 1997 Myron Scholes and Robert Merton were awarded the Nobel Prize (The Bank of Sweden Prize in Economic Sciences in Memory of Alfred Nobel). Unfortunately, Fischer Black died of cancer in 1995 before he also would have received the prize.

It is worth mentioning that it was not the option formula itself that Myron Scholes and Robert Merton were awarded the Nobel Prize for, the formula was actually already invented, but rather for the way they derived it — the replicating portfolio argument, continuous- time dynamic delta hedging, as well as making the formula consistent with the capital asset pricing model (CAPM). The continuous dynamic replication argument is unfortunately far from robust. The popularity among traders for using option formulas heavily relies on hedging options with options and on the top of this dynamic delta hedging, see Higgins (1902), Nelson (1904), Mello and Neuhaus (1998), Derman and Taleb (2005), as well as Haug (2006) for more details on this topic. In any case, this book is about option formulas and not so much about how to derive them.

Provided here are the various versions of the Black-Scholes-Merton formula presented in the literature. All formulas in this section are originally derived based on the underlying asset S follows a geometric Brownian motion

dS = mu * S * dt + v * S * dz

where t is the expected instantaneous rate of return on the underlying asset, a is the instantaneous volatility of the rate of return, and dz is a Wiener process.

The formula derived by Black and Scholes (1973) can be used to value a European option on a stock that does not pay dividends before the option's expiration date. Letting c and p denote the price of European call and put options, respectively, the formula states that

c = S * N(d1) - X * e^(-r * T) * N(d2)

p = X * e^(-r * T) * N(d2) - S * N(d1)

where

d1 = (log(S / X) + (r + v^2 / 2) * T) / (v * T^0.5)

d2 = (log(S / X) + (r - v^2 / 2) * T) / (v * T^0.5) = d1 - v * T^0.5

Inputs
S = Stock price.
X = Strike price of option.
T = Time to expiration in years.
r = Risk-free rate
b = Cost of carry
v = Volatility of the underlying asset price
cnd (x) = The cumulative normal distribution function
nd(x) = The standard normal density function
convertingToCCRate(r, cmp ) = Rate compounder
gImpliedVolatilityNR(string CallPutFlag, float S, float x, float T, float r, float b, float cm, float epsilon) = Implied volatility via Newton Raphson
gBlackScholesImpVolBisection(string CallPutFlag, float S, float x, float T, float r, float b, float cm) = implied volatility via bisection

Implied Volatility: The Bisection Method
The Newton-Raphson method requires knowledge of the partial derivative of the option pricing formula with respect to volatility (vega) when searching for the implied volatility. For some options (exotic and American options in particular), vega is not known analytically. The bisection method is an even simpler method to estimate implied volatility when vega is unknown. The bisection method requires two initial volatility estimates (seed values):

1. A "low" estimate of the implied volatility, al, corresponding to an option value, CL
2. A "high" volatility estimate, aH, corresponding to an option value, CH

The option market price, Cm, lies between CL and cH. The bisection estimate is given as the linear interpolation between the two estimates:

v(i + 1) = v(L) + (c(m) - c(L)) * (v(H) - v(L)) / (c(H) - c(L))

Replace v(L) with v(i + 1) if c(v(i + 1)) < c(m), or else replace v(H) with v(i + 1) if c(v(i + 1)) > c(m) until |c(m) - c(v(i + 1))| <= E, at which point v(i + 1) is the implied volatility and E is the desired degree of accuracy.

Implied Volatility: Newton-Raphson Method
The Newton-Raphson method is an efficient way to find the implied volatility of an option contract. It is nothing more than a simple iteration technique for solving one-dimensional nonlinear equations (any introductory textbook in calculus will offer an intuitive explanation). The method seldom uses more than two to three iterations before it converges to the implied volatility. Let

v(i + 1) = v(i) + (c(v(i)) - c(m)) / (dc / dv(i))

until |c(m) - c(v(i + 1))| <= E at which point v(i + 1) is the implied volatility, E is the desired degree of accuracy, c(m) is the market price of the option, and dc/dv(i) is the vega of the option evaluaated at v(i) (the sensitivity of the option value for a small change in volatility).

Numerical Greeks or Greeks by Finite Difference
Analytical Greeks are the standard approach to estimating Delta, Gamma etc... That is what we typically use when we can derive from closed form solutions. Normally, these are well-defined and available in text books. Previously, we relied on closed form solutions for the call or put formulae differentiated with respect to the Black Scholes parameters. When Greeks formulae are difficult to develop or tease out, we can alternatively employ numerical Greeks - sometimes referred to finite difference approximations. A key advantage of numerical Greeks relates to their estimation independent of deriving mathematical Greeks. This could be important when we examine American options where there may not technically exist an exact closed form solution that is straightforward to work with. (via VinegarHill FinanceLabs)

Things to know
  • Only works on the daily timeframe and for the current source price.
  • You can adjust the text size to fit the screen
Release Notes
Updated H. Vol display format to percentage.
Release Notes
Added forward price.
Release Notes
Corrected display error.
blackscholesmertonblackscholesoptionpricingblacksholesmertongreeksHistorical VolatilityimpliedvolatilitynumericalgreeksoptionpricingoptionsVolatility

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


Public Telegram Group, t.me/algxtrading_public

VIP Membership Info: patreon.com/algxtrading/membership
Also on:

Disclaimer