Skip to content

compute_statistics


function backtide.analysis.compute_statistics(data, price_col="adj_close", risk_free_rate=0.0, periods_per_year=None)

Compute per-symbol summary statistics.

Calculates key performance and risk metrics for each symbol in data. All metrics are annualized based on the detected or specified trading frequency.

Parameters

data : pd.DataFrame | pl.DataFrame

Input data containing columns symbol, the column specified by price_col, and dt with the datetime.

price_col : str, default="close"
Column name used to compute returns.

risk_free_rate : float, default=0.0
Annualized risk-free rate used in Sharpe and Sortino ratio calculations.

periods_per_year : int | None, default=None
Number of trading periods per year for annualization. If None, it is estimated from the median time delta between bars (e.g., 252 for daily data).

Returns

pd.DataFrame | pl.DataFrame

Dataset with one row per symbol and columns for each metric.


See Also

plot_returns

Create a returns distribution histogram.

plot_drawdown

Create a drawdown chart.

plot_mae_mfe

Plot Maximum Adverse Excursion vs Maximum Favourable Excursion per trade.


Example

>>> from backtide.storage import query_bars
>>> from backtide.analysis import compute_statistics

>>> df = query_bars(["AAPL", "MSFT"], "1d")
>>> stats = compute_statistics(df)
>>> print(stats.head())

  symbol    sharpe      cagr  ...  ann_volatility   sortino  total_bars
0   AAPL  0.626975  0.193578  ...        0.437527  0.862337       11452
1   MSFT  0.831927  0.246911  ...        0.331730  1.163854       10126

[2 rows x 8 columns]