OptionMath
State Variables
ALPHA
SD59x18 internal constant ALPHA = SD59x18.wrap(-6.37309208e18);
LAMBDA
SD59x18 internal constant LAMBDA = SD59x18.wrap(-0.61228883e18);
S1
SD59x18 internal constant S1 = SD59x18.wrap(-0.11105481e18);
S2
SD59x18 internal constant S2 = SD59x18.wrap(0.44334159e18);
SQRT_2PI
int256 internal constant SQRT_2PI = 2_506628274631000502;
MIN_INPUT_PRICE
UD60x18 internal constant MIN_INPUT_PRICE = UD60x18.wrap(1e1);
MAX_INPUT_PRICE
UD60x18 internal constant MAX_INPUT_PRICE = UD60x18.wrap(1e34);
Functions
helperNormal
Helper function to evaluate used to compute the normal CDF approximation
function helperNormal(SD59x18 x) internal pure returns (SD59x18 result);
Parameters
Name | Type | Description |
---|---|---|
x | SD59x18 | The input to the normal CDF (18 decimals) |
Returns
Name | Type | Description |
---|---|---|
result | SD59x18 | The value of the evaluated helper function (18 decimals) |
normalCdf
Approximation of the normal CDF
The approximation implemented is based on the paper 'Accurate RMM-Based Approximations for the CDF of the Normal Distribution' by Haim Shore
function normalCdf(SD59x18 x) internal pure returns (SD59x18 result);
Parameters
Name | Type | Description |
---|---|---|
x | SD59x18 | input value to evaluate the normal CDF on, F(Z<=x) (18 decimals) |
Returns
Name | Type | Description |
---|---|---|
result | SD59x18 | The normal CDF evaluated at x (18 decimals) |
normalPdf
Normal Distribution Probability Density Function.
Equal to Z(x) = (1 / σ√2π)e^( (-(x - µ)^2) / 2σ^2 )
. Only computes pdf of a distribution with µ = 0
and
σ = 1
.
function normalPdf(SD59x18 x) internal pure returns (SD59x18 z);
Parameters
Name | Type | Description |
---|---|---|
x | SD59x18 | Number to get PDF for (18 decimals) |
Returns
Name | Type | Description |
---|---|---|
z | SD59x18 | z-number (18 decimals) |
relu
Implementation of the ReLu function f(x)=(x)^+
to compute call / put payoffs
function relu(SD59x18 x) internal pure returns (UD60x18);
Parameters
Name | Type | Description |
---|---|---|
x | SD59x18 | Input value (18 decimals) |
Returns
Name | Type | Description |
---|---|---|
<none> | UD60x18 | result Output of the relu function (18 decimals) |
d1d2
Returns the terms d1
and d2
from the Black-Scholes formula that are used to compute the price of a
call / put option.
function d1d2(UD60x18 spot, UD60x18 strike, UD60x18 timeToMaturity, UD60x18 volAnnualized, UD60x18 riskFreeRate)
internal
pure
returns (SD59x18 d1, SD59x18 d2);
Parameters
Name | Type | Description |
---|---|---|
spot | UD60x18 | The spot price. (18 decimals) |
strike | UD60x18 | The strike price of the option. (18 decimals) |
timeToMaturity | UD60x18 | The time until the option expires. (18 decimals) |
volAnnualized | UD60x18 | The percentage volatility of the geometric Brownian motion. (18 decimals) |
riskFreeRate | UD60x18 | The rate of the risk-less asset, i.e. the risk-free interest rate. (18 decimals) |
Returns
Name | Type | Description |
---|---|---|
d1 | SD59x18 | The term d1 from the Black-Scholes formula. (18 decimals) |
d2 | SD59x18 | The term d2 from the Black-Scholes formula. (18 decimals) |
optionDelta
Calculate option delta
function optionDelta(
UD60x18 spot,
UD60x18 strike,
UD60x18 timeToMaturity,
UD60x18 volAnnualized,
UD60x18 riskFreeRate,
bool isCall
) internal pure returns (SD59x18);
Parameters
Name | Type | Description |
---|---|---|
spot | UD60x18 | Spot price |
strike | UD60x18 | Strike price |
timeToMaturity | UD60x18 | Duration of option contract (in years) |
volAnnualized | UD60x18 | Annualized volatility |
riskFreeRate | UD60x18 | |
isCall | bool | whether to price "call" or "put" option |
Returns
Name | Type | Description |
---|---|---|
<none> | SD59x18 | price Option delta |
blackScholesPrice
Calculate the price of an option using the Black-Scholes model
this implementation assumes zero interest
function blackScholesPrice(
UD60x18 spot,
UD60x18 strike,
UD60x18 timeToMaturity,
UD60x18 volAnnualized,
UD60x18 riskFreeRate,
bool isCall
) internal pure returns (UD60x18);
Parameters
Name | Type | Description |
---|---|---|
spot | UD60x18 | Spot price (18 decimals) |
strike | UD60x18 | Strike price (18 decimals) |
timeToMaturity | UD60x18 | Duration of option contract (in years) (18 decimals) |
volAnnualized | UD60x18 | Annualized volatility (18 decimals) |
riskFreeRate | UD60x18 | The risk-free rate (18 decimals) |
isCall | bool | whether to price "call" or "put" option |
Returns
Name | Type | Description |
---|---|---|
<none> | UD60x18 | price The Black-Scholes option price (18 decimals) |
is8AMUTC
Returns true if the maturity time is 8AM UTC
function is8AMUTC(uint256 maturity) internal pure returns (bool);
Parameters
Name | Type | Description |
---|---|---|
maturity | uint256 | The maturity timestamp of the option |
Returns
Name | Type | Description |
---|---|---|
<none> | bool | True if the maturity time is 8AM UTC, false otherwise |
isFriday
Returns true if the maturity day is Friday
function isFriday(uint256 maturity) internal pure returns (bool);
Parameters
Name | Type | Description |
---|---|---|
maturity | uint256 | The maturity timestamp of the option |
Returns
Name | Type | Description |
---|---|---|
<none> | bool | True if the maturity day is Friday, false otherwise |
isLastFriday
Returns true if the maturity day is the last Friday of the month
function isLastFriday(uint256 maturity) internal pure returns (bool);
Parameters
Name | Type | Description |
---|---|---|
maturity | uint256 | The maturity timestamp of the option |
Returns
Name | Type | Description |
---|---|---|
<none> | bool | True if the maturity day is the last Friday of the month, false otherwise |
calculateTimeToMaturity
Calculates the time to maturity in seconds
function calculateTimeToMaturity(uint256 maturity) internal view returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
maturity | uint256 | The maturity timestamp of the option |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | Time to maturity in seconds |
calculateStrikeInterval
Calculates the strike interval for strike
function calculateStrikeInterval(UD60x18 strike) internal pure returns (UD60x18);
Parameters
Name | Type | Description |
---|---|---|
strike | UD60x18 | The price to calculate strike interval for (18 decimals) |
Returns
Name | Type | Description |
---|---|---|
<none> | UD60x18 | The strike interval (18 decimals) |
roundToStrikeInterval
Rounds strike
using the calculated strike interval
function roundToStrikeInterval(UD60x18 strike) internal pure returns (UD60x18);
Parameters
Name | Type | Description |
---|---|---|
strike | UD60x18 | The price to round (18 decimals) |
Returns
Name | Type | Description |
---|---|---|
<none> | UD60x18 | The rounded strike price (18 decimals) |
scaleDecimals
Converts a number with inputDecimals
, to a number with given amount of decimals
function scaleDecimals(uint256 value, uint8 inputDecimals, uint8 targetDecimals) internal pure returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
value | uint256 | The value to convert |
inputDecimals | uint8 | The amount of decimals the input value has |
targetDecimals | uint8 | The amount of decimals to convert to |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | The converted value |
scaleDecimals
Converts a number with inputDecimals
, to a number with given amount of decimals
function scaleDecimals(int256 value, uint8 inputDecimals, uint8 targetDecimals) internal pure returns (int256);
Parameters
Name | Type | Description |
---|---|---|
value | int256 | The value to convert |
inputDecimals | uint8 | The amount of decimals the input value has |
targetDecimals | uint8 | The amount of decimals to convert to |
Returns
Name | Type | Description |
---|---|---|
<none> | int256 | The converted value |
fromTokenDecimals
Converts a number with decimals
, to a UD60x18 type
function fromTokenDecimals(uint256 value, uint8 decimals) internal pure returns (UD60x18);
Parameters
Name | Type | Description |
---|---|---|
value | uint256 | The value to convert |
decimals | uint8 | The amount of decimals the value has |
Returns
Name | Type | Description |
---|---|---|
<none> | UD60x18 | The number as a UD60x18 |
toTokenDecimals
Converts a UD60x18 number with decimals
, to it's uint256 type scaled down.
function toTokenDecimals(UD60x18 value, uint8 decimals) internal pure returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
value | UD60x18 | The value to convert |
decimals | uint8 | The amount of decimals the value has |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | The number as a scaled down uint256 |
truncate
Truncates a UD60x18 number down to the correct precision.
function truncate(UD60x18 value, uint8 decimals) internal pure returns (UD60x18);
Parameters
Name | Type | Description |
---|---|---|
value | UD60x18 | The value to convert |
decimals | uint8 | The amount of decimals the value has |
Returns
Name | Type | Description |
---|---|---|
<none> | UD60x18 | The truncated UD60x18 number |
countSignificantDigits
Counts the number of significant digits in a UD60x18 number
function countSignificantDigits(UD60x18 value) internal pure returns (uint8);
Parameters
Name | Type | Description |
---|---|---|
value | UD60x18 | The value to count the significant digits of |
Returns
Name | Type | Description |
---|---|---|
<none> | uint8 | The number of significant digits |
log10Floor
Performs a naive log10 calculation on input
returning the floor of the result
function log10Floor(uint256 input) internal pure returns (uint256 count);
computeCLevel
Calculates the C-level given a utilisation value and time since last trade value (duration). (https://www.desmos.com/calculator/0uzv50t7jy)
function computeCLevel(
UD60x18 utilisation,
UD60x18 duration,
UD60x18 alpha,
UD60x18 minCLevel,
UD60x18 maxCLevel,
UD60x18 decayRate
) internal pure returns (UD60x18);
Parameters
Name | Type | Description |
---|---|---|
utilisation | UD60x18 | The utilisation after some collateral is utilised |
duration | UD60x18 | The time since last trade (hours) |
alpha | UD60x18 | (needs to be filled in) |
minCLevel | UD60x18 | The minimum C-level |
maxCLevel | UD60x18 | The maximum C-level |
decayRate | UD60x18 | The decay rate of the C-level back down to minimum level (decay/hour) |
Returns
Name | Type | Description |
---|---|---|
<none> | UD60x18 | The C-level corresponding to the post-utilisation value. |
computeCLevelGeoMean
Calculates the geo-mean C-level given a utilisation before and after collateral is utilised.
function computeCLevelGeoMean(
UD60x18 utilisationBefore,
UD60x18 utilisationAfter,
UD60x18 duration,
UD60x18 alpha,
UD60x18 minCLevel,
UD60x18 maxCLevel,
UD60x18 decayRate
) internal pure returns (UD60x18 cLevel);
Parameters
Name | Type | Description |
---|---|---|
utilisationBefore | UD60x18 | The utilisation before some collateral is utilised. |
utilisationAfter | UD60x18 | The utilisation after some collateral is utilised |
duration | UD60x18 | The time since last trade (hours) |
alpha | UD60x18 | (needs to be filled in) |
minCLevel | UD60x18 | The minimum C-level |
maxCLevel | UD60x18 | The maximum C-level |
decayRate | UD60x18 | The decay rate of the C-level back down to minimum level (decay/hour) |
Returns
Name | Type | Description |
---|---|---|
cLevel | UD60x18 | The C-level corresponding to the geo-mean of the utilisation value before and after collateral is utilised. |
Errors
OptionMath__NonPositiveVol
error OptionMath__NonPositiveVol();
OptionMath__OutOfBoundsPrice
error OptionMath__OutOfBoundsPrice(UD60x18 min, UD60x18 max, UD60x18 price);
OptionMath__Underflow
error OptionMath__Underflow();
OptionMath__UtilisationOutOfBounds
error OptionMath__UtilisationOutOfBounds();
Structs
BlackScholesPriceVarsInternal
struct BlackScholesPriceVarsInternal {
int256 discountFactor;
int256 timeScaledVol;
int256 timeScaledVar;
int256 timeScaledRiskFreeRate;
}