Implementation:AI4Finance Foundation FinGPT Pyfolio Round Trips
| Knowledge Sources | |
|---|---|
| Domains | Trade_Analysis, Portfolio_Analytics, Trading |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for reconstructing round-trip trades from transaction records and computing per-trade profit, return, and duration statistics provided by the vendored pyfolio package within the FinGPT Trading subproject.
Description
The round_trips module pairs sequential buy and sell transactions into round-trip trades, then aggregates statistics (profit factor, win rate, average win/loss, duration) split by all/long/short categories. It defines four stat dictionaries (`PNL_STATS`, `SUMMARY_STATS`, `RETURN_STATS`, `DURATION_STATS`) used as pandas groupby aggregation specs.
Usage
Import this module when you need to analyze the quality and distribution of individual completed trades rather than aggregate daily returns.
Code Reference
Source Location
- Repository: AI4Finance_Foundation_FinGPT
- File: fingpt/FinGPT_Others/FinGPT_Trading/chatgpt-trading-v2/pyfolio/round_trips.py
- Lines: 1-414
Signature
PNL_STATS = OrderedDict(...)
SUMMARY_STATS = OrderedDict(...)
RETURN_STATS = OrderedDict(...)
DURATION_STATS = OrderedDict(...)
def agg_all_long_short(round_trips, col, stats_dict) -> pd.DataFrame: ...
def _groupby_consecutive(txn, max_delta=pd.Timedelta('8h')) -> pd.DataFrame: ...
def extract_round_trips(transactions, product=False) -> pd.DataFrame: ...
def add_long_short_round_trips(round_trips) -> pd.DataFrame: ...
def make_round_trip_portfolio(round_trips, returns) -> pd.DataFrame: ...
def summarize_round_trip_activity(round_trips) -> pd.DataFrame: ...
def round_trip_decay(round_trips, turnover, returns) -> pd.DataFrame: ...
def probs_after_round_trip(round_trips, input_round_trips=None) -> pd.DataFrame: ...
def show_round_trip_life(round_trips, ax=None) -> matplotlib.Axes: ...
Import
from pyfolio.round_trips import extract_round_trips, summarize_round_trip_activity
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| transactions | pd.DataFrame | Yes | DataFrame with columns: dt, sid, amount, price |
| returns | pd.Series | Varies | Daily returns (for portfolio reconstruction) |
| max_delta | pd.Timedelta | No | Max gap for grouping consecutive trades (default 8h) |
Outputs
| Name | Type | Description |
|---|---|---|
| round_trips | pd.DataFrame | One row per completed trade with pnl, returns, open_dt, close_dt, long |
| summary | pd.DataFrame | Aggregated stats split by all/long/short |
Usage Examples
Reconstruct and Summarize Round-Trips
import pandas as pd
from pyfolio.round_trips import extract_round_trips, summarize_round_trip_activity
# 1. Load transaction data (one row per trade)
transactions = pd.read_csv("transactions.csv", parse_dates=["dt"])
# 2. Pair buys with sells into round-trips
rts = extract_round_trips(transactions)
# 3. Summarize trade quality
summary = summarize_round_trip_activity(rts)
print(summary[["Total profit", "Profit factor", "Avg. winning trade"]])