Пример #1
0
    def test_case(self):
        on_bar_timestep = []
        on_symbol_timestep = []

        class DemoStrategy(Strategy):
            def on_init(self, ctx):
                """初始化数据"""
                return

            def on_symbol(self, ctx):
                on_symbol_timestep.append("%s %s %s %s" %
                                          (ctx.pcontract, ctx.strategy_name,
                                           ctx.datetime, ctx.curbar))

            def on_bar(self, ctx):
                t = ctx['oneday.TEST-1.Minute']
                on_bar_timestep.append(
                    "%s %s %s %s" %
                    (t.pcontract, ctx.strategy_name, t.datetime, t.curbar))
                t = ctx['TWODAY.TEST-5.Second']
                on_bar_timestep.append(
                    "%s %s %s %s" %
                    (t.pcontract, ctx.strategy_name, t.datetime, t.curbar))

        add_strategies(['TWODAY.TEST-5.Second', 'oneday.TEST-1.Minute'],
                       [{
                           'strategy': DemoStrategy('A1'),
                           'capital': 1000000.0 * 0.5,
                       }, {
                           'strategy': DemoStrategy('A2'),
                           'capital': 1000000.0 * 0.5,
                       }, {
                           'strategy': DemoStrategy('B1'),
                           'capital': 1000000.0 * 0.5,
                       }, {
                           'strategy': DemoStrategy('B2'),
                           'capital': 1000000.0 * 0.5,
                       }])

        # on_symbol
        fname = os.path.join(os.getcwd(), 'data', 'diffPeriodOnSymbol.txt')
        with open(fname) as f:
            lines = [line.rstrip('\n') for line in f]
        self.assertTrue(on_symbol_timestep == lines, "on_symbol时间对齐失败")

        # on_bar
        fname = os.path.join(os.getcwd(), 'data', 'diffPeriodOnBar.txt')
        lines = [line.rstrip('\n') for line in open(fname)]
        self.assertTrue(on_bar_timestep == lines, "on_bar时间对齐失败")
        logger.info('on_symbol, on_bar 时间对齐测试成功!')
Пример #2
0
    def test_case(self):
        class DemoStrategy(Strategy):
            def on_init(self, ctx):
                """初始化数据"""
                return

            def on_bar(self, ctx):
                assert (ctx.open == ctx['TWODAY.TEST-5.Second'].open)
                assert (ctx.close == ctx['TWODAY.TEST-5.Second'].close)
                assert (ctx.high == ctx['TWODAY.TEST-5.Second'].high)
                assert (ctx.low == ctx['TWODAY.TEST-5.Second'].low)

        add_strategies(['TWODAY.TEST-5.Second', 'TWODAY.TEST-1.Minute'],
                       [{
                           'strategy': DemoStrategy('A1'),
                           'capital': 1000000.0 * 0.5,
                       }])
        logger.info("默认合约测试成功!")
Пример #3
0
    def test_case(self):
        """ 策略: 多空市价开仓且当根bar成交
            买入点:[bt1, bt2, bt3]
            当天卖出点:[st1, st2]
        """
        class DemoStrategy(Strategy):
            def __init__(self, name):
                super(DemoStrategy, self).__init__(name)
                self.cashes = []
                self.equities = []

            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                curtime = ctx.datetime[0].time()
                if curtime in [bt1, bt2, bt3]:
                    ctx.buy(0, 1)
                    ctx.short(0, 2)
                else:
                    if curtime == st1:
                        assert (ctx.pos('long') == 3 and '持仓测试失败!')
                        ctx.sell(0, 2)
                        assert (ctx.pos('short') == 6 and '持仓测试失败!')
                        ctx.cover(0, 4)
                    elif curtime == st2:
                        assert (ctx.pos('long') == 1 and '持仓测试失败!')
                        ctx.sell(0, 1)
                        assert (ctx.pos('short') == 2 and '持仓测试失败!')
                        ctx.cover(0, 2)
                self.cashes.append(ctx.cash())
                self.equities.append(ctx.equity())

            def test(self, test):
                target, cashes, open_equities, open_cashes, dts =\
                    market_trade_closed_curbar(source, capital, lmg, smg, multi)
                for i, hd in enumerate(profiles[0].all_holdings()):
                    test.assertTrue(hd['datetime'] == dts[i], '模拟器测试失败!')
                    test.assertAlmostEqual(hd['equity'], target[i])
                    test.assertAlmostEqual(hd['cash'], cashes[i])

                for i in range(0, len(self.cashes)):
                    test.assertAlmostEqual(self.equities[i], open_equities[i])
                    test.assertAlmostEqual(self.cashes[i], open_cashes[i])

        c1 = DemoStrategy('C1')
        profiles = add_strategies(['future.TEST-1.Minute'],
                                  [{
                                      'strategy': c1,
                                      'capital': capital,
                                  }])
        c1.test(self)
