Implementation:AI4Finance Foundation FinGPT Pyfolio Txn
| 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
- Repository: AI4Finance_Foundation_FinGPT
- File: fingpt/FinGPT_Others/FinGPT_Trading/chatgpt-trading-v2/pyfolio/txn.py
- Lines: 1-206
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())