Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AI4Finance Foundation FinGPT Pyfolio Timeseries

From Leeroopedia
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

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)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment