Skip to content

Commit

Permalink
Merge pull request #18 from valorem-labs-inc/export-greeks
Browse files Browse the repository at this point in the history
chore: rename variable; export optionsgreeks
  • Loading branch information
0xAlcibiades authored Dec 14, 2023
2 parents 8c4af3b + 2265769 commit 5b006bf
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 154 deletions.
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './siwe';
export * from './timestamps';
export { Brent, OptionsGreeks, TypeOfOption } from './vol';
export type { Market, OptionData, Underlying } from './vol';
43 changes: 21 additions & 22 deletions src/utils/vol/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ interface Underlying {
}

// Enumeration for option type
enum OptionType {
enum TypeOfOption {
Call = 'call', // The option is a call
Put = 'put', // The option is a put
}

interface OptionData {
type: OptionType;
type: TypeOfOption;
T: number; // Time of expiration in years, expressed as a decimal
K: number; // Strike price of the option
}
Expand Down Expand Up @@ -192,7 +192,7 @@ class OptionsGreeks {
* @returns The fair value of the option.
*/
public static blackScholesMerton(
type: OptionType,
type: TypeOfOption,
st: number,
K: number,
r: number,
Expand All @@ -209,7 +209,7 @@ class OptionsGreeks {
const d1 = this.d1(st, K, r, q, sigma, tau);
const d2 = this.d2(d1, sigma, tau);

if (type === OptionType.Call) {
if (type === TypeOfOption.Call) {
// Calculate call option price
return (
st * exp(-q * tau) * this.phi(d1) - K * exp(-r * tau) * this.phi(d2)
Expand All @@ -234,7 +234,7 @@ class OptionsGreeks {
* @returns The implied volatility as a decimal.
*/
public static sigma(
type: OptionType,
type: TypeOfOption,
marketPrice: number,
st: number,
K: number,
Expand Down Expand Up @@ -285,7 +285,7 @@ class OptionsGreeks {
* Delta is also used for hedging strategies, where a position can be delta-hedged by taking positions in the underlying asset.
*/
public static delta(
type: OptionType,
type: TypeOfOption,
st: number,
K: number,
r: number,
Expand All @@ -295,7 +295,7 @@ class OptionsGreeks {
): number {
const d1 = this.d1(st, K, r, q, sigma, tau);

if (type === OptionType.Call) {
if (type === TypeOfOption.Call) {
// Delta for a Call option
return exp(-q * tau) * this.phi(d1);
}
Expand Down Expand Up @@ -348,7 +348,7 @@ class OptionsGreeks {
* @returns The theta value of the option. Theta is typically negative since options lose value as time passes.
*/
public static theta(
type: OptionType,
type: TypeOfOption,
st: number,
K: number,
r: number,
Expand All @@ -362,11 +362,10 @@ class OptionsGreeks {
const pdfD1 = exp(-pow(d1, 2) / 2) / sqrt(2 * pi);

// Calculate the first part of the theta formula which is common between call and put
const thetaCommon =
(st * exp(-q * tau) * pdfD1 * sigma) / (2 * sqrt(tau));
const thetaCommon = (st * exp(-q * tau) * pdfD1 * sigma) / (2 * sqrt(tau));

// Depending on the option type calculate the rest of the theta value
if (type === OptionType.Call) {
if (type === TypeOfOption.Call) {
// Call option theta formula
const secondTerm = -q * st * exp(-q * tau) * this.phi(d1);
const thirdTerm = r * K * exp(-r * tau) * this.phi(d2);
Expand All @@ -375,7 +374,7 @@ class OptionsGreeks {
// Put option theta formula
const secondTerm = -q * st * exp(-q * tau) * this.phi(-d1);
const thirdTerm = r * K * exp(-r * tau) * this.phi(-d2);
return (-thetaCommon + secondTerm + thirdTerm);
return -thetaCommon + secondTerm + thirdTerm;
}

/**
Expand All @@ -396,7 +395,7 @@ class OptionsGreeks {
* where a shift in interest rates could have a more pronounced effect on the option's value.
*/
public static rho(
type: OptionType,
type: TypeOfOption,
st: number,
K: number,
r: number,
Expand All @@ -408,7 +407,7 @@ class OptionsGreeks {
const d1 = this.d1(st, K, r, q, sigma, tau);
const d2 = this.d2(d1, sigma, tau);

if (type === OptionType.Call) {
if (type === TypeOfOption.Call) {
// Rho for a Call option
return K * tau * exp(-r * tau) * this.phi(d2) * 0.01;
}
Expand All @@ -433,7 +432,7 @@ class OptionsGreeks {
* with a decrease in dividend yield, while a negative epsilon indicates the price decreases as the dividend yield rises.
*/
public static epsilon(
type: OptionType,
type: TypeOfOption,
st: number,
K: number,
r: number,
Expand All @@ -443,7 +442,7 @@ class OptionsGreeks {
): number {
const d1 = this.d1(st, K, r, q, sigma, tau);

if (type === OptionType.Call) {
if (type === TypeOfOption.Call) {
// Epsilon for a Call option
return -st * tau * exp(-q * tau) * this.phi(d1);
}
Expand All @@ -468,7 +467,7 @@ class OptionsGreeks {
* in the price of the underlying asset. It is similar to Delta but expressed in percentage terms.
*/
public static lambda(
type: OptionType,
type: TypeOfOption,
st: number,
K: number,
r: number,
Expand Down Expand Up @@ -578,7 +577,7 @@ class OptionsGreeks {
* without trading.
*/
public static charm(
type: OptionType,
type: TypeOfOption,
st: number,
K: number,
r: number,
Expand All @@ -597,7 +596,7 @@ class OptionsGreeks {
((2 * (r - q) * tau - d2 * sigma * sqrt(tau)) /
(2 * tau * sigma * sqrt(tau)));

if (type === OptionType.Call) {
if (type === TypeOfOption.Call) {
// Charm for a Call option
return commonTerm;
}
Expand Down Expand Up @@ -842,7 +841,7 @@ class OptionsGreeks {
* @returns The dual delta of the option.
*/
public static dualDelta(
type: OptionType,
type: TypeOfOption,
st: number,
K: number,
r: number,
Expand All @@ -852,7 +851,7 @@ class OptionsGreeks {
): number {
const d2 = this.d2(this.d1(st, K, r, q, sigma, tau), sigma, tau);

if (type === OptionType.Call) {
if (type === TypeOfOption.Call) {
// Dual Delta for a Call option
return -exp(-r * tau) * this.phi(d2);
}
Expand Down Expand Up @@ -888,5 +887,5 @@ class OptionsGreeks {
}
}

export { OptionsGreeks, OptionType, Brent };
export { OptionsGreeks, TypeOfOption, Brent };
export type { Market, Underlying, OptionData };
Loading

0 comments on commit 5b006bf

Please sign in to comment.