Round Trip Tearsheet

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.


In [1]:
%matplotlib inline
import gzip
import os
import pandas as pd
import pyfolio as pf

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

In [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'}

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.


In [4]:
pf.create_round_trip_tear_sheet(returns, positions, transactions, sector_mappings=sect_map)


Summary stats All trades Long trades Short trades
Total number of round_trips 5822.00 4667.00 1155.00
Percent profitable 0.50 0.49 0.52
Winning round_trips 2887.00 2291.00 596.00
Losing round_trips 2917.00 2364.00 553.00
Even round_trips 18.00 12.00 6.00
PnL stats All trades Long trades Short trades
Total profit $65404.25 $61844.15 $3560.10
Gross profit $448803.34 $428194.89 $20608.45
Gross loss $-383399.09 $-366350.75 $-17048.35
Profit factor $1.17 $1.17 $1.21
Avg. trade net profit $11.23 $13.25 $3.08
Avg. winning trade $155.46 $186.90 $34.58
Avg. losing trade $-131.44 $-154.97 $-30.83
Ratio Avg. Win:Avg. Loss $1.18 $1.21 $1.12
Largest winning trade $9500.14 $9500.14 $1623.24
Largest losing trade $-22902.83 $-22902.83 $-661.29
Duration stats All trades Long trades Short trades
Avg duration 13 days 03:21:49.653555 15 days 18:53:36.628026 2 days 10:39:35.064935
Median duration 8 days 00:00:00 12 days 00:00:00 2 days 00:00:00
Avg # round_trips per day 70.14 56.23 96.25
Avg # round_trips per month 1473.04 1180.81 2021.25
Return stats All trades Long trades Short trades
Avg returns all round_trips 0.01% 0.01% 0.00%
Avg returns winning 0.13% 0.15% 0.03%
Avg returns losing -0.11% -0.13% -0.03%
Median returns all round_trips -0.00% -0.00% 0.00%
Median returns winning 0.02% 0.03% 0.01%
Median returns losing -0.01% -0.02% -0.00%
Largest winning trade 6.78% 6.78% 1.37%
Largest losing trade -17.23% -17.23% -0.72%
Symbol stats AMD CERN COST DELL GPS INTC MMM
Avg returns all round_trips -0.00% 0.02% 0.02% -0.03% 0.00% 0.02% 0.01%
Avg returns winning 0.20% 0.15% 0.10% 0.11% 0.10% 0.11% 0.10%
Avg returns losing -0.19% -0.13% -0.07% -0.15% -0.09% -0.06% -0.09%
Median returns all round_trips -0.00% 0.00% 0.00% -0.00% -0.00% -0.00% 0.00%
Median returns winning 0.03% 0.02% 0.02% 0.02% 0.02% 0.01% 0.02%
Median returns losing -0.02% -0.01% -0.01% -0.02% -0.01% -0.01% -0.01%
Largest winning trade 6.78% 6.14% 3.96% 2.78% 1.80% 2.40% 2.45%
Largest losing trade -17.23% -3.92% -2.32% -6.39% -6.86% -4.45% -1.79%
Profitability (PnL / PnL total) per name pnl
symbol
COST 0.40%
INTC 0.38%
CERN 0.32%
MMM 0.22%
GPS 0.05%
AMD -0.06%
DELL -0.31%
Profitability (PnL / PnL total) per name pnl
symbol
Consumer Goods 0.40%
Healthcare 0.32%
Construction 0.22%
Technology 0.06%
<matplotlib.figure.Figure at 0x7f7281a27f28>

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


In [4]:
rts = pf.round_trips.extract_round_trips(transactions, 
                                         portfolio_value=positions.sum(axis='columns') / (returns + 1))

In [5]:
rts.head()


Out[5]:
close_dt long open_dt pnl rt_returns symbol duration returns
0 2004-01-13 True 2004-01-09 -126.000000 -0.022523 AMD 4 days -0.001249
1 2004-01-16 True 2004-01-09 50.020000 0.078507 AMD 7 days 0.000503
2 2004-01-20 True 2004-01-09 1540.099065 0.104696 AMD 11 days 0.015257
3 2004-01-21 False 2004-01-20 287.119806 0.085155 AMD 1 days 0.002861
4 2004-01-22 False 2004-01-20 103.349947 0.112198 AMD 2 days 0.001032

In [7]:
pf.round_trips.print_round_trip_stats(rts)


Summary stats All trades Long trades Short trades
Total number of round_trips 5819.00 4664.00 1155.00
Percent profitable 0.50 0.49 0.52
Winning round_trips 2888.00 2292.00 596.00
Losing round_trips 2914.00 2361.00 553.00
Even round_trips 17.00 11.00 6.00
PnL stats All trades Long trades Short trades
Total profit $67003.94 $63472.61 $3531.32
Gross profit $448674.42 $428094.75 $20579.67
Gross loss $-381670.48 $-364622.13 $-17048.35
Profit factor $1.18 $1.17 $1.21
Avg. trade net profit $11.51 $13.61 $3.06
Avg. winning trade $155.36 $186.78 $34.53
Avg. losing trade $-130.98 $-154.44 $-30.83
Ratio Avg. Win:Avg. Loss $1.19 $1.21 $1.12
Largest winning trade $9500.14 $9500.14 $1623.24
Largest losing trade $-22902.83 $-22902.83 $-661.29
Duration stats All trades Long trades Short trades
Avg duration 13 days 03:27:07.702354 15 days 19:02:40.548885 2 days 10:39:35.064935
Median duration 8 days 00:00:00 12 days 00:00:00 2 days 00:00:00
Avg # round_trips per day 70.11 56.19 96.25
Avg # round_trips per month 1472.28 1180.05 2021.25
Return stats All trades Long trades Short trades
Avg returns all round_trips 0.01% 0.01% 0.00%
Avg returns winning 0.13% 0.15% 0.03%
Avg returns losing -0.11% -0.13% -0.03%
Median returns all round_trips -0.00% -0.00% 0.00%
Median returns winning 0.02% 0.03% 0.01%
Median returns losing -0.01% -0.02% -0.00%
Largest winning trade 6.78% 6.78% 1.37%
Largest losing trade -17.23% -17.23% -0.72%
Symbol stats AMD CERN COST DELL GPS INTC MMM
Avg returns all round_trips -0.00% 0.02% 0.02% -0.03% 0.00% 0.02% 0.01%
Avg returns winning 0.20% 0.15% 0.10% 0.11% 0.10% 0.11% 0.10%
Avg returns losing -0.19% -0.13% -0.07% -0.15% -0.09% -0.06% -0.09%
Median returns all round_trips -0.00% 0.00% 0.00% -0.00% -0.00% -0.00% 0.00%
Median returns winning 0.03% 0.02% 0.02% 0.02% 0.02% 0.01% 0.02%
Median returns losing -0.02% -0.01% -0.01% -0.02% -0.01% -0.01% -0.01%
Largest winning trade 6.78% 6.14% 3.96% 2.78% 1.80% 2.40% 2.45%
Largest losing trade -17.23% -3.92% -2.32% -6.39% -6.86% -4.45% -1.79%