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 Txn

From Leeroopedia
Knowledge Sources
Domains Transaction_Analysis, Portfolio_Analytics, Trading
Last Updated 2026-10-18 00:00 GMT

Overview

Concrete tool for processing transaction data into standardized DataFrames and computing turnover/slippage metrics provided by the vendored pyfolio package within the FinGPT Trading subproject.

Description

The txn module maps individual transaction records into a standardized DataFrame, computes daily turnover, and estimates slippage. `map_transaction` normalizes a single row; `make_transaction_frame` assembles many rows; `get_turnover` computes turnover as traded value divided by portfolio value; `adjust_returns_for_slippage` removes slippage from returns.

Usage

Import this module when you need to compute transaction turnover, format trade logs, or adjust returns for slippage.

Code Reference

Source Location

Signature

def map_transaction(txn) -> dict:
    """Map a single transaction row to a standardized dict."""

def make_transaction_frame(transactions) -> pd.DataFrame:
    """Format a list of transaction dicts into a DataFrame."""

def get_turnover(transactions, positions, period='1D', ...):
    """Compute turnover = traded value / portfolio value."""

def adjust_returns_for_slippage(returns, transactions, positions, ...):
    """Remove estimated slippage from returns."""

def get_txn_vol(transactions) -> pd.DataFrame: ...

Import

from pyfolio.txn import get_turnover, make_transaction_frame, adjust_returns_for_slippage

I/O Contract

Inputs

Name Type Required Description
transactions list/pd.DataFrame Yes Trade records with sid, price, amount, dt
positions pd.DataFrame Yes (turnover) Daily position values
returns pd.Series Yes (slippage) Daily returns

Outputs

Name Type Description
turnover pd.Series Daily turnover ratio
adjusted_returns pd.Series Returns net of slippage

Usage Examples

Compute Turnover

import pandas as pd
from pyfolio.txn import get_turnover

transactions = pd.read_csv("txn.csv", parse_dates=["dt"], index_col="dt")
positions = pd.read_csv("positions.csv", index_col=0, parse_dates=True)

turnover = get_turnover(transactions, positions)
print("Avg turnover:", turnover.mean())

Related Pages

Page Connections

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