black-scholes-model
Version:
Complete plug and play Black-Scholes-Merton model for option pricing with features including implied volatility and Greeks.
80 lines • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.dValue = void 0;
const d1 = ({ S, K, t, rf, sigma }) => {
if (!S) {
throw new Error('Share price cannot be empty');
}
if (S < 0) {
throw new Error('Share price cannot be less than zero');
}
if (!K) {
throw new Error('Strike price cannot be empty');
}
if (K < 0) {
throw new Error('Strike price cannot be less than zero');
}
if (!t) {
throw new Error('Time to expiry cannot be empty');
}
if (t < 0) {
throw new Error('Time to expiry cannot be less than zero');
}
if (!rf) {
throw new Error('Risk-free rate cannot be empty');
}
if (rf < 0) {
throw new Error('Risk-free rate cannot be less than zero');
}
if (!sigma) {
throw new Error('Volatility cannot be empty');
}
if (sigma < 0) {
throw new Error('Volatility cannot be less than zero');
}
const numer = Math.log(S / K) + (rf + Math.pow(sigma, 2) / 2) * t;
const denom = sigma * Math.sqrt(t);
const result = numer / denom;
return result;
};
const d2 = ({ S, K, t, rf, sigma }) => {
if (!S) {
throw new Error('Share price cannot be empty');
}
if (S < 0) {
throw new Error('Share price cannot be less than zero');
}
if (!K) {
throw new Error('Strike price cannot be empty');
}
if (K < 0) {
throw new Error('Strike price cannot be less than zero');
}
if (!t) {
throw new Error('Time to expiry cannot be empty');
}
if (t < 0) {
throw new Error('Time to expiry cannot be less than zero');
}
if (!rf) {
throw new Error('Risk-free rate cannot be empty');
}
if (rf < 0) {
throw new Error('Risk-free rate cannot be less than zero');
}
if (!sigma) {
throw new Error('Volatility cannot be empty');
}
if (sigma < 0) {
throw new Error('Volatility cannot be less than zero');
}
const numer = Math.log(S / K) + (rf - Math.pow(sigma, 2) / 2) * t;
const denom = sigma * Math.sqrt(t);
const result = numer / denom;
return result;
};
exports.dValue = {
d1,
d2,
};
//# sourceMappingURL=dValues.js.map