Exemple #1
0
 def __init__(self,
              engine='plotly',
              data_source='bloomberg',
              market_data_generator=MarketDataGenerator()):
     self._chart = Chart(engine=engine)
     self._market = Market(market_data_generator=market_data_generator)
     self._data_source = data_source
def plot_animated_vol_market():
    market = Market(market_data_generator=MarketDataGenerator())

    cross = ['EURUSD']; start_date = '01 Mar 2017'; finish_date = '21 Apr 2017'; sampling = 'no'

    md_request = MarketDataRequest(start_date=start_date, finish_date=finish_date,
                                   data_source='bloomberg', cut='NYC', category='fx-implied-vol',
                                   tickers=cross, cache_algo='cache_algo_return')

    df = market.fetch_market(md_request)
    if sampling != 'no': df = df.resample(sampling).mean()
    fxvf = FXVolFactory()
    df_vs = []

    # Grab the vol surface for each date and create a dataframe for each date (could have used a panel)
    for i in range(0, len(df.index)): df_vs.append(fxvf.extract_vol_surface_for_date(df, cross[0], i))

    # Do static plot for first day using Plotly
    style = Style(title="FX vol surface of " + cross[0], source="chartpy", color='Blues')

    Chart(df=df_vs[0], chart_type='surface', style=style).plot(engine='plotly')

    # Now do animation (TODO: need to fix animation in chartpy for matplotlib)
    style = Style(title="FX vol surface of " + cross[0], source="chartpy", color='Blues',
                    animate_figure=True, animate_titles=df.index,
                    animate_frame_ms=500, normalize_colormap=False)

    Chart(df=df_vs, chart_type='surface', style=style).plot(engine='plotly')

    # Chart object is initialised with the dataframe and our chart style
    Chart(df=df_vs, chart_type='surface', style=style).plot(engine='plotly')
    def plot_strategy_group_benchmark_pnl_yoy(self, strip = None, silent_plot = False):

        style = self.create_style("", "Group Benchmark PnL YoY - cumulative")
        keys = self._strategy_group_benchmark_ret_stats.keys()
        yoy = []

        for key in keys:
            col = self._strategy_group_benchmark_ret_stats[key].yoy_rets()
            col.columns = [key]
            yoy.append(col)

        calculations = Calculations()
        ret_stats = calculations.pandas_outer_join(yoy)
        ret_stats.index = ret_stats.index.year

        if strip is not None: ret_stats.columns = [k.replace(strip, '') for k in ret_stats.columns]

        # ret_stats = ret_stats.sort_index()
        style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - YoY) ' + str(style.scale_factor) + '.png'
        style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - YoY) ' + str(style.scale_factor) + '.html'
        style.display_brand_label = False
        style.date_formatter = "%Y"

        chart = Chart(ret_stats * 100, engine=self.DEFAULT_PLOT_ENGINE, chart_type='bar', style=style)
        if not (silent_plot): chart.plot()
        return chart
    def plot_strategy_group_benchmark_pnl_yoy(self, strip = None, silent_plot = False):

        style = self.create_style("", "Group Benchmark PnL YoY - cumulative")
        keys = self._strategy_group_benchmark_ret_stats.keys()
        yoy = []

        for key in keys:
            col = self._strategy_group_benchmark_ret_stats[key].yoy_rets()
            col.columns = [key]
            yoy.append(col)

        calculations = Calculations()
        ret_stats = calculations.pandas_outer_join(yoy)
        ret_stats.index = ret_stats.index.year

        if strip is not None: ret_stats.columns = [k.replace(strip, '') for k in ret_stats.columns]

        # ret_stats = ret_stats.sort_index()
        style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - YoY) ' + str(style.scale_factor) + '.png'
        style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - YoY) ' + str(style.scale_factor) + '.html'
        style.display_brand_label = False
        style.date_formatter = "%Y"

        chart = Chart(ret_stats * 100, engine=self.DEFAULT_PLOT_ENGINE, chart_type='bar', style=style)
        if not (silent_plot): chart.plot()
        return chart
Exemple #5
0
    def plot_strategy_group_benchmark_pnl_ir(self,
                                             strip=None,
                                             silent_plot=False):

        style = self.create_style("", "Group Benchmark PnL IR - cumulative")
        keys = self._strategy_group_benchmark_ret_stats.keys()
        ir = []

        for key in keys:
            ir.append(
                self._strategy_group_benchmark_ret_stats[key].inforatio()[0])

        if strip is not None: keys = [k.replace(strip, '') for k in keys]

        ret_stats = pandas.DataFrame(index=keys, data=ir, columns=['IR'])
        # ret_stats = ret_stats.sort_index()
        style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - IR) ' + str(
            style.scale_factor) + '.png'
        style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - IR) ' + str(
            style.scale_factor) + '.html'
        style.display_brand_label = False

        chart = Chart(ret_stats,
                      engine=self.DEFAULT_PLOT_ENGINE,
                      chart_type='bar',
                      style=style)
        if not (silent_plot): chart.plot()
        return chart
Exemple #6
0
    def __init__(self, engine=ChartConstants().chartfactory_default_engine):
        # self.logger = LoggerManager().getLogger(__name__)
        self.DUMP_PATH = 'output_data/' + datetime.date.today().strftime("%Y%m%d") + ' '
        self.DEFAULT_PLOT_ENGINE = engine
        self.chart = Chart(engine=self.DEFAULT_PLOT_ENGINE)

        return
Exemple #7
0
    def plot_strategy_trade_no(self, strip=None, silent_plot=False):
        signal = self._strategy_signal

        ####### how many trades have there been (ignore size of the trades)
        trades = abs(signal - signal.shift(-1))
        trades = trades[trades > 0].count()

        df_trades = pandas.DataFrame(index=trades.index,
                                     columns=['Trades'],
                                     data=trades)

        if strip is not None:
            df_trades.index = [k.replace(strip, '') for k in df_trades.index]

        style = self.create_style("", "")

        try:
            style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy trade no).png'
            style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy trade no).html'

            chart = Chart(self.reduce_plot(df_trades),
                          engine=self.DEFAULT_PLOT_ENGINE,
                          chart_type='bar',
                          style=style)
            if not (silent_plot): chart.plot()
            return chart
        except:
            pass
