black-scholes-model
Version:
Complete plug and play Black-Scholes-Merton model for option pricing with features including implied volatility and Greeks.
35 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.optionPrice = void 0;
const cdf_1 = require("./cdf");
const dValues_1 = require("./dValues");
const optionPrice = (params) => {
const { K, S, rf, sigma, t, type } = params;
if (!K || !S || !rf || !sigma || !t || !type) {
throw new Error('All values are required');
}
const optionObject = {
S: S,
K: K,
t: t,
rf: rf,
sigma: sigma,
};
if (type === 'call') {
const firstPart = (0, cdf_1.cdf)({ x: dValues_1.dValue.d1(optionObject) }) * S;
const secondPart = (0, cdf_1.cdf)({ x: dValues_1.dValue.d2(optionObject) }) * K * Math.exp(-(rf * t));
const price = firstPart - secondPart;
return price;
}
else if (type === 'put') {
const firstPart = (0, cdf_1.cdf)({ x: -dValues_1.dValue.d2(optionObject) }) * K * Math.exp(-(rf * t));
const secondPart = (0, cdf_1.cdf)({ x: -dValues_1.dValue.d1(optionObject) }) * S;
const price = firstPart - secondPart;
return price;
}
else {
throw new Error('Unknown option type');
}
};
exports.optionPrice = optionPrice;
//# sourceMappingURL=optionPrice.js.map