From 85433bd3bda5b0ee75ff2a826ea5616e95e1a4bc Mon Sep 17 00:00:00 2001 From: huaiweicheng Date: Mon, 23 Sep 2019 10:56:13 +0800 Subject: [PATCH 1/3] del useless params in full tear sheet. add and adjust return_fig --- pyfolio/tears.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/pyfolio/tears.py b/pyfolio/tears.py index 2784f79d..5b38f0ab 100644 --- a/pyfolio/tears.py +++ b/pyfolio/tears.py @@ -66,12 +66,6 @@ def create_full_tear_sheet(returns, cone_std=(1.0, 1.5, 2.0), bootstrap=False, unadjusted_returns=None, - style_factors=None, - sectors=None, - caps=None, - shares_held=None, - volumes=None, - percentile=None, turnover_denom='AGB', set_context=True, factor_returns=None, @@ -603,8 +597,8 @@ def create_returns_tear_sheet(returns, positions=None, @plotting.customize def create_position_tear_sheet(returns, positions, show_and_plot_top_pos=2, hide_positions=False, - return_fig=False, sector_mappings=None, - transactions=None, estimate_intraday='infer'): + sector_mappings=None, transactions=None, + estimate_intraday='infer', return_fig=False): """ Generate a number of plots for analyzing a strategy's positions and holdings. @@ -627,8 +621,6 @@ def create_position_tear_sheet(returns, positions, hide_positions : bool, optional If True, will not output any symbol names. Overrides show_and_plot_top_pos to 0 to suppress text output. - return_fig : boolean, optional - If True, returns the figure that was plotted on. sector_mappings : dict or pd.Series, optional Security identifier to sector mapping. Security ids as keys, sectors as values. @@ -638,6 +630,8 @@ def create_position_tear_sheet(returns, positions, estimate_intraday: boolean or str, optional Approximate returns for intraday strategies. See description in create_full_tear_sheet. + return_fig : boolean, optional + If True, returns the figure that was plotted on. """ positions = utils.check_intraday(estimate_intraday, returns, @@ -956,7 +950,8 @@ def create_capacity_tear_sheet(returns, positions, transactions, trade_daily_vol_limit=0.05, last_n_days=utils.APPROX_BDAYS_PER_MONTH * 6, days_to_liquidate_limit=1, - estimate_intraday='infer'): + estimate_intraday='infer', + return_fig=False): """ Generates a report detailing portfolio size constraints set by least liquid tickers. Plots a "capacity sweep," a curve describing @@ -993,6 +988,8 @@ def create_capacity_tear_sheet(returns, positions, transactions, estimate_intraday: boolean or str, optional Approximate returns for intraday strategies. See description in create_full_tear_sheet. + return_fig : boolean, optional + If True, returns the figure that was plotted on. """ positions = utils.check_intraday(estimate_intraday, returns, @@ -1046,7 +1043,7 @@ def create_capacity_tear_sheet(returns, positions, transactions, llt[llt['max_pct_bar_consumed'] > trade_daily_vol_limit * 100]) bt_starting_capital = positions.iloc[0].sum() / (1 + returns.iloc[0]) - _, ax_capacity_sweep = plt.subplots(figsize=(14, 6)) + fig, ax_capacity_sweep = plt.subplots(figsize=(14, 6)) plotting.plot_capacity_sweep(returns, transactions, market_data, bt_starting_capital, min_pv=100000, @@ -1054,6 +1051,9 @@ def create_capacity_tear_sheet(returns, positions, transactions, step_size=1000000, ax=ax_capacity_sweep) + if return_fig: + return fig + @plotting.customize def create_perf_attrib_tear_sheet(returns, @@ -1062,8 +1062,8 @@ def create_perf_attrib_tear_sheet(returns, factor_loadings, transactions=None, pos_in_dollars=True, - return_fig=False, - factor_partitions=FACTOR_PARTITIONS): + factor_partitions=FACTOR_PARTITIONS, + return_fig=False): """ Generate plots and tables for analyzing a strategy's performance. @@ -1093,15 +1093,15 @@ def create_perf_attrib_tear_sheet(returns, Flag indicating whether `positions` are in dollars or percentages If True, positions are in dollars. - return_fig : boolean, optional - If True, returns the figure that was plotted on. - factor_partitions : dict dict specifying how factors should be separated in factor returns and risk exposures plots - Example: {'style': ['momentum', 'size', 'value', ...], 'sector': ['technology', 'materials', ... ]} + + return_fig : boolean, optional + If True, returns the figure that was plotted on. """ portfolio_exposures, perf_attrib_data = perf_attrib.perf_attrib( returns, positions, factor_returns, factor_loadings, transactions, From 870b02282219abedcd62df2405ea735c7509aed4 Mon Sep 17 00:00:00 2001 From: huaiweicheng Date: Mon, 23 Sep 2019 11:10:28 +0800 Subject: [PATCH 2/3] add turnover_denom para in txn tear sheet --- pyfolio/plotting.py | 7 +++++-- pyfolio/tears.py | 6 ++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pyfolio/plotting.py b/pyfolio/plotting.py index fe9cbca5..c4db59dd 100644 --- a/pyfolio/plotting.py +++ b/pyfolio/plotting.py @@ -1323,7 +1323,7 @@ def plot_return_quantiles(returns, live_start_date=None, ax=None, **kwargs): return ax -def plot_turnover(returns, transactions, positions, +def plot_turnover(returns, transactions, positions, turnover_denom='AGB', legend_loc='best', ax=None, **kwargs): """ Plots turnover vs. date. @@ -1345,6 +1345,9 @@ def plot_turnover(returns, transactions, positions, positions : pd.DataFrame Daily net position values. - See full explanation in tears.create_full_tear_sheet. + turnover_denom : str, optional + Either AGB or portfolio_value, default AGB. + - See full explanation in txn.get_turnover. legend_loc : matplotlib.loc, optional The location of the legend on the plot. ax : matplotlib.Axes, optional @@ -1364,7 +1367,7 @@ def plot_turnover(returns, transactions, positions, y_axis_formatter = FuncFormatter(utils.two_dec_places) ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter)) - df_turnover = txn.get_turnover(positions, transactions) + df_turnover = txn.get_turnover(positions, transactions, turnover_denom) df_turnover_by_month = df_turnover.resample("M").mean() df_turnover.plot(color='steelblue', alpha=1.0, lw=0.5, ax=ax, **kwargs) df_turnover_by_month.plot( diff --git a/pyfolio/tears.py b/pyfolio/tears.py index 5b38f0ab..c5e8b778 100644 --- a/pyfolio/tears.py +++ b/pyfolio/tears.py @@ -396,6 +396,7 @@ def create_simple_tear_sheet(returns, plotting.plot_turnover(returns, transactions, positions, + turnover_denom=turnover_denom, ax=ax_turnover) plotting.plot_txn_time_hist(transactions, ax=ax_txn_timings) @@ -691,8 +692,8 @@ def create_position_tear_sheet(returns, positions, @plotting.customize def create_txn_tear_sheet(returns, positions, transactions, - unadjusted_returns=None, estimate_intraday='infer', - return_fig=False): + turnover_denom='AGB', unadjusted_returns=None, + estimate_intraday='infer', return_fig=False): """ Generate a number of plots for analyzing a strategy's transactions. @@ -737,6 +738,7 @@ def create_txn_tear_sheet(returns, positions, transactions, returns, transactions, positions, + turnover_denom=turnover_denom, ax=ax_turnover) plotting.plot_daily_volume(returns, transactions, ax=ax_daily_volume) From ef516e9e4994b211a11f3c86ff7c284b21c1e142 Mon Sep 17 00:00:00 2001 From: huaiweicheng Date: Mon, 23 Sep 2019 13:32:44 +0800 Subject: [PATCH 3/3] add turnover_denom para in txn tear sheet --- pyfolio/plotting.py | 7 +++++-- pyfolio/tears.py | 7 ++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pyfolio/plotting.py b/pyfolio/plotting.py index c4db59dd..bd340136 100644 --- a/pyfolio/plotting.py +++ b/pyfolio/plotting.py @@ -1520,7 +1520,7 @@ def plot_capacity_sweep(returns, transactions, market_data, return ax -def plot_daily_turnover_hist(transactions, positions, +def plot_daily_turnover_hist(transactions, positions, turnover_denom='AGB', ax=None, **kwargs): """ Plots a histogram of daily turnover rates. @@ -1533,6 +1533,9 @@ def plot_daily_turnover_hist(transactions, positions, positions : pd.DataFrame Daily net position values. - See full explanation in tears.create_full_tear_sheet. + turnover_denom : str, optional + Either AGB or portfolio_value, default AGB. + - See full explanation in txn.get_turnover. ax : matplotlib.Axes, optional Axes upon which to plot. **kwargs, optional @@ -1546,7 +1549,7 @@ def plot_daily_turnover_hist(transactions, positions, if ax is None: ax = plt.gca() - turnover = txn.get_turnover(positions, transactions) + turnover = txn.get_turnover(positions, transactions, turnover_denom) sns.distplot(turnover, ax=ax, **kwargs) ax.set_title('Distribution of daily turnover rates') ax.set_xlabel('Turnover rate') diff --git a/pyfolio/tears.py b/pyfolio/tears.py index c5e8b778..53e5c6a5 100644 --- a/pyfolio/tears.py +++ b/pyfolio/tears.py @@ -710,6 +710,9 @@ def create_txn_tear_sheet(returns, positions, transactions, transactions : pd.DataFrame Prices and amounts of executed trades. One row per trade. - See full explanation in create_full_tear_sheet. + turnover_denom : str, optional + Either AGB or portfolio_value, default AGB. + - See full explanation in txn.get_turnover. unadjusted_returns : pd.Series, optional Daily unadjusted returns of the strategy, noncumulative. Will plot additional swippage sweep analysis. @@ -744,7 +747,9 @@ def create_txn_tear_sheet(returns, positions, transactions, plotting.plot_daily_volume(returns, transactions, ax=ax_daily_volume) try: - plotting.plot_daily_turnover_hist(transactions, positions, + plotting.plot_daily_turnover_hist(transactions, + positions, + turnover_denom=turnover_denom, ax=ax_turnover_hist) except ValueError: warnings.warn('Unable to generate turnover plot.', UserWarning)