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 = 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
        equity, stats = backtester.stats(pl, inposition, self.positionsize,
                                         self.costs)

        return (swarm_name, equity, stats, inposition)
示例#2
0
    def test_stats_1bar_inposition(self):
        pl = pd.Series([0, 1, 1, 1, 1, 1, 1, 1, 1, 1])
        inpos = pd.Series([0, 1, 0, 0, 0, 0, 0, 0, 0, 0])

        equity, statsistics = stats(pl,
                                    inpos,
                                    positionsize=pd.Series(1,
                                                           index=inpos.index),
                                    costs=pd.Series(1, index=inpos.index))

        for i in range(len(equity)):
            exp = pd.Series([0, -2, -1, -1, -1, -1, -1, -1, -1, -1])
            self.assertEqual(exp[i], equity.values[i])

        self.assertEqual(1, statsistics['count'])
        self.assertEqual(-1, statsistics['netprofit'])
        self.assertEqual(-1, statsistics['avg'])
示例#3
0
    def test_stats_no_costs_position_size(self):
        data = {'exo': pd.Series(range(10))}
        entry_rule = pd.Series([0, 1, 0, 0, 0, 1, 0, 0, 0, 0])
        exit_rule = pd.Series([0, 0, 0, 1, 0, 0, 0, 0, 1, 0])

        pl, inpos = backtest(data, entry_rule, exit_rule, direction=1)
        equity, statsistics = stats(pl,
                                    inpos,
                                    positionsize=pd.Series(2,
                                                           index=inpos.index),
                                    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])

        self.assertEqual(2, statsistics['count'])
        self.assertEqual(10, statsistics['netprofit'])
        self.assertEqual(5, statsistics['avg'])