Ejemplo n.º 1
0
    def _add_volume_traded(self):
        transactions = self.backtest_result.transactions
        transactions_series = QFSeries(data=transactions,
                                       index=(t.time for t in transactions))
        if transactions_series.empty:
            raise ValueError("Transactions series is empty")

        # Add the chart containing the volume traded in terms of quantity
        quantities = [abs(t.quantity) for t in transactions_series]
        quantities_series = QFSeries(data=quantities,
                                     index=transactions_series.index)

        # Aggregate the quantities for each day
        quantities_series = quantities_series.resample(
            Frequency.DAILY.to_pandas_freq()).sum()

        # Generate chart and add it to the document
        self._add_line_chart_element(quantities_series,
                                     "Volume traded per day [in contracts]")

        # Add the chart containing the exposure of the traded assets
        total_exposures = [
            abs(t.quantity) * t.price * t.contract.contract_size
            for t in transactions_series
        ]
        total_exposures_series = QFSeries(data=total_exposures,
                                          index=transactions_series.index)
        total_exposures_series = total_exposures_series.resample(
            Frequency.DAILY.to_pandas_freq()).sum()

        # Generate chart and add it to the document
        self._add_line_chart_element(
            total_exposures_series,
            "Volume traded per day [notional in currency units]")
Ejemplo n.º 2
0
    def _add_number_of_transactions_chart(self, pandas_freq: str, title: str):
        transactions = self.backtest_result.transactions
        transactions_series = QFSeries(data=transactions,
                                       index=(t.time for t in transactions))

        if transactions_series.empty:
            raise ValueError("Transactions series is empty")

        # Compute the number of transactions per day
        transactions_series = transactions_series.resample(
            Frequency.DAILY.to_pandas_freq()).count()

        # Aggregate the transactions using the given frequency
        if to_offset(pandas_freq) > to_offset('D'):
            transactions_series = transactions_series.rolling(
                pandas_freq).sum()

            # Cut the non complete beginning of the outputs (e.g. in case of 30 days window, cut the first 30 days)
            start_date = transactions_series.index[0]
            transactions_series = transactions_series.loc[start_date +
                                                          Timedelta(pandas_freq
                                                                    ):]
            if transactions_series.empty:
                # The available time period is too short to compute the statistics with the provided frequency
                return

        elif to_offset(pandas_freq) < to_offset('D'):
            raise ValueError(
                "The provided pandas frequency can not be higher than the daily frequency"
            )

        self._add_line_chart_element(transactions_series, title)