Implementation:AI4Finance Foundation FinGPT Pyfolio Timeseries
| Knowledge Sources | |
|---|---|
| Domains | Risk_Metrics, Portfolio_Analytics, Trading |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for computing time-series risk and performance metrics (Sharpe, Sortino, drawdown, VaR, Calmar) for trading strategies provided by the vendored pyfolio package within the FinGPT Trading subproject.
Description
The timeseries module wraps empyrical functions and adds pyfolio-specific helpers: rolling metrics, bootstrap tests, Var/Covar VaR, and the `calc_beta` helper used in tear sheets. Many functions are decorated with `@deprecated` because they delegate to the empyrical package.
⚠️ Deprecation Notice: 17 of the public functions in this module (including `sharpe_ratio`, `sortino_ratio`, `max_drawdown`, `annual_return`, `annual_volatility`, `calmar_ratio`, `downside_risk`, `var_cov_var_normal`, `rolling_beta`, and `calc_beta`) carry the `@deprecated` decorator and emit a `DEPRECATION_WARNING` on every call directing users to the `empyrical` package. New code should prefer `empyrical` directly. See Heuristic:AI4Finance_Foundation_FinGPT_Warning_Deprecated_Pyfolio_Timeseries.
Usage
Import this module when you need to compute summary statistics or rolling risk metrics from a returns time series, or when building a custom performance dashboard.
Code Reference
Source Location
- Repository: AI4Finance_Foundation_FinGPT
- File: fingpt/FinGPT_Others/FinGPT_Trading/chatgpt-trading-v2/pyfolio/timeseries.py
- Lines: 1-1237
Signature
def var_cov_var_normal(P, c, mu=0, sigma=1) -> float: ...
def max_drawdown(returns) -> float: ...
def annual_return(returns, period=DAILY) -> float: ...
def annual_volatility(returns, period=DAILY, alpha=2.0) -> float: ...
def sharpe_ratio(returns, risk_free=0, period=DAILY) -> float: ...
def calmar_ratio(returns, period=DAILY) -> float: ...
def sortino_ratio(returns, required_return=0, period=DAILY) -> float: ...
def downside_risk(returns, required_return=0, period=DAILY) -> float: ...
def capture(returns, factor_returns, period=DAILY) -> float: ...
def up_capture(returns, factor_returns, **kwargs) -> float: ...
def down_capture(returns, factor_returns, **kwargs) -> float: ...
def out_of_sample_vs_in_sample_returns(...): ...
def rolling_beta(returns, factor_returns, rolling_window=...) -> pd.DataFrame: ...
def rolling_fama_factor(...): ...
def plot_rolling_fama_decomposition(...): ...
def calc_beta(returns, factor) -> float: ...
def fragility_index(...): ...
def to_sector_mapping(...): ...
Import
from pyfolio.timeseries import sharpe_ratio, max_drawdown, sortino_ratio, rolling_beta
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| returns | pd.Series | Yes | Daily noncumulative strategy returns |
| period | str | No | DAILY / WEEKLY / MONTHLY (default DAILY) |
| factor_returns | pd.Series | Varies | Benchmark returns for beta / capture |
| rolling_window | int | No | Window size for rolling metrics |
Outputs
| Name | Type | Description |
|---|---|---|
| metric | float | Scalar risk/performance metric |
| rolling_frame | pd.DataFrame | Rolling series (for rolling_beta etc.) |
Usage Examples
Compute Sharpe and Rolling Beta
import pandas as pd
from pyfolio.timeseries import sharpe_ratio, max_drawdown, rolling_beta
returns = pd.read_csv("returns.csv", index_col=0, parse_dates=True)["returns"]
benchmark = pd.read_csv("spy.csv", index_col=0, parse_dates=True)["returns"]
print("Sharpe:", sharpe_ratio(returns))
print("Max DD:", max_drawdown(returns))
rb = rolling_beta(returns, benchmark, rolling_window=63)