#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 5 10:46:42 2019
@author: turinici
"""
import numpy as np
from scipy.stats import norm
def blsprice(Price,Strike,Rate,TimeToMaturity,Volatility,DividendRate=0):
"""input:
S:Price - Current price of the underlying asset.
Strike:Strike - Strike (i.e., exercise) price of the option.
Rate: Rate - Annualized continuously compounded risk-free rate of return over
the life of the option, expressed as a positive decimal number.
TimeToMaturity:Time - Time to expiration of the option, expressed in years.
Volatility: volatility
DividendRate = continuous dividend rate
"""
if TimeToMaturity <= 1e-6: # the option already expired
call = np.max(Price-Strike,0)
put = np.max(Strike-Price,0)
return call,put
d1 = np.log(Price/Strike)+(Rate-DividendRate + Volatility**2/2.0)*TimeToMaturity;
d1 = d1/(Volatility* np.sqrt(TimeToMaturity))
d2 = d1-(Volatility*np.sqrt(TimeToMaturity))
call = Price * np.exp(-DividendRate*TimeToMaturity) * norm.cdf(d1)-Strike* np.exp(-Rate*TimeToMaturity) * norm.cdf(d2)
put = Strike* np.exp(-Rate*TimeToMaturity) * norm.cdf(-d2)-Price* np.exp(-DividendRate*TimeToMaturity) * norm.cdf(-d1)
return call,put
#test: blsprice(100,110,0.05,1,0.2)
#blsprice(np.array([100,101]),np.array([110,111]),0.05,1,0.2)