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:
co-authored by
Claude Sonnet 4.6
parent
2d5000262f
commit
602d675749
@@ -0,0 +1,34 @@
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[pyfunction]
|
||||
pub fn futures_basis(spot: f64, future: f64) -> PyResult<f64> {
|
||||
Ok(ferro_ta_core::futures::basis::basis(spot, future))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn annualized_basis(spot: f64, future: f64, time_to_expiry: f64) -> PyResult<f64> {
|
||||
Ok(ferro_ta_core::futures::basis::annualized_basis(
|
||||
spot,
|
||||
future,
|
||||
time_to_expiry,
|
||||
))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn implied_carry_rate(spot: f64, future: f64, time_to_expiry: f64) -> PyResult<f64> {
|
||||
Ok(ferro_ta_core::futures::basis::implied_carry_rate(
|
||||
spot,
|
||||
future,
|
||||
time_to_expiry,
|
||||
))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn carry_spread(spot: f64, future: f64, rate: f64, time_to_expiry: f64) -> PyResult<f64> {
|
||||
Ok(ferro_ta_core::futures::basis::carry_spread(
|
||||
spot,
|
||||
future,
|
||||
rate,
|
||||
time_to_expiry,
|
||||
))
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
use crate::validation;
|
||||
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[pyfunction]
|
||||
pub fn calendar_spreads<'py>(
|
||||
py: Python<'py>,
|
||||
futures_prices: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<Bound<'py, PyArray1<f64>>> {
|
||||
Ok(
|
||||
ferro_ta_core::futures::curve::calendar_spreads(futures_prices.as_slice()?)
|
||||
.into_pyarray(py),
|
||||
)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn curve_slope<'py>(
|
||||
tenors: PyReadonlyArray1<'py, f64>,
|
||||
futures_prices: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<f64> {
|
||||
let tenors = tenors.as_slice()?;
|
||||
let futures_prices = futures_prices.as_slice()?;
|
||||
validation::validate_equal_length(&[
|
||||
(tenors.len(), "tenors"),
|
||||
(futures_prices.len(), "futures_prices"),
|
||||
])?;
|
||||
Ok(ferro_ta_core::futures::curve::curve_slope(
|
||||
tenors,
|
||||
futures_prices,
|
||||
))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn curve_summary<'py>(
|
||||
spot: f64,
|
||||
tenors: PyReadonlyArray1<'py, f64>,
|
||||
futures_prices: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<(f64, f64, f64, bool)> {
|
||||
let tenors = tenors.as_slice()?;
|
||||
let futures_prices = futures_prices.as_slice()?;
|
||||
validation::validate_equal_length(&[
|
||||
(tenors.len(), "tenors"),
|
||||
(futures_prices.len(), "futures_prices"),
|
||||
])?;
|
||||
let summary = ferro_ta_core::futures::curve::curve_summary(spot, tenors, futures_prices);
|
||||
Ok((
|
||||
summary.front_basis,
|
||||
summary.average_basis,
|
||||
summary.slope,
|
||||
summary.is_contango,
|
||||
))
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//! PyO3 wrappers for futures analytics.
|
||||
|
||||
mod basis;
|
||||
mod curve;
|
||||
mod roll;
|
||||
mod synthetic;
|
||||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_function(pyo3::wrap_pyfunction!(
|
||||
self::synthetic::synthetic_forward,
|
||||
m
|
||||
)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::synthetic::synthetic_spot, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::synthetic::parity_gap, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::basis::futures_basis, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::basis::annualized_basis, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::basis::implied_carry_rate, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::basis::carry_spread, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(
|
||||
self::roll::weighted_continuous_contract,
|
||||
m
|
||||
)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(
|
||||
self::roll::back_adjusted_continuous_contract,
|
||||
m
|
||||
)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(
|
||||
self::roll::ratio_adjusted_continuous_contract,
|
||||
m
|
||||
)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::roll::roll_yield, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::curve::calendar_spreads, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::curve::curve_slope, m)?)?;
|
||||
m.add_function(pyo3::wrap_pyfunction!(self::curve::curve_summary, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
use crate::validation;
|
||||
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[pyfunction]
|
||||
pub fn weighted_continuous_contract<'py>(
|
||||
py: Python<'py>,
|
||||
front: PyReadonlyArray1<'py, f64>,
|
||||
next: PyReadonlyArray1<'py, f64>,
|
||||
next_weights: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<Bound<'py, PyArray1<f64>>> {
|
||||
let front = front.as_slice()?;
|
||||
let next = next.as_slice()?;
|
||||
let next_weights = next_weights.as_slice()?;
|
||||
validation::validate_equal_length(&[
|
||||
(front.len(), "front"),
|
||||
(next.len(), "next"),
|
||||
(next_weights.len(), "next_weights"),
|
||||
])?;
|
||||
Ok(
|
||||
ferro_ta_core::futures::roll::weighted_continuous(front, next, next_weights)
|
||||
.into_pyarray(py),
|
||||
)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn back_adjusted_continuous_contract<'py>(
|
||||
py: Python<'py>,
|
||||
front: PyReadonlyArray1<'py, f64>,
|
||||
next: PyReadonlyArray1<'py, f64>,
|
||||
next_weights: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<Bound<'py, PyArray1<f64>>> {
|
||||
let front = front.as_slice()?;
|
||||
let next = next.as_slice()?;
|
||||
let next_weights = next_weights.as_slice()?;
|
||||
validation::validate_equal_length(&[
|
||||
(front.len(), "front"),
|
||||
(next.len(), "next"),
|
||||
(next_weights.len(), "next_weights"),
|
||||
])?;
|
||||
Ok(
|
||||
ferro_ta_core::futures::roll::back_adjusted_continuous(front, next, next_weights)
|
||||
.into_pyarray(py),
|
||||
)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn ratio_adjusted_continuous_contract<'py>(
|
||||
py: Python<'py>,
|
||||
front: PyReadonlyArray1<'py, f64>,
|
||||
next: PyReadonlyArray1<'py, f64>,
|
||||
next_weights: PyReadonlyArray1<'py, f64>,
|
||||
) -> PyResult<Bound<'py, PyArray1<f64>>> {
|
||||
let front = front.as_slice()?;
|
||||
let next = next.as_slice()?;
|
||||
let next_weights = next_weights.as_slice()?;
|
||||
validation::validate_equal_length(&[
|
||||
(front.len(), "front"),
|
||||
(next.len(), "next"),
|
||||
(next_weights.len(), "next_weights"),
|
||||
])?;
|
||||
Ok(
|
||||
ferro_ta_core::futures::roll::ratio_adjusted_continuous(front, next, next_weights)
|
||||
.into_pyarray(py),
|
||||
)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn roll_yield(front_price: f64, next_price: f64, time_to_expiry: f64) -> PyResult<f64> {
|
||||
Ok(ferro_ta_core::futures::roll::roll_yield(
|
||||
front_price,
|
||||
next_price,
|
||||
time_to_expiry,
|
||||
))
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[pyfunction]
|
||||
pub fn synthetic_forward(
|
||||
call_price: f64,
|
||||
put_price: f64,
|
||||
strike: f64,
|
||||
rate: f64,
|
||||
time_to_expiry: f64,
|
||||
) -> PyResult<f64> {
|
||||
Ok(ferro_ta_core::futures::synthetic::synthetic_forward(
|
||||
call_price,
|
||||
put_price,
|
||||
strike,
|
||||
rate,
|
||||
time_to_expiry,
|
||||
))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (call_price, put_price, strike, rate, time_to_expiry, carry = 0.0))]
|
||||
pub fn synthetic_spot(
|
||||
call_price: f64,
|
||||
put_price: f64,
|
||||
strike: f64,
|
||||
rate: f64,
|
||||
time_to_expiry: f64,
|
||||
carry: f64,
|
||||
) -> PyResult<f64> {
|
||||
Ok(ferro_ta_core::futures::synthetic::synthetic_spot(
|
||||
call_price,
|
||||
put_price,
|
||||
strike,
|
||||
rate,
|
||||
carry,
|
||||
time_to_expiry,
|
||||
))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (call_price, put_price, spot, strike, rate, time_to_expiry, carry = 0.0))]
|
||||
pub fn parity_gap(
|
||||
call_price: f64,
|
||||
put_price: f64,
|
||||
spot: f64,
|
||||
strike: f64,
|
||||
rate: f64,
|
||||
time_to_expiry: f64,
|
||||
carry: f64,
|
||||
) -> PyResult<f64> {
|
||||
Ok(ferro_ta_core::futures::synthetic::parity_gap(
|
||||
call_price,
|
||||
put_price,
|
||||
spot,
|
||||
strike,
|
||||
rate,
|
||||
carry,
|
||||
time_to_expiry,
|
||||
))
|
||||
}
|
||||
Reference in New Issue
Block a user