Round Trip Analysis

When evaluating the performance of an investing strategy, it is helpful to quantify the frequency, duration, and profitability of its independent bets, or “round trip” trades. A round trip trade is started when a new long or short position is opened and then later completely or partially closed out.

The intent of the round trip tearsheet is to help differentiate strategies that profited off a few lucky trades from strategies that profited repeatedly from genuine alpha. Breaking down round trip profitability by traded name and sector can also help inform universe selection and identify exposure risks. For example, even if your equity curve looks robust, if only two securities in your universe of fifteen names contributed to overall profitability, you may have reason to question the logic of your strategy.

To identify round trips, pyfolio reconstructs the complete portfolio based on the transactions that you pass in. When you make a trade, pyfolio checks if shares are already present in your portfolio purchased at a certain price. If there are, we compute the PnL, returns and duration of that round trip trade. In calculating round trips, pyfolio will also append position closing transactions at the last timestamp in the positions data. This closing transaction will cause the PnL from any open positions to realized as completed round trips.

Imports & Settins

[ ]:
# silence warnings
import warnings
warnings.filterwarnings('ignore')
[1]:
import pyfolio as pf
%matplotlib inline
import gzip
import os
import pandas as pd

Load Data

[2]:
transactions = pd.read_csv(gzip.open('../tests/test_data/test_txn.csv.gz'),
                    index_col=0, parse_dates=True)
positions = pd.read_csv(gzip.open('../tests/test_data/test_pos.csv.gz'),
                    index_col=0, parse_dates=True)
returns = pd.read_csv(gzip.open('../tests/test_data/test_returns.csv.gz'),
                      index_col=0, parse_dates=True, header=None)[1]

Add Sector Mapping

[3]:
# Optional: Sector mappings may be passed in as a dict or pd.Series. If a mapping is
# provided, PnL from symbols with mappings will be summed to display profitability by sector.
sect_map = {'COST': 'Consumer Goods', 'INTC':'Technology', 'CERN':'Healthcare', 'GPS':'Technology',
            'MMM': 'Construction', 'DELL': 'Technology', 'AMD':'Technology'}

Run Round Trip Tear Sheet

The easiest way to run the analysis is to call pyfolio.create_round_trip_tear_sheet(). Passing in a sector map is optional. You can also pass round_trips=True to pyfolio.create_full_tear_sheet() to have this be created along all the other analyses.

[4]:
pf.create_round_trip_tear_sheet(returns, positions, transactions, sector_mappings=sect_map)
Summary stats All trades Short trades Long trades
Total number of round_trips 5822.00 1155.00 4667.00
Percent profitable 0.50 0.52 0.49
Winning round_trips 2886.00 595.00 2291.00
Losing round_trips 2917.00 553.00 2364.00
Even round_trips 19.00 7.00 12.00
PnL stats All trades Short trades Long trades
Total profit $65404.25 $3560.10 $61844.15
Gross profit $448803.34 $20608.45 $428194.89
Gross loss $-383399.09 $-17048.35 $-366350.75
Profit factor $1.17 $1.21 $1.17
Avg. trade net profit $11.23 $3.08 $13.25
Avg. winning trade $155.51 $34.64 $186.90
Avg. losing trade $-131.44 $-30.83 $-154.97
Ratio Avg. Win:Avg. Loss $1.18 $1.12 $1.21
Largest winning trade $9500.14 $1623.24 $9500.14
Largest losing trade $-22902.83 $-661.29 $-22902.83
Duration stats All trades Short trades Long trades
Avg duration 13 days 03:21:49.653555479 2 days 10:39:35.064935064 15 days 18:53:36.628026569
Median duration 8 days 00:00:00 2 days 00:00:00 12 days 00:00:00
Longest duration 84 days 00:00:00 13 days 00:00:00 84 days 00:00:00
Shortest duration 0 days 00:00:01 1 days 00:00:00 0 days 00:00:01
Return stats All trades Short trades Long trades
Avg returns all round_trips 0.01% 0.01% 0.01%
Avg returns winning 0.13% 0.13% 0.12%
Avg returns losing -0.11% -0.12% -0.11%
Median returns all round_trips -0.00% 0.00% -0.00%
Median returns winning 0.02% 0.02% 0.02%
Median returns losing -0.01% -0.01% -0.01%
Largest winning trade 6.78% 6.78% 6.19%
Largest losing trade -17.23% -3.95% -17.23%
Symbol stats AMD CERN COST DELL GPS INTC MMM
Avg returns all round_trips 0.04% -0.01% -0.02% 0.02% -0.02% -0.04% 0.07%
Avg returns winning 0.13% 0.08% 0.09% 0.12% 0.12% 0.17% 0.16%
Avg returns losing -0.07% -0.08% -0.10% -0.08% -0.18% -0.21% -0.05%
Median returns all round_trips 0.00% -0.00% -0.00% 0.00% 0.00% -0.00% 0.00%
Median returns winning 0.02% 0.02% 0.02% 0.02% 0.02% 0.02% 0.03%
Median returns losing -0.01% -0.01% -0.01% -0.01% -0.02% -0.03% -0.01%
Largest winning trade 6.78% 1.69% 1.48% 3.29% 2.72% 5.01% 6.14%
Largest losing trade -1.59% -6.39% -4.48% -3.28% -17.23% -6.86% -1.60%
Profitability (PnL / PnL total) per name
symbol
COST 39.90%
INTC 38.27%
CERN 32.31%
MMM 22.15%
GPS 4.94%
AMD -6.41%
DELL -31.15%
Profitability (PnL / PnL total) per name
symbol
Consumer Goods 39.90%
Healthcare 32.31%
Construction 22.15%
Technology 5.65%
../_images/notebooks_round_trip_tear_sheet_example_11_7.png

