Skip to content

Commit adcdea8

Browse files
authored
ENH: Format zipline assets before displaying (#358)
* Add new function for formatting zipline assets. Apply it whenever asset names are displayed. * Check for general asset * Update doc * Use latest PyMC3 for tests
1 parent 816e4f6 commit adcdea8

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ install:
2828
- source activate testenv
2929
- pip install nose_parameterized
3030
#- pip install --no-deps git+https://github.com/quantopian/zipline
31-
- pip install -e .[bayesian]
31+
- pip install -e .
32+
- pip install git+git://github.com/pymc-devs/pymc3.git
3233

3334
before_script:
3435
- "flake8 pyfolio"

pyfolio/plotting.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,8 @@ def show_and_plot_top_positions(returns, positions_alloc,
10481048
The axes that were plotted on.
10491049
10501050
"""
1051+
positions_alloc = positions_alloc.copy()
1052+
positions_alloc.columns = positions_alloc.columns.map(utils.format_asset)
10511053

10521054
df_top_long, df_top_short, df_top_abs = pos.get_top_long_short_abs(
10531055
positions_alloc)
@@ -1733,7 +1735,7 @@ def plot_round_trip_lifetimes(round_trips, disp_amount=16, lsize=18, ax=None):
17331735
linewidth=lsize, solid_capstyle='butt')
17341736

17351737
ax.set_yticks(range(disp_amount))
1736-
ax.set_yticklabels(sample)
1738+
ax.set_yticklabels([utils.format_asset(s) for s in sample])
17371739

17381740
ax.set_ylim((-0.5, min(len(sample), disp_amount) - 0.5))
17391741
blue = patches.Rectangle([0, 0], 1, 1, color='b', label='Long')
@@ -1765,10 +1767,10 @@ def show_profit_attribution(round_trips):
17651767
"""
17661768

17671769
total_pnl = round_trips['pnl'].sum()
1768-
pct_profit_attribution = round_trips.groupby(
1769-
'symbol')['pnl'].sum() / total_pnl
1770+
pnl_attribution = round_trips.groupby('symbol')['pnl'].sum() / total_pnl
17701771

1771-
utils.print_table(pct_profit_attribution.sort_values(
1772+
pnl_attribution.index = pnl_attribution.index.map(utils.format_asset)
1773+
utils.print_table(pnl_attribution.sort_values(
17721774
inplace=False,
17731775
ascending=False),
17741776
name='Profitability (PnL / PnL total) per name',

pyfolio/round_trips.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pandas as pd
2121
import numpy as np
2222

23-
from .utils import print_table
23+
from .utils import print_table, format_asset
2424

2525
PNL_STATS = OrderedDict(
2626
[('Total profit', lambda x: x.sum()),
@@ -404,5 +404,6 @@ def print_round_trip_stats(round_trips, hide_pos=False):
404404
name='Return stats')
405405

406406
if not hide_pos:
407+
stats['symbols'].columns = stats['symbols'].columns.map(format_asset)
407408
print_table(stats['symbols'] * 100,
408409
fmt='{:.2f}%', name='Symbol stats')

pyfolio/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ def _1_bday_ago():
133133
return pd.Timestamp.now().normalize() - _1_bday
134134

135135

136+
def format_asset(asset):
137+
"""
138+
If zipline asset objects are used, we want to print them out prettily
139+
within the tear sheet. This function should only be applied directly
140+
before displaying.
141+
"""
142+
143+
try:
144+
import zipline.assets
145+
except:
146+
return asset
147+
148+
if isinstance(asset, zipline.assets.Asset):
149+
return asset.symbol
150+
else:
151+
return asset
152+
153+
136154
def get_returns_cached(filepath, update_func, latest_dt, **kwargs):
137155
"""
138156
Get returns from a cached file if the cache is recent enough,

0 commit comments

Comments
 (0)