// THANK YOU! brazenly stolen from https://www.math.ucla.edu/~tom/distributions/normal.html
// within the spreadsheet, of course, you can just use =NORMSDIST(X)
function normalcdf(X){ //HASTINGS. MAX ERROR = .000001
var T=1/(1+.2316419*Math.abs(X));
var D=.3989423*Math.exp(-X*X/2);
var Prob=D*T*(.3193815+T*(-.3565638+T*(1.781478+T*(-1.821256+T*1.330274))));
if (X>0) {
Prob=1-Prob
}
return Prob
}
/**
* Provides d1 for Black-Scholes.
*
* @param {30} DTE T-t in days to expiration
* @param {.2} sd Volatility (σ / standard deviation)
* @param {100} S Current security price
* @param {110} K Strike price
* @param {.01} r_f Risk-free rate
* @return d1 for Black-Scholes.
* @customfunction
*/
function d1(S, K, DTE, sd, r_f) {
let dt = DTE/365;
return (Math.log(S/K)+ (r_f + sd * sd / 2) * dt) / sd / Math.sqrt(dt);
}
/**
* Provides d2 for Black-Scholes.
*
* @param {30} DTE T-t in days to expiration
* @param {.2} sd Volatility (σ / standard deviation)
* @param {100} S Current security price
* @param {110} K Strike price
* @param {.01} r_f Risk-free rate
* @return d2 for Black-Scholes.
* @customfunction
*/
function d2(S, K, DTE, sd, r_f) {
let dt = DTE/365;
let d_1 = d1(S, K, DTE, sd, r_f);
return d_1 - sd * Math.sqrt(dt);
}
/**
* Call price for Black-Scholes.
*
* @param {30} DTE T-t in days to expiration
* @param {.2} sd Volatility (σ / standard deviation)
* @param {100} S Current security price
* @param {110} K Strike price
* @param {.01} r_f Risk-free rate
* @return Call price for Black-Scholes.
* @customfunction
*/
function BSCALL(S, K, DTE, sd, r_f) {
let dt = DTE / 365;
let d_1 = d1(S, K, DTE, sd, r_f);
let d_2 = d2(S, K, DTE, sd, r_f);
// return normalcdf(d_1) * S - normalcdf(d_2) * K * Math.exp(-rf*dt);
return normalcdf(d_1) * S - normalcdf(d_2) * K * Math.exp(-r_f*dt);
}
/**
* Put price for Black-Scholes.
*
* @param {30} DTE T-t in days to expiration
* @param {.2} sd Volatility (σ / standard deviation)
* @param {100} S Current security price
* @param {110} K Strike price
* @param {.01} r_f Risk-free rate
* @return Put price for Black-Scholes.
* @customfunction
*/
function BSPUT(S, K, DTE, sd, r_f) {
let dt = DTE / 365;
let d_1 = d1(S, K, DTE, sd, r_f);
let d_2 = d2(S, K, DTE, sd, r_f);
return normalcdf(-d_2) * K * Math.exp(-r_f*dt) - normalcdf(-d_1) * S;
}