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 Pos

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

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())

Related Pages

Page Connections

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