delta

Binary options

Computes Delta for binary cash-or-nothing call and put options using the Black-Scholes model.

Avg. gas

1,717

Max abs. error

1e-13

Signature

solidity
function delta(
    uint128 spot,
    uint128 strike,
    uint32  timeToExp,
    uint64  volatility,
    uint64  rate
) internal pure returns (int128 deltaCall, int128 deltaPut)

Parameters

NameTypeDescription
spotuint128Current spot price, 18-decimal fixed-point.
strikeuint128Strike price, 18-decimal fixed-point. Precision-tuned for the no-arbitrage band against spot — see Bounds.
timeToExpuint32Time to expiration in seconds. timeToExp == 0 is allowed (handled as expired).
volatilityuint64Annualized implied volatility, 18-decimal fixed-point (e.g. 60% → 6e17).
rateuint64Annualized risk-free rate, 18-decimal fixed-point.

Returns

NameTypeDescription
deltaCallint128Binary call delta for unit payout in 18-decimal fixed-point.
deltaPutint128Binary put delta for unit payout in 18-decimal fixed-point. Equal to −deltaCall.

Bounds

BoundValue
MIN_SPOT1e-6 smallest allowed spot price (1e12)
MAX_SPOT1e15 largest allowed spot price (1e33)
MAX_STSP_RATIO5× (strike must lie within [spot/5, spot·5])
MAX_EXPIRATION32 years (1,009,152,000 seconds)
MAX_RATE400% annual (4e18)

Behavior

  • Validates all five inputs against module-wide constants and reverts with a typed error on any violation.
  • Returns both call and put delta from one evaluation — δput = −δcall, so the second value is free.
  • Volatility has no explicit revert — it's bounded only by its uint64 type (max ≈ 1.84e19, i.e. ~1840% annualized).
  • Fast-path on expiration: when timeToExp == 0, returns (0, 0).
  • Composes four DeFiMath primitives — ln, sqrtTime (specialized sqrt for years), expPositive (the discount factor), and exp (the density φ(d₂)).
  • Pure internal function; no external calls, no storage. Inlined into the caller's bytecode at compile time.

How it works

Binary delta is the sensitivity of the cash-or-nothing price to spot. Unlike a vanilla delta it is a sharp density peak at the strike, not a smooth [0, 1] ramp:

δcall=erTφ(d2)SσT,δput=δcall\delta_{call} = \frac{e^{-rT} \, \varphi(d_2)}{S \, \sigma \sqrt{T}}, \qquad \delta_{put} = -\delta_{call}

The density φ(d₂) is evaluated as Math.exp(−d₂²/2) / √(2π) via the precomputed SQRT_2PI constant, discounted by 1 / Math.expPositive(rT), and divided by spot · σ√T (√T from Math.sqrtTime, ln(spot/strike) from Math.ln). Put delta is the exact negative of call delta.

On the supported domain the magnitude stays well below 1 (the 1/S factor with an $1,000-scale spot crushes it), so the suite enforces an absolute error only — 1e-13 — with no relative bound. Head-to-head measurements live in defimath-compare.

Errors

ErrorTrigger
SpotLowerBoundErrorspot ≤ MIN_SPOT
SpotUpperBoundErrorspot ≥ MAX_SPOT
StrikeLowerBoundErrorstrike · 5 < spot
StrikeUpperBoundErrorspot · 5 < strike
TimeToExpiryUpperBoundErrortimeToExp ≥ MAX_EXPIRATION
RateUpperBoundErrorrate ≥ MAX_RATE

Example

solidity
import "defimath-lib/contracts/derivatives/BinaryOptions.sol";

(int128 deltaCall, int128 deltaPut) = BinaryOptions.delta(
    1000e18,         // spot = $1,000
    1050e18,         // strike = $1,050
    30 days,         // 30 days to expiry
    0.60e18,         // 60% annualized vol
    0.05e18          // 5% risk-free rate
);
// deltaCall ≈ 0.0037e18 per $1, deltaPut = -deltaCall