Exemple #1
0
    def compare_strategy_vs_benchmark(self, br, strategy_df, benchmark_df):
        """
        compare_strategy_vs_benchmark - Compares the trading strategy we are backtesting against a benchmark

        Parameters
        ----------
        br : BacktestRequest
            Parameters for backtest such as start and finish dates

        strategy_df : pandas.DataFrame
            Strategy time series

        benchmark_df : pandas.DataFrame
            Benchmark time series
        """

        include_benchmark = False
        calc_stats = False

        if hasattr(br, 'include_benchmark'): include_benchmark = br.include_benchmark
        if hasattr(br, 'calc_stats'): calc_stats = br.calc_stats

        if include_benchmark:
            tsd = TimeSeriesDesc()
            cash_backtest = CashBacktest()
            ts_filter = TimeSeriesFilter()
            ts_calcs = TimeSeriesCalcs()

            # align strategy time series with that of benchmark
            strategy_df, benchmark_df = strategy_df.align(benchmark_df, join='left', axis = 0)

            # if necessary apply vol target to benchmark (to make it comparable with strategy)
            if hasattr(br, 'portfolio_vol_adjust'):
                if br.portfolio_vol_adjust is True:
                    benchmark_df = cash_backtest.calculate_vol_adjusted_index_from_prices(benchmark_df, br = br)

            # only calculate return statistics if this has been specified (note when different frequencies of data
            # might underrepresent vol
            if calc_stats:
                benchmark_df = benchmark_df.fillna(method='ffill')
                tsd.calculate_ret_stats_from_prices(benchmark_df, br.ann_factor)
                benchmark_df.columns = tsd.summary()

            # realign strategy & benchmark
            strategy_benchmark_df = strategy_df.join(benchmark_df, how='inner')
            strategy_benchmark_df = strategy_benchmark_df.fillna(method='ffill')

            strategy_benchmark_df = ts_filter.filter_time_series_by_date(br.plot_start, br.finish_date, strategy_benchmark_df)
            strategy_benchmark_df = ts_calcs.create_mult_index_from_prices(strategy_benchmark_df)

            self._benchmark_pnl = benchmark_df
            self._benchmark_tsd = tsd

            return strategy_benchmark_df

        return strategy_df
Exemple #2
0
    def construct_individual_strategy(self, br, spot_df, spot_df2, asset_df,
                                      tech_params, key):
        """
        construct_individual_strategy - Combines the signal with asset returns to find the returns of an individual
        strategy

        Parameters
        ----------
        br : BacktestRequest
            Parameters for backtest such as start and finish dates

        spot_df : pandas.DataFrame
            Market time series for generating signals

        spot_df2 : pandas.DataFrame
            Market time series for generated signals (can be of different frequency)

        tech_params : TechParams
            Parameters for generating signals

        Returns
        -------
        cumportfolio : pandas.DataFrame
        cash_backtest : CashBacktest
        """
        cash_backtest = CashBacktest()

        signal_df = self.construct_signal(spot_df, spot_df2,
                                          tech_params)  # get trading signal
        cash_backtest.calculate_trading_PnL(br, asset_df,
                                            signal_df)  # calculate P&L

        cumpnl = cash_backtest.get_cumpnl()

        if br.write_csv: cumpnl.to_csv(self.DUMP_CSV + key + ".csv")

        cumportfolio = cash_backtest.get_cumportfolio()

        if br.calc_stats:
            cumportfolio.columns = [
                key + ' ' + str(cash_backtest.get_portfolio_pnl_desc()[0])
            ]
        else:
            cumportfolio.columns = [key]

        return cumportfolio, cash_backtest
    def run_arbitrary_sensitivity(self, strat, parameter_list = None, parameter_names = None,
                                  pretty_portfolio_names = None, parameter_type = None):

        asset_df, spot_df, spot_df2, basket_dict = strat.fill_assets()

        port_list = None

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

            current_parameter = parameter_list[i]

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

            strat.br = br   # for calculating signals

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

            cash_backtest = CashBacktest()
            self.logger.info("Calculating... " + pretty_portfolio_names[i])

            cash_backtest.calculate_trading_PnL(br, asset_df, signal_df)
            stats = str(cash_backtest.get_portfolio_pnl_desc()[0])

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

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

        pf = PlotFactory()
        gp = GraphProperties()

        gp.color = 'Blues'
        gp.resample = 'B'
        gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + '.png'
        gp.scale_factor = self.scale_factor
        gp.title = strat.FINAL_STRATEGY + ' ' + parameter_type
        pf.plot_line_graph(port_list, adapter = 'pythalesians', gp = gp)
    def construct_individual_strategy(self, br, spot_df, spot_df2, asset_df, tech_params, key):
        """
        construct_individual_strategy - Combines the signal with asset returns to find the returns of an individual
        strategy

        Parameters
        ----------
        br : BacktestRequest
            Parameters for backtest such as start and finish dates

        spot_df : pandas.DataFrame
            Market time series for generating signals

        spot_df2 : pandas.DataFrame
            Market time series for generated signals (can be of different frequency)

        tech_params : TechParams
            Parameters for generating signals

        Returns
        -------
        cumportfolio : pandas.DataFrame
        cash_backtest : CashBacktest
        """
        cash_backtest = CashBacktest()

        signal_df = self.construct_signal(spot_df, spot_df2, tech_params, br)   # get trading signal
        cash_backtest.calculate_trading_PnL(br, asset_df, signal_df)            # calculate P&L

        cumpnl = cash_backtest.get_cumpnl()

        if br.write_csv: cumpnl.to_csv(self.DUMP_CSV + key + ".csv")

        cumportfolio = cash_backtest.get_cumportfolio()

        if br.calc_stats:
            cumportfolio.columns = [key + ' ' + str(cash_backtest.get_portfolio_pnl_desc()[0])]
        else:
            cumportfolio.columns = [key]

        return cumportfolio, cash_backtest
