Skip to content

Commit

Permalink
BUG Long/short/cash positions did not sum to 1.
Browse files Browse the repository at this point in the history
This commit fixes this issue by renormalizing.

Also, and addition kwarg is added to allow rescaling to leverage as requested in #30.
  • Loading branch information
twiecki committed Jun 29, 2015
1 parent 68d3b8b commit dd6a69c
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion quantrisk/positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_portfolio_alloc(df_pos_vals):
return df_pos_alloc


def get_long_short_pos(df_pos):
def get_long_short_pos(df_pos, gross_lev=1.):
df_pos_wo_cash = df_pos.drop('cash', axis='columns')
df_long = df_pos_wo_cash.apply(lambda x: x[x > 0].sum(), axis='columns')
df_short = -df_pos_wo_cash.apply(lambda x: x[x < 0].sum(), axis='columns')
Expand All @@ -61,6 +61,12 @@ def get_long_short_pos(df_pos):
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


Expand Down

0 comments on commit dd6a69c

Please sign in to comment.