Skip to content

Commit 657be93

Browse files
author
vikram-narayan
committed
MAINT: add perf attrib to full tearsheet, refactoring
1 parent 7cc9f91 commit 657be93

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

pyfolio/perf_attrib.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import matplotlib.pyplot as plt
2121
from pyfolio.pos import get_percent_alloc
22-
from pyfolio.utils import print_table, set_legend_location, COLORS
22+
from pyfolio.utils import print_table, set_legend_location
2323

2424

2525
def perf_attrib(returns, positions, factor_returns, factor_loadings,
@@ -161,7 +161,7 @@ def show_perf_attrib_stats(returns, positions, factor_returns,
161161
positions,
162162
factor_returns,
163163
factor_loadings,
164-
pos_in_dollars=pos_in_dollars
164+
pos_in_dollars=pos_in_dollars,
165165
)
166166

167167
perf_attrib_stats = create_perf_attrib_stats(perf_attrib_data)
@@ -270,8 +270,8 @@ def plot_factor_contribution_to_perf(perf_attrib_data, ax=None):
270270
factors_and_specific = perf_attrib_data.drop(
271271
['total_returns', 'common_returns'], axis='columns')
272272

273-
for s in factors_and_specific:
274-
ax.plot(factors_and_specific[s])
273+
for col in factors_and_specific:
274+
ax.plot(factors_and_specific[col])
275275

276276
ax.axhline(0, color='k')
277277
set_legend_location(ax)
@@ -304,8 +304,8 @@ def plot_risk_exposures(exposures, ax=None):
304304
if ax is None:
305305
ax = plt.gca()
306306

307-
for s in exposures:
308-
ax.plot(exposures[s])
307+
for col in exposures:
308+
ax.plot(exposures[col])
309309

310310
set_legend_location(ax)
311311
ax.set_ylabel('Factor exposures')

pyfolio/tears.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ def create_full_tear_sheet(returns,
7777
volumes=None,
7878
percentile=None,
7979
turnover_denom='AGB',
80-
set_context=True):
80+
set_context=True,
81+
factor_returns=None,
82+
factor_loadings=None,
83+
pos_in_dollars=True,
84+
return_fig=False):
8185
"""
8286
Generate a number of tear sheets that are useful
8387
for analyzing a strategy's performance.
@@ -160,6 +164,13 @@ def create_full_tear_sheet(returns,
160164
set_context : boolean, optional
161165
If True, set default plotting style context.
162166
- See plotting.context().
167+
factor_returns : pd.Dataframe, optional
168+
Returns by factor, with date as index and factors as columns
169+
factor_loadings : pd.Dataframe, optional
170+
Factor loadings for all days in the date range, with date and
171+
ticker as index, and factors as columns.
172+
pos_in_dollars : boolean, optional
173+
indicates whether positions is in dollars
163174
"""
164175

165176
if benchmark_rets is None:
@@ -219,6 +230,11 @@ def create_full_tear_sheet(returns,
219230
create_risk_tear_sheet(positions, style_factor_panel, sectors,
220231
caps, shares_held, volumes, percentile)
221232

233+
if factor_returns is not None and factor_loadings is not None:
234+
create_perf_attrib_tearsheet(returns, positions, factor_returns,
235+
factor_loadings,
236+
pos_in_dollars=pos_in_dollars)
237+
222238
if bayesian:
223239
create_bayesian_tear_sheet(returns,
224240
live_start_date=live_start_date,

pyfolio/utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -623,5 +623,7 @@ def set_legend_location(ax, autofmt_xdate=True):
623623
ax.legend(frameon=True, framealpha=0.5, loc='upper left',
624624
bbox_to_anchor=(1.05, 1))
625625

626+
ax.set_prop_cycle('color', COLORS)
627+
626628
if autofmt_xdate:
627629
ax.figure.autofmt_xdate()

0 commit comments

Comments
 (0)