Exemple #5
0
    def run_arbitrary_sensitivity(self,
                                  strat,
                                  parameter_list=None,
                                  parameter_names=None,
                                  pretty_portfolio_names=None,
                                  parameter_type=None):

        asset_df, spot_df, spot_df2, basket_dict = strat.fill_assets()

        port_list = None
        tsd_list = []

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

            current_parameter = parameter_list[i]

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

            strat.br = br  # for calculating signals

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

            cash_backtest = CashBacktest()
            self.logger.info("Calculating... " + pretty_portfolio_names[i])

            cash_backtest.calculate_trading_PnL(br, asset_df, signal_df)
            tsd_list.append(cash_backtest.get_portfolio_pnl_tsd())
            stats = str(cash_backtest.get_portfolio_pnl_desc()[0])

            port = cash_backtest.get_cumportfolio().resample('B')
            port.columns = [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
        strat.br = strat.fill_backtest_request()

        pf = PlotFactory()
        gp = GraphProperties()

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

        # gp.color = 'Blues'
        # plot all the variations
        gp.resample = 'B'
        gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + '.png'
        gp.scale_factor = self.scale_factor
        gp.title = strat.FINAL_STRATEGY + ' ' + parameter_type
        pf.plot_line_graph(port_list, adapter='pythalesians', gp=gp)

        # plot all the IR in a bar chart form (can be easier to read!)
        gp = GraphProperties()
        gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + ' IR.png'
        gp.scale_factor = self.scale_factor
        gp.title = strat.FINAL_STRATEGY + ' ' + parameter_type
        summary = pandas.DataFrame(index=pretty_portfolio_names,
                                   data=ir,
                                   columns=['IR'])
        pf.plot_bar_graph(summary, adapter='pythalesians', gp=gp)

        return port_list
    def run_arbitrary_sensitivity(self, strat, parameter_list = None, parameter_names = None,
                                  pretty_portfolio_names = None, parameter_type = None):

        asset_df, spot_df, spot_df2, basket_dict = strat.fill_assets()

        port_list = None
        tsd_list = []

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

            current_parameter = parameter_list[i]

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

            strat.br = br   # for calculating signals

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

            cash_backtest = CashBacktest()
            self.logger.info("Calculating... " + pretty_portfolio_names[i])

            cash_backtest.calculate_trading_PnL(br, asset_df, signal_df)
            tsd_list.append(cash_backtest.get_portfolio_pnl_tsd())
            stats = str(cash_backtest.get_portfolio_pnl_desc()[0])

            port = cash_backtest.get_cumportfolio().resample('B').mean()
            port.columns = [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
        strat.br = strat.fill_backtest_request()

        pf = PlotFactory()
        gp = GraphProperties()

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

        # gp.color = 'Blues'
        # plot all the variations
        gp.resample = 'B'
        gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + '.png'
        gp.scale_factor = self.scale_factor
        gp.title = strat.FINAL_STRATEGY + ' ' + parameter_type
        pf.plot_line_graph(port_list, adapter = 'pythalesians', gp = gp)

        # plot all the IR in a bar chart form (can be easier to read!)
        gp = GraphProperties()
        gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' ' + parameter_type + ' IR.png'
        gp.scale_factor = self.scale_factor
        gp.title = strat.FINAL_STRATEGY + ' ' + parameter_type
        summary = pandas.DataFrame(index = pretty_portfolio_names, data = ir, columns = ['IR'])
        pf.plot_bar_graph(summary, adapter = 'pythalesians', gp = gp)

        return port_list
    # for logging
    from pythalesians.util.loggermanager import LoggerManager

    # for signal generation
    from pythalesians.timeseries.techind.techindicator import TechIndicator
    from pythalesians.timeseries.techind.techparams import TechParams

    # for plotting
    from pythalesians_graphics.graphs.graphproperties import GraphProperties
    from pythalesians_graphics.graphs import PlotFactory

    logger = LoggerManager().getLogger(__name__)

    import datetime

    cash_backtest = CashBacktest()
    br = BacktestRequest()
    fxconv = FXConv()

    # get all asset data
    br.start_date = "02 Jan 1990"
    br.finish_date = datetime.datetime.utcnow()
    br.spot_tc_bp = 2.5                             # 2.5 bps bid/ask spread
    br.ann_factor = 252

    # have vol target for each signal
    br.signal_vol_adjust = True
    br.signal_vol_target = 0.05
    br.signal_vol_max_leverage = 3
    br.signal_vol_periods = 60
    br.signal_vol_obs_in_year = 252
Exemple #8
0
    # for logging
    from pythalesians.util.loggermanager import LoggerManager

    # for signal generation
    from pythalesians.timeseries.techind.techindicator import TechIndicator
    from pythalesians.timeseries.techind.techparams import TechParams

    # for plotting
    from pythalesians.graphics.graphs.graphproperties import GraphProperties
    from pythalesians.graphics.graphs.plotfactory import PlotFactory

    logger = LoggerManager().getLogger(__name__)

    import datetime

    cash_backtest = CashBacktest()
    br = BacktestRequest()
    fxconv = FXConv()

    # get all asset data
    br.start_date = "02 Jan 1990"
    br.finish_date = datetime.datetime.utcnow()
    br.spot_tc_bp = 2.5                             # 2.5 bps bid/ask spread
    br.ann_factor = 252

    # have vol target for each signal
    br.signal_vol_adjust = True
    br.signal_vol_target = 0.05
    br.signal_vol_max_leverage = 3
    br.signal_vol_periods = 60
    br.signal_vol_obs_in_year = 252
    # for logging
    from pythalesians.util.loggermanager import LoggerManager

    # for signal generation
    from pythalesians.timeseries.techind.techindicator import TechIndicator
    from pythalesians.timeseries.techind.techparams import TechParams

    # for plotting
    from chartesians.graphs.graphproperties import GraphProperties
    from chartesians.graphs import PlotFactory

    logger = LoggerManager().getLogger(__name__)

    import datetime

    cash_backtest = CashBacktest()
    br = BacktestRequest()
    fxconv = FXConv()

    # get all asset data
    br.start_date = "02 Jan 1990"
    br.finish_date = datetime.datetime.utcnow()
    br.spot_tc_bp = 2.5  # 2.5 bps bid/ask spread
    br.ann_factor = 252

    # have vol target for each signal
    br.signal_vol_adjust = True
    br.signal_vol_target = 0.05
    br.signal_vol_max_leverage = 3
    br.signal_vol_periods = 60
    br.signal_vol_obs_in_year = 252