Exemple #8
0
    def plot_sharpe(self, silent_plot=False, reduce_plot=True):
        """
        Plots the sharpe ratio of the model
        Returns the created Chart

        Parameters
        ---------
        silent_plot
            if you want to only return the chart created
        reduce_plot:
            when plotting many points use WebGl version of plotly if specified
        """
        #sharpe does not take into account risk free rate for simplicity
        style = self.models[self.ref_index]._create_style(
            "", "Sharpe Curve", reduce_plot=reduce_plot)

        models = self.models
        ref = self.ref_index

        returns = [model._strategy_pnl.pct_change() for model in models]
        stdev_of_returns = np.std(returns)

        annualized_sharpe = returns / stdev_of_returns * np.sqrt(250)

        df = pd.concat(annualized_sharpe, axis=1)

        chart = Chart(df,
                      engine=self.DEFAULT_PLOT_ENGINE,
                      chart_type='bar',
                      style=style)
        if not silent_plot:
            chart.plot()
        return chart
    def plot_strategy_group_leverage(self, silent_plot = False):

        style = self.create_style("Leverage", "Group Leverage")

        chart = Chart(self.reduce_plot(self._strategy_group_leverage), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
        if not (silent_plot): chart.plot()
        return chart
    def plot_strategy_signal_proportion(self, strip = None, silent_plot = False):

        signal = self._strategy_signal

        ####### count number of long, short and flat periods in our sample
        long = signal[signal > 0].count()
        short = signal[signal < 0].count()
        flat = signal[signal == 0].count()

        df = pandas.DataFrame(index = long.index, columns = ['Long', 'Short', 'Flat'])

        df['Long'] = long
        df['Short']  = short
        df['Flat'] = flat

        if strip is not None: df.index = [k.replace(strip, '') for k in df.index]

        style = self.create_style("", "")

        try:
            style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).png'
            style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).html'

            chart = Chart(self.reduce_plot(df), engine=self.DEFAULT_PLOT_ENGINE, chart_type='bar', style=style)
            if not(silent_plot): chart.plot()
            return chart
        except: pass
    def plot_strategy_trade_notional(self,
                                     diff=True,
                                     silent_plot=False,
                                     reduce_plot=True):
        style = self.models[self.ref_index]._create_style(
            "", "Trades (Scaled by Notional)", reduce_plot=reduce_plot)

        models = self.models
        ref = self.ref_index

        strategy_trade_notional = [
            model._strategy_trade_notional for model in models
        ]

        df = pd.concat(strategy_trade_notional, axis=1)

        if diff:
            df = df.subtract(strategy_trade_notional[ref], axis='index')
        if self.labels is not None:
            df.columns = self.labels

        chart = Chart(df,
                      engine=self.DEFAULT_PLOT_ENGINE,
                      chart_type='bar',
                      style=style)
        if not silent_plot:
            chart.plot()

        return chart
    def plot_strategy_signal_proportion(self, strip = None, silent_plot = False):

        signal = self._strategy_signal

        ####### count number of long, short and flat periods in our sample
        long = signal[signal > 0].count()
        short = signal[signal < 0].count()
        flat = signal[signal == 0].count()

        df = pandas.DataFrame(index = long.index, columns = ['Long', 'Short', 'Flat'])

        df['Long'] = long
        df['Short']  = short
        df['Flat'] = flat

        if strip is not None: df.index = [k.replace(strip, '') for k in df.index]

        style = self.create_style("", "")

        try:
            style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).png'
            style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy signal proportion).html'

            chart = Chart(self.reduce_plot(df), engine=self.DEFAULT_PLOT_ENGINE, chart_type='bar', style=style)
            if not(silent_plot): chart.plot()
            return chart
        except: pass
    def plot_strategy_group_leverage(self, silent_plot = False):

        style = self.create_style("Leverage", "Group Leverage")

        chart = Chart(self.reduce_plot(self._strategy_group_leverage), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
        if not (silent_plot): chart.plot()
        return chart
    def plot_strategy_signals(self, date = None, strip = None, silent_plot = False):

        ######## plot signals
        strategy_signal = self._strategy_signal
        strategy_signal = 100 * (strategy_signal)

        if date is None:
            last_day = strategy_signal.ix[-1].transpose().to_frame()
        else:
            if not(isinstance(date, list)):
                date = [date]

            last_day = []

            for d in date:
                last_day.append(strategy_signal.ix[d].transpose().to_frame())

            last_day = pandas.concat(last_day, axis=1)
            last_day = last_day.sort_index(axis=1)

        if strip is not None:
            last_day.index = [x.replace(strip, '') for x in last_day.index]

        style = self.create_style("positions (% portfolio notional)", "Positions")

        chart = Chart(last_day, engine=self.DEFAULT_PLOT_ENGINE, chart_type='bar', style=style)
        if not (silent_plot): chart.plot()
        return chart
Exemple #15
0
    def __init__(self,
                 computation_results,
                 title='Cuemacro Computation',
                 renderer=CanvasRenderer(),
                 chart_report_height=constants.chart_report_height,
                 chart_report_width=constants.chart_report_width):
        """Initialize class, with the computation results we wish to convert into a report like format

        Parameters
        ----------
        computation_results : ComputationResults
            The results of a large scale computation, which contains charts and DataFrames

        title : str
            Title of webpage to be rendered
        """
        self._util_func = UtilFunc()
        self._computation_results = computation_results
        self._title = title
        self._chart = Chart(engine='plotly')
        self._renderer = renderer
        self._computation_request = computation_results.computation_request

        self._chart_report_width = chart_report_width
        self._chart_report_height = chart_report_height
Exemple #16
0
def example_request_mid_benchmark():
    """Example of how to do a calculation to do market analysis to calculate mid, resample etc. without any trade data

    """
    from tcapy.analysis.algos.benchmark import BenchmarkMarketMid, BenchmarkMarketSpreadToMid, BenchmarkMarketResampleOffset, \
        BenchmarkMarketFilter
    from tcapy.analysis.algos.resultsform import BarResultsForm, TimelineResultsForm

    tca_request = get_tca_request()

    # Allow analysis to be done in a parallel approach day by day
    # (note: can't do analysis which requires data outside of the daily chunks to do this!)
    tca_request.multithreading_params['splice_request_by_dates'] = use_multithreading

    # Filter market data by time of day between 15:00-17:00 LDN
    # Then calculate the market mid, then calculate the spread to the mid,
    # Then resample the data into 1 minute, taking the mean of each minute (and TWAP) and calculating the absolute range
    tca_request.benchmark_calcs = [BenchmarkMarketFilter(time_of_day={'start_time' : "15:00", 'finish_time' : "17:00"},
                                                         time_zone='Europe/London'),
                                   BenchmarkMarketMid(), BenchmarkMarketSpreadToMid(),
                                   BenchmarkMarketResampleOffset(market_resample_freq='1', market_resample_unit='min',
                                        price_field='mid', resample_how=['mean', 'twap', 'absrange'], dropna=True),
                                   ]

    # Calculate the mean spread to mid for EURUSD by time of day during our sample (do not weight by any other field)
    # Calculate the mean absrange for EURUSD by time of day (London timezone)/month of _year (ie. proxy for volatility)
    tca_request.results_form = \
        [TimelineResultsForm(market_trade_order_list='EURUSD', metric_name='ask_mid_spread',
                             weighting_field=None, by_date='time', scalar=10000.0),
         TimelineResultsForm(market_trade_order_list='EURUSD', metric_name='absrange',
                             weighting_field=None, by_date=['month', 'timeldn'], scalar=10000.0)
        ]

    # return
    tca_request.use_multithreading = True

    tca_engine = TCAEngineImpl()

    dict_of_df = tca_engine.calculate_tca(tca_request)

    # Print out all keys for all the DataFrames returned
    print(dict_of_df.keys())

    # Print market data snapshots
    print(dict_of_df['EURUSD_df'])
    print(dict_of_df['USDJPY_df'])
    print(dict_of_df['EURUSD_df'].columns)
    print(dict_of_df['USDJPY_df'].columns)

    # Print out mean spread by time of day
    print(dict_of_df['timeline_EURUSD_ask_mid_spread_by/mean_time/all'])

    # Plot mean spread by time of day and absrange by time of day (in London timezone)
    Chart(engine='plotly').plot(dict_of_df['timeline_EURUSD_ask_mid_spread_by/mean_time/all'])

    # Plot absolute range over each minute, averaged by time of day and month of the _year
    Chart(engine='plotly').plot(dict_of_df['timeline_EURUSD_absrange_by/mean_month_timeldn/all'],
                                style=Style(title='EURUSD absolute range by time of day (LDN)', color='Reds', scale_factor=-1))
def dataframe_tca_example():
    """Example for doing detailed TCA analysis on all the trades in a CSV, calculating metrics for slippage,
    transient market impact & permanent market impact. It also calculates benchmarks for arrival price of each trade and
    spread to mid).

    Collects results for slippage into a daily timeline and also average by venue (by default weights by reporting
    currency)
    """
    PLOT = False

    # clear entire cache
    # Mediator.get_volatile_cache(version='pro').clear_cache()

    tca_engine = TCAEngineImpl(version=tca_version)

    trade_order_type = 'trade_df'
    trade_order_list = ['trade_df']

    trade_df = DatabaseSourceCSV(trade_data_database_csv=csv_trade_order_mapping['trade_df']).fetch_trade_order_data()

    data_frame_trade_order_mapping = OrderedDict([('trade_df', trade_df)])

    start_date = trade_df.index[0]; finish_date = trade_df.index[-1]

    ticker_list = FXConv().correct_unique_notation_list(trade_df['ticker'].unique().tolist())

    # Specify the TCA request
    tca_request = TCARequest(start_date=start_date, finish_date=finish_date, ticker=ticker_list,
                             tca_type='aggregated', dummy_market=True,
                             trade_data_store='dataframe', market_data_store=market_data_store,
                             metric_calcs=[MetricSlippage(trade_order_list=trade_order_list),
                                           MetricTransientMarketImpact(transient_market_impact_gap={'ms': 100},
                                                                       trade_order_list=trade_order_list),
                                           MetricPermanentMarketImpact(permanent_market_impact_gap={'h': 1},
                                                                       trade_order_list=trade_order_list)],
                             results_form=[TimelineResultsForm(metric_name='slippage', by_date='date'),
                                           BarResultsForm(metric_name='slippage', aggregate_by_field='venue')],
                             benchmark_calcs=[BenchmarkArrival(), BenchmarkSpreadToMid()],
                             trade_order_mapping=data_frame_trade_order_mapping, use_multithreading=False)

    # Dictionary of dataframes as output from TCA calculation
    dict_of_df = tca_engine.calculate_tca(tca_request)

    print(dict_of_df.keys())

    timeline_df = dict_of_df['timeline_' + trade_order_type + '_slippage_by_all']  # average slippage per day
    metric_df = dict_of_df[trade_order_type]['permanent_market_impact']  # permanent market impact for every trade

    print(metric_df.head(500))

    if PLOT:
        from chartpy import Chart, Style

        # plot slippage by timeline
        Chart(engine='plotly').plot(timeline_df)

        # plot market impact (per trade)
        Chart(engine='plotly').plot(metric_df.head(500))
    def plot_strategy_pnl(self, silent_plot = False):

        style = self.create_style("", "Strategy PnL")

        try:
            chart = Chart(self.reduce_plot(self._strategy_pnl), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
            if not(silent_plot): chart.plot()
            return chart
        except: pass
    def plot_individual_leverage(self, silent_plot=False):

        style = self.create_style("Leverage", "Individual Leverage")

        try:
            chart = Chart(self.reduce_plot(self._individual_leverage), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
            if not(silent_plot): chart.plot()
            return chart
        except: pass
    def plot_strategy_pnl(self, silent_plot = False):

        style = self.create_style("", "Strategy PnL")

        try:
            chart = Chart(self.reduce_plot(self._strategy_pnl), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
            if not(silent_plot): chart.plot()
            return chart
        except: pass
    def plot_individual_leverage(self, silent_plot=False):

        style = self.create_style("Leverage", "Individual Leverage")

        try:
            chart = Chart(self.reduce_plot(self._individual_leverage), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
            if not(silent_plot): chart.plot()
            return chart
        except: pass
Exemple #22
0
    def plot_single_var_regression(self,
                                   y,
                                   x,
                                   y_variable_names,
                                   x_variable_names,
                                   statistic,
                                   tag='stats',
                                   title=None,
                                   pretty_index=None,
                                   output_path=None,
                                   scale_factor=None,
                                   silent_plot=False,
                                   shift=[0]):

        if not (isinstance(statistic, list)):
            statistic = [statistic]

        # TODO optimise loop so that we are calculating each regression *once* at present calculating it
        # for each statistic, which is redundant
        for st in statistic:
            stats_df = []

            for sh in shift:
                x_sh = x.shift(sh)
                stats_temp = self.report_single_var_regression(
                    y, x_sh, y_variable_names, x_variable_names, st,
                    pretty_index)

                stats_temp.columns = [
                    x + "_" + str(sh) for x in stats_temp.columns
                ]

                stats_df.append(stats_temp)

            stats_df = pandas.concat(stats_df, axis=1)
            stats_df = stats_df.dropna(how='all')

            if silent_plot: return stats_df

            chart = Chart()
            style = Style()

            if title is None: title = statistic

            style.title = title
            style.display_legend = True
            style.scale_factor = scale_factor
            # style.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink']

            if output_path is not None:
                style.file_output = output_path + ' (' + tag + ' ' + st + ').png'

            chart.plot(stats_df, style=style)

        return stats_df
Exemple #23
0
    def _plot_signal(self, sig, label = ' ', caption = '', date = None, strip = None, silent_plot = False):

        ######## plot signals
        strategy_signal = 100 * (sig)
        last_day = self.grab_signals(strategy_signal, date=date, strip=strip)

        style = self.create_style(label, caption)

        chart = Chart(last_day, engine=self.DEFAULT_PLOT_ENGINE, chart_type='bar', style=style)
        if not (silent_plot): chart.plot()
        return chart
    def plot_strategy_group_benchmark_annualised_pnl(self, cols = None, silent_plot = False):
        # TODO - unfinished, needs checking!

        if cols is None: cols = self._strategy_group_benchmark_annualised_pnl.columns

        style = self.create_style("", "Group Benchmark Annualised PnL")
        style.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink']

        chart = Chart(self.reduce_plot(self._strategy_group_benchmark_annualised_pnl[cols]), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
        if not (silent_plot): chart.plot()
        return chart
    def _plot_signal(self, sig, label = ' ', caption = '', date = None, strip = None, silent_plot = False):

        ######## plot signals
        strategy_signal = 100 * (sig)
        last_day = self.grab_signals(strategy_signal, date=date, strip=strip)

        style = self.create_style(label, caption)

        chart = Chart(last_day, engine=self.DEFAULT_PLOT_ENGINE, chart_type='bar', style=style)
        if not (silent_plot): chart.plot()
        return chart
    def plot_strategy_group_benchmark_annualised_pnl(self, cols = None, silent_plot = False):
        # TODO - unfinished, needs checking!

        if cols is None: cols = self._strategy_group_benchmark_annualised_pnl.columns

        style = self.create_style("", "Group Benchmark Annualised PnL")
        style.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink']

        chart = Chart(self.reduce_plot(self._strategy_group_benchmark_annualised_pnl[cols]), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
        if not (silent_plot): chart.plot()
        return chart
    def plot_strategy_group_benchmark_pnl(self, silent_plot = False):

        style = self.create_style("", "Group Benchmark PnL - cumulative")

        strat_list = self._strategy_group_benchmark_pnl.columns #.sort_values()

        for line in strat_list:
            self.logger.info(line)

        # plot cumulative line of returns
        chart = Chart(self.reduce_plot(self._strategy_group_benchmark_pnl), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line',
                      style=style)
        if not (silent_plot): chart.plot()
        return chart
    def plot_strategy_group_benchmark_pnl(self, silent_plot = False):

        style = self.create_style("", "Group Benchmark PnL - cumulative")

        strat_list = self._strategy_group_benchmark_pnl.columns #.sort_values()

        for line in strat_list:
            self.logger.info(line)

        # plot cumulative line of returns
        chart = Chart(self.reduce_plot(self._strategy_group_benchmark_pnl), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line',
                      style=style)
        if not (silent_plot): chart.plot()
        return chart
    def plot_strategy_group_pnl_trades(self, silent_plot = False):

        style = self.create_style("(bp)", "Individual Trade PnL")

        # zero when there isn't a trade exit
        # strategy_pnl_trades = self._strategy_pnl_trades * 100 * 100
        # strategy_pnl_trades = strategy_pnl_trades.dropna()

        # note only works with single large basket trade
        try:
            strategy_pnl_trades = self._strategy_pnl_trades.fillna(0) * 100 * 100
            chart = Chart(self.reduce_plot(strategy_pnl_trades), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
            if not(silent_plot): chart.plot()
            return chart
        except: pass
    def plot_strategy_group_pnl_trades(self, silent_plot = False):

        style = self.create_style("(bp)", "Individual Trade PnL")

        # zero when there isn't a trade exit
        # strategy_pnl_trades = self._strategy_pnl_trades * 100 * 100
        # strategy_pnl_trades = strategy_pnl_trades.dropna()

        # note only works with single large basket trade
        try:
            strategy_pnl_trades = self._strategy_pnl_trades.fillna(0) * 100 * 100
            chart = Chart(self.reduce_plot(strategy_pnl_trades), engine=self.DEFAULT_PLOT_ENGINE, chart_type='line', style=style)
            if not(silent_plot): chart.plot()
            return chart
        except: pass
Exemple #31
0
def venue_tca_aggregated_example():
    """Example of doing an aggregated TCA computation on a single ticker, and then later calculating the probability
    distribution function of slippage split by venue (when weighted by executed notional)
    """
    tca_engine = TCAEngineImpl(version=tca_version)

    tca_request = TCARequest(start_date=start_date,
                             finish_date=finish_date,
                             ticker=ticker,
                             tca_type='aggregated',
                             trade_data_store=trade_data_store,
                             market_data_store=market_data_store,
                             metric_calcs=MetricSlippage())

    dict_of_df = tca_engine.calculate_tca(tca_request)

    summary = ResultsSummary()

    summary_slippage_df = summary.field_distribution(
        dict_of_df['trade_df'],
        metric_name='slippage',
        aggregate_by_field='venue',
        pdf_only=True,
        weighting_field='executed_notional')

    # Plot PDF of slippage, split up by venue
    Chart(engine='plotly').plot(summary_slippage_df,
                                style=Style(plotly_plot_mode='offline_html',
                                            connect_line_gaps=True))
Exemple #32
0
    def __init__(self, computation_results, title='Cuemacro Computation'):
        """Initialize class, with the computation results we wish to convert into a report like format

        Parameters
        ----------
        computation_results : ComputationResults
            The results of a large scale computation, which contains charts and DataFrames

        title : str
            Title of webpage to be rendered
        """
        self._util_func = UtilFunc()
        self._computation_results = computation_results
        self._title = title
        self._canvas_plotter = 'plain'
        self._chart = Chart(engine='plotly')
    def __init__(self, engine = ChartConstants().chartfactory_default_engine):
        self.logger = LoggerManager().getLogger(__name__)
        self.DUMP_PATH = 'output_data/' + datetime.date.today().strftime("%Y%m%d") + ' '
        self.SCALE_FACTOR = 3
        self.DEFAULT_PLOT_ENGINE = engine
        self.chart = Chart(engine=self.DEFAULT_PLOT_ENGINE)

        return
Exemple #34
0
    def plot_single_var_regression(self, y, x, y_variable_names, x_variable_names, statistic,
                                   tag = 'stats',
                                   title = None,
                                   pretty_index = None, output_path = None,
                                   scale_factor = None,
                                   silent_plot = False,
                                   shift=[0]):

        if not(isinstance(statistic, list)):
            statistic = [statistic]

        # TODO optimise loop so that we are calculating each regression *once* at present calculating it
        # for each statistic, which is redundant
        for st in statistic:
            stats_df = []

            for sh in shift:
                x_sh = x.shift(sh)
                stats_temp = self.report_single_var_regression(y, x_sh, y_variable_names, x_variable_names, st,
                                                             pretty_index)

                stats_temp.columns = [ x + "_" + str(sh) for x in stats_temp.columns]

                stats_df.append(stats_temp)

            stats_df = pandas.concat(stats_df, axis=1)
            stats_df = stats_df.dropna(how='all')

            if silent_plot: return stats_df

            chart = Chart()
            style = Style()

            if title is None: title = statistic

            style.title = title
            style.display_legend = True
            style.scale_factor = scale_factor
            # style.color = ['red', 'blue', 'purple', 'gray', 'yellow', 'green', 'pink']

            if output_path is not None:
                style.file_output = output_path + ' (' + tag + ' ' + st + ').png'

            chart.plot(stats_df, style=style)

        return stats_df
Exemple #35
0
    def plot_strategy_trade_notional(self,
                                     diff=True,
                                     silent_plot=False,
                                     reduce_plot=True):
        """
        Plots the notional trades of the model
        Returns the created Chart

        Parameters
        ---------
        diff
            if you want to get the profit differences instead of raw values
        silent_plot
            if you want to only return the chart created
        reduce_plot:
            when plotting many points use WebGl version of plotly if specified
        """
        style = self.models[self.ref_index]._create_style(
            "", "Trades (Scaled by Notional)", reduce_plot=reduce_plot)

        models = self.models
        ref = self.ref_index

        strategy_trade_notional = [
            model._strategy_trade_notional for model in models
        ]

        df = pd.concat(strategy_trade_notional, axis=1)
        #if you want to get the profit differences instead of raw values
        if diff:
            df = df.subtract(strategy_trade_notional[ref], axis='index')
        if self.labels is not None:
            df.columns = self.labels

        chart = Chart(df,
                      engine=self.DEFAULT_PLOT_ENGINE,
                      chart_type='bar',
                      style=style)
        if not silent_plot:
            chart.plot()

        return chart
    def plot_strategy_group_benchmark_pnl_ir(self, strip = None, silent_plot = False):

        style = self.create_style("", "Group Benchmark PnL IR - cumulative")
        keys = self._strategy_group_benchmark_ret_stats.keys()
        ir = []

        for key in keys:
            ir.append(self._strategy_group_benchmark_ret_stats[key].inforatio()[0])

        if strip is not None: keys = [k.replace(strip, '') for k in keys]

        ret_stats = pandas.DataFrame(index=keys, data=ir, columns=['IR'])
        # ret_stats = ret_stats.sort_index()
        style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - IR) ' + str(style.scale_factor) + '.png'
        style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Group Benchmark PnL - IR) ' + str(style.scale_factor) + '.html'
        style.display_brand_label = False

        chart = Chart(ret_stats, engine=self.DEFAULT_PLOT_ENGINE, chart_type='bar', style=style)
        if not (silent_plot): chart.plot()
        return chart
    def plot_sharpe(self, silent_plot=False, reduce_plot=True):
        style = self.models[self.ref_index]._create_style(
            "", "Sharpe Curve", reduce_plot=reduce_plot)

        models = self.models
        ref = self.ref_index

        returns = [model._strategy_pnl.pct_change() for model in models]
        stdev_of_returns = np.std(returns)

        annualized_sharpe = returns / stdev_of_returns * np.sqrt(250)

        df = pd.concat(annualized_sharpe, axis=1)

        chart = Chart(df,
                      engine=self.DEFAULT_PLOT_ENGINE,
                      chart_type='bar',
                      style=style)
        if not silent_plot:
            chart.plot()
        return chart
    def __init__(self):
        super(TradingModel, self).__init__()

        ##### FILL IN WITH YOUR OWN PARAMETERS FOR display, dumping, TSF etc.
        self.market = Market(market_data_generator=MarketDataGenerator())
        self.DUMP_PATH = ''
        self.FINAL_STRATEGY = 'FX trend'
        self.SCALE_FACTOR = 1
        self.DEFAULT_PLOT_ENGINE = 'matplotlib'

        self.chart = Chart(engine=self.DEFAULT_PLOT_ENGINE)
        self.br = self.load_parameters()
        return
    def plot_strategy_trade_no(self, strip = None, silent_plot = False):
        signal = self._strategy_signal

        ####### how many trades have there been (ignore size of the trades)
        trades = abs(signal - signal.shift(-1))
        trades = trades[trades > 0].count()

        df_trades = pandas.DataFrame(index=trades.index, columns=['Trades'], data=trades)

        if strip is not None: df_trades.index = [k.replace(strip, '') for k in df_trades.index]

        style = self.create_style("", "")

        try:
            style.file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy trade no).png'
            style.html_file_output = self.DUMP_PATH + self.FINAL_STRATEGY + ' (Strategy trade no).html'

            chart = Chart(self.reduce_plot(df_trades), engine=self.DEFAULT_PLOT_ENGINE, chart_type='bar', style=style)
            if not(silent_plot): chart.plot()
            return chart
        except:
            pass
Exemple #40
0
    def calculate_button(self, *args):

        n_clicks, ticker = args

        if ticker == '':
            return {}, "Here is an example of using chartpy with dash"

        try:
            df = pd.DataFrame(quandl.get(ticker))

            df_vol = (df / df.shift(1)).rolling(
                window=20).std() * math.sqrt(252) * 100.0

            spot_fig = Chart(engine="plotly").plot(df,
                                                   style=Style(
                                                       title='Spot',
                                                       plotly_plot_mode='dash',
                                                       width=980,
                                                       height=480,
                                                       scale_factor=-1))

            vol_fig = Chart(engine="plotly").plot(df_vol,
                                                  style=Style(
                                                      title='Realized Vol 1M',
                                                      plotly_plot_mode='dash',
                                                      width=980,
                                                      height=480,
                                                      scale_factor=-1))
            msg = "Plotted " + ticker + " at " + datetime.datetime.utcnow(
            ).strftime("%b %d %Y %H:%M:%S")

            return spot_fig, vol_fig, msg
        except Exception as e:
            print(str(e))
            pass

        return {}, "Failed to download"
Exemple #41
0
    def plot_pnl(self, diff=True, silent_plot=False, reduce_plot=True):
        """
        Plots the profit and loss graph of the model.
        Returns the created Chart

        Parameters
        ---------
        diff
            if you want to get the profit differences instead of raw values
        silent_plot
            if you want to only return the chart created
        reduce_plot
            when plotting many points use WebGl version of plotly if specified
        """
        style = self.models[self.ref_index]._create_style(
            "", "Strategy PnL", reduce_plot=reduce_plot)

        models = self.models
        ref = self.ref_index

        pnls = [model._strategy_pnl for model in models]

        df = pd.concat(pnls, axis=1)
        if diff:
            df = df.subtract(pnls[ref], axis='index')
        if self.labels is not None:
            df.columns = self.labels

        chart = Chart(df,
                      engine=self.DEFAULT_PLOT_ENGINE,
                      chart_type='line',
                      style=style)
        if not silent_plot:
            chart.plot()

        return chart
    def plot_pnl(self, diff=True, silent_plot=False, reduce_plot=True):
        style = self.models[self.ref_index]._create_style(
            "", "Strategy PnL", reduce_plot=reduce_plot)

        models = self.models
        ref = self.ref_index

        pnls = [model._strategy_pnl for model in models]

        df = pd.concat(pnls, axis=1)

        if diff:
            df = df.subtract(pnls[ref], axis='index')
        if self.labels is not None:
            df.columns = self.labels

        chart = Chart(df,
                      engine=self.DEFAULT_PLOT_ENGINE,
                      chart_type='line',
                      style=style)
        if not silent_plot:
            chart.plot()

        return chart
Exemple #43
0
def plot_animated_vol_market():
    market = Market(market_data_generator=MarketDataGenerator())

    cross = ['GBPUSD']
    start_date = '01 Jun 2016'
    finish_date = '01 Aug 2016'
    sampling = 'no'

    md_request = MarketDataRequest(start_date=start_date,
                                   finish_date=finish_date,
                                   data_source='bloomberg',
                                   cut='LDN',
                                   category='fx-implied-vol',
                                   tickers=cross,
                                   cache_algo='internet_load_return')

    df = market.fetch_market(md_request)
    if sampling != 'no': df = df.resample(sampling).mean()
    fxvf = FXVolFactory()
    df_vs = []

    # grab the vol surface for each date and create a dataframe for each date (could have used a panel)
    for i in range(0, len(df.index)):
        df_vs.append(fxvf.extract_vol_surface_for_date(df, cross[0], i))

    style = Style(title="FX vol surface of " + cross[0],
                  source="chartpy",
                  color='Blues',
                  animate_figure=True,
                  animate_titles=df.index,
                  animate_frame_ms=500,
                  normalize_colormap=False)

    # Chart object is initialised with the dataframe and our chart style
    Chart(df=df_vs, chart_type='surface',
          style=style).plot(engine='matplotlib')
__author__ = 'saeedamen'

# loading data
import datetime

from chartpy import Chart, Style
from findatapy.market import Market, MarketDataGenerator, MarketDataRequest

from findatapy.util.loggermanager import LoggerManager

logger = LoggerManager().getLogger(__name__)

chart = Chart(engine='matplotlib')

market = Market(market_data_generator=MarketDataGenerator())

# choose run_example = 0 for everything
# run_example = 1 - download BoE data from quandl

run_example = 0

###### fetch data from Quandl for BoE rate (using Bloomberg data)
if run_example == 1 or run_example == 0:
    # Monthly average of UK resident monetary financial institutions' (excl. Central Bank) sterling
    # Weighted average interest rate, other loans, new advances, on a fixed rate to private non-financial corporations (in percent)
    # not seasonally adjusted
    md_request = MarketDataRequest(
        start_date="01 Jan 2000",  # start date
        data_source='quandl',  # use Quandl as data source
        tickers=['Weighted interest rate'],
        fields=['close'],  # which fields to download
    es = EventStudy()

    # work out cumulative asset price moves moves over the event
    df_event = es.get_intraday_moves_over_custom_event(df, df_event_times)

    # create an average move
    df_event['Avg'] = df_event.mean(axis=1)

    # plotting spot over economic data event
    style = Style()
    style.scale_factor = 3
    style.file_output = 'usdjpy-nfp.png'

    style.title = 'USDJPY spot moves over recent NFP'

    # plot in shades of blue (so earlier releases are lighter, later releases are darker)
    style.color = 'Blues';
    style.color_2 = []
    style.y_axis_2_series = []
    style.display_legend = False

    # last release will be in red, average move in orange
    style.color_2_series = [df_event.columns[-2], df_event.columns[-1]]
    style.color_2 = ['red', 'orange']  # red, pink
    style.linewidth_2 = 2
    style.linewidth_2_series = style.color_2_series

    chart = Chart(engine='matplotlib')
    chart.plot(df_event * 100, style=style)
import pandas

from chartpy import Chart, Style
from finmarketpy.economics import Seasonality
from findatapy.market import Market, MarketDataGenerator, MarketDataRequest

from chartpy.style import Style
from findatapy.timeseries import Calculations
from findatapy.util.loggermanager import LoggerManager

seasonality = Seasonality()
calc = Calculations()
logger = LoggerManager().getLogger(__name__)

chart = Chart(engine='matplotlib')

market = Market(market_data_generator=MarketDataGenerator())

# choose run_example = 0 for everything
# run_example = 1 - seasonality of gold
# run_example = 2 - seasonality of FX vol
# run_example = 3 - seasonality of gasoline
# run_example = 4 - seasonality in NFP
# run_example = 5 - seasonal adjustment in NFP

run_example = 0

###### calculate seasonal moves in Gold (using Bloomberg data)
if run_example == 1 or run_example == 0:
    md_request = MarketDataRequest(
class TradeAnalysis(object):
    """Applies some basic trade analysis for a trading strategy (as defined by TradingModel). Use PyFolio to create some
    basic trading statistics. Also allows you test multiple parameters for a specific strategy (like TC).

    """

    def __init__(self, engine = ChartConstants().chartfactory_default_engine):
        self.logger = LoggerManager().getLogger(__name__)
        self.DUMP_PATH = 'output_data/' + datetime.date.today().strftime("%Y%m%d") + ' '
        self.SCALE_FACTOR = 3
        self.DEFAULT_PLOT_ENGINE = engine
        self.chart = Chart(engine=self.DEFAULT_PLOT_ENGINE)

        return

    def run_strategy_returns_stats(self, trading_model, index = None, engine = 'pyfolio'):
        """Plots useful statistics for the trading strategy (using PyFolio)

        Parameters
        ----------
        trading_model : TradingModel
            defining trading strategy
        index: DataFrame
            define strategy by a time series

        """

        if index is None:
            pnl = trading_model.get_strategy_pnl()
        else:
            pnl = index

        tz = Timezone()
        calculations = Calculations()

        if engine == 'pyfolio':
            # PyFolio assumes UTC time based DataFrames (so force this localisation)
            try:
                pnl = tz.localise_index_as_UTC(pnl)
            except: pass

            # set the matplotlib style sheet & defaults
            # at present this only works in Matplotlib engine
            try:
                matplotlib.rcdefaults()
                plt.style.use(ChartConstants().chartfactory_style_sheet['chartpy-pyfolio'])
            except: pass

            # TODO for intraday strategies, make daily

            # convert DataFrame (assumed to have only one column) to Series
            pnl = calculations.calculate_returns(pnl)
            pnl = pnl.dropna()
            pnl = pnl[pnl.columns[0]]
            fig = pf.create_returns_tear_sheet(pnl, return_fig=True)

            try:
                plt.savefig (trading_model.DUMP_PATH + "stats.png")
            except: pass

            plt.show()
        elif engine == 'finmarketpy':

            # assume we have TradingModel
            # to do to take in a time series
            from chartpy import Canvas, Chart
            pnl = trading_model.plot_strategy_pnl(silent_plot=True)                         # plot the final strategy
            individual = trading_model.plot_strategy_group_pnl_trades(silent_plot=True)     # plot the individual trade P&Ls

            pnl_comp = trading_model.plot_strategy_group_benchmark_pnl(silent_plot=True)    # plot all the cumulative P&Ls of each component
            ir_comp = trading_model.plot_strategy_group_benchmark_pnl_ir(silent_plot=True)  # plot all the IR of each component

            leverage = trading_model.plot_strategy_leverage(silent_plot=True)               # plot the leverage of the portfolio
            ind_lev = trading_model.plot_strategy_group_leverage(silent_plot=True)          # plot all the individual leverages

            canvas = Canvas([[pnl, individual],
                             [pnl_comp, ir_comp],
                             [leverage, ind_lev]]
                             )

            canvas.generate_canvas(silent_display=False, canvas_plotter='plain')

    def run_excel_trade_report(self, trading_model, excel_file = 'model.xlsx'):
        """
        run_excel_trade_report - Creates an Excel spreadsheet with model returns and latest trades

        Parameters
        ----------
        trading_model : TradingModel
            defining trading strategy (can be a list)

        """

        trading_model_list = trading_model

        if not(isinstance(trading_model_list, list)):
            trading_model_list = [trading_model]

        writer = pandas.ExcelWriter(excel_file, engine='xlsxwriter')

        for tm in trading_model_list:
            strategy_name = tm.FINAL_STRATEGY
            returns = tm.get_strategy_group_benchmark_pnl()

            returns.to_excel(writer, sheet_name=strategy_name + ' rets', engine='xlsxwriter')

            # write raw position/trade sizes
            self.save_positions_trades(tm, tm.get_strategy_signal(),tm.get_strategy_trade(),
                                       'pos', 'trades', writer)

            if hasattr(tm, '_strategy_signal_notional'):
                # write position/trade sizes scaled by notional
                self.save_positions_trades(tm,
                                           tm.get_strategy_signal_notional(),
                                           tm.get_strategy_trade_notional(), 'pos - Not', 'trades - Not', writer)

            if hasattr(tm, '_strategy_signal_contracts'):
                # write position/trade sizes in terms of contract sizes
                self.save_positions_trades(tm,
                                           tm.get_strategy_signal_contracts(),
                                           tm.get_strategy_trade_contracts(), 'pos - Cont', 'trades - Cont', writer)

        # TODO Add summary sheet comparing return statistics for all the different models in the list

        writer.save()
        writer.close()

    def save_positions_trades(self, tm, signals, trades, signal_caption, trade_caption, writer):
        signals.to_excel(writer, sheet_name=tm.FINAL_STRATEGY + ' hist ' + signal_caption, engine='xlsxwriter')

        if hasattr(tm, 'STRIP'):
            strip = tm.STRIP

        recent_signals = tm.grab_signals(signals, date=[-1, -2, -5, -10, -20], strip=strip)
        recent_trades = tm.grab_signals(trades, date=[-1, -2, -5, -10, -20], strip=strip)

        recent_signals.to_excel(writer, sheet_name=tm.FINAL_STRATEGY + ' ' + signal_caption, engine='xlsxwriter')
        recent_trades.to_excel(writer, sheet_name=tm.FINAL_STRATEGY + ' ' + trade_caption, engine='xlsxwriter')

    def run_tc_shock(self, strategy, tc = None):
        if tc is None: tc = [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2.0]

        parameter_list = [{'spot_tc_bp' : x } for x in tc]
        pretty_portfolio_names = [str(x) + 'bp' for x in tc]    # names of the portfolio
        parameter_type = 'TC analysis'                          # broad type of parameter name

        return self.run_arbitrary_sensitivity(strategy,
                                 parameter_list=parameter_list,
                                 pretty_portfolio_names=pretty_portfolio_names,
                                 parameter_type=parameter_type)

    ###### Parameters and signal generations (need to be customised for every model)
    def run_arbitrary_sensitivity(self, trading_model, parameter_list = None, parameter_names = None,
                                  pretty_portfolio_names = None, parameter_type = None):

        asset_df, spot_df, spot_df2, basket_dict = trading_model.load_assets()

        port_list = None
        ret_stats_list = []

        for i in range(0, len(parameter_list)):
            br = trading_model.load_parameters()

            current_parameter = parameter_list[i]

            # for calculating P&L
            for k in current_parameter.keys():
                setattr(br, k, current_parameter[k])

            trading_model.br = br   # for calculating signals

            signal_df = trading_model.construct_signal(spot_df, spot_df2, br.tech_params, br)

            backtest = Backtest()
            self.logger.info("Calculating... " + str(pretty_portfolio_names[i]))

            backtest.calculate_trading_PnL(br, asset_df, signal_df)
            ret_stats_list.append(backtest.get_portfolio_pnl_ret_stats())
            stats = str(backtest.get_portfolio_pnl_desc()[0])

            port = backtest.get_cumportfolio().resample('B').mean()
            port.columns = [str(pretty_portfolio_names[i]) + ' ' + stats]

            if port_list is None:
                port_list = port
            else:
                port_list = port_list.join(port)

        # reset the parameters of the strategy
        trading_model.br = trading_model.load_parameters()

        style = Style()

        ir = [t.inforatio()[0] for t in ret_stats_list]

        # if we have too many combinations remove legend and use scaled shaded colour
        # if len(port_list) > 10:
            # style.color = 'Blues'
            # style.display_legend = False

        # plot all the variations
        style.resample = 'B'
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + '.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + '.html'
        style.scale_factor = self.SCALE_FACTOR
        style.title = trading_model.FINAL_STRATEGY + ' ' + parameter_type

        self.chart.plot(port_list, chart_type='line', style=style)

        # plot all the IR in a bar chart form (can be easier to read!)
        style = Style()
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' IR.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' ' + parameter_type + ' IR.html'
        style.scale_factor = self.SCALE_FACTOR
        style.title = trading_model.FINAL_STRATEGY + ' ' + parameter_type
        summary = pandas.DataFrame(index = pretty_portfolio_names, data = ir, columns = ['IR'])

        self.chart.plot(summary, chart_type='bar', style=style)

        return port_list

    ###### Parameters and signal generations (need to be customised for every model)
    ###### Plot all the output seperately
    def run_arbitrary_sensitivity_separately(self, trading_model, parameter_list = None,
                                             pretty_portfolio_names = None, strip = None):

        # asset_df, spot_df, spot_df2, basket_dict = strat.fill_assets()
        final_strategy = trading_model.FINAL_STRATEGY

        for i in range(0, len(parameter_list)):
            br = trading_model.fill_backtest_request()

            current_parameter = parameter_list[i]

            # for calculating P&L
            for k in current_parameter.keys():
                setattr(br, k, current_parameter[k])

            trading_model.FINAL_STRATEGY = final_strategy + " " + pretty_portfolio_names[i]

            self.logger.info("Calculating... " + pretty_portfolio_names[i])
            trading_model.br = br
            trading_model.construct_strategy(br = br)

            trading_model.plot_strategy_pnl()
            trading_model.plot_strategy_leverage()
            trading_model.plot_strategy_group_benchmark_pnl(strip = strip)

        # reset the parameters of the strategy
        trading_model.br = trading_model.fill_backtest_request()
        trading_model.FINAL_STRATEGY = final_strategy

    def run_day_of_month_analysis(self, trading_model):
        from finmarketpy.economics.seasonality import Seasonality

        calculations = Calculations()
        seas = Seasonality()
        trading_model.construct_strategy()
        pnl = trading_model.get_strategy_pnl()

        # get seasonality by day of the month
        pnl = pnl.resample('B').mean()
        rets = calculations.calculate_returns(pnl)
        bus_day = seas.bus_day_of_month_seasonality(rets, add_average = True)

        # get seasonality by month
        pnl = pnl.resample('BM').mean()
        rets = calculations.calculate_returns(pnl)
        month = seas.monthly_seasonality(rets)

        self.logger.info("About to plot seasonality...")
        style = Style()

        # Plotting spot over day of month/month of year
        style.color = 'Blues'
        style.scale_factor = self.SCALE_FACTOR
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality day of month.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality day of month.html'
        style.title = trading_model.FINAL_STRATEGY + ' day of month seasonality'
        style.display_legend = False
        style.color_2_series = [bus_day.columns[-1]]
        style.color_2 = ['red'] # red, pink
        style.linewidth_2 = 4
        style.linewidth_2_series = [bus_day.columns[-1]]
        style.y_axis_2_series = [bus_day.columns[-1]]

        self.chart.plot(bus_day, chart_type='line', style=style)

        style = Style()

        style.scale_factor = self.SCALE_FACTOR
        style.file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality month of year.png'
        style.html_file_output = self.DUMP_PATH + trading_model.FINAL_STRATEGY + ' seasonality month of year.html'
        style.title = trading_model.FINAL_STRATEGY + ' month of year seasonality'

        self.chart.plot(month, chart_type='line', style=style)

        return month