Implementation:AI4Finance Foundation FinGPT Pyfolio Utils
| Knowledge Sources | |
|---|---|
| Domains | Portfolio_Analytics, Trading, Utilities |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for shared constants, formatting helpers, and table display utilities used across the vendored pyfolio package within the FinGPT Trading subproject.
Description
The utils module defines business-day constants (`APPROX_BDAYS_PER_MONTH`, `APPROX_BDAYS_PER_YEAR`), period strings (`DAILY`, `WEEKLY`, `MONTHLY`, `YEARLY`), annualization factors, a color palette, axis formatters (`one_dec_places`, `two_dec_places`, `percentage`), and high-level helpers like `print_table`, `configure_legend`, and `format_asset` used by the plotting, tears, and round_trips modules.
Usage
Import this module when you need reusable formatting or constants for pyfolio-style analytics, or when building custom tear-sheet components that share pyfolio's styling conventions.
Code Reference
Source Location
- Repository: AI4Finance_Foundation_FinGPT
- File: fingpt/FinGPT_Others/FinGPT_Trading/chatgpt-trading-v2/pyfolio/utils.py
- Lines: 1-541
Signature
APPROX_BDAYS_PER_MONTH = 21
APPROX_BDAYS_PER_YEAR = 252
MM_DISPLAY_UNIT = 1000000.0
DAILY = 'daily'; WEEKLY = 'weekly'; MONTHLY = 'monthly'; YEARLY = 'yearly'
ANNUALIZATION_FACTORS = {...}
COLORS = [...]
def one_dec_places(x, pos) -> str: ...
def two_dec_places(x, pos) -> str: ...
def percentage(x, pos) -> str: ...
def format_asset(asset) -> str: ...
def format_percent(float) -> str: ...
def print_table(table, name=None, float_format='{:.2f}'.format) -> None: ...
def print_return_percentages(...): ...
def configure_legend(ax, ...) -> None: ...
def sticky_header(table, ...) -> None: ...
def extract_rets_pos_txn_from_zipline(backtest): ...
def convert_to_daily_returns(...): ...
def strip_nans(...): ...
def default_returns_func(...): ...
Import
from pyfolio.utils import print_table, APPROX_BDAYS_PER_YEAR, DAILY, COLORS
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| table | pd.DataFrame | Yes (print_table) | DataFrame to render as formatted output |
| float_format | callable | No | Format function for floats |
| ax | matplotlib.Axes | Varies | Axes for configure_legend |
Outputs
| Name | Type | Description |
|---|---|---|
| print_table output | None | Renders formatted table to stdout/IPython display |
| formatters | callable | Axis formatter functions for matplotlib |
Usage Examples
Render a Summary Table
import pandas as pd
from pyfolio.utils import print_table
summary = pd.DataFrame({
"Sharpe": [1.8423],
"Max DD": [-0.1245],
}, index=["Strategy"])
print_table(summary, name="Performance Summary")