Skip to content

Commit

Permalink
ENH Add ability to pass hide_positions=True when a tear-sheet gets cr…
Browse files Browse the repository at this point in the history
…eated to suppress display of any positions.
  • Loading branch information
twiecki committed Oct 21, 2015
1 parent 151f61e commit d962f99
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 32 deletions.
203 changes: 173 additions & 30 deletions pyfolio/examples/zipline_algo_example.ipynb

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion pyfolio/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ def plot_exposures(returns, positions_alloc, ax=None, **kwargs):


def show_and_plot_top_positions(returns, positions_alloc,
show_and_plot=2,
show_and_plot=2, hide_positions=False,
legend_loc='real_best', ax=None,
**kwargs):
"""Prints and/or plots the exposures of the top 10 held positions of
Expand All @@ -864,6 +864,8 @@ def show_and_plot_top_positions(returns, positions_alloc,
show_and_plot : int, optional
By default, this is 2, and both prints and plots.
If this is 0, it will only plot; if 1, it will only print.
hide_positions : bool, optional
If True, will not output any symbol names.
legend_loc : matplotlib.loc, optional
The location of the legend on the plot.
By default, the legend will display below the plot.
Expand Down Expand Up @@ -931,6 +933,10 @@ def show_and_plot_top_positions(returns, positions_alloc,
df_cum_rets = timeseries.cum_returns(returns, starting_value=1)
ax.set_xlim((df_cum_rets.index[0], df_cum_rets.index[-1]))
ax.set_ylabel('Exposure by stock')

if hide_positions:
ax.legend_.remove()

return ax


Expand Down
12 changes: 11 additions & 1 deletion pyfolio/tears.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def create_full_tear_sheet(returns, positions=None, transactions=None,
gross_lev=None,
slippage=None,
live_start_date=None, bayesian=False,
hide_positions=False,
sector_mappings=None,
cone_std=(1.0, 1.5, 2.0), set_context=True):
"""
Expand Down Expand Up @@ -102,6 +103,8 @@ def create_full_tear_sheet(returns, positions=None, transactions=None,
live_start_date : datetime, optional
The point in time when the strategy began live trading,
after its backtest period.
hide_positions : bool, optional
If True, will not output any symbol names.
bayesian: boolean, optional
If True, causes the generation of a Bayesian tear sheet.
cone_std : float, or tuple, optional
Expand Down Expand Up @@ -144,6 +147,7 @@ def create_full_tear_sheet(returns, positions=None, transactions=None,
if positions is not None:
create_position_tear_sheet(returns, positions,
gross_lev=gross_lev,
hide_positions=hide_positions,
set_context=set_context)

if transactions is not None:
Expand Down Expand Up @@ -321,7 +325,7 @@ def create_returns_tear_sheet(returns, live_start_date=None,

@plotting_context
def create_position_tear_sheet(returns, positions, gross_lev=None,
show_and_plot_top_pos=2,
show_and_plot_top_pos=2, hide_positions=False,
return_fig=False, sector_mappings=None):
"""
Generate a number of plots for analyzing a
Expand All @@ -345,6 +349,9 @@ def create_position_tear_sheet(returns, positions, gross_lev=None,
By default, this is 2, and both prints and plots the
top 10 positions.
If this is 0, it will only plot; if 1, it will only print.
hide_positions : bool, optional
If True, will not output any symbol names.
Overrides show_and_plot_top_pos to 0 to suppress text output.
return_fig : boolean, optional
If True, returns the figure that was plotted on.
set_context : boolean, optional
Expand All @@ -354,6 +361,8 @@ def create_position_tear_sheet(returns, positions, gross_lev=None,
Security ids as keys, sectors as values.
"""

if hide_positions:
show_and_plot_top_pos = 0
vertical_sections = 5 if sector_mappings is not None else 4

fig = plt.figure(figsize=(14, vertical_sections * 6))
Expand All @@ -374,6 +383,7 @@ def create_position_tear_sheet(returns, positions, gross_lev=None,
returns,
positions_alloc,
show_and_plot=show_and_plot_top_pos,
hide_positions=hide_positions,
ax=ax_top_positions)

plotting.plot_holdings(returns, positions_alloc, ax=ax_holdings)
Expand Down

0 comments on commit d962f99

Please sign in to comment.