From 3dcb13fa259ee2a32a8406b22d3860fe6b1d0e7f Mon Sep 17 00:00:00 2001 From: gusgordon Date: Wed, 4 Jan 2017 18:34:13 +0000 Subject: [PATCH] ENH Format assets in capacity tear sheet; fix warnings --- pyfolio/capacity.py | 7 ++++--- pyfolio/tears.py | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pyfolio/capacity.py b/pyfolio/capacity.py index 7c7a0f20..aeb3316d 100644 --- a/pyfolio/capacity.py +++ b/pyfolio/capacity.py @@ -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) @@ -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 @@ -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']] diff --git a/pyfolio/tears.py b/pyfolio/tears.py index c94d7920..d7bb46f5 100644 --- a/pyfolio/tears.py +++ b/pyfolio/tears.py @@ -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( @@ -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(