UNPKG

@volare.finance/volare.js

Version:
177 lines 6.92 kB
"use strict"; /** * @file greeks.ts * @description Calculation of option greeks. See {@link http://en.wikipedia.org/wiki/Black%E2%80%93Scholes_model#The_Greeks|Wikipedia} * @author astra <astra@volare.finance> * @date 2022 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getGamma = exports.getTheta = exports.getVega = exports.getRho = exports.getDelta = void 0; const tslib_1 = require("tslib"); const bs = tslib_1.__importStar(require("./black-scholes")); /** * @description Calculates the delta of an option. * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @param {Boolean} isPut The type of option * @returns {Number} The delta of the option */ function getDelta(s, k, t, iv, r, isPut) { return isPut ? _putDelta(s, k, t, iv, r) : _callDelta(s, k, t, iv, r); } exports.getDelta = getDelta; /** * @description Calculates the delta of a call option. * @private * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @returns {Number} The delta of the call option */ function _callDelta(s, k, t, iv, r) { const w = bs.getW(s, k, t, iv, r); if (!isFinite(w)) { return s > k ? 1 : 0; } else { return bs.stdNormCDF(w); } } /** * @description Calculates the delta of a put option. * @private * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @returns {Number} The delta of the put option */ function _putDelta(s, k, t, iv, r) { const delta = _callDelta(s, k, t, iv, r) - 1; return delta == -1 && k == s ? 0 : delta; } /** * @description Calculates the rho of an option. * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @param {Boolean} isPut The type of option * @param {Number} [scale=100] The value to scale rho by (100=100BPS=1%, 10000=1BPS=.01%) * @returns {Number} The rho of the option */ function getRho(s, k, t, iv, r, isPut, scale = 100) { return isPut ? _putRho(s, k, t, iv, r) / scale : _callRho(s, k, t, iv, r) / scale; } exports.getRho = getRho; /** * @description Calculates the rho of a call option. * @private * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @returns {Number} The rho of the call option */ function _callRho(s, k, t, iv, r) { const w = bs.getW(s, k, t, iv, r); return !isNaN(w) ? k * t * Math.pow(Math.E, -1 * r * t) * bs.stdNormCDF(w - iv * Math.sqrt(t)) : 0; } /** *@description Calculates the rho of a put option. * @private * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @returns {Number} The rho of the put option */ function _putRho(s, k, t, iv, r) { const w = bs.getW(s, k, t, iv, r); return !isNaN(w) ? -1 * k * t * Math.pow(Math.E, -1 * r * t) * bs.stdNormCDF(iv * Math.sqrt(t) - w) : 0; } /** * @description Calculates the vega of a call and put option. * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @returns {Number} The vega of the option */ function getVega(s, k, t, iv, r) { const w = bs.getW(s, k, t, iv, r); return isFinite(w) ? (s * Math.sqrt(t) * bs.stdNormDensity(w)) / 100 : 0; } exports.getVega = getVega; /** * @description Calculates the theta of an option. * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @param {Boolean} isPut The type of option * @param {Number} [scale=365] The number of days to scale theta by - usually 365 or 252 * @returns {Number} The theta of the option */ function getTheta(s, k, t, iv, r, isPut, scale = 365) { return isPut ? _putTheta(s, k, t, iv, r) / scale : _callTheta(s, k, t, iv, r) / scale; } exports.getTheta = getTheta; /** * @description Calculates the theta of a call option. * @private * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @returns {Number} The theta of the call option */ function _callTheta(s, k, t, iv, r) { const w = bs.getW(s, k, t, iv, r); return isFinite(w) ? (-1 * iv * s * bs.stdNormDensity(w)) / (2 * Math.sqrt(t)) - k * r * Math.pow(Math.E, -1 * r * t) * bs.stdNormCDF(w - iv * Math.sqrt(t)) : 0; } /** * @description Calculates the theta of a put option. * @private * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @returns {Number} The theta of the put option */ function _putTheta(s, k, t, iv, r) { const w = bs.getW(s, k, t, iv, r); return isFinite(w) ? (-1 * iv * s * bs.stdNormDensity(w)) / (2 * Math.sqrt(t)) + k * r * Math.pow(Math.E, -1 * r * t) * bs.stdNormCDF(iv * Math.sqrt(t) - w) : 0; } /** * @description Calculates the gamma of a call and put option. * @param {Number} s Current price of the underlying * @param {Number} k Strike price * @param {Number} t Time to expatriation in years * @param {Number} iv Volatility as a decimal * @param {Number} r Annual risk-free interest rate as a decimal * @returns {Number} The gamma of the option */ function getGamma(s, k, t, iv, r) { const w = bs.getW(s, k, t, iv, r); return isFinite(w) ? bs.stdNormDensity(w) / (s * iv * Math.sqrt(t)) : 0; } exports.getGamma = getGamma; //# sourceMappingURL=greeks.js.map