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

Closes #296 #301

Merged
merged 2 commits into from
May 25, 2016
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
3 changes: 1 addition & 2 deletions pyfolio/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,8 @@ def plot_exposures(returns, positions_alloc, ax=None, **kwargs):
ax = plt.gca()

df_long_short = pos.get_long_short_pos(positions_alloc)

df_long_short.plot(
kind='area', color=['lightblue', 'green'], alpha=1.0,
kind='line', style=['-g', '-r', '--k'], 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]))
Expand Down
11 changes: 6 additions & 5 deletions pyfolio/pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ def get_long_short_pos(positions):

pos_wo_cash = positions.drop('cash', axis=1)
longs = pos_wo_cash[pos_wo_cash > 0].sum(axis=1).fillna(0)
shorts = pos_wo_cash[pos_wo_cash < 0].abs().sum(axis=1).fillna(0)
shorts = pos_wo_cash[pos_wo_cash < 0].sum(axis=1).fillna(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't removing abs() not change the results of the plot?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will. One of the issue tasks is to make the "shorts" line show up
as negative instead of positive.

"Make Long exposure line positive number, and Short exposure line all
negative numbers"

On Mon, May 23, 2016 at 3:34 AM, Thomas Wiecki notifications@github.com
wrote:

In pyfolio/pos.py
#301 (comment):

@@ -57,13 +57,14 @@ def get_long_short_pos(positions):

 pos_wo_cash = positions.drop('cash', axis=1)
 longs = pos_wo_cash[pos_wo_cash > 0].sum(axis=1).fillna(0)
  • shorts = pos_wo_cash[pos_wo_cash < 0].abs().sum(axis=1).fillna(0)
  • shorts = pos_wo_cash[pos_wo_cash < 0].sum(axis=1).fillna(0)

Won't removing abs() not change the results of the plot?


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/quantopian/pyfolio/pull/301/files/12907415ecd871a19bd60e39c35613cc13692168#r64178908

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@twiecki @ahgnaw see line 62 below where cash is added in the net_liquidation value. I believe this results in the cash taken in from shorts being double counted. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if shorts is a negative number, net liquidation should be longs + shorts + cash

Copy link
Contributor

@justinlent justinlent Jun 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from our offline chat:

The explicit calculation would be net_liquidation = longs - shorts + (cash + shorts)

which removes the short exposure from the cash component
which then begs the simplification to just net_liquidation = longs + cash
where net_liquidation == portfolio_value (e.g. the amount of $ you could actually withdraw from the account)

will have to think about whether that makes sense though

cash = positions.cash
net_liquidation = longs - shorts + cash
df_long_short = pd.DataFrame({'long': longs,
'short': shorts})

return df_long_short.divide(net_liquidation, axis='index')
df_pos = pd.DataFrame({'long': longs.divide(net_liquidation, axis='index'),
'short': shorts.divide(net_liquidation,
axis='index')})
df_pos['net exposure'] = df_pos['long'] + df_pos['short']
return df_pos


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