Esempio n. 1
0
    def _add_heat_map_grid(self, tickers: Sequence[Ticker], parameters_list: Sequence[tuple], third_param=None):
        grid = GridElement(mode=PlottingMode.PDF, figsize=self.image_size)
        result_df = QFDataFrame()

        for param_tuple in parameters_list:
            row = param_tuple[0]
            column = param_tuple[1]
            trades_eval_result = self.backtest_evaluator.evaluate_params_for_tickers(param_tuple, tickers)
            result_df.loc[row, column] = trades_eval_result

        result_df.sort_index(axis=0, inplace=True, ascending=False)
        result_df.sort_index(axis=1, inplace=True)

        sqn_avg_nr_trades = result_df.applymap(lambda x: x.sqn_per_avg_nr_trades)
        sqn_per100trades = result_df.applymap(lambda x: x.sqn_per100trades)
        avg_nr_of_trades = result_df.applymap(lambda x: x.avg_nr_of_trades_1Y)
        annualised_return = result_df.applymap(lambda x: x.annualised_return)
        drawdown = result_df.applymap(lambda x: x.drawdown)

        grid.add_chart(self._create_single_heat_map("SQN / AVG #trades", sqn_avg_nr_trades, tickers, 0, 1, third_param))
        grid.add_chart(self._create_single_heat_map("SQN / 100 trades", sqn_per100trades, tickers, 0, 2, third_param))
        grid.add_chart(self._create_single_heat_map("Avg # trades 1Y", avg_nr_of_trades, tickers, 3, 30, third_param))
        grid.add_chart(self._create_single_heat_map("An Return", annualised_return, tickers, -0.1, 0.2, third_param))
        grid.add_chart(self._create_single_heat_map("Drawdown", drawdown, tickers, -0.5, -0.1, third_param))

        self.document.add_element(grid)
Esempio n. 2
0
    def _add_ret_distribution_and_similarity(self):
        grid = GridElement(mode=PlottingMode.PDF,
                           figsize=self.half_image_size,
                           dpi=self.dpi)

        title = "Distribution of Monthly Returns - Strategy"
        chart_strategy = create_returns_distribution(self.strategy_series,
                                                     title=title)
        grid.add_chart(chart_strategy)

        chart = RegressionChart(self.benchmark_series, self.strategy_series)
        grid.add_chart(chart)

        title = "Distribution of Monthly Returns - Benchmark"
        chart_benchmark = create_returns_distribution(self.benchmark_series,
                                                      title=title)
        grid.add_chart(chart_benchmark)

        chart = RegressionChart(self.strategy_series, self.benchmark_series)
        grid.add_chart(chart)

        # put the same x axis range on both histograms
        min_x = min(
            [chart_strategy.series.min(),
             chart_benchmark.series.min()])
        max_x = max(
            [chart_strategy.series.max(),
             chart_benchmark.series.max()])
        chart_strategy.set_x_range(min_x, max_x)
        chart_benchmark.set_x_range(min_x, max_x)

        self.document.add_element(grid)
Esempio n. 3
0
    def _add_heat_maps(self, tickers: Sequence[Ticker]):
        parameters_list = sorted(
            self.backtest_evaluator.params_backtest_summary_elem_dict.keys())

        # Group plots by type, so that they appear in the given logical order
        title_to_grid = defaultdict(lambda: GridElement(
            mode=PlottingMode.PDF, figsize=self.image_size))
        for start_time, end_time in [
            (self.backtest_summary.start_date, self.out_of_sample_start_date),
            (self.out_of_sample_start_date, self.backtest_summary.end_date)
        ]:
            results = QFDataFrame()

            for param_tuple in parameters_list:
                trades_eval_result = self.backtest_evaluator.evaluate_params_for_tickers(
                    param_tuple, tickers, start_time, end_time)
                row, column = param_tuple
                results.loc[row, column] = trades_eval_result

            results.sort_index(axis=0, inplace=True, ascending=False)
            results.sort_index(axis=1, inplace=True)
            results.fillna(TradesEvaluationResult(), inplace=True)

            sqn_avg_nr_trades = results.applymap(
                lambda x: x.sqn_per_avg_nr_trades).fillna(0)
            avg_nr_of_trades = results.applymap(
                lambda x: x.avg_nr_of_trades_1Y).fillna(0)
            annualised_return = results.applymap(
                lambda x: x.annualised_return).fillna(0)

            adjusted_start_time = results.applymap(
                lambda x: x.start_date).min().min()
            adjusted_end_time = results.applymap(
                lambda x: x.end_date).max().max()
            if adjusted_start_time >= adjusted_end_time:
                adjusted_end_time = adjusted_start_time if adjusted_start_time <= self.backtest_summary.end_date \
                    else end_time
                adjusted_start_time = start_time
            title = "{} - {} ".format(adjusted_start_time.strftime("%Y-%m-%d"),
                                      adjusted_end_time.strftime("%Y-%m-%d"))

            title_to_grid["SQN (Arithmetic return) per year"].add_chart(
                self._create_single_heat_map(title, sqn_avg_nr_trades, 0, 0.5))

            title_to_grid["Avg # trades 1Y"].add_chart(
                self._create_single_heat_map(title, avg_nr_of_trades, 2, 15))

            if len(tickers) == 1:
                title_to_grid["Annualised return"].add_chart(
                    self._create_single_heat_map(title, annualised_return, 0.0,
                                                 0.3))

        tickers_used = "Many tickers" if len(tickers) > 1 else (
            tickers[0].name if isinstance(
                tickers[0], FutureTicker) else tickers[0].as_string())

        for description, grid in title_to_grid.items():
            self.document.add_element(
                HeadingElement(3, "{} - {}".format(description, tickers_used)))
            self.document.add_element(grid)
