Skip to content

Commit

Permalink
Merge pull request #20 from valorem-labs-inc/fix/bsm
Browse files Browse the repository at this point in the history
fix formula
  • Loading branch information
0xAlcibiades authored Dec 21, 2023
2 parents 5b55fa9 + 55b34c7 commit 5b3be6e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
21 changes: 11 additions & 10 deletions src/utils/vol/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ class OptionsGreeks {
sigma: number,
tau: number,
): number {
return (
(log(st / K) + (r - q + pow(sigma, 2) / 2) * tau) / (sigma * sqrt(tau))
);
return (log(st / K) + (r + pow(sigma, 2) / 2) * tau) / (sigma * sqrt(tau));
}

/**
Expand Down Expand Up @@ -205,20 +203,23 @@ class OptionsGreeks {
return 0;
}

let ist = st;

if (q !== 0) {
const dividendValue = st * q * tau * exp(-r * tau);
ist = st - dividendValue;
}

// Calculate d1 and d2
const d1 = this.d1(st, K, r, q, sigma, tau);
const d1 = this.d1(ist, K, r, q, sigma, tau);
const d2 = this.d2(d1, sigma, tau);

if (type === TypeOfOption.Call) {
// Calculate call option price
return (
st * exp(-q * tau) * this.phi(d1) - K * exp(-r * tau) * this.phi(d2)
);
return ist * this.phi(d1) - K * exp(-r * tau) * this.phi(d2);
}
// Calculate put option price
return (
K * exp(-r * tau) * this.phi(-d2) - st * exp(-q * tau) * this.phi(-d1)
);
return K * exp(-r * tau) * this.phi(-d2) - ist * this.phi(-d1);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/utils/vol/vol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ describe('Brent root-finding algorithm', () => {

describe('Options Greeks BSM and IV convergence', () => {
it('should calculate the correct price for a European put option using Black-Scholes-Merton model', () => {
const st = 100; // Spot price of the underlying asset
const K = 95; // Strike price of the option
const st = 40; // Spot price of the underlying asset
const K = 40; // Strike price of the option
const q = 0.05; // Dividend yield
const t = 0.5; // Time to expiration
const r = 0.1; // Risk-free interest rate
const sigma = 0.2; // Volatility of the asset
const r = 0.09; // Risk-free interest rate
const sigma = 0.3; // Volatility of the asset

const calculatedPutPrice = OptionsGreeks.blackScholesMerton(
TypeOfOption.Put,
TypeOfOption.Call,
st,
K,
r,
Expand All @@ -63,7 +63,7 @@ describe('Options Greeks BSM and IV convergence', () => {
);

// assertEquals in QUnit is analogous to toBeCloseTo in vitest with a precision of 4 decimal places
expect(calculatedPutPrice).toBeCloseTo(2.4648, 4);
expect(calculatedPutPrice).toBeCloseTo(3.6817718494101577, 2);
});

it('should calculate the correct implied volatility', () => {
Expand Down

0 comments on commit 5b3be6e

Please sign in to comment.