Created
March 15, 2020 16:49
-
-
Save crapher/b1c1413f7a07d09114a27c4db4109e56 to your computer and use it in GitHub Desktop.
Black Scholes Volatility and Price calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import * | |
# Cumulative standard normal distribution | |
def cdf(x): | |
return (1.0 + erf(x / sqrt(2.0))) / 2.0 | |
# Call Price based on Black Scholes Model | |
# Parameters | |
# underlying_price: Price of underlying asset | |
# exercise_price: Exercise price of the option | |
# time_in_years: Time to expiration in years (ie. 33 days to expiration is 33/365) | |
# risk_free_rate: Risk free rate (ie. 2% is 0.02) | |
# volatility: Volatility percentage (ie. 30% volatility is 0.30) | |
def black_scholes_call(underlying_price, exercise_price, time_in_years, risk_free_rate, volatility): | |
d1 = (log(underlying_price / exercise_price) + risk_free_rate * time_in_years) / (volatility * sqrt(time_in_years)) + 0.5 * volatility * sqrt(time_in_years) | |
d2 = d1 - (volatility * sqrt(time_in_years)) | |
return underlying_price * cdf(d1) - exercise_price * exp(-time_in_years * risk_free_rate) * cdf(d2) | |
# Put Price based on Black Scholes Model | |
# Parameters | |
# underlying_price: Price of underlying asset | |
# exercise_price: Exercise price of the option | |
# time_in_years: Time to expiration in years (ie. 33 days to expiration is 33/365) | |
# risk_free_rate: Risk free rate (ie. 2% is 0.02) | |
# volatility: Volatility percentage (ie. 30% volatility is 0.30) | |
def black_scholes_put(underlying_price, exercise_price, time_in_years, risk_free_rate, volatility): | |
return black_scholes_call(underlying_price, exercise_price, time_in_years, risk_free_rate, volatility) + exercise_price * exp(-risk_free_rate * time_in_years) - underlying_price | |
# Call Implied Volatility | |
# Parameters | |
# underlying_price: Price of underlying asset | |
# exercise_price: Exercise price of the option | |
# time_in_years: Time to expiration in years (ie. 33 days to expiration is 33/365) | |
# risk_free_rate: Risk free rate (ie. 2% is 0.02) | |
# option_price: It is the market price of the option | |
def implied_volatility_call(underlying_price, exercise_price, time_in_years, risk_free_rate, option_price): | |
high = 5 | |
low = 0 | |
while (high - low) > 0.0001: | |
if black_scholes_call(underlying_price, exercise_price, time_in_years, risk_free_rate, (high + low) / 2) > option_price: | |
high = (high + low) / 2 | |
else: | |
low = (high + low) / 2 | |
return (high + low) / 2 | |
# Put Implied Volatility | |
# Parameters | |
# underlying_price: Price of underlying asset | |
# exercise_price: Exercise price of the option | |
# time_in_years: Time to expiration in years (ie. 33 days to expiration is 33/365) | |
# risk_free_rate: Risk free rate (ie. 2% is 0.02) | |
# option_price: It is the market price of the option | |
def implied_volatility_put(underlying_price, exercise_price, time_in_years, risk_free_rate, option_price): | |
high = 5 | |
low = 0 | |
while (high - low) > 0.0001: | |
if black_scholes_put(underlying_price, exercise_price, time_in_years, risk_free_rate, (high + low) / 2) > option_price: | |
high = (high + low) / 2 | |
else: | |
low = (high + low) / 2 | |
return (high + low) / 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment