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 struction) of symbol-sector mappings, where symbols are keys and sectors are values. create_full_tearsheet
will also take symbol-sector mappings as keyword argument sector_mappings
.
In [1]:
%matplotlib inline
import pyfolio as pf
import gzip
import os
import pandas as pd
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]
gross_lev = pd.read_csv(gzip.open('../tests/test_data/test_gross_lev.csv.gz'),
index_col=0, parse_dates=0, header=None)[1]
In [3]:
returns.index = returns.index.tz_localize("UTC")
positions.index = positions.index.tz_localize("UTC")
transactions.index = transactions.index.tz_localize("UTC")
gross_lev.index = gross_lev.index.tz_localize("UTC")
In [4]:
positions.head(2)
Out[4]:
In [5]:
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, gross_lev=gross_lev, sector_mappings=sect_map)
In [7]:
pf.create_round_trip_tear_sheet(positions, transactions, sector_mappings=sect_map)