Пример #4
0
    def test_case(self):
        lmg = Contract.long_margin_ratio('stock.TEST')
        multi = Contract.volume_multiple('stock.TEST')
        smg = Contract.short_margin_ratio('stock.TEST')
        profiles = None

        class DemoStrategy1(Strategy):
            """ 限价只买多头仓位的策略 """

            def on_init(self, ctx):
                """初始化数据"""
                self.cashes = []
                self.equities = []

            def on_bar(self, ctx):
                curtime = ctx.datetime[0].time()
                if curtime in [bt1, bt2, bt3]:
                    ctx.buy(ctx.close, 1)
                elif ctx.pos() >= 2 and curtime == st1:
                    ctx.sell(ctx.close, 2)
                elif ctx.pos() >= 1 and curtime == st2:
                    ctx.sell(ctx.close, 1)
                self.cashes.append(ctx.cash())
                self.equities.append(ctx.equity())

            def test(self, test):
                equities, cashes, open_equities, open_casheses, dts =\
                    trade_closed_curbar(source, capital * 0.3, lmg, smg, multi, 1)

                test.assertTrue(len(self.cashes) == len(cashes), 'cash接口测试失败!')
                for i in range(0, len(self.cashes)):
                    test.assertAlmostEqual(self.cashes[i], open_casheses[i])
                    test.assertAlmostEqual(self.equities[i], open_equities[i])

                for i, hd in enumerate(profiles[0].all_holdings()):
                    test.assertTrue(hd['datetime'] == dts[i], 'all_holdings接口测试失败!')
                    test.assertAlmostEqual(hd['equity'], equities[i])
                    test.assertAlmostEqual(hd['cash'], cashes[i])

        class DemoStrategy2(Strategy):
            """ 限价买多卖空的策略 """

            def on_init(self, ctx):
                """初始化数据"""
                self.cashes = []

            def on_bar(self, ctx):
                curtime = ctx.datetime[0].time()
                if curtime in [bt1, bt2, bt3]:
                    ctx.buy(ctx.close, 1)
                    ctx.short(ctx.close, 1)
                elif curtime == st1:
                    if ctx.pos() >= 3:
                        assert(ctx.pos() == 3 and '默认持仓查询测试失败!')
                        ctx.sell(ctx.close, 2)
                    if ctx.pos('short') >= 3:
                        assert(ctx.pos('short') == 3 and '默认持仓查询测试失败!')
                        ctx.cover(ctx.close, 2)
                elif curtime == st2:
                    if ctx.pos() >= 1:
                        ctx.sell(ctx.close, 1)
                    if ctx.pos('short') >= 1:
                        ctx.cover(ctx.close, 1)
                self.cashes.append(ctx.test_cash())

            def test(self, test):
                e0, c0, oe0, oc0, dts = trade_closed_curbar(source, capital * 0.3 / 2, lmg, smg, multi, 1)
                e1, c1, oe1, oc1, dts = trade_closed_curbar(source, capital * 0.3 / 2, lmg, smg, multi, -1)
                equities = [x + y for x, y in zip(e0, e1)]
                cashes = [x + y for x, y in zip(c0, c1)]
                for i, hd in enumerate(profiles[1].all_holdings()):
                    test.assertTrue(hd['datetime'] == dts[i], 'all_holdings接口测试失败!')
                    test.assertAlmostEqual(hd['equity'], equities[i])
                test.assertTrue(len(self.cashes) == len(cashes), 'cash接口测试失败!')
                for i in range(0, len(self.cashes) - 1):  # 最后一根强平了无法比较
                    test.assertAlmostEqual(self.cashes[i], cashes[i])

        class DemoStrategy3(Strategy):
            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                return

        a1 = DemoStrategy1('A1')
        a2 = DemoStrategy2('A2')
        a3 = DemoStrategy3('A3')
        profiles = add_strategies(['stock.TEST-1.Minute'], [
            {
                'strategy': a1,
                'capital': capital * 0.3,
            },
            {
                'strategy': a2,
                'capital': capital * 0.3,
            },
            {
                'strategy': a3,
                'capital': capital * 0.4,
            },
        ])

        a1.test(self)
        a2.test(self)

        all_holdings = Profile.all_holdings_sum(profiles)
        self.assertTrue(len(source) > 0 and len(source) == len(all_holdings), '模拟器测试失败!')
        for i in range(0, len(all_holdings)):
            hd = all_holdings[i]
            hd0 = profiles[0].all_holdings()[i]
            hd1 = profiles[1].all_holdings()[i]
            hd2 = profiles[2].all_holdings()[i]
            self.assertTrue(hd['cash'] == hd0['cash'] + hd1['cash'] + hd2['cash'],
                            'all_holdings接口测试失败!')
            self.assertTrue(hd['commission'] == hd0['commission'] +
                            hd1['commission'] + hd2['commission'], 'all_holdings接口测试失败!')
            self.assertTrue(hd['equity'] == hd0['equity'] + hd1['equity'] + hd2['equity'], 'all_holdings接口测试失败!')
