gamma

Binary options

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

Avg. gas

1,859

Max abs. error

1e-15

Signature

solidity
function gamma(
    uint128 spot,
    uint128 strike,
    uint32  timeToExp,
    uint64  volatility,
    uint64  rate
) internal pure returns (int128 gammaCall, int128 gammaPut)

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
gammaCallint128Binary call gamma for unit payout in 18-decimal fixed-point. Signed.
gammaPutint128Binary put gamma for unit payout in 18-decimal fixed-point. Equal to −gammaCall.

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.
  • Binary gamma is signed and changes sign at-the-money (d₁ = 0) — unlike vanilla gamma, which is always ≥ 0. γput = −γcall.
  • 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 gamma is the second derivative of the cash-or-nothing price with respect to spot — the rate at which binary delta changes:

Γcall=erTφ(d2)d1S2σ2T,Γput=Γcall\Gamma_{call} = -\frac{e^{-rT} \, \varphi(d_2) \, d_1}{S^2 \, \sigma^2 \, T}, \qquad \Gamma_{put} = -\Gamma_{call}

The d₁ factor in the numerator is what makes it sign-changing: gamma is positive on one side of the strike and negative on the other, passing through zero at-the-money. The density φ(d₂) uses Math.exp, the discount factor e^(−rT) uses Math.expPositive, and √T / ln(spot/strike) use Math.sqrtTime / Math.ln.

On the supported domain the magnitude stays well below 1 (the 1/S² factor at an $1,000-scale spot crushes it), so the suite enforces an absolute error only — 1e-15 — 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 gammaCall, int128 gammaPut) = BinaryOptions.gamma(
    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
);
// gammaCall signed (sign flips across the strike), gammaPut = -gammaCall