OPEN-SOURCE SCRIPT

Generalized Black-Scholes-Merton w/ Analytical Greeks [Loxx]

Generalized Black-Scholes-Merton w/ Analytical Greeks [Loxx] is an adaptation of the Black-Scholes-Merton Option Pricing Model including Analytical Greeks and implied volatility calculations. The following information is an excerpt from Espen Gaarder Haug's book "Option Pricing Formulas". The options sensitivities (Greeks) are the partial derivatives of the Black-Scholes-Merton (BSM) formula. Analytical Greeks for our purposes here are broken down into various categories:

  • Delta Greeks: Delta, DDeltaDvol, Elasticity
  • Gamma Greeks: Gamma, GammaP, DGammaDSpot/speed, DGammaDvol/Zomma
  • Vega Greeks: Vega, DVegaDvol/Vomma, VegaP
  • Theta Greeks: Theta
  • Rate/Carry Greeks: Rho, Rho futures option, Carry Rho, Phi/Rho2
  • Probability Greeks: StrikeDelta, Risk Neutral Density

(See the code for more details)

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 ).

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
版本注释
Corrected Rho values
analyticalgreeksblackscholesblackscholesmertonblackscholesoptionpricingBSMgreeksHistorical VolatilityoptionpricingoptionsVolatility

开源脚本

本着真正的TradingView精神,此脚本的作者已将其开源,以便交易者可以理解和验证它。向作者致敬!您可以免费使用它,但在出版物中重复使用此代码受网站规则约束。 您可以收藏它以在图表上使用。

想在图表上使用此脚本?


Public Telegram Group, t.me/algxtrading_public

VIP Membership Info: patreon.com/algxtrading/membership
更多:

免责声明