掘金社区

Strategy.stop()无效?Pinned highlighted

gscq 发表在掘金2 2016-01-20 11:38:41

掘金2
1742
8
0

用的python windows gmsdk 2.7.0 , 将例子简单改如下:

# !/usr/bin/env python
# -*- coding: utf-8 -*-
from gmsdk.api import StrategyBase


class MyStrategy(StrategyBase):
    def __init__(self, *args, **kwargs):
        super(MyStrategy, self).__init__(*args, **kwargs)
        self.oc = True

    def on_bar(self, bar):
        print('on_bar ', self)
        if self.oc:
            self.open_long(bar.exchange, bar.sec_id, 0, 100)
        else:
            self.close_long(bar.exchange, bar.sec_id, 0, 100)
        self.oc = not self.oc

    def run(self):
        print('run ', self)
        super().run()


if __name__ == '__main__':
    mystrategy = MyStrategy(
        username='demo@myquant.cn',
        password='123456',
        strategy_id='strategy_2',
        subscribe_symbols='SHSE.600000.bar.daily',
        mode=4,
        td_addr='localhost:8001')
    ret = mystrategy.backtest_config(
        start_time='2015-04-15 9:00:00',
        end_time='2015-05-15 15:00:00',
        initial_cash=1000000,
        transaction_ratio=1,
        commission_ratio=0,
        slippage_ratio=0,
        price_type=1)
    print('config status: ', ret)
    ret = mystrategy.run()
    print('exit code: ', ret)
    mystrategy.stop()

    mystrategy2 = MyStrategy(
        username='demo@myquant.cn',
        password='123456',
        strategy_id='strategy_2',
        subscribe_symbols='SHSE.600000.bar.daily',
        mode=4,
        td_addr='localhost:8001')
    ret = mystrategy2.backtest_config(
        start_time='2015-04-15 9:00:00',
        end_time='2015-05-15 15:00:00',
        initial_cash=1000000,
        transaction_ratio=1,
        commission_ratio=0,
        slippage_ratio=0,
        price_type=1)
    print('config status: ', ret)
    ret = mystrategy2.run()
    print('exit code: ', ret)

打印:

C:Anaconda3python.exe C:/Users/gsc/Documents/Apps/gmsdk-2.7.0-py3-x64-windows/examples/test_backtest.py
config status:  0
run  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
exit code:  None
config status:  0
run  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
on_bar  <__main__.MyStrategy object at 0x00000000028D7470>
on_bar  <__main__.MyStrategy object at 0x00000000028D72E8>
exit code:  None

Process finished with exit code 0

说明mystrategy2 运行的时候mystrategy 的on_bar还是会被调用,那么stop()有什么用?怎么解决这种情况的问题?

评论: 8
  • @bidong @A_10 看起来strategystop()并没有stop,请技术人员解答一下

    2016-01-20 11:44:17
  • run是一直阻塞的,所以你调用stop的代码实际上永远执行不到。你需要在事件的回调中调用stop:比如某种事件触发后,你需要退出策略。 这是事件驱动模型的设计方式。

    2016-01-21 01:57:29
  • 是的。一个策略进程只能存在一个策略对象。

    2016-01-21 02:04:48
  • 为什么mystrategy.stop()下面的都执行到了,你说stop执行不到,不是阻塞完之后才能打印exit code: None吗,另外我试了在on_bar里加入self.unsubscribe(self.subscribe_symbols)和self.stop()效果也一样。

    2016-01-21 02:30:22
  • 回测模式下,回测结束后run会自动退出,这个时候stop没有意义。stop的调用结果就是让run退出阻塞。

    2016-01-21 02:34:45
  • 实时和模拟模式下,除非stop被调用,run永远不会退出。

    2016-01-21 02:35:11
  • 就是没意义,主要问题是退出后再启动一个,为什么原来那个的on_bar仍然会被调用@bidong

    2016-01-21 02:36:08
  • 其实我也想问这个问题,我创建了两个对象,怎么才能够独立运行(时间上错开,先算完一个再算另一个),但是怎么才能隔开两个的on_bar呢? 怎么才能让第一个对象的on_bar彻底结束,然后开始计算第二个对象的on_bar呢?@bidong

    2016-01-21 03:03:33

Looks like your connection to 掘金量化社区 - 量化交易者的策略交流学习社区 was lost, please wait while we try to reconnect.