Implementation:AI4Finance Foundation FinGPT Pyfolio Pos
| Knowledge Sources | |
|---|---|
| Domains | Position_Analysis, Portfolio_Analytics, Trading |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for computing portfolio allocation percentages, top long/short positions, and exposure metrics from position data provided by the vendored pyfolio package within the FinGPT Trading subproject.
Description
The pos module provides position-analysis utilities: `get_percent_alloc` computes percentage allocations from dollar positions, `get_top_long_short_abs` finds the largest long/short/absolute positions, `get_exposures` computes gross/net/short exposure over time, and `get_max_median_drawdown` calculates drawdown statistics. It optionally uses zipline asset multipliers for futures.
Usage
Import this module when you need to analyze portfolio allocation, concentration, or exposure from a position DataFrame.
Code Reference
Source Location
- Repository: AI4Finance_Foundation_FinGPT
- File: fingpt/FinGPT_Others/FinGPT_Trading/chatgpt-trading-v2/pyfolio/pos.py
- Lines: 1-236
Signature
def get_percent_alloc(values) -> pd.DataFrame: ...
def get_top_long_short_abs(positions, top=10) -> pd.DataFrame: ...
def get_top_long_short_pos(positions, top=10) -> pd.DataFrame: ...
def get_exposures(positions) -> pd.DataFrame: ...
def get_max_drawdowns(positions) -> pd.DataFrame: ...
def get_max_median_drawdowns(...): ...
def extract_pos(positions, cash_metric='cash'): ...
def asset_sector_distribution(...): ...
def asset_sector_allocation(...): ...
Import
from pyfolio.pos import get_percent_alloc, get_top_long_short_abs, get_exposures
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| positions | pd.DataFrame | Yes | Daily dollar position values, date-indexed |
| top | int | No | Number of top positions to return (default 10) |
Outputs
| Name | Type | Description |
|---|---|---|
| allocations | pd.DataFrame | Percentage allocations |
| top_positions | pd.DataFrame | Ranked long/short/absolute positions |
| exposures | pd.DataFrame | Gross, net, short exposure over time |
Usage Examples
Analyze Portfolio Exposure
import pandas as pd
from pyfolio.pos import get_percent_alloc, get_exposures
positions = pd.read_csv("positions.csv", index_col=0, parse_dates=True)
alloc = get_percent_alloc(positions.drop(columns=["cash"]))
exposures = get_exposures(positions)
print("Gross exposure:\n", exposures["gross"].tail())
print("Net exposure:\n", exposures["net"].tail())