Пример #5
0
    def test_case(self):

        class DemoStrategy1(Strategy):
            """ 限价只买多头仓位的策略 """

            def __init__(self, name):
                super(DemoStrategy1, self).__init__(name)
                self.tobuy = {}
                self.tosell = {}
                self._cashes = {}
                self._equities = {}

            def on_init(self, ctx):
                pass

            def on_symbol(self, ctx):
                """"""
                weekday = ctx.datetime[0].weekday()
                self.tobuy[ctx.symbol] = False
                self.tosell[ctx.symbol] = False
                if weekday == 0:
                    self.tobuy[ctx.symbol] = True
                elif weekday == 4:
                    self.tosell[ctx.symbol] = True

            def on_bar(self, ctx):
                if self.tobuy['600522.SH']:
                    ctx.buy(ctx['600522.SH'].close, 1, symbol='600522.SH')
                if self.tosell['600522.SH'] and ctx.pos(symbol='600522.SH')>0:
                    ctx.sell(ctx['600522.SH'].close, ctx.pos(symbol='600522.SH'), '600522.SH')

                self._cashes[ctx.datetime[0]] = ctx.cash()
                self._equities[ctx.datetime[0]] = ctx.equity()

            def test(self, test, profiles):
                lmg = Contract.long_margin_ratio('600522.SH')
                multi = Contract.volume_multiple('600522.SH')
                test.assertTrue(lmg == 1)
                test.assertTrue(multi == 1)
                fname = os.path.join(os.getcwd(), 'data', '1DAY', 'SH', '600522.csv')
                source = pd.read_csv(fname, parse_dates=True, index_col=0)
                equities, cashes, open_equities, open_cashes, dts = \
                    buy_monday_sell_friday(source, capital * 0.3, lmg, multi)
                count = 0
                all_holdings0 = profiles[0].all_holdings()
                for i, hd in enumerate(all_holdings0):
                    dt = hd['datetime']
                    if dt in cashes:
                        test.assertAlmostEqual(hd['cash'], cashes[dt])
                        test.assertAlmostEqual(hd['equity'], equities[dt])
                        test.assertAlmostEqual(self._cashes[dt], open_cashes[dt])
                        test.assertAlmostEqual(self._equities[dt], open_equities[dt])
                        count += 1
                    else:
                        # 两支股票的混合,总数据长度和source不一样。
                        test.assertAlmostEqual(all_holdings0[i - 1]['cash'], hd['cash'])
                        test.assertAlmostEqual(all_holdings0[i - 1]['equity'], hd['equity'])
                test.assertTrue(count == len(dts))

        class DemoStrategy2(Strategy):
            """ 选股,并且时间没对齐的日线数据。 """
            def __init__(self, name):
                super(DemoStrategy2, self).__init__(name)
                self.tobuys = []
                self.tosells = []
                self._cashes = {}
                self._equities = {}

            def on_symbol(self, ctx):
                """"""
                weekday = ctx.datetime[0].weekday()
                if weekday == 0:
                    self.tobuys.append(ctx.symbol)
                elif weekday == 4:
                    self.tosells.append(ctx.symbol)

            def on_bar(self, ctx):
                """初始化数据"""
                for symbol in self.tobuys:
                    ctx.buy(ctx[symbol].close, 1, symbol)
                for symbol in self.tosells:
                    if ctx.pos(symbol=symbol) > 0:
                        ctx.sell(ctx[symbol].close, ctx.pos(symbol=symbol), symbol)

                self._equities[ctx.datetime[0]] = ctx.equity()
                self._cashes[ctx.datetime[0]] = ctx.cash()
                self.tobuys = []
                self.tosells = []

            def test(self, test, profiles):
                fname = os.path.join(os.getcwd(), 'data', '1DAY', 'SH', '600521.csv')
                source = pd.read_csv(fname, parse_dates=True, index_col=0)
                fname = os.path.join(os.getcwd(), 'data', '1DAY', 'SH', '600522.csv')
                source2 = pd.read_csv(fname, parse_dates=True, index_col=0)
                equities0, cashes0, open_equities0, open_cashes0, dts = \
                    buy_monday_sell_friday(source, capital * 0.3 / 2, 1, 1)
                equities1, cashes1, open_equities1, open_cashes1, dts = \
                    buy_monday_sell_friday(source2, capital * 0.3 / 2, 1, 1)
                last_equity0 = 0
                last_equity1 = 0
                last_cash0 = 0
                last_cash1 = 0
                for i, hd in enumerate(profiles.all_holdings(1)):
                    dt = hd['datetime']
                    equity = 0
                    cash = 0
                    open_equity = 0
                    open_cash = 0
                    if dt in equities0:
                        equity = equities0[dt]
                        cash = cashes0[dt]
                        open_equity = open_equities0[dt]
                        open_cash = open_cashes0[dt]
                        last_equity0 = equities0[dt]
                        last_cash0 = cashes0[dt]
                    else:
                        equity += last_equity0
                        cash += last_cash0
                        open_equity += last_equity0
                        open_cash += last_cash0

                    if dt in equities1:
                        equity += equities1[dt]
                        cash += cashes1[dt]
                        last_equity1 = equities1[dt]
                        last_cash1 = cashes1[dt]
                        open_equity += open_equities1[dt]
                        open_cash += open_cashes1[dt]
                    else:
                        equity += last_equity1
                        cash += last_cash1
                        open_equity += last_equity1
                        open_cash += last_cash1

                    test.assertAlmostEqual(hd['equity'], equity)
                    test.assertAlmostEqual(hd['cash'], cash)
                    test.assertAlmostEqual(self._equities[dt], open_equity)
                    test.assertAlmostEqual(self._cashes[dt], open_cash)

        class DemoStrategy3(Strategy):
            """ 测试平仓未成交时的持仓,撤单后的持仓,撤单。 """
            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                return

        b1 = DemoStrategy1('B1')
        b2 = DemoStrategy2('B2')
        b3 = DemoStrategy3('B3')
        profiles = add_strategies(['600521', '600522'], [
            {
                'strategy': b1,
                'capital': capital * 0.3,
            },
            {
                'strategy': b2,
                'capital': capital * 0.3,
            },
            {
                'strategy': b3,
                'capital': capital * 0.4,
            },
        ])
        b1.test(self, profiles)
        b2.test(self, profiles)
