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 Round Trips

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

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"]])

Related Pages

Page Connections

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