@volare.finance/volare.js
Version:
The SDK for Volare Protocol
47 lines • 1.84 kB
JavaScript
;
/**
* @file greeks.ts
* @description Determine implied volatility of options based on their prices.
* @author astra <astra@volare.finance>
* @date 2022
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImpliedVolatility = void 0;
const tslib_1 = require("tslib");
const bs = tslib_1.__importStar(require("./black-scholes"));
/**
* @description Calculate a close estimate of implied volatility given an option price. A binary search type approach is used to determine the implied volatility.
* @param {Number} expectedCost The market price of the option
* @param {Number} s Current price of the underlying
* @param {Number} k Strike price
* @param {Number} t Time to expatriation in years
* @param {Number} r Annual risk-free interest rate as a decimal
* @param {Boolean} isPut The type of option priced
* @param {Number} [estimate=.1] An initial estimate of implied volatility
* @returns {Number} The implied volatility estimate
*/
function getImpliedVolatility(expectedCost, s, k, t, r, isPut, estimate = 0.1) {
let low = 0;
let high = Infinity;
// perform 100 iterations max
for (let i = 0; i < 100; i++) {
const actualCost = bs.blackScholes(s, k, t, estimate, r, isPut);
// compare the price down to the cent
if (expectedCost * 100 == Math.floor(actualCost * 100)) {
break;
}
else if (actualCost > expectedCost) {
high = estimate;
estimate = (estimate - low) / 2 + low;
}
else {
low = estimate;
estimate = (high - estimate) / 2 + estimate;
if (!isFinite(estimate))
estimate = low * 2;
}
}
return estimate;
}
exports.getImpliedVolatility = getImpliedVolatility;
//# sourceMappingURL=implied-volatility.js.map