Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Plot max and median long/short exposures #237

Merged
merged 1 commit into from
Dec 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pyfolio/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,37 @@ def show_and_plot_top_positions(returns, positions_alloc,
return ax


def plot_max_median_position_concentration(positions, ax=None, **kwargs):
"""
Plots the max and median of long and short position concentrations
over the time.

Parameters
----------
positions : pd.DataFrame
The positions that the strategy takes over time.
ax : matplotlib.Axes, optional
Axes upon which to plot.

Returns
-------
ax : matplotlib.Axes
The axes that were plotted on.
"""
if ax is None:
ax = plt.gcf()

alloc_summary = pos.get_max_median_position_concentration(positions)
colors = ['mediumblue', 'steelblue', 'tomato', 'firebrick']
alloc_summary.plot(linewidth=1, color=colors, alpha=0.6, ax=ax)

ax.legend(loc='center left')
ax.set_ylabel('Exposure')
ax.set_title('Long/Short Max and Median Position Concentration')

return ax


def plot_sector_allocations(returns, sector_alloc, ax=None, **kwargs):
"""Plots the sector exposures of the portfolio over time.

Expand Down
31 changes: 31 additions & 0 deletions pyfolio/pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,37 @@ def get_top_long_short_abs(positions, top=10):
return df_top_long, df_top_short, df_top_abs


def get_max_median_position_concentration(positions):
"""
Finds the max and median long and short position concentrations
in each time period specified by the index of positions.

Parameters
----------
positions : pd.DataFrame
The positions that the strategy takes over time.

Returns
-------
pd.DataFrame
Columns are max long, max short, median long, and median short
position concentrations. Rows are timeperiods.
"""
expos = get_percent_alloc(positions)
expos = expos.drop('cash', axis=1)

longs = expos.where(expos.applymap(lambda x: x > 0))
shorts = expos.where(expos.applymap(lambda x: x < 0))

alloc_summary = pd.DataFrame()
alloc_summary['max_long'] = longs.max(axis=1)
alloc_summary['median_long'] = longs.median(axis=1)
alloc_summary['median_short'] = shorts.median(axis=1)
alloc_summary['max_short'] = shorts.min(axis=1)

return alloc_summary


def extract_pos(positions, cash):
"""Extract position values from backtest object as returned by
get_backtest() on the Quantopian research platform.
Expand Down
10 changes: 7 additions & 3 deletions pyfolio/tears.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,15 @@ def create_position_tear_sheet(returns, positions, gross_lev=None,

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

fig = plt.figure(figsize=(14, vertical_sections * 6))
gs = gridspec.GridSpec(vertical_sections, 3, wspace=0.5, hspace=0.5)
ax_gross_leverage = plt.subplot(gs[0, :])
ax_exposures = plt.subplot(gs[1, :], sharex=ax_gross_leverage)
ax_top_positions = plt.subplot(gs[2, :], sharex=ax_gross_leverage)
ax_holdings = plt.subplot(gs[3, :], sharex=ax_gross_leverage)
ax_max_median_pos = plt.subplot(gs[3, :], sharex=ax_gross_leverage)
ax_holdings = plt.subplot(gs[4, :], sharex=ax_gross_leverage)

positions_alloc = pos.get_percent_alloc(positions)

Expand All @@ -414,14 +415,17 @@ def create_position_tear_sheet(returns, positions, gross_lev=None,
hide_positions=hide_positions,
ax=ax_top_positions)

plotting.plot_max_median_position_concentration(positions,
ax=ax_max_median_pos)

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

if sector_mappings is not None:
sector_exposures = pos.get_sector_exposures(positions, sector_mappings)
if len(sector_exposures.columns) > 1:
sector_alloc = pos.get_percent_alloc(sector_exposures)
sector_alloc = sector_alloc.drop('cash', axis='columns')
ax_sector_alloc = plt.subplot(gs[4, :], sharex=ax_gross_leverage)
ax_sector_alloc = plt.subplot(gs[5, :], sharex=ax_gross_leverage)
plotting.plot_sector_allocations(returns, sector_alloc,
ax=ax_sector_alloc)

Expand Down
25 changes: 24 additions & 1 deletion pyfolio/tests/test_pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
from numpy import (
arange,
zeros_like,
nan,
)

import warnings

from pyfolio.pos import (get_percent_alloc,
extract_pos,
get_sector_exposures)
get_sector_exposures,
get_max_median_position_concentration)


class PositionsTestCase(TestCase):
Expand Down Expand Up @@ -115,3 +117,24 @@ def test_sector_exposure(self, positions, mapping,
self.assertEqual(len(w), 1)
else:
self.assertEqual(len(w), 0)

@parameterized.expand([
(DataFrame([[1.0, 2.0, 3.0, 14.0]]*len(dates),
columns=[0, 1, 2, 'cash'], index=dates),
DataFrame([[0.15, 0.1, nan, nan]]*len(dates),
columns=['max_long', 'median_long',
'median_short', 'max_short'], index=dates)),
(DataFrame([[1.0, -2.0, -13.0, 15.0]]*len(dates),
columns=[0, 1, 2, 'cash'], index=dates),
DataFrame([[1.0, 1.0, -7.5, -13.0]]*len(dates),
columns=['max_long', 'median_long',
'median_short', 'max_short'], index=dates)),
(DataFrame([[nan, 2.0, nan, 8.0]]*len(dates),
columns=[0, 1, 2, 'cash'], index=dates),
DataFrame([[0.2, 0.2, nan, nan]]*len(dates),
columns=['max_long', 'median_long',
'median_short', 'max_short'], index=dates))
])
def test_max_median_exposure(self, positions, expected):
alloc_summary = get_max_median_position_concentration(positions)
assert_frame_equal(expected, alloc_summary)