BLACK_SCHOLES
Overview
The BLACK_SCHOLES
function calculates the theoretical price of a European call or put option using the Black-Scholes formula. This function is widely used in financial engineering for pricing European-style options, risk management, and portfolio valuation. The Black-Scholes model assumes the underlying asset price follows a geometric Brownian motion with constant drift and volatility.
The Black-Scholes formulas are:
where:
- : Current price of the underlying asset
- : Strike price
- : Time to expiration in years
- : Annual risk-free interest rate (decimal)
- : Volatility of the underlying asset (decimal)
- : Cumulative distribution function of the standard normal distribution
Assumptions:
-
European option (exercisable only at expiration)
-
No dividends during the option’s life
-
Efficient markets
-
No transaction costs
-
Constant risk-free rate and volatility
-
Log-normal returns
This example function is provided as-is without any representation of accuracy.
Usage
To use in Excel:
=BLACK_SCHOLES(S, K, T, r, sigma, [option_type])
S
(float, required): Current stock price.K
(float, required): Strike price of the option.T
(float, required): Time to expiration in years.r
(float, required): Risk-free interest rate (decimal).sigma
(float, required): Volatility of the stock (decimal).option_type
(string, optional, default=“call”): Type of option (“call” or “put”).
The function returns the Black-Scholes option price as a float, or an error message as a string if the input is invalid.
Examples
Example 1: European Call Option
In Excel:
=BLACK_SCHOLES(100, 105, 0.5, 0.03, 0.2, "call")
Expected output:
Option Price |
---|
4.18 |
Example 2: European Put Option
In Excel:
=BLACK_SCHOLES(50, 45, 1, 0.02, 0.25, "put")
Expected output:
Option Price |
---|
2.31 |
This means the function returns the theoretical price of the specified European option.
Python Code
import math
from scipy.stats import norm
def black_scholes(S, K, T, r, sigma, option_type="call"):
"""Calculate the Black-Scholes price for a European call or put option.
Args:
S: Current price of the underlying asset (float).
K: Strike price (float).
T: Time to expiration in years (float).
r: Annual risk-free interest rate (decimal, float).
sigma: Volatility of the underlying asset (decimal, float).
option_type: 'call' or 'put' (string, optional, default="call").
Returns:
float: Theoretical price of the option, or
str: Error message if calculation fails.
This example function is provided as-is without any representation of accuracy.
"""
try:
S = float(S)
K = float(K)
T = float(T)
r = float(r)
sigma = float(sigma)
except (ValueError, TypeError):
return "Error: S, K, T, r, and sigma must be numeric."
if not (S > 0):
return "Error: S must be positive."
if not (K > 0):
return "Error: K must be positive."
if not (T > 0):
return "Error: T must be positive."
if not (r >= 0):
return "Error: r must be non-negative."
if not (sigma > 0):
return "Error: sigma must be positive."
if option_type not in ("call", "put"):
return "Error: option_type must be 'call' or 'put'."
d1 = (math.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * math.sqrt(T))
d2 = d1 - sigma * math.sqrt(T)
if option_type == "call":
price = S * norm.cdf(d1) - K * math.exp(-r * T) * norm.cdf(d2)
else:
price = K * math.exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1)
return float(price)
Live Notebook
Edit this function in a live notebook .