Skip to content

Commit 5b3be6e

Browse files
authored
Merge pull request #20 from valorem-labs-inc/fix/bsm
fix formula
2 parents 5b55fa9 + 55b34c7 commit 5b3be6e

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/utils/vol/index.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ class OptionsGreeks {
164164
sigma: number,
165165
tau: number,
166166
): number {
167-
return (
168-
(log(st / K) + (r - q + pow(sigma, 2) / 2) * tau) / (sigma * sqrt(tau))
169-
);
167+
return (log(st / K) + (r + pow(sigma, 2) / 2) * tau) / (sigma * sqrt(tau));
170168
}
171169

172170
/**
@@ -205,20 +203,23 @@ class OptionsGreeks {
205203
return 0;
206204
}
207205

206+
let ist = st;
207+
208+
if (q !== 0) {
209+
const dividendValue = st * q * tau * exp(-r * tau);
210+
ist = st - dividendValue;
211+
}
212+
208213
// Calculate d1 and d2
209-
const d1 = this.d1(st, K, r, q, sigma, tau);
214+
const d1 = this.d1(ist, K, r, q, sigma, tau);
210215
const d2 = this.d2(d1, sigma, tau);
211216

212217
if (type === TypeOfOption.Call) {
213218
// Calculate call option price
214-
return (
215-
st * exp(-q * tau) * this.phi(d1) - K * exp(-r * tau) * this.phi(d2)
216-
);
219+
return ist * this.phi(d1) - K * exp(-r * tau) * this.phi(d2);
217220
}
218221
// Calculate put option price
219-
return (
220-
K * exp(-r * tau) * this.phi(-d2) - st * exp(-q * tau) * this.phi(-d1)
221-
);
222+
return K * exp(-r * tau) * this.phi(-d2) - ist * this.phi(-d1);
222223
}
223224

224225
/**

src/utils/vol/vol.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ describe('Brent root-finding algorithm', () => {
4545

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

5555
const calculatedPutPrice = OptionsGreeks.blackScholesMerton(
56-
TypeOfOption.Put,
56+
TypeOfOption.Call,
5757
st,
5858
K,
5959
r,
@@ -63,7 +63,7 @@ describe('Options Greeks BSM and IV convergence', () => {
6363
);
6464

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

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

0 commit comments

Comments
 (0)