Skip to content

Commit

Permalink
ENH Format assets in capacity tear sheet; fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
gusgordon committed Jan 4, 2017
1 parent adcdea8 commit 3dcb13f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pyfolio/capacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def days_to_liquidate_positions(positions, market_data,
"""

DV = market_data['volume'] * market_data['price']
roll_mean_dv = pd.rolling_mean(DV, mean_volume_window).shift()
roll_mean_dv = DV.rolling(window=mean_volume_window,
center=False).mean().shift()
roll_mean_dv = roll_mean_dv.replace(0, np.nan)

positions_alloc = pos.get_percent_alloc(positions)
Expand Down Expand Up @@ -148,7 +149,7 @@ def get_max_days_to_liquidate_by_ticker(positions, market_data,
liq_desc.index.levels[0].name = 'symbol'
liq_desc.index.levels[1].name = 'date'

worst_liq = liq_desc.reset_index().sort(
worst_liq = liq_desc.reset_index().sort_values(
'days_to_liquidate', ascending=False).groupby('symbol').first()

return worst_liq
Expand Down Expand Up @@ -184,7 +185,7 @@ def get_low_liquidity_transactions(transactions, market_data,
bar_consumption = txn_daily_w_bar.assign(
max_pct_bar_consumed=(
txn_daily_w_bar.amount/txn_daily_w_bar.volume)*100
).sort('max_pct_bar_consumed', ascending=False)
).sort_values('max_pct_bar_consumed', ascending=False)
max_bar_consumption = bar_consumption.groupby('symbol').first()

return max_bar_consumption[['date', 'max_pct_bar_consumed']]
Expand Down
6 changes: 6 additions & 0 deletions pyfolio/tears.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ def create_capacity_tear_sheet(returns, positions, transactions,
max_bar_consumption=liquidation_daily_vol_limit,
capital_base=1e6,
mean_volume_window=5)
max_days_by_ticker.index = (
max_days_by_ticker.index.map(utils.format_asset))

print("Whole backtest:")
utils.print_table(
Expand All @@ -810,12 +812,16 @@ def create_capacity_tear_sheet(returns, positions, transactions,
capital_base=1e6,
mean_volume_window=5,
last_n_days=last_n_days)
max_days_by_ticker_lnd.index = (
max_days_by_ticker_lnd.index.map(utils.format_asset))

print("Last {} trading days:".format(last_n_days))
utils.print_table(
max_days_by_ticker_lnd[max_days_by_ticker_lnd.days_to_liquidate > 1])

llt = capacity.get_low_liquidity_transactions(transactions, market_data)
llt.index = llt.index.map(utils.format_asset)

print('Tickers with daily transactions consuming >{}% of daily bar \n'
'all backtest:'.format(trade_daily_vol_limit * 100))
utils.print_table(
Expand Down

0 comments on commit 3dcb13f

Please sign in to comment.