[Add]开始开发Web服务端

This commit is contained in:
vn.py 2018-01-21 15:36:48 +08:00
parent 73a69e8785
commit 3a9ebc1bfa
5 changed files with 234 additions and 0 deletions

View File

@ -0,0 +1,4 @@
{
"repAddress": "tcp://*:2014",
"pubAddress": "tcp://*:0602"
}

View File

@ -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"]
}

146
examples/WebTrader/run.py Normal file
View File

@ -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)

View File

@ -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()

View File

@ -294,6 +294,11 @@ class MainEngine(object):
"""查询所有委托""" """查询所有委托"""
return self.dataEngine.getAllOrders() return self.dataEngine.getAllOrders()
#----------------------------------------------------------------------
def getAllTrades(self):
"""查询所有成交"""
return self.dataEngine.getAllTrades()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def getAllPositionDetails(self): def getAllPositionDetails(self):
"""查询本地持仓缓存细节""" """查询本地持仓缓存细节"""
@ -375,6 +380,9 @@ class DataEngine(object):
# 保存委托数据的字典 # 保存委托数据的字典
self.orderDict = {} self.orderDict = {}
# 保存成交数据的字典
self.tradeDict = {}
# 保存活动委托数据的字典(即可撤销) # 保存活动委托数据的字典(即可撤销)
self.workingOrderDict = {} self.workingOrderDict = {}
@ -487,6 +495,11 @@ class DataEngine(object):
"""获取所有委托""" """获取所有委托"""
return self.orderDict.values() return self.orderDict.values()
#----------------------------------------------------------------------
def getAllTrades(self):
"""获取所有成交"""
return self.tradeDict.values()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def getPositionDetail(self, vtSymbol): def getPositionDetail(self, vtSymbol):
"""查询持仓细节""" """查询持仓细节"""