Esempio n. 4
0
    def _add_ret_distribution_and_similarity(self):
        grid = GridElement(mode=PlottingMode.PDF,
                           figsize=self.half_image_size, dpi=self.dpi)
        # Distribution of Monthly Returns
        chart = create_returns_distribution(self.strategy_series)
        grid.add_chart(chart)

        # Regression chart
        chart = RegressionChart(self.benchmark_series, self.strategy_series)
        grid.add_chart(chart)

        # Distribution of Monthly Returns
        chart = create_returns_distribution(self.benchmark_series)
        grid.add_chart(chart)

        # Regression chart
        chart = RegressionChart(self.strategy_series, self.benchmark_series)
        grid.add_chart(chart)

        self.document.add_element(grid)
Esempio n. 5
0
    def _add_histogram_and_cumulative(self):
        grid = GridElement(mode=PlottingMode.PDF, figsize=self.half_image_size, dpi=self.dpi)

        perf_chart = self._get_perf_chart()
        grid.add_chart(perf_chart)

        histogram_chart = self._get_histogram_chart()
        grid.add_chart(histogram_chart)

        self.document.add_element(grid)
    def _add_ret_distribution_and_qq(self):
        grid = GridElement(mode=PlottingMode.PDF, figsize=self.half_image_size, dpi=self.dpi)

        # Distribution of Monthly Returns
        chart = create_returns_distribution(self.strategy_series)
        grid.add_chart(chart)

        # QQ chart
        chart = create_qq_chart(self.strategy_series)
        grid.add_chart(chart)

        self.document.add_element(grid)
Esempio n. 7
0
    def _add_underwater_and_skewness(self):
        grid = GridElement(mode=PlottingMode.PDF,
                           figsize=self.half_image_size,
                           dpi=self.dpi)

        # Underwater plot
        grid.add_chart(self._get_underwater_chart(self.strategy_series))

        # Skewness chart
        chart = create_skewness_chart(self.strategy_series, title="Skewness")
        grid.add_chart(chart)

        self.document.add_element(grid)
Esempio n. 8
0
 def _get_new_grid(self) -> GridElement:
     return GridElement(mode=PlottingMode.PDF,
                        figsize=self.half_image_size,
                        dpi=self.dpi)
Esempio n. 9
0
    def _add_line_chart_grid(self, tickers: Sequence[Ticker]):
        grid = GridElement(mode=PlottingMode.PDF, figsize=self.image_size)
        params = sorted(self.backtest_evaluator.params_backtest_summary_elem_dict.keys())  # this will sort the tuples
        params_as_values = [param_tuple[0] for param_tuple in params]

        results = []
        for param_tuple in params:
            trades_eval_result = self.backtest_evaluator.evaluate_params_for_tickers(param_tuple, tickers)
            results.append(trades_eval_result)

        sqn_avg_trades = [elem.sqn_per_avg_nr_trades for elem in results]
        sqn_per100trades = [elem.sqn_per100trades for elem in results]
        avg_nr_of_trades = [elem.avg_nr_of_trades_1Y for elem in results]
        annualised_return = [elem.annualised_return for elem in results]
        drawdown = [elem.drawdown for elem in results]

        grid.add_chart(self._crete_single_line_chart("SQN / AVG #trades", params_as_values, sqn_avg_trades, tickers))
        grid.add_chart(self._crete_single_line_chart("SQN / 100 trades", params_as_values, sqn_per100trades, tickers))
        grid.add_chart(self._crete_single_line_chart("Avg # trades 1Y", params_as_values, avg_nr_of_trades, tickers))
        grid.add_chart(self._crete_single_line_chart("An Return", params_as_values, annualised_return, tickers))
        grid.add_chart(self._crete_single_line_chart("Drawdown", params_as_values, drawdown, tickers))

        self.document.add_element(grid)