breakeven_fee

Defined in fynance.backtest

breakeven_fee(weights, X, period=252, tol=1e-6)[source]

Proportional fee at which the net Sharpe ratio crosses zero.

Bisects on a ProportionalCost fee: the bracket’s upper bound is doubled from a small seed until the net Sharpe turns non-positive, capped at _FEE_CAP (10% per trade traded).

Parameters:
weightsarray-like

Position/weight book, shape (T,) or (T, N).

Xarray-like

Price levels aligned with weights; passed to backtest as returns_input=False.

periodint

Annualization factor for the Sharpe ratio.

tolfloat

Bisection tolerance on the fee (absolute).

Returns:
float

The proportional fee at which the net Sharpe ratio is (approximately) zero.

Raises:
ValueError

If the gross (fee=0) Sharpe ratio is already non-positive (message 'strategy unprofitable gross'), or if the net Sharpe ratio is still positive at the 10%-per-trade cap (message 'no breakeven below 10%').

Examples

Same planted-edge, noisy-turnover strategy as in capacity_curve: a proportional fee somewhere below the 10%-per-trade cap wipes out the net Sharpe ratio.

>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> steps = np.cumsum(rng.normal(0.0, 0.07, size=1000))
>>> weights = np.clip(steps, -1.0, 1.0)
>>> noise = rng.normal(0.0, 0.01, size=1000)
>>> returns = np.empty(1000)
>>> returns[0] = noise[0]
>>> returns[1:] = 0.0006 * weights[:-1] + noise[1:]
>>> prices = 100.0 * np.cumprod(1.0 + returns)
>>> fee = breakeven_fee(weights, prices)
>>> bool(0.0 < fee < 0.10)
True