write a python script that will calculate option prices
- User
write a python script that will calculate option prices
- Assistant
write a python script that will calculate option prices
To calculate option prices in Python, we can use the Black-Scholes model for European options (i.e., options that can only be exercised at expiration) on non-dividend paying stocks. The Black-Scholes model gives us a formula for estimating the prices of both call and put options based on factors including the stock price, strike price, time to expiration, volatility, and risk-free interest rate.
The Black-Scholes formula for a call option is: [C = S_0 \Phi(d_1) - K e^{-rT}\Phi(d_2)]
And for a put option: [P = K e^{-rT} \Phi(-d_2) - S_0 \Phi(-d_1)]
Where:
Here is a Python script that defines functions, black_scholes_call and black_scholes_put, for computing call and put option prices respectively using the Black-Scholes model:
import numpy as np
from scipy.stats import norm
def black_scholes_call(S, K, T, r, sigma):
"""
Calculate call option price using Black-Scholes formula.
Parameters:
- S: current stock price
- K: strike price
- T: time to expiration (in years)
- r: risk-free interest rate (annual)
- sigma: volatility of the stock's return (annualized)
"""
d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
call_price = (S * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2))
return call_price
def black_scholes_put(S, K, T, r, sigma):
"""
Calculate put option price using Black-Scholes formula.
Parameters:
- S: current stock price
- K: strike price
- T: time to expiration (in years)
- r: risk-free interest rate (annual)
- sigma: volatility of the stock's return (annualized)
"""
d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
put_price = (K * np.exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1))
return put_price
# Example usage:
S = 100 # Current stock price
K = 100 # Strike price
T = 1 # Time to expiration (1 year)
r = 0.05 # Risk-free interest rate (5%)
sigma = 0.2 # Volatility (20%)
call_price = black_scholes_call(S, K, T, r, sigma)
put_price = black_scholes_put(S, K, T, r, sigma)
print(f"Call Option Price: ${call_price:.2f}")
print(f"Put Option Price: ${put_price:.2f}")
This script includes essential imports (NumPy for mathematical operations and norm from SciPy for the cumulative distribution function), defines functions for calculating call and put prices using the Black-Scholes formula, and provides an example of how to use these functions.
Not yet included in a published release.