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.
In [2]:
import pyfolio as pf
import pandas as pd
%matplotlib inline
import gzip
import os
# silence warnings
import warnings
warnings.filterwarnings('ignore')
In [3]:
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')
In [4]:
positions.head()
Out[4]:
In [5]:
# define our sector mappings
sect_map = {'COST': 'Consumer Goods',
'INTC': 'Technology',
'CERN': 'Healthcare',
'GPS': 'Technology',
'MMM': 'Construction',
'DELL': 'Technology',
'AMD': 'Technology'}
In [6]:
pf.create_position_tear_sheet(returns, positions, sector_mappings=sect_map)
In [7]:
pf.create_round_trip_tear_sheet(returns, positions, transactions, sector_mappings=sect_map)
In [ ]: