From c0e09795feb93c6800a1a67a3d9dc005577dae03 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 22 Jan 2018 22:52:47 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E6=B7=BB=E5=8A=A0CTA=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E5=BC=95=E6=93=8E=E7=9B=B8=E5=85=B3=E7=9A=84restful=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/WebTrader/run.py | 134 +++++++++++++++++++++ vnpy/trader/app/ctaStrategy/ctaEngine.py | 5 + vnpy/trader/app/ctaStrategy/uiCtaWidget.py | 3 +- 3 files changed, 141 insertions(+), 1 deletion(-) diff --git a/examples/WebTrader/run.py b/examples/WebTrader/run.py index 0533109e..6f3fe6fc 100644 --- a/examples/WebTrader/run.py +++ b/examples/WebTrader/run.py @@ -235,6 +235,133 @@ class Tick(Resource): return True +######################################################################## +class CtaStrategyInit(Resource): + """初始化策略""" + + #---------------------------------------------------------------------- + def __init__(self): + """初始化""" + self.parser = reqparse.RequestParser() + self.parser.add_argument('name') + super(CtaStrategyInit, self).__init__() + + #---------------------------------------------------------------------- + def post(self): + """订阅""" + args = self.parser.parse_args() + name = args['name'] + + engine = me.getApp('CtaStrategy') + if not name: + engine.initAll() + else: + engine.initStrategy(name) + + +######################################################################## +class CtaStrategyStart(Resource): + """启动策略""" + + #---------------------------------------------------------------------- + def __init__(self): + """初始化""" + self.parser = reqparse.RequestParser() + self.parser.add_argument('name') + super(CtaStrategyStart, self).__init__() + + #---------------------------------------------------------------------- + def post(self): + """订阅""" + args = self.parser.parse_args() + name = args['name'] + + engine = me.getApp('CtaStrategy') + if not name: + engine.startAll() + else: + engine.startStrategy(name) + + +######################################################################## +class CtaStrategyStop(Resource): + """停止策略""" + + #---------------------------------------------------------------------- + def __init__(self): + """初始化""" + self.parser = reqparse.RequestParser() + self.parser.add_argument('name') + super(CtaStrategyStop, self).__init__() + + #---------------------------------------------------------------------- + def post(self): + """订阅""" + args = self.parser.parse_args() + name = args['name'] + + engine = me.getApp('CtaStrategy') + if not name: + engine.stopAll() + else: + engine.stopStrategy(name) + + +######################################################################## +class CtaStrategyLoad(Resource): + """加载策略""" + + #---------------------------------------------------------------------- + def post(self): + """订阅""" + engine = me.getApp('CtaStrategy') + engine.loadSetting() + l = engine.getStrategyNames() + return l + + +######################################################################## +class CtaStrategyParam(Resource): + """查询策略参数""" + + #---------------------------------------------------------------------- + def __init__(self): + """初始化""" + self.parser = reqparse.RequestParser() + self.parser.add_argument('name') + super(CtaStrategyParam, self).__init__() + + #---------------------------------------------------------------------- + def get(self): + """订阅""" + args = self.parser.parse_args() + name = args['name'] + + engine = me.getApp('CtaStrategy') + return engine.getStrategyParam(name) + + +######################################################################## +class CtaStrategyVar(Resource): + """查询策略变量""" + + #---------------------------------------------------------------------- + def __init__(self): + """初始化""" + self.parser = reqparse.RequestParser() + self.parser.add_argument('name') + super(CtaStrategyVar, self).__init__() + + #---------------------------------------------------------------------- + def get(self): + """订阅""" + args = self.parser.parse_args() + name = args['name'] + + engine = me.getApp('CtaStrategy') + return engine.getStrategyVar(name) + + # 注册资源 api.add_resource(Gateway, '/gateway') api.add_resource(Order, '/order') @@ -246,6 +373,13 @@ api.add_resource(Log, '/log') api.add_resource(Error, '/error') api.add_resource(Tick, '/tick') +api.add_resource(CtaStrategyLoad, '/ctastrategy/load') +api.add_resource(CtaStrategyInit, '/ctastrategy/init') +api.add_resource(CtaStrategyStart, '/ctastrategy/start') +api.add_resource(CtaStrategyStop, '/ctastrategy/stop') +api.add_resource(CtaStrategyParam, '/ctastrategy/param') +api.add_resource(CtaStrategyVar, '/ctastrategy/var') + # SocketIO #---------------------------------------------------------------------- diff --git a/vnpy/trader/app/ctaStrategy/ctaEngine.py b/vnpy/trader/app/ctaStrategy/ctaEngine.py index 6d438ee6..ef53b3e0 100644 --- a/vnpy/trader/app/ctaStrategy/ctaEngine.py +++ b/vnpy/trader/app/ctaStrategy/ctaEngine.py @@ -555,6 +555,11 @@ class CtaEngine(object): else: self.writeCtaLog(u'策略实例不存在:' + name) return None + + #---------------------------------------------------------------------- + def getStrategyNames(self): + """查询所有策略名称""" + return self.strategyDict.keys() #---------------------------------------------------------------------- def putStrategyEvent(self, name): diff --git a/vnpy/trader/app/ctaStrategy/uiCtaWidget.py b/vnpy/trader/app/ctaStrategy/uiCtaWidget.py index 2b0cb212..8599e0c1 100644 --- a/vnpy/trader/app/ctaStrategy/uiCtaWidget.py +++ b/vnpy/trader/app/ctaStrategy/uiCtaWidget.py @@ -219,7 +219,8 @@ class CtaEngineManager(QtWidgets.QWidget): w = QtWidgets.QWidget() vbox = QtWidgets.QVBoxLayout() - for name in self.ctaEngine.strategyDict.keys(): + l = self.ctaEngine.getStrategyNames() + for name in l: strategyManager = CtaStrategyManager(self.ctaEngine, self.eventEngine, name) vbox.addWidget(strategyManager)