Пример #6
0
    def test_case(self):
        close, open, dt, high, low, volume = [], [], [], [], [], []
        open3, dt3 = [], []
        operator_test = []
        user_vars = {
            'numseries': [],
            'numseries3': [],
            'dtseries': [],
        }

        class DemoStrategy(Strategy):
            def on_init(self, ctx):
                """初始化数据"""
                ctx.ma3 = MA(ctx.close, 3)
                ctx.numseries = NumberSeries()
                ctx.dtseries = DateTimeSeries()

            def on_symbol(self, ctx):
                # @TODO * /
                operator_test.append(ctx.open - 0 == ctx.open[0])
                operator_test.append(ctx.close - 0 == ctx.close[0])
                operator_test.append(ctx.high + 0 == ctx.high[0])
                operator_test.append(ctx.low + 0 == ctx.low[0])
                operator_test.append(ctx.volume + 0 == ctx.volume[0])
                open.append(ctx.open[0])
                close.append(ctx.close[0])
                high.append(ctx.high[0])
                low.append(ctx.low[0])
                volume.append(int(ctx.volume[0]))
                dt.append(ctx.datetime[0])
                open3.append(ctx.open[3])
                dt3.append(ctx.datetime[3])

                if ctx.curbar >= 100 and ctx.curbar < 300:
                    ctx.numseries.update(100)
                    ctx.dtseries.update(datetime.datetime(1000, 1, 1))
                elif ctx.curbar >= 300:
                    ctx.dtseries.update(datetime.datetime(3000, 1, 1))
                    ctx.numseries.update(300)
                user_vars['numseries3'].append(ctx.numseries[3])
                user_vars['numseries'].append(ctx.numseries[0])
                user_vars['dtseries'].append(ctx.dtseries[0])

        add_strategies(['BB.TEST-1.Minute'], [{
            'strategy': DemoStrategy('A1'),
            'capital': 1000000.0,
        }])

        # 序列变量默认值
        self.assertTrue(NumberSeries.DEFAULT_VALUE == 0.0, "默认值测试失败")
        self.assertTrue(
            DateTimeSeries.DEFAULT_VALUE == datetime.datetime(1980, 1, 1),
            "默认值测试失败")
        self.assertTrue(all(operator_test), "类型转化错误!")

        # 系统序列变量测试
        target = pd.DataFrame({
            'open': open,
            'close': close,
            'high': high,
            'low': low,
            'volume': volume
        })
        target.index = dt
        target = target.loc[:, ['open', 'close', 'high', 'low', 'volume']]
        fname = os.path.join(os.getcwd(), 'data', '1MINUTE', 'TEST', 'BB.csv')
        source = pd.read_csv(fname, parse_dates=True, index_col=0)
        self.assertTrue(source.equals(target), "系统时间序列变量正测试出错")
        fname = os.path.join(os.getcwd(), 'data', '1MINUTE', 'TEST', 'CC.csv')
        source = pd.read_csv(fname, parse_dates=True, index_col=0)
        self.assertFalse(source.equals(target), "系统时间序列变量反测试出错")

        # ctx.curbar,用户普通变量测试
        self.assertTrue(
            len(user_vars['numseries']) == len(open) and len(open) > 0,
            '系列变量长度不一致')
        logger.info('-- 用户普通变量测试成功 --')
        logger.info('-- curbar测试成功 --')

        # 用户序列变量
        numseries = user_vars['numseries']
        dtseries = user_vars['dtseries']
        dt1980 = datetime.datetime(1980, 1, 1)
        dt1000 = datetime.datetime(1000, 1, 1)
        dt3000 = datetime.datetime(3000, 1, 1)
        for i in range(0, len(numseries)):
            # 用户序列变量自动追加测试成功
            if i < 99:
                self.assertTrue(numseries[i] == NumberSeries.DEFAULT_VALUE,
                                '用户数字序列变量测试失败!')
                self.assertTrue(dtseries[i] == dt1980, '用户时间序列变量测试失败!')
            elif i >= 99 and i < 299:
                self.assertTrue(numseries[i] == 100, '用户数字序列变量测试失败!')
                self.assertTrue(dtseries[i] == dt1000, '用户时间序列变量测试失败!')
            elif i >= 299:
                self.assertTrue(numseries[i] == 300, '用户数字序列变量测试失败!')
                self.assertTrue(dtseries[i] == dt3000, '用户时间序列变量测试失败!')

        # 序列变量回溯测试
        for i in range(0, len(open)):
            if i - 3 >= 0:
                self.assertTrue(open3[i] == open[i - 3], "系统序列变量回溯测试失败!")
                self.assertTrue(dt3[i] == dt[i - 3], "系统序列变量回溯测试失败!")
                self.assertTrue(user_vars['numseries3'][i] == numseries[i - 3],
                                "用户序列变量回溯测试失败!")
            else:
                self.assertTrue(open3[i] == NumberSeries.DEFAULT_VALUE,
                                "系统序列变量回溯测试失败!")
                self.assertTrue(
                    user_vars['numseries3'][i] == NumberSeries.DEFAULT_VALUE,
                    "用户序列变量回溯测试失败!")
                self.assertTrue(dt3[i] == DateTimeSeries.DEFAULT_VALUE,
                                "系统序列时间变量回溯测试失败!")
        logger.info('-- 序列变量测试成功 --')
Пример #7
0
    def test_case(self):
        """
        案例:两个策略组合,每个策略组合下分别有两个策略,每个组合运行于两个周期合约中。
        测试:on_bar, on_symbol, on_exit 的运行频次,数据和策略遍历的粗粒度测试;
              ctx.prontract, ctx.strategy
        """
        on_exit = {
            'strategy': [],
        }
        on_bar = {
            'strategy': [],
        }
        on_symbol = {'combination': set(), 'step_num': 0}

        class DemoStrategy(Strategy):
            def on_init(self, ctx):
                """初始化数据"""
                return

            def on_symbol(self, ctx):
                # six.print_(ctx.strategy, ctx.pcontract)
                on_symbol['combination'].add(
                    (str(ctx.pcontract), ctx.strategy_name))
                on_symbol['step_num'] += 1

            def on_bar(self, ctx):
                on_bar['strategy'].append(ctx.strategy_name)

            def on_exit(self, ctx):
                on_exit['strategy'].append(ctx.strategy_name)

        add_strategies(['BB.TEST-1.Minute', 'AA.TEST-1.Minute'],
                       [{
                           'strategy': DemoStrategy('A1'),
                           'capital': 1000000.0 * 0.5,
                       }, {
                           'strategy': DemoStrategy('A2'),
                           'capital': 1000000.0 * 0.5,
                       }, {
                           'strategy': DemoStrategy('B1'),
                           'capital': 1000000.0 * 0.5,
                       }, {
                           'strategy': DemoStrategy('B2'),
                           'capital': 1000000.0 * 0.5,
                       }])

        fname = os.path.join(os.getcwd(), 'data', '1MINUTE', 'TEST', 'BB.csv')
        blen = len(pd.read_csv(fname))
        fname = os.path.join(os.getcwd(), 'data', '1MINUTE', 'TEST', 'AA.csv')
        alen = len(pd.read_csv(fname))
        sample = set([('BB.TEST-1.MINUTE', 'A1'), ('BB.TEST-1.MINUTE', 'A2'),
                      ('AA.TEST-1.MINUTE', 'A1'), ('AA.TEST-1.MINUTE', 'A2'),
                      ('BB.TEST-1.MINUTE', 'B1'), ('BB.TEST-1.MINUTE', 'B2'),
                      ('AA.TEST-1.MINUTE', 'B1'), ('AA.TEST-1.MINUTE', 'B2')])
        self.assertTrue(alen > 0 and blen > 0)

        # 测试on_symbol
        self.assertTrue(on_symbol['combination'] == sample, "on_symbol测试失败!")
        self.assertTrue(on_symbol['step_num'] == alen * 4 + blen * 4,
                        "on_symbol测试失败!")
        length = max(blen, alen)
        target = ['A1'] * length + ['A2'] * length + ['B1'] * length + [
            'B2'
        ] * length
        self.assertTrue(target == on_bar['strategy'], 'on_bar测试失败!')
        self.assertTrue(['A1', 'A2', 'B1', 'B2'] == on_exit['strategy'],
                        'on_exit测试失败!')
        logger.info('-- 策略on_xxx主函数测试成功 --')
