The Black-Scholes model prices European options using five inputs: stock price, strike price, time to expiration, volatility, and risk-free rate. It assumes markets are efficient and returns are log-normally distributed. This model won Fischer Black and Myron Scholes the Nobel Prize in Economics (1997).
// Cumulative Normal Distribution
function normalCDF(x) {
const a1 = 0.254829592, a2 = -0.284496736;
const a3 = 1.421413741, a4 = -1.453152027;
const a5 = 1.061405429, p = 0.3275911;
const sign = x < 0 ? -1 : 1;
x = Math.abs(x) / Math.sqrt(2);
const t = 1 / (1 + p * x);
const y = 1 - (((((a5*t + a4)*t + a3)*t + a2)*t + a1)*t
* Math.exp(-x*x);
return 0.5 * (1 + sign * y);
}
// Black-Scholes Formula
function blackScholes(S, K, T, sigma, r) {
const d1 = (Math.log(S/K) + (r + sigma*sigma/2)*T)
/ (sigma * Math.sqrt(T));
const d2 = d1 - sigma * Math.sqrt(T);
const call = S * normalCDF(d1) - K * Math.exp(-r*T) * normalCDF(d2);
const put = K * Math.exp(-r*T) * normalCDF(-d2) - S * normalCDF(-d1);
return { call, put };
}