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

MAINT Fix gross leverage - round 2 #147

Merged
merged 1 commit into from
Sep 26, 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
13 changes: 4 additions & 9 deletions pyfolio/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division
import warnings

import pandas as pd
import numpy as np
Expand Down Expand Up @@ -799,7 +798,7 @@ def plot_gross_leverage(returns, gross_lev, ax=None, **kwargs):


def plot_exposures(returns, positions_alloc, ax=None, **kwargs):
"""Plots a cake chart of long, short, and cash exposure.
"""Plots a cake chart of the long and short exposure.

Parameters
----------
Expand All @@ -818,23 +817,19 @@ def plot_exposures(returns, positions_alloc, ax=None, **kwargs):
-------
ax : matplotlib.Axes
The axes that were plotted on.

"""
"""

if ax is None:
ax = plt.gca()

df_long_short = pos.get_long_short_pos(positions_alloc)

if np.any(df_long_short.cash < 0):
warnings.warn('Negative cash, taking absolute for area plot.')
df_long_short = df_long_short.abs()
df_long_short.plot(
kind='area', color=['lightblue', 'green', 'coral'], alpha=1.0,
kind='area', color=['lightblue', 'green'], alpha=1.0,
ax=ax, **kwargs)
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_title("Long/Short/Cash Exposure")
ax.set_title("Long/Short Exposure")
ax.set_ylabel('Exposure')
ax.set_xlabel('')
return ax
Expand Down
34 changes: 13 additions & 21 deletions pyfolio/pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,31 @@ def get_portfolio_alloc(positions):
)


def get_long_short_pos(positions, gross_lev=1.):
def get_long_short_pos(positions):
"""
Determines the long amount, short amount, and cash of a portfolio.
Determines the long and short allocations in a portfolio.

Parameters
----------
positions : pd.DataFrame
The positions that the strategy takes over time.
gross_lev : float, optional
The porfolio's gross leverage (default 1).

Returns
-------
df_long_short : pd.DataFrame
Net long, short, and cash positions.
Long and short allocations as a decimal
percentage of the total net liquidation
"""

positions_wo_cash = positions.drop('cash', axis='columns')
df_long = positions_wo_cash.apply(lambda x: x[x > 0].sum(), axis='columns')
df_short = - \
positions_wo_cash.apply(lambda x: x[x < 0].sum(), axis='columns')
# Shorting positions adds to cash
df_cash = positions.cash.abs() - df_short
df_long_short = pd.DataFrame({'long': df_long,
'short': df_short,
'cash': df_cash})
# Renormalize
df_long_short /= df_long_short.sum(axis='columns')

# Renormalize to leverage
df_long_short *= gross_lev

return df_long_short
pos_wo_cash = positions.drop('cash', axis=1)
longs = pos_wo_cash[pos_wo_cash > 0].sum(axis=1)
shorts = pos_wo_cash[pos_wo_cash < 0].abs().sum(axis=1)
cash = positions.cash
net_liquidation = longs - shorts + cash
df_long_short = pd.DataFrame({'long': longs,
'short': shorts})

return df_long_short / net_liquidation


def get_top_long_short_abs(positions, top=10):
Expand Down