Пример #8
0
    def test_case(self):
        """
        测试:
        * 指标变量
            1) 指标变量和数值间的运算。 ctx.ma2 - 0
            2) 指标变量回溯  ctx.ma2[3]
            3) 单值和多值测试
        """
        close, open, ma, ma3, tech_operator = [], [], [], [], []
        boll = {'upper': [], 'middler': [], 'lower': []}
        boll3 = {'upper': [], 'middler': [], 'lower': []}

        class DemoStrategy(Strategy):
            def on_init(self, ctx):
                """初始化数据"""
                ctx.ma = MA(ctx.close, 2)
                ctx.boll = BOLL(ctx.close, 2)

            def on_symbol(self, ctx):
                if ctx.curbar >= 2:
                    # @todo + * /
                    tech_operator.append(ctx.ma - 0 == ctx.ma[0])

                ma3.append(ctx.ma[3])
                ma.append(ctx.ma[0])
                close.append(ctx.close[0])
                open.append(ctx.open[0])
                boll['upper'].append(float(ctx.boll['upper']))
                boll['middler'].append(ctx.boll['middler'][0])
                boll['lower'].append(ctx.boll['lower'][0])
                boll3['upper'].append(ctx.boll['upper'][3])
                boll3['middler'].append(ctx.boll['middler'][3])
                boll3['lower'].append(ctx.boll['lower'][3])
                assert (isinstance(ctx.boll['lower'], NumberSeries))
                assert (isinstance(ctx.ma, MA))

        add_strategies(['BB.TEST-1.Minute'], [{
            'strategy': DemoStrategy('A1'),
            'capital': 1000000.0,
        }])

        # 单值指标运算和回溯测试
        source_ma = talib.SMA(np.asarray(close), 2)
        self.assertTrue(all(tech_operator), "指标运算错误!")
        self.assertFalse(ma[0] == ma[0], "指标NaN值测试失败!")
        for source, target in zip(source_ma[1:], ma[1:]):
            self.assertTrue(target == source, "单值指标计算测试失败!")
        for source, target in zip(ma[1:], ma3[4:]):
            self.assertTrue(target == source, "单值指标回溯测试失败!")
        for nan in ma3[:4]:
            self.assertFalse(nan == nan, "单值指标回溯NaN值测试失败!")
        logger.info('-- 单值指标测试成功 --')

        # 多值指标运算和回溯测试
        upper, middler, lower = talib.BBANDS(np.asarray(close), 2, 2, 2)
        ta_boll = {'upper': upper, 'middler': middler, 'lower': lower}
        for v in ['upper', 'lower', 'middler']:
            self.assertFalse(boll[v][0] == boll[v][0], "多值指标NaN值测试失败!")
            for source, target in zip(ta_boll[v][1:], boll[v][1:]):
                self.assertTrue(target == source, "多值指标计算测试失败!")
            for nan in boll3[v][:4]:
                self.assertFalse(nan == nan, "多值指标回溯NaN值测试失败!")
            for source, target in zip(boll[v][1:], boll3[v][4:]):
                self.assertTrue(target == source, "多值指标回溯测试失败!")
        logger.info('-- 多值指标测试成功 --')
        logger.info('***** 指标测试成功 *****\n')
