To generate sector allocation plots in the positions tearsheet and PnL by sector in the round trips tearsheet, you must pass pyfolio a dictionary (or dict-like data structure) of symbol-sector mappings, where symbols are keys and sectors are values. create_full_tearsheet
will also take symbol-sector mappings as the keyword argument sector_mappings
.
In this notebook, we generate the positions tear sheet and round-trip tear sheet with sector mappings.
import pyfolio as pf
import pandas as pd
%matplotlib inline
import gzip
import os
# silence warnings
import warnings
warnings.filterwarnings('ignore')
returns = pd.read_csv(gzip.open('../tests/test_data/test_returns.csv.gz'),
index_col=0, parse_dates=True, header=None)[1]
positions = pd.read_csv(gzip.open('../tests/test_data/test_pos.csv.gz'),
index_col=0, parse_dates=True)
transactions = pd.read_csv(gzip.open('../tests/test_data/test_txn.csv.gz'),
index_col=0, parse_dates=True)
returns.index = returns.index.tz_localize('UTC')
positions.index = positions.index.tz_localize('UTC')
transactions.index = transactions.index.tz_localize('UTC')
positions.head()
# define our sector mappings
sect_map = {'COST': 'Consumer Goods',
'INTC': 'Technology',
'CERN': 'Healthcare',
'GPS': 'Technology',
'MMM': 'Construction',
'DELL': 'Technology',
'AMD': 'Technology'}
pf.create_position_tear_sheet(returns, positions, sector_mappings=sect_map)
pf.create_round_trip_tear_sheet(returns, positions, transactions, sector_mappings=sect_map)