Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 5x 5x 5x 44x 44x 44x 44x 44x 37x 37x 37x 37x 37x 37x 37x 37x 37x 21x 21x 1x 20x 7x 6x 7x 44x 44x 155x 7x 80164x 80162x 39x 40153x 120077x 26x 33x 18x | import {
isValidStyle,
isValidType,
isNumberGreaterThanZero,
} from "./utils/ValidationUtils.js";
import { price as priceBS } from "./pricing-models/BlackScholes.js";
import { price as priceBT } from "./pricing-models/BinomialTree.js";
import { price as priceMCS } from "./pricing-models/MonteCarloSimulation.js";
const METHOD_BLACK_SCHOLES = ["bs", "black-scholes"];
const METHOD_BINOMIAL_TREE = ["bt", "binomial-tree"];
const METHOD_MONTE_CARLO_SIMULATION = ["mcs", "monte-carlo-simulation"];
/**
* @class Option
*/
class Option {
#style;
#type;
#initialSpotPrice;
#strikePrice;
#timeToMaturity;
#volatility;
#riskFreeRate;
#dividendYield;
/**
* @constructor Option
* @param {object} option
* @param {string} option.style style of the option defined by the exercise rights ("european" or "american")
* @param {string} option.type type of the option ("call" or "put")
* @param {number} option.initialSpotPrice initial price of the underlying asset (S₀ > 0)
* @param {number} option.strikePrice strike/exercise price of the option (K > 0)
* @param {number} option.timeToMaturity time until maturity/expiration in years (τ = T - t > 0)
* @param {number} option.volatility underlying volatility (σ > 0)
* @param {number} [option.riskFreeRate=0] [optional] annualized risk-free interest rate continuously compounded (r)
* @param {number} [option.dividendYield=0] [optional] annual dividend yield continuously compounded (q)
* @throws if the option params are invalid
*/
constructor({
style,
type,
initialSpotPrice,
strikePrice,
timeToMaturity,
volatility,
riskFreeRate,
dividendYield,
}) {
Option.#checkStyle(style);
this.#style = style;
Option.#checkType(type);
this.#type = type;
Option.#checkIsNumberGreaterThanZero(
initialSpotPrice,
"initial spot price"
);
this.#initialSpotPrice = initialSpotPrice;
Option.#checkIsNumberGreaterThanZero(strikePrice, "strike price");
this.#strikePrice = strikePrice;
Option.#checkIsNumberGreaterThanZero(timeToMaturity, "time to maturity");
this.#timeToMaturity = timeToMaturity;
Option.#checkIsNumberGreaterThanZero(volatility, "volatility");
this.#volatility = volatility;
this.#riskFreeRate = Number.isFinite(riskFreeRate) ? riskFreeRate : 0;
this.#dividendYield = Number.isFinite(dividendYield) ? dividendYield : 0;
}
// Price
/**
* Calculates the price of the option.
* @param {string} method pricing method/model ("bsm"|"black-scholes-merton", "crr"|"cox-ross-rubinstein")
* @param {object} [params=null] [optional] params for numerical methods
* @returns {number|undefined} analytically calculated option value, or undefined if the analytical solution is not known
* @throws if the pricing method is not valid
* @throws if the params are invalid for the selected pricing method
*/
price(method, params = null) {
const methodLowerCase = method.toLowerCase();
if (!METHOD_BLACK_SCHOLES.includes(methodLowerCase) && params === null) {
throw new Error("params must be supplied for numerical methods");
}
switch (true) {
case METHOD_BLACK_SCHOLES.includes(methodLowerCase):
return priceBS(this);
case METHOD_BINOMIAL_TREE.includes(methodLowerCase):
return priceBT(this, params);
case METHOD_MONTE_CARLO_SIMULATION.includes(methodLowerCase):
return priceMCS(this, params);
default:
throw new Error(`unknown method: ${method}`);
}
}
// Checkers
static #checkStyle(style) {
Iif (!isValidStyle(style)) {
throw new Error(`invalid style: ${style}`);
}
}
static #checkType(type) {
Iif (!isValidType(type)) {
throw new Error(`Invalid type: ${type}`);
}
}
static #checkIsNumberGreaterThanZero(value, argumentName) {
if (!isNumberGreaterThanZero(value)) {
throw new Error(
`Invalid ${argumentName} (${value}), must be a number greater than zero.`
);
}
}
// Getters and setters
get style() {
return this.#style;
}
set style(newStyle) {
Option.#checkStyle(newStyle);
this.#style = newStyle;
}
get type() {
return this.#type;
}
set type(newType) {
Option.#checkType(newType);
this.#type = newType;
}
get initialSpotPrice() {
return this.#initialSpotPrice;
}
set initialSpotPrice(newInitialSpotPrice) {
Option.#checkIsNumberGreaterThanZero(newInitialSpotPrice);
this.#initialSpotPrice = newInitialSpotPrice;
}
get strikePrice() {
return this.#strikePrice;
}
set strikePrice(newStrikePrice) {
Option.#checkIsNumberGreaterThanZero(newStrikePrice);
this.#strikePrice = newStrikePrice;
}
get timeToMaturity() {
return this.#timeToMaturity;
}
set timeToMaturity(newTimeToMaturity) {
Option.#checkIsNumberGreaterThanZero(newTimeToMaturity);
this.#timeToMaturity = newTimeToMaturity;
}
get volatility() {
return this.#volatility;
}
set volatility(newVolatility) {
Option.#checkIsNumberGreaterThanZero(newVolatility);
this.#volatility = newVolatility;
}
get riskFreeRate() {
return this.#riskFreeRate;
}
set riskFreeRate(newRiskFreeRate) {
this.#riskFreeRate = Number.isFinite(newRiskFreeRate) ? newRiskFreeRate : 0;
}
get dividendYield() {
return this.#dividendYield;
}
set dividendYield(newDividendYield) {
this.#dividendYield = Number.isFinite(newDividendYield)
? newDividendYield
: 0;
}
}
export { Option };
|