BlackScholes Options Pricing AFL functions with some Key Greeks.
/************************************** *************************************************************************************************************************************
Options Pricing and Greeks
Inspired and Adapted from Beppe Post on Amibroker forum.
// Adapted from source code in many programming languages here:
// http://www.espenhaug.com/black_scholes.html
// Greeks forumlas used form Options Pricing Model excel sheet from http://www.optiontradingtips.com
Written By : Yogesh Dalvi
Date : 10-Sep-2020
Email : yogesh.dalvi@gmail.com
twitter : @dyogesh21
Telegram : https:/t.me/Dyogesh
*****************************************************************************************************************************************************************************/
//Options Functions
// BlackScholes function parameters:
// isCallFlag = True for Call / False for Put
// S = Stock price
// X = Strike price
// T = Years to maturity (DTE / 365) - DTE = Days to expiration
// r = Risk-free rate
// v = Volatility
// d = Divident
// mp = Market Price
function OptionPrice(isCallFlag, S, X , T, r, iv,d)
{
local d1, d2, result;
d1 = (log(S / X) + (r - d + (iv * iv * 0.5)) * T) / (iv * sqrt(T));
d2 = d1 - iv * sqrt(T);
if (isCallFlag)
result = ((S * exp(-d * T)* NormDist(d1)) - (X * exp(-r * T) * NormDist(d2)) );
else
result = ( (X * exp(-r * T) * NormDist(-d2)) - (S * NormDist(-d1) * exp(-d * T) ));
return result;
}
function OptionDelta(isCallFlag, S, X, T, r, iv,d)
{
local d1, d2, result;
d1 = (log(S / X) + (r - d + (iv * iv * 0.5)) * T) / (iv * sqrt(T));
d2 = d1 - iv * sqrt(T);
if (isCallFlag)
result = NormDist(d1);
else
result = NormDist(d1) - 1;
return result;
}
function OptionGamma(S, X, T, r, iv,d)
{
local d1, d2, nd1,Pi,result;
Pi = 3.14159265358979;
d1 = (log(S / X) + (r - d + (iv * iv * 0.5)) * T) / (iv * sqrt(T));
d2 = d1 - iv * sqrt(T);
nd1 = exp(-(d1*d1) / 2) / sqrt(2 * Pi);
result = nd1 / (S * (iv * sqrt(T)));
return result;
}
function OptionTheta(isCallFlag, S, X, T, r, iv,d)
{
local d1, d2,nd1,nd2,Pi,theta,result;
Pi = 3.14159265358979;
d1 = (log(S / X) + (r - d + (iv * iv * 0.5)) * T) / (iv * sqrt(T));
d2 = d1 - iv * sqrt(T);
nd1 = exp(-(d1*d1) / 2) / sqrt(2 * Pi);
nd2 = d1 - iv * sqrt(T);
if (isCallFlag)
{
theta = - (((S * iv * nd1) / (2 * sqrt(T))) - (r * X * exp(-r * T) * nd2)) ;
}
else
{
theta = - (((S * iv * nd1) / (2 * sqrt(T))) + (r * X * exp(-r * T) * (1- nd2))) ;
}
if (T < 1 / 365)
result = theta * T ;
else
result = theta / 365;
return result;
}
function OptionVega(S, X, T, r, iv,d)
{
local d1, d2, nd1,Pi,result;
Pi = 3.14159265358979;
d1 = (log(S / X) + (r - d + (iv * iv * 0.5)) * T) / (iv * sqrt(T));
d2 = d1 - iv * sqrt(T);
nd1 = exp(-(d1*d1) / 2) / sqrt(2 * Pi);
result = 0.01 * S * sqrt(T) * nd1;
return result;
}
function OptionRho(isCallFlag,S, X, T, r, iv,d)
{
local d1, d2, nd1,Pi,result;
Pi = 3.14159265358979;
d1 = (log(S / X) + (r - d + (iv * iv * 0.5)) * T) / (iv * sqrt(T));
d2 = d1 - iv * sqrt(T);
nd1 = exp(-(d1*d1) / 2) / sqrt(2 * Pi);
if(iscallFlag)
result = 0.01 * X * T * exp(-r * T) * NormDist(d2);
else
result = - 0.01 * X * T * exp(-r * T) * (1 -NormDist(d2));
}
function OptionIV(isCallFlag, S, X, T, r,mp,d)
{
local hval,lval,result;
hval = 3;
lval = 0;
if(isCallFlag)
{
do
{
if(OptionPrice(isCallFlag,S,X,T,r,(hval+lval) / 2 ,d) > mp)
hval = (hval + lval) / 2;
else
lval = (hval + lval) / 2;
} while ((hval - lval) > 0.0001);
}
else
{
do
{
if(OptionPrice(isCallFlag,S,X,T,r,(hval+lval) / 2 ,d) > mp)
hval = (hval + lval) / 2;
else
lval = (hval + lval) / 2;
} while ((hval - lval) > 0.0001) ;
}
result = (hval + lval) / 2;
return result;
}