delta

Black-Scholes

Computes Delta for both call and put options using the Black-Scholes model (sensitivity to spot price change).

Avg. gas

1,661

Max abs. error

1.2e-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
deltaCallint128Call delta in 18-decimal fixed-point. δcall ∈ [0, 1].
deltaPutint128Put delta in 18-decimal fixed-point. δput = δcall − 1 ∈ [−1, 0].

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 a single Φ(d₁) evaluation — δput = δcall − 1 by put-call parity, 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). The MIN_VOL_IV / MAX_VOL_IV constants apply only to the impliedVolatility solver, not the greeks.
  • Fast-path on expiration: when timeToExp == 0, delta collapses to its degenerate expiry value (0 or ±1 by moneyness) without running the pricer.
  • Composes three DeFiMath primitives — ln, sqrtTime (specialized sqrt for years), and stdNormCDF. Delta needs no discount factor, so there is no exp call.
  • Pure internal function; no external calls, no storage. Inlined into the caller's bytecode at compile time.

How it works

delta is the first derivative of option value with respect to spot. Under Black-Scholes it is simply the standard normal CDF of d₁:

δcall=Φ(d1),δput=Φ(d1)1\delta_{call} = \Phi(d_1), \qquad \delta_{put} = \Phi(d_1) - 1
d1=ln(S/K)+(r+σ22)TσTd_1 = \frac{\ln(S/K) + \left(r + \tfrac{\sigma^2}{2}\right) T}{\sigma \sqrt{T}}

The function annualizes timeToExp (seconds) by dividing by SECONDS_IN_YEAR, scales volatility by √T with Math.sqrtTime, forms ln(spot/strike) with Math.ln, and evaluates Φ(d₁) with Math.stdNormCDF. Because put delta differs from call delta by exactly 1, both are returned from one CDF evaluation — roughly halving gas versus computing them separately.

Delta is bounded to [−1, 1], so the suite enforces an absolute error only — no relative bound. The 1.2e-13 max absolute error is the bound enforced at spot = $1,000 across a full sweep of strike, time, vol, and rate — head-to-head measurements against other libraries 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/BlackScholes.sol";

(int128 deltaCall, int128 deltaPut) = BlackScholes.delta(
    1000e18,         // spot = $1,000
    980e18,          // strike = $980
    60 days,         // 60 days to expiry
    0.60e18,         // 60% annualized vol
    0.05e18          // 5% risk-free rate
);
// deltaCall ≈ 0.58e18, deltaPut ≈ -0.42e18