From 3a9ebc1bfac61e2fb265ef65bbd4f8b33e55827e Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 21 Jan 2018 15:36:48 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E5=BC=80=E5=A7=8B=E5=BC=80=E5=8F=91Web?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/WebTrader/RS_setting.json | 4 + examples/WebTrader/VT_setting.json | 18 ++++ examples/WebTrader/run.py | 146 +++++++++++++++++++++++++++++ examples/WebTrader/server.py | 53 +++++++++++ vnpy/trader/vtEngine.py | 13 +++ 5 files changed, 234 insertions(+) create mode 100644 examples/WebTrader/RS_setting.json create mode 100644 examples/WebTrader/VT_setting.json create mode 100644 examples/WebTrader/run.py create mode 100644 examples/WebTrader/server.py diff --git a/examples/WebTrader/RS_setting.json b/examples/WebTrader/RS_setting.json new file mode 100644 index 00000000..d325f36d --- /dev/null +++ b/examples/WebTrader/RS_setting.json @@ -0,0 +1,4 @@ +{ + "repAddress": "tcp://*:2014", + "pubAddress": "tcp://*:0602" +} \ No newline at end of file diff --git a/examples/WebTrader/VT_setting.json b/examples/WebTrader/VT_setting.json new file mode 100644 index 00000000..a00ff35e --- /dev/null +++ b/examples/WebTrader/VT_setting.json @@ -0,0 +1,18 @@ +{ + "fontFamily": "微软雅黑", + "fontSize": 12, + + "mongoHost": "localhost", + "mongoPort": 27017, + "mongoLogging": true, + + "darkStyle": true, + "language": "chinese", + + "logActive": true, + "logLevel": "debug", + "logConsole": true, + "logFile": true, + + "tdPenalty": ["IF", "IH", "IC"] +} \ No newline at end of file diff --git a/examples/WebTrader/run.py b/examples/WebTrader/run.py new file mode 100644 index 00000000..6ff8b476 --- /dev/null +++ b/examples/WebTrader/run.py @@ -0,0 +1,146 @@ +# encoding: UTF-8 + +# 修改编码 +import sys +reload(sys) +sys.setdefaultencoding('utf8') + +# 创建主引擎代理对象 +from vnpy.event import EventEngine2 +from vnpy.trader.vtEvent import EVENT_LOG +from vnpy.trader.app.rpcService.rsClient import MainEngineProxy + +reqAddress = 'tcp://localhost:2014' +subAddress = 'tcp://localhost:0602' + +ee = EventEngine2() +me = MainEngineProxy(ee) +me.init(reqAddress, subAddress) + +#---------------------------------------------------------------------- +def printLog(event): + """打印日志""" + log = event.dict_['data'] + print log.logTime, log.logContent + +ee.register(EVENT_LOG, printLog) + + +# 创建Flask对象 +from flask import Flask +from flask.ext.restful import Api, Resource, reqparse + +app = Flask(__name__) +api = Api(app) + +# 创建资源 +######################################################################## +class Gateways(Resource): + """接口""" + + #---------------------------------------------------------------------- + def __init__(self): + """初始化""" + self.parser = reqparse.RequestParser() + self.parser.add_argument('name') + super(Gateways, self).__init__() + + #---------------------------------------------------------------------- + def get(self): + """查询""" + l = me.getAllGatewayDetails() + return l + + #---------------------------------------------------------------------- + def post(self): + """连接""" + args = self.parser.parse_args() + name = args['name'] + me.connect(name) + + +######################################################################## +class Orders(Resource): + """委托""" + + #---------------------------------------------------------------------- + def __init__(self): + """初始化""" + self.parser = reqparse.RequestParser() + self.parser.add_argument('name') + super(Gateways, self).__init__() + + #---------------------------------------------------------------------- + def get(self): + """查询""" + data = me.getAllOrders() + l = {o.__dict__ for o in data} + return l + + #---------------------------------------------------------------------- + def post(self): + """发单""" + pass + + #---------------------------------------------------------------------- + def delete(self): + """撤单""" + pass + + +######################################################################## +class Trades(Resource): + """成交""" + + #---------------------------------------------------------------------- + def get(self): + """查询""" + data = me.getAllTrades() + l = {o.__dict__ for o in data} + return l + + +######################################################################## +class Accounts(Resource): + """账户""" + + #---------------------------------------------------------------------- + def get(self): + """查询""" + pass + + +######################################################################## +class Positions(Resource): + """持仓""" + + #---------------------------------------------------------------------- + def get(self): + """查询""" + pass + + +######################################################################## +class Contracts(Resource): + """合约""" + + #---------------------------------------------------------------------- + def get(self): + """查询""" + pass + + + + +# 注册资源 +api.add_resource(Gateways, '/gateways') +api.add_resource(Orders, '/orders') +api.add_resource(Trades, '/trades') +api.add_resource(Accounts, '/accounts') +api.add_resource(Positions, '/positions') +api.add_resource(Contracts, '/contracts') + + + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/examples/WebTrader/server.py b/examples/WebTrader/server.py new file mode 100644 index 00000000..7337fff7 --- /dev/null +++ b/examples/WebTrader/server.py @@ -0,0 +1,53 @@ +# encoding: UTF-8 + +# 重载sys模块,设置默认字符串编码方式为utf8 +import sys +reload(sys) +sys.setdefaultencoding('utf8') + +from time import sleep + +# vn.trader模块 +from vnpy.event import EventEngine2 +from vnpy.trader.vtEngine import MainEngine, LogEngine + +# 加载底层接口 +from vnpy.trader.gateway import ctpGateway + +# 加载上层应用 +from vnpy.trader.app import ctaStrategy, rpcService + + +#---------------------------------------------------------------------- +def main(): + """主程序入口""" + le = LogEngine() + le.setLogLevel(le.LEVEL_INFO) + le.addConsoleHandler() + le.addFileHandler() + + le.info(u'服务器进程启动') + + # 创建事件引擎 + ee = EventEngine2() + le.info(u'事件引擎创建成功') + + # 创建主引擎 + me = MainEngine(ee) + + # 添加交易接口 + me.addGateway(ctpGateway) + + # 添加上层应用 + me.addApp(ctaStrategy) + me.addApp(rpcService) + + le.info(u'主引擎创建成功') + + # 阻塞运行 + le.info(u'服务器启动成功') + while 1: + sleep(1) + +if __name__ == '__main__': + main() diff --git a/vnpy/trader/vtEngine.py b/vnpy/trader/vtEngine.py index 41430b44..385d9b3d 100644 --- a/vnpy/trader/vtEngine.py +++ b/vnpy/trader/vtEngine.py @@ -294,6 +294,11 @@ class MainEngine(object): """查询所有委托""" return self.dataEngine.getAllOrders() + #---------------------------------------------------------------------- + def getAllTrades(self): + """查询所有成交""" + return self.dataEngine.getAllTrades() + #---------------------------------------------------------------------- def getAllPositionDetails(self): """查询本地持仓缓存细节""" @@ -375,6 +380,9 @@ class DataEngine(object): # 保存委托数据的字典 self.orderDict = {} + # 保存成交数据的字典 + self.tradeDict = {} + # 保存活动委托数据的字典(即可撤销) self.workingOrderDict = {} @@ -487,6 +495,11 @@ class DataEngine(object): """获取所有委托""" return self.orderDict.values() + #---------------------------------------------------------------------- + def getAllTrades(self): + """获取所有成交""" + return self.tradeDict.values() + #---------------------------------------------------------------------- def getPositionDetail(self, vtSymbol): """查询持仓细节"""