Explore underlying functions

Under the hood, several functions are being called. extract_round_trips() does the portfolio reconstruction and creates the round-trip trades.

[5]:
rts = pf.round_trips.extract_round_trips(transactions,
                                         portfolio_value=positions.sum(axis='columns') / (returns + 1))
[6]:
rts.head()
[6]:
pnl open_dt close_dt long rt_returns symbol duration returns
0 -126.000000 2004-01-09 00:00:00+00:00 2004-01-13 00:00:00+00:00 True -0.022523 AMD 4 days 0.000793
1 50.020000 2004-01-09 00:00:00+00:00 2004-01-16 00:00:00+00:00 True 0.078507 AMD 7 days 0.000000
2 1540.099065 2004-01-09 00:00:00+00:00 2004-01-20 00:00:00+00:00 True 0.104696 AMD 11 days 0.000184
3 287.119806 2004-01-20 00:00:00+00:00 2004-01-21 00:00:00+00:00 False 0.085155 AMD 1 days 0.000984
4 103.349947 2004-01-20 00:00:00+00:00 2004-01-22 00:00:00+00:00 False 0.112198 AMD 2 days -0.001249
[7]:
pf.round_trips.print_round_trip_stats(rts)
Summary stats All trades Short trades Long trades
Total number of round_trips 5819.00 1155.00 4664.00
Percent profitable 0.50 0.52 0.49
Winning round_trips 2887.00 595.00 2292.00
Losing round_trips 2914.00 553.00 2361.00
Even round_trips 18.00 7.00 11.00
PnL stats All trades Short trades Long trades
Total profit $67003.94 $3531.32 $63472.61
Gross profit $448674.42 $20579.67 $428094.75
Gross loss $-381670.48 $-17048.35 $-364622.13
Profit factor $1.18 $1.21 $1.17
Avg. trade net profit $11.51 $3.06 $13.61
Avg. winning trade $155.41 $34.59 $186.78
Avg. losing trade $-130.98 $-30.83 $-154.44
Ratio Avg. Win:Avg. Loss $1.19 $1.12 $1.21
Largest winning trade $9500.14 $1623.24 $9500.14
Largest losing trade $-22902.83 $-661.29 $-22902.83
Duration stats All trades Short trades Long trades
Avg duration 13 days 03:27:07.702354356 2 days 10:39:35.064935064 15 days 19:02:40.548885077
Median duration 8 days 00:00:00 2 days 00:00:00 12 days 00:00:00
Longest duration 84 days 00:00:00 13 days 00:00:00 84 days 00:00:00
Shortest duration 1 days 00:00:00 1 days 00:00:00 1 days 00:00:00
Return stats All trades Short trades Long trades
Avg returns all round_trips 0.01% -0.00% 0.01%
Avg returns winning 0.13% 0.12% 0.13%
Avg returns losing -0.11% -0.11% -0.11%
Median returns all round_trips -0.00% -0.00% 0.00%
Median returns winning 0.02% 0.02% 0.02%
Median returns losing -0.01% -0.02% -0.01%
Largest winning trade 6.78% 6.78% 6.19%
Largest losing trade -17.23% -3.92% -17.23%
Symbol stats AMD CERN COST DELL GPS INTC MMM
Avg returns all round_trips 0.04% -0.01% -0.02% 0.02% -0.02% -0.04% 0.08%
Avg returns winning 0.13% 0.08% 0.09% 0.12% 0.12% 0.17% 0.16%
Avg returns losing -0.07% -0.08% -0.10% -0.08% -0.18% -0.21% -0.05%
Median returns all round_trips 0.00% -0.00% -0.00% 0.00% 0.00% -0.00% 0.00%
Median returns winning 0.02% 0.01% 0.01% 0.02% 0.02% 0.02% 0.03%
Median returns losing -0.01% -0.01% -0.01% -0.01% -0.02% -0.03% -0.01%
Largest winning trade 6.78% 1.69% 1.48% 3.29% 2.72% 5.01% 6.14%
Largest losing trade -1.59% -6.39% -4.48% -3.28% -17.23% -6.86% -1.60%