def test_stats_exposure_delta(self):
        data = pd.DataFrame(
            {
                'exo': pd.Series(range(10)),
                'delta': pd.Series(np.ones(10) * 2)
            },
            dtype=np.float)
        entry_rule = pd.Series([0, 1, 0, 0, 0, 1, 0, 0, 0, 0], dtype=np.uint8)
        exit_rule = pd.Series([0, 0, 0, 1, 0, 0, 0, 0, 1, 0], dtype=np.uint8)

        pl, inpos = backtest(data,
                             entry_rule.values,
                             exit_rule.values,
                             direction=1)
        equity, statistics = stats_exposure(data,
                                            inpos * 1.0,
                                            costs=None,
                                            extendedstats=True)

        self.assertTrue('delta' in equity)

        delta = equity['delta']

        self.assertEqual(delta[0], 0)
        self.assertEqual(delta[1], 2)
        self.assertEqual(delta[2], 2)
        self.assertEqual(delta[3], 0)
        self.assertEqual(delta[4], 0)
        self.assertEqual(delta[5], 2)
        self.assertEqual(delta[6], 2)
        self.assertEqual(delta[7], 2)
        self.assertEqual(delta[8], 0)
        self.assertEqual(delta[9], 0)
    def test_pnl(self):
        data = pd.DataFrame({'exo': pd.Series(range(10))}, dtype=np.float)
        entry_rule = pd.Series([0, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.uint8)
        exit_rule = pd.Series([0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=np.uint8)

        pl, inpos = backtest(data,
                             entry_rule.values,
                             exit_rule.values,
                             direction=-1)

        for i in range(len(inpos)):
            exp = pd.Series([0, 0, -1, -1, 0, 0, 0, 0, 0, 0])
            self.assertEqual(exp[i], pl.values[i])
    def test_inposition(self):
        data = pd.DataFrame({'exo': pd.Series(range(10))}, dtype=np.float)
        entry_rule = pd.Series([0, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.uint8)
        exit_rule = pd.Series([0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=np.uint8)

        pl, inpos = backtest(data,
                             entry_rule.values,
                             exit_rule.values,
                             direction=1)

        for i in range(len(inpos)):
            # Fix: 2016-05-30 Exit bar is not in position
            exp = pd.Series([0, 1, 1, 0, 0, 0, 0, 0, 0, 0])
            self.assertEqual(exp[i], inpos.values[i])
    def test_stats_exposure_size(self):
        data = pd.DataFrame({'exo': pd.Series(range(10))}, dtype=np.float)
        entry_rule = pd.Series([0, 1, 0, 0, 0, 1, 0, 0, 0, 0], dtype=np.uint8)
        exit_rule = pd.Series([0, 0, 0, 1, 0, 0, 0, 0, 1, 0], dtype=np.uint8)

        pl, inpos = backtest(data,
                             entry_rule.values,
                             exit_rule.values,
                             direction=1)
        #equity, statsistics = stats(pl, inpos, positionsize=pd.Series(2, index=inpos.index, dtype=np.float), costs=None)
        equity, statsistics = stats_exposure(data, inpos * 2.0, costs=None)

        for i in range(len(equity)):
            exp = pd.Series([0, 0, 2, 4, 4, 4, 6, 8, 10, 10])
            self.assertEqual(exp[i], equity.values[i])
    def test_stats_exposure_no_costs(self):
        data = pd.DataFrame({'exo': pd.Series(range(10))}, dtype=np.float)
        entry_rule = pd.Series([0, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.uint8)
        exit_rule = pd.Series([0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=np.uint8)

        pl, inpos = backtest(data,
                             entry_rule.values,
                             exit_rule.values,
                             direction=1)

        equity, statsistics = stats_exposure(data, inpos * 1.0, costs=None)

        self.assertEqual(statsistics, {})

        for i in range(len(equity)):
            exp = pd.Series([0, 0, 1, 2, 2, 2, 2, 2, 2, 2])
            self.assertEqual(exp[i], equity.values[i])
    def test_same_bar_exit(self):
        """
        Entering to position even is we have exit_signal on the same bar
        :return:
        """
        data = pd.DataFrame({'exo': pd.Series(range(10))}, dtype=np.float)
        entry_rule = pd.Series([0, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.uint8)
        exit_rule = pd.Series([0, 1, 0, 1, 0, 0, 0, 0, 0, 0], dtype=np.uint8)

        pl, inpos = backtest(data,
                             entry_rule.values,
                             exit_rule.values,
                             direction=1)

        for i in range(len(inpos)):
            exp = pd.Series([0, 1, 1, 0, 0, 0, 0, 0, 0, 0])
            self.assertEqual(exp[i], inpos.values[i])
    def test_stats_exposure_costs(self):
        data = pd.DataFrame({'exo': pd.Series(range(10))}, dtype=np.float)
        entry_rule = pd.Series([0, 1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.uint8)
        exit_rule = pd.Series([0, 0, 0, 1, 0, 0, 0, 0, 0, 0], dtype=np.uint8)

        pl, inpos = backtest(data,
                             entry_rule.values,
                             exit_rule.values,
                             direction=1)
        costs = pd.DataFrame({
            'rollover_costs':
            pd.Series(0.0, index=inpos.index),
            'transaction_costs':
            pd.Series(1.0, index=inpos.index)
        })
        series_df, statsistics = stats_exposure(data, inpos * 1.0, costs=costs)

        for i in range(len(series_df)):
            exp = pd.Series([0, -1, 0, 0, 0, 0, 0, 0, 0, 0])
            self.assertEqual(exp[i], series_df['equity'].values[i])
Exemplo n.º 8
0
    def _calc_swarm_member(self, opts):

        # In bi-directional mode 1-st opt argument must be *direction* of the swarm
        direction = opts[0]
        if direction != 1 and direction != -1:
            raise Exception(
                'In bi-direction mode fist opt argument must be Direction: 1 or -1'
            )

        swarm_name, entry_rule, exit_rule, calc_info = self.calculate(opts)

        # Backtesting routine
        pl, inposition = backtest(self.data, entry_rule.values,
                                  exit_rule.values, direction)
        #pl, inposition = backtester.backtest(self.data, entry_rule, exit_rule, direction)

        # Apply global filter to trading system entries
        if self._filtered_swarm is not None:
            filtered_inpos = self._filtered_swarm[self.get_member_name(opts)]

            # Binary AND to arrays
            if self.global_filter is not None:
                inposition = inposition & filtered_inpos & self.global_filter
            else:
                inposition = inposition & filtered_inpos

        # Do backtest (exposure based)
        # Exposure Direction * PositionSize * InPositionFlag
        exposure = inposition.astype(
            np.uint8) * float(direction) * self.positionsize

        # Do quick backtest (equity line only without stats)
        series_df, stats_dict = stats_exposure(self.data,
                                               exposure,
                                               self.costs,
                                               extendedstats=False)

        return (swarm_name, series_df['equity'], exposure, inposition)