diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cbfbbe..4e86e66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## 1.5.1 (2025-12-24) + +- Added the calculation of the probability of an option ever getting ITM before expiration (see function `get_probability_of_touch` in black_scholes.py and the corresponding fields in models.py). +- Updated documentation. + ## 1.5.0 (2025-12-14) - Changed `get_pop` to compute the expected profit and expected loss of a strategy with the Black-Scholes model by calling a new function, `_compute_expected_returns_bs`. diff --git a/README.md b/README.md index 498de44..129a1ba 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,37 @@ - + # OptionLab -This package is a lightweight library written entirely in Python, designed to provide -quick evaluation of option strategy ideas. +This package is a lightweight library designed to provide quick evaluation of options trading +strategies. It produces various outputs: -The code produces various outputs, including the profit/loss profile of the strategy on -a user-defined target date, the range of stock prices for which the strategy is -profitable (i.e., generating a return greater than \$0.01), the Greeks associated with -each leg of the strategy using the Black-Sholes model, the resulting debit or credit on the -trading account, the maximum and minimum returns within a specified lower and higher price -range of the underlying asset, and an estimate of the strategy's probability of profit. +- the profit/loss profile of the strategy on a user-defined target date + +- the range of stock prices for which the strategy is profitable (i.e., generating a return of +at least \$0.01) + +- the Greeks (delta, theta, rho, vega and gamma) associated with each leg of the strategy + +- the resulting debit or credit on the trading account + +- the maximum and minimum returns within a specified lower and higher price +range of the underlying asset + +- The expected profit when the strategy is profitable and the expected loss if it proves unprofitable + +- the strategy's probability of profit. + +## Contact If you have any questions, corrections, comments or suggestions, just [drop a message](mailto:roberto.veiga@ufabc.edu.br). You can also reach me on [Linkedin](https://www.linkedin.com/in/roberto-gomes-phd-8a718317b/) or -follow me on [X](https://x.com/rgaveiga). When I have some free time, which is rare, I publish articles -on [Medium](https://medium.com/@rgaveiga). +follow me on [X](https://x.com/rgaveiga). -If you want to support this and other open source projects that I maintain, become a -[sponsor on Github](https://github.com/sponsors/rgaveiga). +> [!NOTE] +> If you want to support this and other open source projects that I maintain, become a +>[sponsor on Github](https://github.com/sponsors/rgaveiga). ## Installation @@ -48,8 +59,9 @@ This is free software and is provided as is. The author makes no guarantee that results are accurate and is not responsible for any losses caused by the use of the code. -Options are very risky derivatives and, like any other type of financial vehicle, -trading options requires due diligence. This code is provided for educational and -research purposes only. - Bugs can be reported as issues. + +> [!CAUTION] +> Options are very risky derivatives and, like any other type of financial vehicle, +> trading options requires due diligence. This code is provided for educational and +> research purposes only. diff --git a/docs/optionlab.html b/docs/optionlab.html index c8d756e..de5a36a 100644 --- a/docs/optionlab.html +++ b/docs/optionlab.html @@ -369,7 +369,7 @@
78def get_option_price( - 79 option_type: OptionType, - 80 s0: FloatOrNdarray, - 81 x: FloatOrNdarray, - 82 r: float, - 83 years_to_maturity: float, - 84 d1: FloatOrNdarray, - 85 d2: FloatOrNdarray, - 86 y: float = 0.0, - 87) -> FloatOrNdarray: - 88 """ - 89 Returns the price of an option. - 90 - 91 ### Parameters - 92 - 93 `option_type`: either *'call'* or *'put'*. - 94 - 95 `s0`: spot price(s) of the underlying asset. - 96 - 97 `x`: strike price(s). +@@ -774,38 +858,38 @@86def get_option_price( + 87 option_type: OptionType, + 88 s0: FloatOrNdarray, + 89 x: FloatOrNdarray, + 90 r: float, + 91 years_to_maturity: float, + 92 d1: FloatOrNdarray, + 93 d2: FloatOrNdarray, + 94 y: float = 0.0, + 95) -> FloatOrNdarray: + 96 """ + 97 Returns the price of an option. 98 - 99 `r`: annualize risk-free interest rate. + 99 ### Parameters 100 -101 `years_to_maturity`: time remaining to maturity, in years. +101 `option_type`: either *'call'* or *'put'*. 102 -103 `d1`: `d1` in Black-Scholes formula. +103 `s0`: spot price(s) of the underlying asset. 104 -105 `d2`: `d2` in Black-Scholes formula. +105 `x`: strike price(s). 106 -107 `y`: annualized dividend yield. +107 `r`: annualize risk-free interest rate. 108 -109 ### Returns +109 `years_to_maturity`: time remaining to maturity, in years. 110 -111 Option price(s). -112 """ -113 -114 s = s0 * exp(-y * years_to_maturity) -115 -116 if option_type == "call": -117 return round( -118 s * stats.norm.cdf(d1) -119 - x * exp(-r * years_to_maturity) * stats.norm.cdf(d2), -120 2, -121 ) -122 elif option_type == "put": -123 return round( -124 x * exp(-r * years_to_maturity) * stats.norm.cdf(-d2) -125 - s * stats.norm.cdf(-d1), -126 2, -127 ) -128 else: -129 raise ValueError("Option type must be either 'call' or 'put'!") +111 `d1`: `d1` in Black-Scholes formula. +112 +113 `d2`: `d2` in Black-Scholes formula. +114 +115 `y`: annualized dividend yield. +116 +117 ### Returns +118 +119 Option price(s). +120 """ +121 +122 s = s0 * exp(-y * years_to_maturity) +123 +124 if option_type == "call": +125 return round( +126 s * stats.norm.cdf(d1) +127 - x * exp(-r * years_to_maturity) * stats.norm.cdf(d2), +128 2, +129 ) +130 elif option_type == "put": +131 return round( +132 x * exp(-r * years_to_maturity) * stats.norm.cdf(-d2) +133 - s * stats.norm.cdf(-d1), +134 2, +135 ) +136 else: +137 raise ValueError("Option type must be either 'call' or 'put'!")Returns
132def get_delta( -133 option_type: OptionType, -134 d1: FloatOrNdarray, -135 years_to_maturity: float, -136 y: float = 0.0, -137) -> FloatOrNdarray: -138 """ -139 Returns the option's Greek Delta. -140 -141 ### Parameters -142 -143 `option_type`: either *'call'* or *'put'*. -144 -145 `d1`: `d1` in Black-Scholes formula. -146 -147 `years_to_maturity`: time remaining to maturity, in years. +@@ -839,38 +923,38 @@140def get_delta( +141 option_type: OptionType, +142 d1: FloatOrNdarray, +143 years_to_maturity: float, +144 y: float = 0.0, +145) -> FloatOrNdarray: +146 """ +147 Returns the option's Greek Delta. 148 -149 `y`: annualized dividend yield. +149 ### Parameters 150 -151 ### Returns +151 `option_type`: either *'call'* or *'put'*. 152 -153 Option's Greek Delta. -154 """ -155 -156 yfac = exp(-y * years_to_maturity) -157 -158 if option_type == "call": -159 return yfac * stats.norm.cdf(d1) -160 elif option_type == "put": -161 return yfac * (stats.norm.cdf(d1) - 1.0) -162 else: -163 raise ValueError("Option must be either 'call' or 'put'!") +153 `d1`: `d1` in Black-Scholes formula. +154 +155 `years_to_maturity`: time remaining to maturity, in years. +156 +157 `y`: annualized dividend yield. +158 +159 ### Returns +160 +161 Option's Greek Delta. +162 """ +163 +164 yfac = exp(-y * years_to_maturity) +165 +166 if option_type == "call": +167 return yfac * stats.norm.cdf(d1) +168 elif option_type == "put": +169 return yfac * (stats.norm.cdf(d1) - 1.0) +170 else: +171 raise ValueError("Option must be either 'call' or 'put'!")Returns
166def get_gamma( -167 s0: float, -168 vol: float, -169 years_to_maturity: float, -170 d1: FloatOrNdarray, -171 y: float = 0.0, -172) -> FloatOrNdarray: -173 """ -174 Returns the option's Greek Gamma. -175 -176 ### Parameters -177 -178 `s0`: spot price of the underlying asset. -179 -180 `vol`: annualized volatitily. -181 -182 `years_to_maturity`: time remaining to maturity, in years. +@@ -906,63 +990,63 @@174def get_gamma( +175 s0: float, +176 vol: float, +177 years_to_maturity: float, +178 d1: FloatOrNdarray, +179 y: float = 0.0, +180) -> FloatOrNdarray: +181 """ +182 Returns the option's Greek Gamma. 183 -184 `d1`: `d1` in Black-Scholes formula. +184 ### Parameters 185 -186 `y`: annualized divident yield. +186 `s0`: spot price of the underlying asset. 187 -188 ### Returns +188 `vol`: annualized volatitily. 189 -190 Option's Greek Gamma. -191 """ -192 -193 yfac = exp(-y * years_to_maturity) -194 -195 cdf_d1_prime = exp(-0.5 * d1 * d1) / sqrt(2.0 * pi) -196 -197 return yfac * cdf_d1_prime / (s0 * vol * sqrt(years_to_maturity)) +190 `years_to_maturity`: time remaining to maturity, in years. +191 +192 `d1`: `d1` in Black-Scholes formula. +193 +194 `y`: annualized divident yield. +195 +196 ### Returns +197 +198 Option's Greek Gamma. +199 """ +200 +201 yfac = exp(-y * years_to_maturity) +202 +203 cdf_d1_prime = exp(-0.5 * d1 * d1) / sqrt(2.0 * pi) +204 +205 return yfac * cdf_d1_prime / (s0 * vol * sqrt(years_to_maturity))Returns
200def get_theta( -201 option_type: OptionType, -202 s0: float, -203 x: FloatOrNdarray, -204 r: float, -205 vol: float, -206 years_to_maturity: float, -207 d1: FloatOrNdarray, -208 d2: FloatOrNdarray, -209 y: float = 0.0, -210) -> FloatOrNdarray: -211 """ -212 Returns the option's Greek Theta. -213 -214 ### Parameters -215 -216 `option_type`: either *'call'* or *'put'*. -217 -218 `s0`: spot price of the underlying asset. -219 -220 `x`: strike price(s). +@@ -1006,35 +1090,35 @@208def get_theta( +209 option_type: OptionType, +210 s0: float, +211 x: FloatOrNdarray, +212 r: float, +213 vol: float, +214 years_to_maturity: float, +215 d1: FloatOrNdarray, +216 d2: FloatOrNdarray, +217 y: float = 0.0, +218) -> FloatOrNdarray: +219 """ +220 Returns the option's Greek Theta. 221 -222 `r`: annualized risk-free interest rate. +222 ### Parameters 223 -224 `vol`: annualized volatility. +224 `option_type`: either *'call'* or *'put'*. 225 -226 `years_to_maturity`: time remaining to maturity, in years. +226 `s0`: spot price of the underlying asset. 227 -228 `d1`: `d1` in Black-Scholes formula. +228 `x`: strike price(s). 229 -230 `d2`: `d2` in Black-Scholes formula. +230 `r`: annualized risk-free interest rate. 231 -232 `y`: annualized dividend yield. +232 `vol`: annualized volatility. 233 -234 ### Returns +234 `years_to_maturity`: time remaining to maturity, in years. 235 -236 Option's Greek Theta. -237 """ -238 -239 s = s0 * exp(-y * years_to_maturity) -240 -241 cdf_d1_prime = exp(-0.5 * d1 * d1) / sqrt(2.0 * pi) -242 -243 if option_type == "call": -244 return -( -245 s * vol * cdf_d1_prime / (2.0 * sqrt(years_to_maturity)) -246 + r * x * exp(-r * years_to_maturity) * stats.norm.cdf(d2) -247 - y * s * stats.norm.cdf(d1) -248 ) -249 elif option_type == "put": -250 return -( -251 s * vol * cdf_d1_prime / (2.0 * sqrt(years_to_maturity)) -252 - r * x * exp(-r * years_to_maturity) * stats.norm.cdf(-d2) -253 + y * s * stats.norm.cdf(-d1) -254 ) -255 else: -256 raise ValueError("Option type must be either 'call' or 'put'!") +236 `d1`: `d1` in Black-Scholes formula. +237 +238 `d2`: `d2` in Black-Scholes formula. +239 +240 `y`: annualized dividend yield. +241 +242 ### Returns +243 +244 Option's Greek Theta. +245 """ +246 +247 s = s0 * exp(-y * years_to_maturity) +248 +249 cdf_d1_prime = exp(-0.5 * d1 * d1) / sqrt(2.0 * pi) +250 +251 if option_type == "call": +252 return -( +253 s * vol * cdf_d1_prime / (2.0 * sqrt(years_to_maturity)) +254 + r * x * exp(-r * years_to_maturity) * stats.norm.cdf(d2) +255 - y * s * stats.norm.cdf(d1) +256 ) +257 elif option_type == "put": +258 return -( +259 s * vol * cdf_d1_prime / (2.0 * sqrt(years_to_maturity)) +260 - r * x * exp(-r * years_to_maturity) * stats.norm.cdf(-d2) +261 + y * s * stats.norm.cdf(-d1) +262 ) +263 else: +264 raise ValueError("Option type must be either 'call' or 'put'!")Returns
259def get_vega( -260 s0: float, -261 years_to_maturity: float, -262 d1: FloatOrNdarray, -263 y: float = 0.0, -264) -> FloatOrNdarray: -265 """ -266 Returns the option's Greek Vega. -267 -268 ### Parameters -269 -270 `s0`: spot price of the underlying asset. -271 -272 `years_to_maturity`: time remaining to maturity, in years. -273 -274 `d1`: `d1` in Black-Scholes formula. +@@ -1068,51 +1152,51 @@267def get_vega( +268 s0: float, +269 years_to_maturity: float, +270 d1: FloatOrNdarray, +271 y: float = 0.0, +272) -> FloatOrNdarray: +273 """ +274 Returns the option's Greek Vega. 275 -276 `y`: annualized dividend yield. +276 ### Parameters 277 -278 ### Returns +278 `s0`: spot price of the underlying asset. 279 -280 Option's Greek Vega. -281 """ -282 -283 s = s0 * exp(-y * years_to_maturity) -284 -285 cdf_d1_prime = exp(-0.5 * d1 * d1) / sqrt(2.0 * pi) -286 -287 return s * cdf_d1_prime * sqrt(years_to_maturity) / 100 +280 `years_to_maturity`: time remaining to maturity, in years. +281 +282 `d1`: `d1` in Black-Scholes formula. +283 +284 `y`: annualized dividend yield. +285 +286 ### Returns +287 +288 Option's Greek Vega. +289 """ +290 +291 s = s0 * exp(-y * years_to_maturity) +292 +293 cdf_d1_prime = exp(-0.5 * d1 * d1) / sqrt(2.0 * pi) +294 +295 return s * cdf_d1_prime * sqrt(years_to_maturity) / 100Returns
290def get_rho( -291 option_type: OptionType, -292 x: FloatOrNdarray, -293 r: float, -294 years_to_maturity: float, -295 d2: FloatOrNdarray, -296) -> FloatOrNdarray: -297 """ -298 Returns the option's Greek Rho. -299 -300 ### Parameters -301 -302 `option_type`: either *'call'* or *'put'*. -303 -304 `x`: strike price(s). -305 -306 `r`: annualized risk-free interest rate. +@@ -1148,39 +1232,39 @@298def get_rho( +299 option_type: OptionType, +300 x: FloatOrNdarray, +301 r: float, +302 years_to_maturity: float, +303 d2: FloatOrNdarray, +304) -> FloatOrNdarray: +305 """ +306 Returns the option's Greek Rho. 307 -308 `years_to_maturity`: time remaining to maturity, in years. +308 ### Parameters 309 -310 `d2`: `d2` in Black-Scholes formula. +310 `option_type`: either *'call'* or *'put'*. 311 -312 ### Returns +312 `x`: strike price(s). 313 -314 Option's Greek Rho. -315 """ -316 -317 if option_type == "call": -318 return ( -319 x -320 * years_to_maturity -321 * exp(-r * years_to_maturity) -322 * stats.norm.cdf(d2) -323 / 100 -324 ) -325 elif option_type == "put": +314 `r`: annualized risk-free interest rate. +315 +316 `years_to_maturity`: time remaining to maturity, in years. +317 +318 `d2`: `d2` in Black-Scholes formula. +319 +320 ### Returns +321 +322 Option's Greek Rho. +323 """ +324 +325 if option_type == "call": 326 return ( -327 -x +327 x 328 * years_to_maturity 329 * exp(-r * years_to_maturity) -330 * stats.norm.cdf(-d2) +330 * stats.norm.cdf(d2) 331 / 100 332 ) -333 else: -334 raise ValueError("Option must be either 'call' or 'put'!") +333 elif option_type == "put": +334 return ( +335 -x +336 * years_to_maturity +337 * exp(-r * years_to_maturity) +338 * stats.norm.cdf(-d2) +339 / 100 +340 ) +341 else: +342 raise ValueError("Option must be either 'call' or 'put'!")Returns
337def get_d1( -338 s0: FloatOrNdarray, -339 x: FloatOrNdarray, -340 r: float, -341 vol: FloatOrNdarray, -342 years_to_maturity: float, -343 y: float = 0.0, -344) -> FloatOrNdarray: -345 """ -346 Returns `d1` used in Black-Scholes formula. -347 -348 ### Parameters -349 -350 `s0`: spot price(s) of the underlying asset. -351 -352 `x`: strike price(s). -353 -354 `r`: annualized risk-free interest rate. +@@ -1218,39 +1302,39 @@345def get_d1( +346 s0: FloatOrNdarray, +347 x: FloatOrNdarray, +348 r: float, +349 vol: FloatOrNdarray, +350 years_to_maturity: float, +351 y: float = 0.0, +352) -> FloatOrNdarray: +353 """ +354 Returns `d1` used in Black-Scholes formula. 355 -356 `vol`: annualized volatility(ies). +356 ### Parameters 357 -358 `years_to_maturity`: time remaining to maturity, in years. +358 `s0`: spot price(s) of the underlying asset. 359 -360 `y`: annualized divident yield. +360 `x`: strike price(s). 361 -362 ### Returns +362 `r`: annualized risk-free interest rate. 363 -364 `d1` in Black-Scholes formula. -365 """ -366 -367 return (log(s0 / x) + (r - y + vol * vol / 2.0) * years_to_maturity) / ( -368 vol * sqrt(years_to_maturity) -369 ) +364 `vol`: annualized volatility(ies). +365 +366 `years_to_maturity`: time remaining to maturity, in years. +367 +368 `y`: annualized divident yield. +369 +370 ### Returns +371 +372 `d1` in Black-Scholes formula. +373 """ +374 +375 return (log(s0 / x) + (r - y + vol * vol / 2.0) * years_to_maturity) / ( +376 vol * sqrt(years_to_maturity) +377 )Returns
372def get_d2( -373 s0: FloatOrNdarray, -374 x: FloatOrNdarray, -375 r: float, -376 vol: FloatOrNdarray, -377 years_to_maturity: float, -378 y: float = 0.0, -379) -> FloatOrNdarray: -380 """ -381 Returns `d2` used in Black-Scholes formula. -382 -383 ### Parameters -384 -385 `s0`: spot price(s) of the underlying asset. -386 -387 `x`: strike price(s). -388 -389 `r`: annualized risk-free interest rate. +@@ -1288,47 +1372,47 @@380def get_d2( +381 s0: FloatOrNdarray, +382 x: FloatOrNdarray, +383 r: float, +384 vol: FloatOrNdarray, +385 years_to_maturity: float, +386 y: float = 0.0, +387) -> FloatOrNdarray: +388 """ +389 Returns `d2` used in Black-Scholes formula. 390 -391 `vol`: annualized volatility(ies). +391 ### Parameters 392 -393 `years_to_maturity`: time remaining to maturity, in years. +393 `s0`: spot price(s) of the underlying asset. 394 -395 `y`: annualized divident yield. +395 `x`: strike price(s). 396 -397 ### Returns +397 `r`: annualized risk-free interest rate. 398 -399 `d2` in Black-Scholes formula. -400 """ -401 -402 return (log(s0 / x) + (r - y - vol * vol / 2.0) * years_to_maturity) / ( -403 vol * sqrt(years_to_maturity) -404 ) +399 `vol`: annualized volatility(ies). +400 +401 `years_to_maturity`: time remaining to maturity, in years. +402 +403 `y`: annualized divident yield. +404 +405 ### Returns +406 +407 `d2` in Black-Scholes formula. +408 """ +409 +410 return (log(s0 / x) + (r - y - vol * vol / 2.0) * years_to_maturity) / ( +411 vol * sqrt(years_to_maturity) +412 )Returns
407def get_implied_vol( -408 option_type: OptionType, -409 oprice: float, -410 s0: float, -411 x: float, -412 r: float, -413 years_to_maturity: float, -414 y: float = 0.0, -415) -> float: -416 """ -417 Returns the implied volatility of an option. -418 -419 ### Parameters -420 -421 `option_type`: either *'call'* or *'put'*. -422 -423 `oprice`: market price of an option. -424 -425 `s0`: spot price of the underlying asset. +@@ -1368,38 +1452,38 @@415def get_implied_vol( +416 option_type: OptionType, +417 oprice: float, +418 s0: float, +419 x: float, +420 r: float, +421 years_to_maturity: float, +422 y: float = 0.0, +423) -> float: +424 """ +425 Returns the implied volatility of an option. 426 -427 `x`: strike price. +427 ### Parameters 428 -429 `r`: annualized risk-free interest rate. +429 `option_type`: either *'call'* or *'put'*. 430 -431 `years_to_maturity`: time remaining to maturity, in years. +431 `oprice`: market price of an option. 432 -433 `y`: annualized dividend yield. +433 `s0`: spot price of the underlying asset. 434 -435 ### Returns +435 `x`: strike price. 436 -437 Option's implied volatility. -438 """ -439 -440 vol = 0.001 * arange(1, 1001) -441 d1 = get_d1(s0, x, r, vol, years_to_maturity, y) -442 d2 = get_d2(s0, x, r, vol, years_to_maturity, y) -443 dopt = abs( -444 get_option_price(option_type, s0, x, r, years_to_maturity, d1, d2, y) - oprice -445 ) -446 -447 return vol[argmin(dopt)] +437 `r`: annualized risk-free interest rate. +438 +439 `years_to_maturity`: time remaining to maturity, in years. +440 +441 `y`: annualized dividend yield. +442 +443 ### Returns +444 +445 Option's implied volatility. +446 """ +447 +448 vol = 0.001 * arange(1, 1001) +449 d1 = get_d1(s0, x, r, vol, years_to_maturity, y) +450 d2 = get_d2(s0, x, r, vol, years_to_maturity, y) +451 dopt = abs( +452 get_option_price(option_type, s0, x, r, years_to_maturity, d1, d2, y) - oprice +453 ) +454 +455 return vol[argmin(dopt)]Returns
450def get_itm_probability( -451 option_type: OptionType, -452 d2: FloatOrNdarray, -453 years_to_maturity: float, -454 y: float = 0.0, -455) -> FloatOrNdarray: -456 """ -457 Returns the probability(ies) that the option(s) will expire in-the-money (ITM). -458 -459 ### Parameters -460 -461 `option_type`: either *'call'* or *'put'*. -462 -463 `d2`: `d2` in Black-Scholes formula. -464 -465 `years_to_maturity`: time remaining to maturity, in years. +@@ -1421,6 +1505,117 @@458def get_itm_probability( +459 option_type: OptionType, +460 d2: FloatOrNdarray, +461 years_to_maturity: float, +462 y: float = 0.0, +463) -> FloatOrNdarray: +464 """ +465 Returns the probability(ies) that the option(s) will expire in-the-money (ITM). 466 -467 `y`: annualized dividend yield. +467 ### Parameters 468 -469 ### Returns +469 `option_type`: either *'call'* or *'put'*. 470 -471 Probability(ies) that the option(s) will expire in-the-money (ITM). -472 """ -473 -474 yfac = exp(-y * years_to_maturity) -475 -476 if option_type == "call": -477 return yfac * stats.norm.cdf(d2) -478 elif option_type == "put": -479 return yfac * stats.norm.cdf(-d2) -480 else: -481 raise ValueError("Option type must be either 'call' or 'put'!") +471 `d2`: `d2` in Black-Scholes formula. +472 +473 `years_to_maturity`: time remaining to maturity, in years. +474 +475 `y`: annualized dividend yield. +476 +477 ### Returns +478 +479 Probability(ies) that the option(s) will expire in-the-money (ITM). +480 """ +481 +482 yfac = exp(-y * years_to_maturity) +483 +484 if option_type == "call": +485 return yfac * stats.norm.cdf(d2) +486 elif option_type == "put": +487 return yfac * stats.norm.cdf(-d2) +488 else: +489 raise ValueError("Option type must be either 'call' or 'put'!")Returns
492def get_probability_of_touch( +493 option_type: OptionType, +494 s: float, +495 x: FloatOrNdarray, +496 r: float, +497 vol: float, +498 years_to_maturity: float, +499 y: float = 0.0, +500) -> FloatOrNdarray: +501 """ +502 Returns the probability(ies) that the option(s) will ever get in-the-money (ITM) +503 before expiration. +504 +505 > [!NOTE] +506 > This function implements equations 2.66 and 2.67 (see pages 80 and 81) in +507 > *The complete guide to option pricing formulas*, 2nd edition, authored by +508 > Espen Gaarder Haug, PhD, and published by McGraw-Hill. +509 +510 ### Parameters +511 +512 `option_type`: either *'call'* or *'put'*. +513 +514 `s`: stock price. +515 +516 `x`: strike price(s). +517 +518 `r`: annualized risk-free interest rate. +519 +520 `vol`: annualized volatility. +521 +522 `years_to_maturity`: time remaining to maturity, in years. +523 +524 `y`: annualized dividend yield. +525 +526 ### Returns +527 +528 Probability(ies) that the option(s) will ever get in-the-money (ITM) before +529 expiration. +530 """ +531 +532 mu = (r - y - 0.5 * vol * vol) / (vol * vol) +533 lam = sqrt((mu * mu) + 2.0 * r / (vol * vol)) +534 sigma = vol * sqrt(years_to_maturity) +535 z = log(x / s) / sigma + lam * sigma +536 exp1 = mu + lam +537 exp2 = mu - lam +538 +539 if option_type == "call": +540 if s >= x: +541 return 1.0 +542 else: +543 return ((x / s) ** exp1) * stats.norm.cdf(-z) + ( +544 (x / s) ** exp2 +545 ) * stats.norm.cdf(2.0 * lam * sigma - z) +546 elif option_type == "put": +547 if s <= x: +548 return 1.0 +549 else: +550 return ((x / s) ** exp1) * stats.norm.cdf(z) + ( +551 (x / s) ** exp2 +552 ) * stats.norm.cdf(z - 2.0 * lam * sigma) +553 else: +554 raise ValueError("Option type must be either 'call' or 'put'!") +
Returns the probability(ies) that the option(s) will ever get in-the-money (ITM) +before expiration.
+ +This function implements equations 2.66 and 2.67 (see pages 80 and 81) in + The complete guide to option pricing formulas, 2nd edition, authored by + Espen Gaarder Haug, PhD, and published by McGraw-Hill.
+option_type: either 'call' or 'put'.
s: stock price.
x: strike price(s).
r: annualized risk-free interest rate.
vol: annualized volatility.
years_to_maturity: time remaining to maturity, in years.
y: annualized dividend yield.
Probability(ies) that the option(s) will ever get in-the-money (ITM) before +expiration.
+