Skip to content

Commit

Permalink
Options Pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
ApurvShah007 committed Jul 24, 2020
1 parent e1720fd commit 64c9e7e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Code/portfolio_optimizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import statsmodels.api as sm
import time
import math
import datetime
42 changes: 42 additions & 0 deletions options-fair-price-valuation/blackScholes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import numpy as np
import datetime
import scipy.stats as si
import numpy as np
import scipy.stats as si
import yfinance as yf

def black_scholes_calc(S, K, T, r, sigma, option = 'call'):
#S: Current Stock Price
#K: Strike price
#T: Time to maturity in Years
#r: Risk Free interest rate
#sigma: Volatility of underlying asset

d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
d2 = (np.log(S / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))

if option == 'call':
result = (S * si.norm.cdf(d1, 0.0, 1.0) - K * np.exp(-r * T) * si.norm.cdf(d2, 0.0, 1.0))
if option == 'put':
result = (K * np.exp(-r * T) * si.norm.cdf(-d2, 0.0, 1.0) - S * si.norm.cdf(-d1, 0.0, 1.0))

return result

'''
Sample Run
'''
s = yf.Ticker('AAPL')
opt = s.option_chain('2020-08-21')
call= opt.calls
S = s.history(period = "max")['Close'].iloc[-1]
T = (datetime.date(2020,11,20) - datetime.date(2020,7,24)).days / 365.0
print(S, T)
for i in range(len(call)):
v = call.iloc[i]['impliedVolatility']
r = 0.17900
K = call.iloc[i]['strike']
print(K)
print ('Price: %.4f' % black_scholes_calc(S, K, T, 0.014, v))



40 changes: 40 additions & 0 deletions options-fair-price-valuation/monteCarlo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import datetime
from random import gauss
from math import exp,sqrt
import yfinance as yf
import pandas as pd


def monterCarloPriceGenerator(sP, vol, rfi, time_diff):
price = sP*exp((rfi - 0.5*vol**2)*time_diff+ vol*sqrt(time_diff)*gauss(0,1.0))
return price

def call_payoff(sT, k):
return max(sT-k, 0)
#Stock
s = yf.Ticker('AAPL')
opt = s.option_chain('2020-08-21')
call= opt.calls

def finalPrice(S,v,r,T,K):
simulations = 900000
payoffs = []
discount_factor = exp(-r * T)

for i in range(simulations):
S_T = monterCarloPriceGenerator(S,v,r,T)
payoffs.append(call_payoff(S_T, K))
price = discount_factor * (sum(payoffs) / float(simulations))
print ('Price: %.4f' % price)

#print(call.iloc[30]['impliedVolatility'])
#print(call.head(40))
S = s.history(period = "max")['Close'].iloc[-1]
r = 0.17900 # rate of 0.14%
T = (datetime.date(2020,8,21) - datetime.date(2020,7,24)).days / 365.0
for i in range(len(call)):
v = call.iloc[i]['impliedVolatility']
K = call.iloc[i]['strike']
print(K)
finalPrice(S,v,r,T,K)

0 comments on commit 64c9e7e

Please sign in to comment.