#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 5 10:58:02 2019
@author: turinici
"""
import numpy as np
from scipy.stats import norm
def blsdelta(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 = (Price>=Strike).astype(np.float) # 1 if in the money, zero otherwise
put = (Price<=Strike).astype(np.float) # cf above
return call,put
d1 = np.log(Price/Strike)+(Rate-DividendRate + Volatility**2/2.0)*TimeToMaturity;
d1 = d1/(Volatility* np.sqrt(TimeToMaturity))
call = np.exp(-DividendRate*TimeToMaturity) * norm.cdf(d1)
put = -np.exp(-DividendRate*TimeToMaturity) * norm.cdf(-d1)
return call,put
#test: blsdelta(100,110,0.05,1,0.2)
#blsdelta(np.array([100,101]),np.array([110,111]),0.05,1,0.2)