BacktestResult

Defined in fynance.backtest

class BacktestResult(equity, returns, gross_returns, positions, costs, index=None, asset_gross_returns=None, cost_components=None)[source]

Bases: object

Output 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 to gross_returns); None for a single-asset run.

cost_componentsdict of str to numpy.ndarray, optional

Per-step cost broken down by component (e.g. transaction vs market_impact); the values sum to costs. Populated when the cost model exposes the optional components convention, else None.

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 — see fynance.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_returns and costs. positions becomes a single positions column for a single-asset (T,) book, or pos_0..``pos_{N-1}`` columns for a multi-asset (T, N) book. When present, asset_gross_returns similarly expands into asset_gross_return_0..``asset_gross_return_{N-1}`` columns, and each cost_components entry becomes a cost_<name> column. The DataFrame index is index when set, else pandas’ default RangeIndex.

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_summary on trades.

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_trades on positions and returns, 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.