Пример #9
0
    def test_case(self):
        profiles = None

        class DemoStrategy1(Strategy):
            """ 限价只买多头仓位的策略 """
            def __init__(self, name):
                super(DemoStrategy1, self).__init__(name)
                self.open_cash = []
                self.open_equity = []

            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                curtime = ctx.datetime[0].time()
                if curtime in [bt1, bt2, bt3]:
                    ctx.buy(ctx.close, 1)
                else:
                    if curtime == st1:
                        assert (ctx.pos() == 3 and '持仓测试失败!')
                        ctx.sell(ctx.close, 2)
                    elif curtime == st2:
                        assert (ctx.pos() == 1 and '持仓测试失败!')
                        ctx.sell(ctx.close, 1)
                # 前一根的交易信号在当前价格撮合后的可用资金
                self.open_cash.append(ctx.cash())
                self.open_equity.append(ctx.equity())

            def test(self, test):
                close_equity, close_cash, open_equity, open_cashes, dts = trade_closed_curbar(
                    source, capital * 0.3, lmg, smg, multi, 1)
                for i, hd in enumerate(profiles[0].all_holdings()):
                    test.assertAlmostEqual(self.open_equity[i], open_equity[i])
                    test.assertAlmostEqual(self.open_cash[i], open_cashes[i])
                    test.assertAlmostEqual(hd['equity'], close_equity[i])
                    test.assertAlmostEqual(hd['cash'], close_cash[i])
                    test.assertTrue(hd['datetime'] == dts[i],
                                    'all_holdings接口测试失败!')
                    test.assertTrue(
                        len(profiles[0].all_holdings()) == len(close_equity)
                        and len(close_equity) > 0, 'holdings接口测试失败!')

        class DemoStrategy2(Strategy):
            """ 限价买多卖空的策略 """
            def __init__(self, name):
                super(DemoStrategy2, self).__init__(name)
                self.open_cash = []
                self.open_equity = []

            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                curtime = ctx.datetime[0].time()
                if curtime in [bt1, bt2, bt3]:
                    ctx.buy(ctx.close, 1)
                    ctx.short(ctx.close, 1)
                else:
                    if curtime == st1:
                        assert (ctx.pos() == 3 and '默认持仓查询测试失败!')
                        ctx.sell(ctx.close, 2)
                        assert (ctx.pos('short') == 3 and '持仓测试失败!')
                        ctx.cover(ctx.close, 2)
                    elif curtime == st2:
                        assert (ctx.pos('long') == 1 and '持仓测试失败!')
                        ctx.sell(ctx.close, 1)
                        assert (ctx.pos('short') == 1 and '持仓测试失败!')
                        ctx.cover(ctx.close, 1)
                self.open_cash.append(ctx.cash())
                self.open_equity.append(ctx.equity())

            def test(self, test):
                #  确保资金够用,所以不影响
                e0, c0, oe0, oc0, dts = trade_closed_curbar(
                    source, capital * 0.3 / 2, lmg, smg, multi, 1)
                e1, c1, oe1, oc1, dts = trade_closed_curbar(
                    source, capital * 0.3 / 2, lmg, smg, multi, -1)
                close_equity = [x + y for x, y in zip(e0, e1)]
                close_cash = [x + y for x, y in zip(c0, c1)]
                open_equity = [x + y for x, y in zip(oe0, oe1)]
                open_cash = [x + y for x, y in zip(oc0, oc1)]
                test.assertTrue(
                    len(close_equity) == len(profiles[1].all_holdings()))
                for i in range(len(close_equity)):
                    hd = profiles[1].all_holdings()[i]
                    test.assertAlmostEqual(self.open_equity[i], open_equity[i])
                    test.assertAlmostEqual(self.open_cash[i], open_cash[i])
                    test.assertAlmostEqual(hd['equity'], close_equity[i])
                    test.assertAlmostEqual(hd['cash'], close_cash[i])
                    test.assertTrue(hd['datetime'] == dts[i],
                                    'all_holdings接口测试失败!')
                    test.assertTrue(
                        len(profiles[0].all_holdings()) == len(close_equity)
                        and len(close_equity) > 0, 'holdings接口测试失败!')

        class DemoStrategy3(Strategy):
            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                if ctx.curbar == 1:
                    ctx.short(138, 1)
                    ctx.short(138, 1)
                    ctx.buy(ctx.close, 1)

                # 保证测单动作不会在当前时间点生效,而是从下一个时间点开始。
                if ctx.curbar == 3:
                    assert (len(ctx.open_orders) == 2)
                    ctx.cancel(ctx.open_orders[0])
                    assert (len(ctx.open_orders) == 2 and '撤单测试失败')
                elif ctx.curbar == 4:
                    assert (len(ctx.open_orders) == 1 and '撤单测试失败!')

                # 测试可平仓位和所有仓位不同。
                if ctx.curbar == 5:
                    assert (ctx.pos() == 1)
                    ctx.sell(300, 1)  # 下无法成交的平仓,测试持仓。
                elif ctx.curbar == 7:
                    assert (len(ctx.all_positions()) == 1 and '持仓测试失败!')
                    assert (ctx.pos() == 0 and '持仓测试失败!')
                    assert (len(ctx.open_orders) == 2 and '撤单测试失败!')
                    order = list(
                        filter(lambda x: x.side == TradeSide.CLOSE,
                               ctx.open_orders))[0]
                    ctx.cancel(order)
                elif ctx.curbar == 8:
                    assert (len(ctx.open_orders) == 1 and '撤单测试失败!')
                    assert (ctx.pos() == 1 and '持仓测试失败!')

                # 隔夜未成交订单自动清空。
                if ctx.curbar == 9:
                    ctx.sell(300, 1)
                elif ctx.curbar == 10:
                    assert (ctx.pos() == 0 and '持仓测试失败!')
                elif ctx.curbar > 1 and ctx.datetime[0].date(
                ) != ctx.datetime[1].date():
                    assert (ctx.pos() == 1 and '隔夜未成交订单清空测试失败')
                    assert (len(ctx.open_orders) == 0 and '隔夜未成交订单清空测试失败')

        s1 = DemoStrategy1('A1')
        s2 = DemoStrategy2('A2')
        s3 = DemoStrategy3('A3')
        profiles = add_strategies(['future.TEST-1.Minute'], [
            {
                'strategy': s1,
                'capital': capital * 0.3,
            },
            {
                'strategy': s2,
                'capital': capital * 0.3,
            },
            {
                'strategy': s3,
                'capital': capital * 0.4,
            },
        ])

        # 绘制k线,交易信号线
        # from quantdigger.digger import finance, plotting
        # plotting.plot_strategy(profiles.data(), deals=profiles.deals(0))

        all_holdings = Profile.all_holdings_sum(profiles)
        self.assertTrue(
            len(source) > 0 and len(source) == len(all_holdings), '模拟器测试失败!')
        self.assertAlmostEqual(lmg, 0.4)
        self.assertAlmostEqual(smg, 0.4)
        self.assertAlmostEqual(multi, 3)

        s1.test(self)
        s2.test(self)

        # test all_holdings
        for i in range(0, len(profiles[0].all_holdings())):
            hd = all_holdings[i]
            hd0 = profiles[0].all_holdings()[i]
            hd1 = profiles[1].all_holdings()[i]
            hd2 = profiles[2].all_holdings()[i]
            self.assertTrue(
                hd['cash'] == hd0['cash'] + hd1['cash'] + hd2['cash'],
                'all_holdings接口测试失败!')
            self.assertTrue(
                hd['commission'] == hd0['commission'] + hd1['commission'] +
                hd2['commission'], 'all_holdings接口测试失败!')
            self.assertTrue(
                hd['equity'] == hd0['equity'] + hd1['equity'] + hd2['equity'],
                'all_holdings接口测试失败!')
