Skip to content

fix formula #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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