feat: add full derivatives analytics layer (options + futures)

Implements all phases of the derivatives expansion plan:

Rust core (crates/ferro_ta_core/src/options/, src/futures/):
- BSM and Black-76 pricing (scalar + vectorized batch)
- Greeks: delta, gamma, vega, theta, rho
- Implied volatility solver (Newton + bisection fallback)
- Smile/skew metrics: ATM IV, 25-delta RR/BF, skew slope, convexity
- Chain helpers: moneyness labels, strike selection by offset or delta
- Synthetic forwards, basis, annualized basis, implied carry, carry spread
- Continuous contract stitching: weighted, back-adjusted, ratio-adjusted
- Curve analytics: calendar spreads, slope, contango/backwardation summary

PyO3 bindings (src/options/, src/futures/):
- All Rust functions registered and exposed via _ferro_ta extension

Python API (python/ferro_ta/analysis/):
- options.py: pricing, greeks, IV, smile, chain, legacy iv_rank/percentile/zscore
- futures.py: basis, carry, curve, roll, synthetic, continuous contracts
- options_strategy.py: typed strategy schemas (expiry/strike selectors, leg presets, risk controls, simulation limits)
- derivatives_payoff.py: multi-leg payoff aggregation and Greeks aggregation

Bug fix: wrap _to_f64 calls in iv_rank/iv_percentile/iv_zscore to raise
FerroTAInputError (not plain ValueError) for 2D array input.

Docs: derivatives.rst, derivatives-analytics.md, options-volatility.md,
quickstart.rst, index.rst, api/analysis.rst all updated.

Tests: 2053 pass, 12 skipped. All CI checks pass locally.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Pratik Bhadane
2026-03-24 02:41:50 +05:30
co-authored by Claude Sonnet 4.6
parent 2d5000262f
commit 602d675749
47 changed files with 4538 additions and 280 deletions
+50 -72
View File
@@ -1,101 +1,79 @@
# Options and Implied Volatility
ferro-ta provides optional helpers for implied volatility (IV) analysis
via the `ferro_ta.options` module. This document describes the scope,
data format, dependency strategy, and limitations.
---
`ferro-ta` exposes options analytics from `ferro_ta.analysis.options`.
## Scope
The `ferro_ta.options` module focuses on **IV series analysis**:
The module now covers both classic IV-series helpers and model-based option
analytics:
- **IV rank** — where today's IV sits relative to the min/max over a look-back window.
- **IV percentile** — fraction of observations over a look-back window at or below today's IV.
- **IV z-score** — how many standard deviations today's IV is above the rolling mean.
- `iv_rank`, `iv_percentile`, `iv_zscore`
- Black-Scholes-Merton pricing
- Black-76 pricing
- Delta, gamma, vega, theta, rho
- Implied volatility inversion
- Smile metrics and chain helpers
These functions accept any 1-D IV series (e.g. VIX daily closes, single-name
30-day IV, etc.) and return rolling statistics.
Heavy computation runs in Rust through the `_ferro_ta` extension.
**Out of scope (for now):** Black-Scholes pricing, Greeks, option chain
parsing, synthetic forward construction, dividend adjustment. For full
option-pricing functionality consider `py_vollib`, `mibian`, or similar.
## IV-series helpers
---
## Data format
All functions accept a 1-D NumPy array (or any array-like) of IV values.
IV values are typically in **percentage points** (e.g. VIX = 20 means 20%
annualised volatility), but the helpers are unit-agnostic — they only
compare values within the rolling window.
The original rolling helpers remain available and keep their public names:
```python
import numpy as np
from ferro_ta.options import iv_rank, iv_percentile, iv_zscore
from ferro_ta.analysis.options import iv_rank, iv_percentile, iv_zscore
# VIX-like daily close series
iv = np.array([18.5, 22.3, 19.1, 25.0, 30.2, 27.8, 21.4, 19.0])
rank = iv_rank(iv, window=5) # rolling IV rank in [0, 1]
pct = iv_percentile(iv, window=5) # rolling IV percentile in [0, 1]
z = iv_zscore(iv, window=5) # rolling z-score
rank = iv_rank(iv, window=5)
pct = iv_percentile(iv, window=5)
z = iv_zscore(iv, window=5)
```
---
These helpers accept a 1-D IV series and return rolling statistics with
`NaN` during the warmup period.
## Dependency strategy
## Pricing and Greeks
The `ferro_ta.options` module uses **only NumPy** (already a core dependency).
No additional packages are required for the helpers described here.
```python
from ferro_ta.analysis.options import greeks, implied_volatility, option_price
For advanced option analytics (Black-Scholes, volatility surface
interpolation), install the optional extra:
```bash
pip install "ferro-ta[options]"
price = option_price(100.0, 100.0, 0.05, 1.0, 0.20, option_type="call")
iv = implied_volatility(price, 100.0, 100.0, 0.05, 1.0, option_type="call")
g = greeks(100.0, 100.0, 0.05, 1.0, 0.20, option_type="call")
```
This may install additional packages in the future (e.g. `py_vollib`).
Conventions:
---
- Volatility is decimal annualized volatility: `0.20` means 20%.
- Rates are decimal annualized rates: `0.05` means 5%.
- `time_to_expiry` is measured in years.
- `model="bsm"` uses spot as the underlying input.
- `model="black76"` uses forward as the underlying input.
## API reference
## Smile and chain helpers
### `iv_rank(iv_series, window=252)`
```python
from ferro_ta.analysis.options import label_moneyness, select_strike, smile_metrics
Rolling IV rank.
strikes = [80, 90, 100, 110, 120]
vols = [0.30, 0.25, 0.20, 0.22, 0.27]
```
rank_t = (IV_t - min(IV[t-window+1:t+1])) / (max(IV[t-window+1:t+1]) - min(IV[t-window+1:t+1]))
metrics = smile_metrics(strikes, vols, 100.0, 0.5)
labels = label_moneyness(strikes, 100.0, option_type="call")
atm = select_strike(strikes, 100.0, selector="ATM")
delta_strike = select_strike(
strikes,
100.0,
selector="DELTA0.25",
option_type="call",
volatilities=vols,
time_to_expiry=0.5,
)
```
Returns values in [0, 1]. NaN for the first `window - 1` bars.
## Related futures analytics
### `iv_percentile(iv_series, window=252)`
Rolling IV percentile: fraction of the *window* bars whose IV was at or
below the current value.
### `iv_zscore(iv_series, window=252)`
Rolling z-score: `(IV_t - rolling_mean) / rolling_std`.
---
## Limitations
- All functions use **O(n × window)** time complexity (pure Python loops).
For large windows or series consider vectorised alternatives.
- No option chain support; the module assumes IV series as input.
- Streaming (bar-by-bar) versions of these functions are not yet
implemented. For live use, maintain a rolling buffer and call the
functions on the buffer at each bar.
---
## See also
- `ferro_ta.options` — module source.
- `ferro_ta.statistic` — general statistical functions (STDDEV, VAR, CORREL, etc.).
- `ferro_ta.volatility` — price-based volatility indicators (ATR, NATR).
See `ferro_ta.analysis.futures` and
[`docs/derivatives-analytics.md`](./derivatives-analytics.md) for synthetic
forwards, basis, carry, curve, and roll analytics.