BacktestResult¶
Defined in fynance.backtest
- class BacktestResult(equity, returns, gross_returns, positions, costs, index=None, asset_gross_returns=None, cost_components=None)[source]
Bases:
objectOutput of
backtest.- Attributes:
- equitynumpy.ndarray
Equity curve.
- returnsnumpy.ndarray
Net strategy returns (after costs).
- gross_returnsnumpy.ndarray
Strategy returns before costs.
- positionsnumpy.ndarray
Position/weight book used.
- costsnumpy.ndarray
Per-step transaction costs.
- indexnumpy.ndarray, optional
Temporal index carried from the input.
- asset_gross_returnsnumpy.ndarray, optional
Per-asset gross return contributions
(T, N)for a multi-asset book (they sum togross_returns);Nonefor a single-asset run.- cost_componentsdict of str to numpy.ndarray, optional
Per-step cost broken down by component (e.g.
transactionvsmarket_impact); the values sum tocosts. Populated when the cost model exposes the optionalcomponentsconvention, elseNone.
Methods
to_numpy()Return the equity curve as a numpy array.
to_price_series()Return the equity curve as a
PriceSeries.to_pandas()Return this result as a
pandas.DataFrame(lazy import).summary([period])Standard performance summary.
trades()Round-trip trades extracted from the strategy's own arrays.
trade_summary()Summary statistics of the strategy's round-trip trades.
- summary(period=252)[source]
Standard performance summary.
Delegates the risk-adjusted ratios and drawdown to
fynance.metrics.summary(computed on the equity curve) and adds, from the strategy’s own data, the hit-rate, total transaction cost and the trading-profile churn (n_sign_changes/trades_per_year, summed over the book — seefynance.metrics.sign_changes).
- to_numpy()[source]
Return the equity curve as a numpy array.
- to_pandas()[source]
Return this result as a
pandas.DataFrame(lazy import).One row per time step. Always carries
equity,returns,gross_returnsandcosts.positionsbecomes a singlepositionscolumn for a single-asset(T,)book, orpos_0..``pos_{N-1}`` columns for a multi-asset(T, N)book. When present,asset_gross_returnssimilarly expands intoasset_gross_return_0..``asset_gross_return_{N-1}`` columns, and eachcost_componentsentry becomes acost_<name>column. The DataFrame index isindexwhen set, else pandas’ defaultRangeIndex.- Raises:
- ImportError
If pandas is not installed, with a clear, actionable message.
Examples
>>> from fynance.backtest import backtest >>> res = backtest(np.array([0.01, 0.02, -0.01]), np.ones(3)) >>> list(res.to_pandas().columns) ['equity', 'returns', 'gross_returns', 'costs', 'positions']
- to_price_series()[source]
Return the equity curve as a
PriceSeries.
- trade_summary()[source]
Summary statistics of the strategy’s round-trip trades.
Delegates to
fynance.metrics.trades.trade_summaryontrades.- Returns:
- dict of str to float
See
trade_summary.
- trades()[source]
Round-trip trades extracted from the strategy’s own arrays.
Delegates to
fynance.metrics.trades.extract_tradesonpositionsandreturns, taken exactly as stored (see that function’s docstring for the alignment convention this relies on).- Returns:
- numpy.ndarray
Structured array, one row per trade – see
extract_trades.