Пример #10
0
    def test_case(self):
        """ 测试跨合约交易的持仓, 资金, 持仓"""
        class DemoStrategy(Strategy):
            def __init__(self, name):
                super(DemoStrategy, self).__init__(name)
                self.cashes = []
                self.equities = []

            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                curtime = ctx.datetime[0].time()
                if curtime in [bt1, bt2, bt3]:
                    ctx.buy(ctx.close, 1)  # 默认future.TEST
                    ctx.short(ctx['future2.TEST-1.Minute'].close, 1,
                              'future2.TEST')
                else:
                    if curtime == st1:
                        for pos in ctx.all_positions():
                            if str(pos.contract) == 'FUTURE.TEST':
                                assert (pos.quantity == 3)
                                assert (pos.closable == 3)
                                assert (pos.direction == Direction.LONG)
                            else:
                                assert (pos.quantity == 3)
                                assert (pos.closable == 3)
                                assert (pos.direction == Direction.SHORT)

                        assert (ctx.pos('long', 'future.TEST') == 3
                                and '持仓测试失败!')
                        ctx.sell(ctx.close, 2)
                        assert (ctx.pos('short', 'future2.TEST') == 3
                                and '持仓测试失败!')
                        ctx.cover(ctx['future2.TEST-1.Minute'].close, 2,
                                  'future2.TEST')
                    elif curtime == st2:
                        assert (ctx.pos('long', 'future.TEST') == 1
                                and '跨合约持仓测试失败!')
                        ctx.sell(ctx.close, 1, 'future.TEST')
                        assert (ctx.pos('short', 'future2.TEST') == 1
                                and '持仓测试失败!')
                        ctx.cover(ctx['future2.TEST-1.Minute'].close, 1,
                                  'future2.TEST')
                self.cashes.append(ctx.cash())
                self.equities.append(ctx.equity())

            def test(self, test):
                fname = os.path.join(os.getcwd(), 'data', '1MINUTE', 'TEST',
                                     'FUTURE2.csv')
                source2 = pd.read_csv(fname, parse_dates=True, index_col=0)
                global smg, multi
                target1, cashes1, t1, c1, dts = trade_closed_curbar(
                    source, capital / 2, lmg, smg, multi, 1)
                # 期货
                multi = Contract.volume_multiple('future2.TEST')
                smg = Contract.short_margin_ratio('future2.TEST')
                target2, cashes2, t2, c2, dts = trade_closed_curbar(
                    source2, capital / 2, lmg, smg, multi, -1)
                target = [x + y for x, y in zip(target1, target2)]
                cashes = [x + y for x, y in zip(cashes1, cashes2)]
                open_equities = [x + y for x, y in zip(t1, t2)]
                open_cashes = [x + y for x, y in zip(c1, c2)]

                test.assertTrue(len(self.cashes) == len(cashes), 'cash接口测试失败!')
                for i in range(0, len(self.cashes) - 1):  # 最后一根强平了无法比较
                    test.assertAlmostEqual(self.cashes[i], open_cashes[i])
                    test.assertAlmostEqual(self.equities[i], open_equities[i])

                for i, hd in enumerate(Profile.all_holdings_sum(profiles)):
                    test.assertTrue(hd['datetime'] == dts[i],
                                    'all_holdings接口测试失败!')
                    test.assertAlmostEqual(hd['equity'], target[i])
                    test.assertAlmostEqual(hd['cash'], cashes[i])

        d1 = DemoStrategy('D1')
        profiles = add_strategies(
            ['future.TEST-1.Minute', 'future2.TEST-1.Minute'],
            [{
                'strategy': d1,
                'capital': capital,
            }])

        d1.test(self)
