UNPKG

@bmancini55/finance

Version:

Finance utilities for JavaScript

199 lines 7.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calcIV = exports.estimateIV = exports.calcOptionRho = exports.calcOptionVega = exports.calcOptionTheta = exports.calcOptionGamma = exports.calcOptionDelta = exports.calcCallFromPut = exports.calcPutFromCall = exports.calcOptionPrice = void 0; const stats_normal_1 = require("./stats-normal"); const calc_cont_pv_1 = require("./calc-cont-pv"); const math_newton_raphson_1 = require("./math-newton-raphson"); /** * Calculates D1 in Black-Scholes * * @param spot spot price * @param strike strike price * @param time time in days * @param rfr risk free rate * @param sigma volatility */ function calcBlackScholesD1(spot, strike, time, rfr, sigma) { return (Math.log(spot / strike) + (rfr + (sigma * sigma) / 2) * time) / (sigma * Math.sqrt(time)); } /** * Calculates D2 in Black-Scholes * * @param d1 D1 value in Black-Scholes * @param sigma volatility * @param time time in days */ function calcBlackScholesD2(d1, sigma, time) { return d1 - sigma * Math.sqrt(time); } /** * Using Black-Scholes, this will calculate the call price for a European options given * the current spot price, strike price, time in years til expiration, a risk free return, * the volatility of the asset. * * @remarks * https://en.wikipedia.org/wiki/Black%E2%80%93Scholes_model#Black%E2%80%93Scholes_formula * * @param call true for call, fasle for put * @param spot spot price * @param strike strike price * @param time time in years * @param rfr risk free rate * @param sigma volatility */ function calcOptionPrice(call, spot, strike, time, rfr, sigma) { const d1 = calcBlackScholesD1(spot, strike, time, rfr, sigma); const d2 = calcBlackScholesD2(d1, sigma, time); const pv = calc_cont_pv_1.calcContPV(strike, rfr, time); const cp = call ? 1 : -1; // call: cdf(d1 ) * spot - cdf(d2 ) * pv // put: -cdf(-d1) * spot + cdf(-d2) * pv return cp * stats_normal_1.Normal.standard.cdf(cp * d1) * spot - cp * stats_normal_1.Normal.standard.cdf(cp * d2) * pv; } exports.calcOptionPrice = calcOptionPrice; /** * Using Black-Scholes and put-call parity, calculate the expected put price given * call price, current spot price, strike price, time and riskFreeRate * * @remarks * https://en.wikipedia.org/wiki/Black%E2%80%93Scholes_model#Black%E2%80%93Scholes_formula * * @param option price * @param spot price * @param strike price * @param time in years * @param rfr * @returns price of the put option */ function calcPutFromCall(option, spot, strike, time, rfr) { const pv = calc_cont_pv_1.calcContPV(strike, rfr, time); return option + pv - spot; } exports.calcPutFromCall = calcPutFromCall; /** * Using Black-Scholes and put-call parity, calculate the expected call price given * put price, current spot price, strike price, time and riskFreeRate * * @param option price * @param spot price * @param strike price * @param time in years * @param rfr * @returns price of the call option */ function calcCallFromPut(option, spot, strike, time, rfr) { const pv = calc_cont_pv_1.calcContPV(strike, rfr, time); return option + spot - pv; } exports.calcCallFromPut = calcCallFromPut; /** * Calculates delta * @param call true for call, false for put * @param spot spot price of the asset * @param strike strike price of the option * @param time days until expiration * @param rfr risk free rate * @param sigma volatility of the asset */ function calcOptionDelta(call, spot, strike, time, rfr, sigma) { const d1 = calcBlackScholesD1(spot, strike, time, rfr, sigma); return stats_normal_1.Normal.standard.cdf(d1) - (call ? 0 : 1); } exports.calcOptionDelta = calcOptionDelta; /** * Calculates gamma * @param spot spot price of the asset * @param strike strike price of the option * @param time days until expiration * @param rfr risk free rate * @param sigma volatility of the asset */ function calcOptionGamma(spot, strike, time, rfr, sigma) { const d1 = calcBlackScholesD1(spot, strike, time, rfr, sigma); return stats_normal_1.Normal.standard.pdf(d1) / (spot * sigma * Math.sqrt(time)); } exports.calcOptionGamma = calcOptionGamma; /** * Calculates theta * @param call true for call, false for put * @param spot spot price of the asset * @param strike strike price of the option * @param time days until expiration * @param rfr risk free rate * @param sigma volatility of the asset */ function calcOptionTheta(call, spot, strike, time, rfr, sigma) { const d1 = calcBlackScholesD1(spot, strike, time, rfr, sigma); const d2 = calcBlackScholesD2(d1, sigma, time); const cp = call ? 1 : -1; return (-(spot * stats_normal_1.Normal.standard.pdf(d1) * sigma) / (2 * Math.sqrt(time)) - cp * rfr * strike * Math.exp(-rfr * time) * stats_normal_1.Normal.standard.cdf(cp * d2)); } exports.calcOptionTheta = calcOptionTheta; /** * Calculates the vega for a European option. Vega is the first * derivative of an option’s price with respect to volatility. * * Note that this calculates based on the sigma as the unit, * so it represents a 100% change in volatility. You must divide * by 100 to get the 1% change (which is more useful) for calculating * price changes. * * @param spot spot price of the asset * @param strike strike price of the option * @param time days until expiration * @param rfr risk free rate * @param sigma volatility of the asset */ function calcOptionVega(spot, strike, time, rfr, sigma) { const d1 = calcBlackScholesD1(spot, strike, time, rfr, sigma); return spot * Math.sqrt(time) * stats_normal_1.Normal.standard.pdf(d1); } exports.calcOptionVega = calcOptionVega; /** * Calculates rho for a European option. * @param call true for call, false for put * @param spot spot price of the asset * @param strike strike price of the option * @param time days until expiration * @param rfr risk free rate * @param sigma volatility of the asset */ function calcOptionRho(call, spot, strike, time, rfr, sigma) { const d1 = calcBlackScholesD1(spot, strike, time, rfr, sigma); const d2 = calcBlackScholesD2(d1, sigma, time); const cp = call ? 1 : -1; return cp * strike * time * Math.exp(-rfr * time) * stats_normal_1.Normal.standard.cdf(cp * d2); } exports.calcOptionRho = calcOptionRho; /** * Estimate the implied volatility via the Brenner and Subrahmanyam method. * * @param option price of the option * @param spot spot price of the asset * @param time time in days */ function estimateIV(option, spot, time) { return Math.sqrt((2 * Math.PI) / time) * (option / spot); } exports.estimateIV = estimateIV; /** * Calculate the implied volatility by using Newton-Raphson where BS(v) * and Vega(v) are used as f(x) and g(x) respectively. * * @param call true for call, false for put * @param option price of the option * @param spot spot price of the asset * @param strike strike price of the option * @param time time in days * @param rfr risk free rate * @param estIV optional estimated IV, defaults to 1 */ function calcIV(call, option, spot, strike, time, rfr, estIV = 1) { const x0 = estIV; const fx = volatility => calcOptionPrice(call, spot, strike, time, rfr, volatility) - option; const gx = volatility => calcOptionVega(spot, strike, time, rfr, volatility); return math_newton_raphson_1.calcNewtonRaphson(fx, gx, x0, 64, 1e-3); } exports.calcIV = calcIV; //# sourceMappingURL=black-scholes.js.map