FinancialToolbox.jl

Black and Scholes utility functions.
View on GitHub Star

About this Package

FinancialToolbox.jl is a Julia package containing some useful Financial functions for Pricing and Risk Management under the Black and Scholes Model.
The syntax is the same of the Matlab Financial Toolbox. Currently supports classical numerical input and other less common like:

Getting started

In order to get started, just add the package:

pkg> add FinancialToolbox

and load it:

using FinancialToolbox

Example of Usage

Pricing a European Option in Black and Scholes framework:
According to Black and Scholes model, the price of a european option can be expressed as follows:

European Call Price

Cbs(S,K,r,T,σ,d)=SedTN(d1)KerTN(d2) C_{bs}(S,K,r,T,\sigma,d) = S e^{-d\,T} N(d_1) - Ke^{-r\,T} N(d_2)

European Put Price

Pbs(S,K,r,T,σ,d)=KerTN(d2)SedTN(d1) P_{bs}(S,K,r,T,\sigma,d) = Ke^{-r\,T} N(-d_2) - S e^{-d\,T} N(-d_1)

where:

d1=ln(SK)+(rq+σ22)TσTd_1 = \frac{\ln\left(\frac{S}{K}\right) + \left(r - q + \frac{\sigma^2}{2}\right) T}{\sigma\sqrt{T}} d2=d1σTd_2 = d_1 - \sigma\sqrt{T}


And:

  • SS is the underlying spot price.

  • KK is the strike price.

  • rr is the risk free rate.

  • TT is the time to maturity.

  • σ\sigma is the implied volatility of the underlying.

  • dd is the implied dividend of the underlying.

The way to compute the price in this library is:

European Call Price

S=100.0; K=100.0; r=0.02; T=1.2; σ=0.2; d=0.01;
Price_call=blsprice(S,K,r,T,σ,d)
9.169580760087896

European Put Price

S=100.0; K=100.0; r=0.02; T=1.2; σ=0.2; d=0.01;
Price_put=blsprice(S,K,r,T,σ,d,false)
7.990980449685762


Pricing a European Option in Black framework:
According to Black model, the price of a european option can be expressed as follows:
European Call Price

Cbk(F,K,r,T,σ)=FerTN(d1)KerTN(d2) C_{bk}(F,K,r,T,\sigma) = F e^{-r\,T} N(d_1) - Ke^{-r\,T} N(d_2)

European Put Price

Pbk(F,K,r,T,σ)=KerTN(d2)FerTN(d1) P_{bk}(F,K,r,T,\sigma) = Ke^{-r\,T} N(-d_2) - F e^{-r\,T} N(-d_1)

where:

d1=ln(FK)+σ22TσTd_1 = \frac{\ln\left(\frac{F}{K}\right) + \frac{\sigma^2}{2}T}{\sigma\sqrt{T}} d2=d1σTd_2 = d_1 - \sigma\sqrt{T}


And:

  • FF is the underlying forward price.

  • KK is the strike price.

  • rr is the risk free rate.

  • TT is the time to maturity.

  • σ\sigma is the implied volatility of the underlying.

The way to compute the price in this library is:

European Call Price

F=100.0; K=102.0; r=0.02; T=1.2; σ=0.2;
Price_call=blkprice(S,K,r,T,σ)
7.659923984582901

European Put Price

F=100.0; K=102.0; r=0.02; T=1.2; σ=0.2;
Price_put=blkprice(S,K,r,T,σ,false)
9.612495404098706


Computing Implied Volatility in a Black and Scholes framework: Given an option price VV, the Black and Scholes implied volatility is defined as the positive number σ\sigma which solves:
From European Call Price

V=SedTN(d1)KerTN(d2) V = S e^{-d\,T} N(d_1) - Ke^{-r\,T} N(d_2)

From European Put Price

V=KerTN(d2)SedTN(d1) V = Ke^{-r\,T} N(-d_2) - S e^{-d\,T} N(-d_1)

where:

d1=ln(SK)+(rq+σ22)TσTd_1 = \frac{\ln\left(\frac{S}{K}\right) + \left(r - q + \frac{\sigma^2}{2}\right) T}{\sigma\sqrt{T}} d2=d1σTd_2 = d_1 - \sigma\sqrt{T}


The way to compute the volatility in this library is:

Implied Volatility from Call Price

S=100.0; K=100.0; r=0.02; T=1.2; d=0.01;
call_price=9.169580760087896
σ=blsimpv(S,K,r,T,call_price,d)
0.20000000000000046

Implied Volatility from Put Price

S=100.0; K=100.0; r=0.02; T=1.2; d=0.01;
put_price=7.990980449685762
σ=blsimpv(S,K,r,T,put_price,d,false)
0.19999999999999976

blsimpv accepts additional arguments such as the absolute tolerance and the maximum numbers of steps of the numerical inversion.
Computing Implied Volatility in Black framework:
Given an option price VV, the Black implied volatility is defined as the positive number σ\sigma which solves:
From European Call Price

V=FerTN(d1)KerTN(d2) V = F e^{-r\,T} N(d_1) - Ke^{-r\,T} N(d_2)

From European Put Price

V=KerTN(d2)FerTN(d1) V = Ke^{-r\,T} N(-d_2) - F e^{-r\,T} N(-d_1)

where:

d1=ln(FK)+σ22TσTd_1 = \frac{\ln\left(\frac{F}{K}\right) + \frac{\sigma^2}{2}T}{\sigma\sqrt{T}} d2=d1σTd_2 = d_1 - \sigma\sqrt{T}


The way to compute the volatility in this library is:

Implied Volatility from Call Price

S=100.0; K=102.0; r=0.02; T=1.2;
call_price=7.659923984582901
σ=blkimpv(S,K,r,T,call_price)
0.20000000000000026

Implied Volatility from Put Price

S=100.0; K=102.0; r=0.02; T=1.2;
put_price=9.612495404098706
σ=blkimpv(S,K,r,T,put_price,false)
0.19999999999999984

blkimpv accepts additional arguments such as the absolute tolerance and the maximum numbers of steps of the numerical inversion.