Пример #11
0
    def test_case2(self):
        """ 测试限价的延迟成交, 与是否是期货还是股票无关。
            测试延迟成交的资金占用
        """
        buy_entries, sell_entries = [], []
        short_entries, cover_entries = [], []

        class DemoStrategyBuy(Strategy):
            """ 只开多头仓位的策略 """
            def __init__(self, name):
                super(DemoStrategyBuy, self).__init__(name)
                self.cashes = []
                self.equities = []

            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                if ctx.datetime[0] in buy_entries:
                    ctx.buy(ctx.low - OFFSET, 1)  # 确保在下一根Bar成交
                # 默认多头
                elif ctx.pos() > 0 and ctx.datetime[0].time() == st1:
                    ctx.sell(ctx.close, ctx.pos())
                self.cashes.append(ctx.cash())
                self.equities.append(ctx.equity())

            def test(self, test):
                equities, cashes, open_equities, open_cashes, dts =\
                    in_closed_nextbar(source, buy_entries, capital / 4, lmg, smg, multi, 1)
                test.assertTrue(
                    len(profiles[0].all_holdings()) == len(equities)
                    and len(equities) > 0, '模拟器测试失败!')
                for i, hd in enumerate(profiles[0].all_holdings()):
                    test.assertTrue(hd['datetime'] == dts[i], '模拟器测试失败!')
                    test.assertAlmostEqual(hd['equity'], equities[i])
                    test.assertAlmostEqual(hd['cash'], cashes[i])

                for i in range(0, len(self.equities)):
                    test.assertAlmostEqual(self.equities[i], open_equities[i])
                for i in range(0, len(self.cashes)):
                    test.assertAlmostEqual(self.cashes[i], open_cashes[i])

        class DemoStrategyShort(Strategy):
            """ 只开空头仓位的策略 """
            def __init__(self, name):
                super(DemoStrategyShort, self).__init__(name)
                self.cashes = []
                self.equities = []

            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                if ctx.datetime[0] in short_entries:
                    ctx.short(ctx.high + OFFSET, 1)
                elif ctx.pos('short') > 0 and ctx.datetime[0].time() == st1:
                    ctx.cover(ctx.close, ctx.pos('short'))
                self.cashes.append(ctx.cash())
                self.equities.append(ctx.equity())

            def test(self, test):
                # short
                equities, cashes, open_equities, open_cashes, dts =\
                    in_closed_nextbar(source, short_entries, capital / 4, lmg, smg, multi, -1)
                test.assertTrue(
                    len(profiles[2].all_holdings()) == len(equities)
                    and len(equities) > 0, '模拟器测试失败!')
                for i, hd in enumerate(profiles[2].all_holdings()):
                    test.assertTrue(hd['datetime'] == dts[i], '模拟器测试失败!')
                    test.assertAlmostEqual(hd['equity'], equities[i])
                    test.assertAlmostEqual(hd['cash'], cashes[i])

                for i in range(0, len(self.equities)):
                    test.assertAlmostEqual(self.equities[i], open_equities[i])
                    test.assertAlmostEqual(self.cashes[i], open_cashes[i])

        class DemoStrategySell(Strategy):
            """ 只开多头仓位的策略 """
            def __init__(self, name):
                super(DemoStrategySell, self).__init__(name)
                self.cashes = []
                self.equities = []

            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                if ctx.datetime[0].time() == bt1:
                    ctx.buy(ctx.close, 1)
                elif ctx.pos('long') > 0 and ctx.datetime[0] in sell_entries:
                    ctx.sell(ctx.high + OFFSET, ctx.pos())
                elif ctx.pos('long') > 0 and ctx.datetime[0].time() == st3:
                    ctx.sell(ctx.close, ctx.pos())
                self.cashes.append(ctx.cash())
                self.equities.append(ctx.equity())

            def test(self, test):
                target, cashes, open_equities, open_cashes, dts =\
                    out_closed_nextbar(source, sell_entries, capital / 4, lmg, smg, multi, 1)
                test.assertTrue(
                    len(profiles[1].all_holdings()) == len(target)
                    and len(target) > 0, '模拟器测试失败!')
                for i, hd in enumerate(profiles[1].all_holdings()):
                    test.assertTrue(hd['datetime'] == dts[i], '模拟器测试失败!')
                    test.assertAlmostEqual(hd['equity'], target[i])
                    test.assertAlmostEqual(hd['cash'], cashes[i])

                for i in range(0, len(self.cashes)):
                    test.assertAlmostEqual(self.equities[i], open_equities[i])
                    test.assertAlmostEqual(self.cashes[i], open_cashes[i])

        class DemoStrategyCover(Strategy):
            def __init__(self, name):
                super(DemoStrategyCover, self).__init__(name)
                self.cashes = []
                self.equities = []

            def on_init(self, ctx):
                """初始化数据"""
                pass

            def on_bar(self, ctx):
                if ctx.datetime[0].time() == bt1:
                    ctx.short(ctx.close, 1)
                elif ctx.pos('short') > 0 and ctx.datetime[0] in cover_entries:
                    ctx.cover(ctx.low - OFFSET, ctx.pos('short'))
                elif ctx.pos('short') > 0 and ctx.datetime[0].time() == st3:
                    ctx.cover(ctx.close, ctx.pos('short'))
                self.cashes.append(ctx.cash())
                self.equities.append(ctx.equity())

            def test(self, test):
                target, cashes, open_equities, open_cashes, dts =\
                    out_closed_nextbar(source, cover_entries, capital / 4, lmg, smg, multi, -1)
                test.assertTrue(
                    len(profiles[3].all_holdings()) == len(target)
                    and len(target) > 0, '模拟器测试失败!')
                for i, hd in enumerate(profiles[3].all_holdings()):
                    test.assertTrue(hd['datetime'] == dts[i], '模拟器测试失败!')
                    test.assertAlmostEqual(hd['equity'], target[i])
                    test.assertAlmostEqual(hd['cash'], cashes[i])
                for i in range(0, len(self.cashes)):
                    test.assertAlmostEqual(self.equities[i], open_equities[i])
                    test.assertAlmostEqual(self.cashes[i], open_cashes[i])

        b1 = DemoStrategyBuy('B1')
        b2 = DemoStrategySell('B2')
        b3 = DemoStrategyShort('B3')
        b4 = DemoStrategyCover('B4')
        buy_entries, sell_entries, short_entries, cover_entries = entries_maked_nextbar(
            source)
        profiles = add_strategies(['future.TEST-1.Minute'], [
            {
                'strategy': b1,
                'capital': capital * 0.25,
            },
            {
                'strategy': b2,
                'capital': capital * 0.25,
            },
            {
                'strategy': b3,
                'capital': capital * 0.25,
            },
            {
                'strategy': b4,
                'capital': capital * 0.25,
            },
        ])
        b1.test(self)
        b2.test(self)
        b3.test(self)
        b4.test(self)
Пример #12
0
                ctx.sell(ctx.close, ctx.pos())

        return

    def on_exit(self, ctx):
        return


if __name__ == '__main__':
    import timeit
    start = timeit.default_timer()
    set_config({'source': 'csv'})
    profiles = add_strategies(['BB.SHFE-1.Day'],
                              [{
                                  'strategy': DemoStrategy('A1'),
                                  'capital': 50000.0 * 0.5,
                              }, {
                                  'strategy': DemoStrategy2('A2'),
                                  'capital': 50000.0 * 0.5,
                              }])
    stop = timeit.default_timer()
    print("运行耗时: %d秒" % ((stop - start)))

    # 绘制k线,交易信号线
    from quantdigger.digger import finance, plotting
    s = 0
    # 绘制策略A1, 策略A2, 组合的资金曲线
    curve0 = finance.create_equity_curve(profiles[0].all_holdings())
    curve1 = finance.create_equity_curve(profiles[1].all_holdings())
    curve = finance.create_equity_curve(Profile.all_holdings_sum(profiles))
    plotting.plot_strategy(profiles[0].data(), profiles[0].technicals(),
                           profiles[0].deals(), curve0.equity.values,