2018-01-21 07:36:48 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
|
|
# 修改编码
|
|
|
|
import sys
|
|
|
|
reload(sys)
|
|
|
|
sys.setdefaultencoding('utf8')
|
|
|
|
|
|
|
|
# 创建主引擎代理对象
|
|
|
|
from vnpy.event import EventEngine2
|
2018-01-21 09:07:13 +00:00
|
|
|
from vnpy.trader.vtEvent import (EVENT_TICK, EVENT_ORDER, EVENT_TRADE,
|
|
|
|
EVENT_ACCOUNT, EVENT_POSITION, EVENT_LOG,
|
|
|
|
EVENT_ERROR, EVENT_CONTRACT)
|
2018-01-21 08:37:01 +00:00
|
|
|
from vnpy.trader.vtObject import VtSubscribeReq, VtOrderReq, VtCancelOrderReq
|
2018-01-21 07:36:48 +00:00
|
|
|
from vnpy.trader.app.rpcService.rsClient import MainEngineProxy
|
2018-02-07 02:12:47 +00:00
|
|
|
from vnpy.trader.app.ctaStrategy.ctaBase import EVENT_CTA_LOG, EVENT_CTA_STRATEGY
|
2018-01-21 07:36:48 +00:00
|
|
|
|
2018-01-30 05:19:49 +00:00
|
|
|
reqAddress = 'tcp://localhost:6688'
|
|
|
|
subAddress = 'tcp://localhost:8866'
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2018-01-23 13:16:02 +00:00
|
|
|
# 载入Web密码
|
|
|
|
import json
|
|
|
|
import base64
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
TODAY = str(datetime.datetime.now().date())
|
|
|
|
|
|
|
|
with open("WEB_setting.json") as f:
|
|
|
|
setting = json.load(f)
|
|
|
|
USERNAME = setting['username']
|
|
|
|
PASSWORD = setting['password']
|
2018-02-09 09:09:18 +00:00
|
|
|
TOKEN = base64.encodestring(TODAY+PASSWORD).replace('\n','')
|
2018-01-23 13:16:02 +00:00
|
|
|
|
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
# 创建Flask对象
|
2018-02-12 15:02:47 +00:00
|
|
|
from flask import Flask, send_file
|
2018-01-21 07:36:48 +00:00
|
|
|
from flask.ext.restful import Api, Resource, reqparse
|
2018-01-21 09:07:13 +00:00
|
|
|
from flask.ext.socketio import SocketIO
|
2018-02-09 09:09:18 +00:00
|
|
|
from flask_cors import *
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2018-02-09 09:09:18 +00:00
|
|
|
CORS(app, supports_credentials=True)
|
2018-01-21 07:36:48 +00:00
|
|
|
api = Api(app)
|
2018-01-21 09:07:13 +00:00
|
|
|
socketio = SocketIO(app)
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
# 创建资源
|
2018-01-23 13:16:02 +00:00
|
|
|
########################################################################
|
|
|
|
class Token(Resource):
|
|
|
|
"""登录验证"""
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('username')
|
|
|
|
self.parser.add_argument('password')
|
2018-01-25 06:14:08 +00:00
|
|
|
super(Token, self).__init__()
|
2018-01-23 13:16:02 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""查询"""
|
|
|
|
args = self.parser.parse_args()
|
|
|
|
username = args['username']
|
|
|
|
password = args['password']
|
|
|
|
|
|
|
|
if username == USERNAME and password == PASSWORD:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':TOKEN}
|
2018-01-23 13:16:02 +00:00
|
|
|
else:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'wrong username or password'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
########################################################################
|
2018-01-21 08:07:48 +00:00
|
|
|
class Gateway(Resource):
|
2018-01-21 07:36:48 +00:00
|
|
|
"""接口"""
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
2018-01-21 08:37:01 +00:00
|
|
|
self.parser.add_argument('gatewayName')
|
2018-01-23 13:16:02 +00:00
|
|
|
self.parser.add_argument('token')
|
|
|
|
|
2018-01-21 09:07:13 +00:00
|
|
|
super(Gateway, self).__init__()
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""查询"""
|
2018-01-23 13:16:02 +00:00
|
|
|
args = self.parser.parse_args()
|
|
|
|
token = args['token']
|
2018-03-08 01:35:25 +00:00
|
|
|
|
2018-01-23 13:16:02 +00:00
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
l = me.getAllGatewayDetails()
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def post(self):
|
|
|
|
"""连接"""
|
|
|
|
args = self.parser.parse_args()
|
2018-01-23 13:16:02 +00:00
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
print 'token error'
|
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 08:37:01 +00:00
|
|
|
gatewayName = args['gatewayName']
|
|
|
|
me.connect(gatewayName)
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':''}
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
2018-01-21 08:07:48 +00:00
|
|
|
class Order(Resource):
|
2018-01-21 07:36:48 +00:00
|
|
|
"""委托"""
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
2018-01-23 13:16:02 +00:00
|
|
|
self.getParser = reqparse.RequestParser()
|
|
|
|
self.getParser.add_argument('token')
|
|
|
|
|
2018-01-21 08:37:01 +00:00
|
|
|
self.postParser = reqparse.RequestParser()
|
|
|
|
self.postParser.add_argument('vtSymbol')
|
|
|
|
self.postParser.add_argument('price')
|
|
|
|
self.postParser.add_argument('volume')
|
|
|
|
self.postParser.add_argument('priceType')
|
|
|
|
self.postParser.add_argument('direction')
|
|
|
|
self.postParser.add_argument('offset')
|
2018-01-23 13:16:02 +00:00
|
|
|
self.postParser.add_argument('token')
|
2018-01-21 08:37:01 +00:00
|
|
|
|
|
|
|
self.deleteParser = reqparse.RequestParser()
|
2018-01-22 09:34:49 +00:00
|
|
|
self.deleteParser.add_argument('vtOrderID')
|
2018-01-23 13:16:02 +00:00
|
|
|
self.deleteParser.add_argument('token')
|
2018-01-21 08:37:01 +00:00
|
|
|
|
2018-01-21 09:07:13 +00:00
|
|
|
super(Order, self).__init__()
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""查询"""
|
2018-01-23 13:16:02 +00:00
|
|
|
args = self.getParser.parse_args()
|
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
data = me.getAllOrders()
|
2018-01-30 05:19:49 +00:00
|
|
|
l = [o.__dict__ for o in data]
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def post(self):
|
|
|
|
"""发单"""
|
2018-02-03 15:22:18 +00:00
|
|
|
args = self.postParser.parse_args()
|
2018-01-23 13:16:02 +00:00
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
|
|
|
print args
|
2018-01-21 08:37:01 +00:00
|
|
|
vtSymbol = args['vtSymbol']
|
|
|
|
price = args['price']
|
|
|
|
volume = args['volume']
|
|
|
|
priceType = args['priceType']
|
|
|
|
direction = args['direction']
|
|
|
|
offset = args['offset']
|
|
|
|
|
|
|
|
contract = me.getContract(vtSymbol)
|
|
|
|
if not contract:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'contract error'}
|
|
|
|
|
|
|
|
priceType_map = {'PRICETYPE_LIMITPRICE' : u'限价','PRICETYPE_MARKETPRICE' : u'市价','PRICETYPE_FAK' : u'FAK','PRICETYPE_FOK' : u'FOK'}
|
|
|
|
direction_map = {'DIRECTION_LONG' : u'多','DIRECTION_SHORT' : u'空'}
|
|
|
|
offset_map = {'OFFSET_OPEN' : u'开仓', 'OFFSET_CLOSE' : u'平仓','OFFSET_CLOSETODAY' : u'平今','OFFSET_CLOSEYESTERDAY' : u'平昨'}
|
2018-01-21 08:37:01 +00:00
|
|
|
|
|
|
|
req = VtOrderReq()
|
2018-02-09 09:09:18 +00:00
|
|
|
req.symbol = contract.symbol
|
|
|
|
req.exchange = contract.exchange
|
|
|
|
req.price = float(price)
|
|
|
|
req.volume = int(volume)
|
|
|
|
req.priceType = priceType_map[ priceType ]
|
|
|
|
req.direction = direction_map[ direction ]
|
|
|
|
req.offset = offset_map[ offset ]
|
|
|
|
vtOrderID = me.sendOrder(req, contract.gatewayName)
|
|
|
|
return {'result_code':'success','data':vtOrderID}
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def delete(self):
|
|
|
|
"""撤单"""
|
2018-01-21 08:37:01 +00:00
|
|
|
args = self.deleteParser.parse_args()
|
2018-01-23 13:16:02 +00:00
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
|
|
|
|
|
|
|
vtOrderID = args['vtOrderID']
|
2018-01-21 08:37:01 +00:00
|
|
|
|
2018-02-13 09:22:42 +00:00
|
|
|
# 撤单某一委托
|
|
|
|
if vtOrderID:
|
|
|
|
order = me.getOrder(vtOrderID)
|
|
|
|
if not order:
|
|
|
|
return {'result_code':'error','message':'vtOrderID error'}
|
|
|
|
|
|
|
|
self.cancel(order)
|
|
|
|
# 全撤
|
|
|
|
else:
|
|
|
|
l = me.getAllWorkingOrders()
|
|
|
|
|
|
|
|
for order in l:
|
|
|
|
self.cancel(order)
|
|
|
|
|
|
|
|
return {'result_code':'success','data':""}
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def cancel(self, order):
|
|
|
|
"""撤单"""
|
2018-02-09 09:09:18 +00:00
|
|
|
req = VtCancelOrderReq()
|
|
|
|
req.orderID = order.orderID
|
|
|
|
req.exchange = order.exchange
|
|
|
|
req.symbol = order.symbol
|
2018-02-13 09:22:42 +00:00
|
|
|
req.frontID = order.frontID
|
|
|
|
req.sessionID = order.sessionID
|
|
|
|
me.cancelOrder(req, order.gatewayName)
|
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
########################################################################
|
2018-01-21 08:07:48 +00:00
|
|
|
class Trade(Resource):
|
2018-01-21 07:36:48 +00:00
|
|
|
"""成交"""
|
2018-01-23 13:16:02 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('token')
|
|
|
|
|
|
|
|
super(Trade, self).__init__()
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""查询"""
|
2018-01-23 13:16:02 +00:00
|
|
|
args = self.parser.parse_args()
|
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
data = me.getAllTrades()
|
2018-01-30 05:19:49 +00:00
|
|
|
l = [o.__dict__ for o in data]
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
2018-01-21 08:07:48 +00:00
|
|
|
class Account(Resource):
|
2018-01-21 07:36:48 +00:00
|
|
|
"""账户"""
|
2018-01-23 13:16:02 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('token')
|
|
|
|
|
|
|
|
super(Account, self).__init__()
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""查询"""
|
2018-01-23 13:16:02 +00:00
|
|
|
args = self.parser.parse_args()
|
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 08:07:48 +00:00
|
|
|
data = me.getAllAccounts()
|
2018-01-30 05:19:49 +00:00
|
|
|
l = [o.__dict__ for o in data]
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
2018-01-21 08:07:48 +00:00
|
|
|
class Position(Resource):
|
2018-01-21 07:36:48 +00:00
|
|
|
"""持仓"""
|
2018-01-23 13:16:02 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('token')
|
|
|
|
|
|
|
|
super(Position, self).__init__()
|
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""查询"""
|
2018-01-23 13:16:02 +00:00
|
|
|
args = self.parser.parse_args()
|
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 08:07:48 +00:00
|
|
|
data = me.getAllPositions()
|
2018-02-09 09:09:18 +00:00
|
|
|
print 'position',data
|
2018-01-30 05:19:49 +00:00
|
|
|
l = [o.__dict__ for o in data]
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
2018-01-21 08:07:48 +00:00
|
|
|
class Contract(Resource):
|
2018-01-21 07:36:48 +00:00
|
|
|
"""合约"""
|
|
|
|
|
2018-01-23 13:16:02 +00:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('token')
|
|
|
|
|
|
|
|
super(Contract, self).__init__()
|
|
|
|
|
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""查询"""
|
2018-01-23 13:16:02 +00:00
|
|
|
args = self.parser.parse_args()
|
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 08:07:48 +00:00
|
|
|
data = me.getAllContracts()
|
2018-02-09 09:09:18 +00:00
|
|
|
print 'Contract',data
|
2018-01-30 05:19:49 +00:00
|
|
|
l = [o.__dict__ for o in data]
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-01-21 08:07:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
class Log(Resource):
|
|
|
|
"""日志"""
|
2018-01-23 13:16:02 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('token')
|
|
|
|
|
|
|
|
super(Log, self).__init__()
|
2018-01-21 08:07:48 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""查询"""
|
2018-01-23 13:16:02 +00:00
|
|
|
args = self.parser.parse_args()
|
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 08:07:48 +00:00
|
|
|
data = me.getLog()
|
2018-01-30 05:19:49 +00:00
|
|
|
l = [o.__dict__ for o in data]
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-01-21 08:07:48 +00:00
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
|
2018-01-21 08:07:48 +00:00
|
|
|
########################################################################
|
|
|
|
class Error(Resource):
|
|
|
|
"""错误"""
|
2018-01-23 13:16:02 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('token')
|
|
|
|
|
|
|
|
super(Error, self).__init__()
|
2018-01-21 07:36:48 +00:00
|
|
|
|
2018-01-21 08:07:48 +00:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""查询"""
|
2018-01-23 13:16:02 +00:00
|
|
|
args = self.parser.parse_args()
|
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 08:07:48 +00:00
|
|
|
data = me.getError()
|
2018-01-30 05:19:49 +00:00
|
|
|
l = [o.__dict__ for o in data]
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
|
2018-01-21 08:37:01 +00:00
|
|
|
########################################################################
|
|
|
|
class Tick(Resource):
|
|
|
|
"""行情"""
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-21 08:37:01 +00:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('vtSymbol')
|
2018-01-23 13:16:02 +00:00
|
|
|
self.parser.add_argument('token')
|
2018-01-21 09:07:13 +00:00
|
|
|
super(Tick, self).__init__()
|
2018-01-21 08:37:01 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def post(self):
|
|
|
|
"""订阅"""
|
|
|
|
args = self.parser.parse_args()
|
2018-01-23 13:16:02 +00:00
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
|
|
|
|
2018-01-21 08:37:01 +00:00
|
|
|
vtSymbol = args['vtSymbol']
|
2018-02-09 09:09:18 +00:00
|
|
|
|
2018-01-21 08:37:01 +00:00
|
|
|
contract = me.getContract(vtSymbol)
|
|
|
|
if not contract:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'contract error'}
|
|
|
|
|
2018-01-21 08:37:01 +00:00
|
|
|
req = VtSubscribeReq()
|
|
|
|
req.symbol = contract.symbol
|
|
|
|
req.exchange = contract.exchange
|
|
|
|
req.vtSymbol = contract.vtSymbol
|
2018-02-09 09:09:18 +00:00
|
|
|
|
2018-01-21 08:37:01 +00:00
|
|
|
me.subscribe(req, contract.gatewayName)
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':''}
|
2018-01-21 08:37:01 +00:00
|
|
|
|
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
########################################################################
|
|
|
|
class CtaStrategyInit(Resource):
|
|
|
|
"""初始化策略"""
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('name')
|
2018-01-23 13:16:02 +00:00
|
|
|
self.parser.add_argument('token')
|
2018-01-22 14:52:47 +00:00
|
|
|
super(CtaStrategyInit, self).__init__()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def post(self):
|
|
|
|
"""订阅"""
|
|
|
|
args = self.parser.parse_args()
|
2018-01-23 13:16:02 +00:00
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
name = args['name']
|
|
|
|
|
|
|
|
engine = me.getApp('CtaStrategy')
|
|
|
|
if not name:
|
|
|
|
engine.initAll()
|
|
|
|
else:
|
|
|
|
engine.initStrategy(name)
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':''}
|
2018-01-22 14:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
class CtaStrategyStart(Resource):
|
|
|
|
"""启动策略"""
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('name')
|
2018-01-23 13:16:02 +00:00
|
|
|
self.parser.add_argument('token')
|
2018-01-22 14:52:47 +00:00
|
|
|
super(CtaStrategyStart, self).__init__()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def post(self):
|
|
|
|
"""订阅"""
|
|
|
|
args = self.parser.parse_args()
|
2018-01-23 13:16:02 +00:00
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
name = args['name']
|
|
|
|
|
|
|
|
engine = me.getApp('CtaStrategy')
|
|
|
|
if not name:
|
|
|
|
engine.startAll()
|
|
|
|
else:
|
|
|
|
engine.startStrategy(name)
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':''}
|
2018-01-22 14:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
class CtaStrategyStop(Resource):
|
|
|
|
"""停止策略"""
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('name')
|
2018-01-23 13:16:02 +00:00
|
|
|
self.parser.add_argument('token')
|
2018-01-22 14:52:47 +00:00
|
|
|
super(CtaStrategyStop, self).__init__()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def post(self):
|
|
|
|
"""订阅"""
|
|
|
|
args = self.parser.parse_args()
|
2018-01-23 13:16:02 +00:00
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
name = args['name']
|
|
|
|
|
|
|
|
engine = me.getApp('CtaStrategy')
|
|
|
|
if not name:
|
|
|
|
engine.stopAll()
|
|
|
|
else:
|
|
|
|
engine.stopStrategy(name)
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':''}
|
2018-01-22 14:52:47 +00:00
|
|
|
|
2018-02-09 05:37:05 +00:00
|
|
|
########################################################################
|
|
|
|
class CtaStrategyName(Resource):
|
2018-02-09 09:09:18 +00:00
|
|
|
"""»ñÈ¡²ßÂÔÃû"""
|
2018-02-09 05:37:05 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
2018-02-09 09:09:18 +00:00
|
|
|
"""³õʼ»¯"""
|
2018-02-09 05:37:05 +00:00
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('token')
|
|
|
|
super(CtaStrategyName, self).__init__()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
2018-02-09 09:09:18 +00:00
|
|
|
"""»ñÈ¡²ßÂÔÃû"""
|
2018-02-09 05:37:05 +00:00
|
|
|
args = self.parser.parse_args()
|
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-02-09 05:37:05 +00:00
|
|
|
|
|
|
|
engine = me.getApp('CtaStrategy')
|
|
|
|
l = engine.getStrategyNames()
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-02-09 05:37:05 +00:00
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
########################################################################
|
|
|
|
class CtaStrategyLoad(Resource):
|
|
|
|
"""加载策略"""
|
|
|
|
|
2018-01-23 13:16:02 +00:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('token')
|
|
|
|
super(CtaStrategyLoad, self).__init__()
|
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def post(self):
|
2018-01-23 13:16:02 +00:00
|
|
|
"""订阅"""
|
|
|
|
args = self.parser.parse_args()
|
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
engine = me.getApp('CtaStrategy')
|
|
|
|
engine.loadSetting()
|
|
|
|
l = engine.getStrategyNames()
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'success','data':l}
|
2018-01-22 14:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
class CtaStrategyParam(Resource):
|
|
|
|
"""查询策略参数"""
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('name')
|
2018-01-23 13:16:02 +00:00
|
|
|
self.parser.add_argument('token')
|
2018-01-22 14:52:47 +00:00
|
|
|
super(CtaStrategyParam, self).__init__()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""订阅"""
|
|
|
|
args = self.parser.parse_args()
|
2018-01-23 13:16:02 +00:00
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
name = args['name']
|
|
|
|
|
|
|
|
engine = me.getApp('CtaStrategy')
|
2018-02-09 09:09:18 +00:00
|
|
|
l = engine.getStrategyParam(name)
|
|
|
|
return {'result_code':'success','data':l}
|
2018-01-22 14:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
class CtaStrategyVar(Resource):
|
|
|
|
"""查询策略变量"""
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def __init__(self):
|
|
|
|
"""初始化"""
|
|
|
|
self.parser = reqparse.RequestParser()
|
|
|
|
self.parser.add_argument('name')
|
2018-01-23 13:16:02 +00:00
|
|
|
self.parser.add_argument('token')
|
2018-01-22 14:52:47 +00:00
|
|
|
super(CtaStrategyVar, self).__init__()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def get(self):
|
|
|
|
"""订阅"""
|
|
|
|
args = self.parser.parse_args()
|
2018-01-23 13:16:02 +00:00
|
|
|
token = args['token']
|
|
|
|
if token != TOKEN:
|
2018-02-09 09:09:18 +00:00
|
|
|
return {'result_code':'error','message':'token error'}
|
2018-01-23 13:16:02 +00:00
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
name = args['name']
|
|
|
|
|
|
|
|
engine = me.getApp('CtaStrategy')
|
2018-02-09 09:09:18 +00:00
|
|
|
l = engine.getStrategyVar(name)
|
|
|
|
return {'result_code':'success','data':l}
|
2018-01-22 14:52:47 +00:00
|
|
|
|
2018-02-12 15:02:47 +00:00
|
|
|
|
|
|
|
########################################################################
|
|
|
|
@app.route('/')
|
|
|
|
def index_html():
|
|
|
|
"""首页"""
|
2018-02-13 09:22:42 +00:00
|
|
|
return send_file('./templates/index.html')
|
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
# 注册资源
|
2018-01-25 06:14:08 +00:00
|
|
|
api.add_resource(Token, '/token')
|
2018-01-21 08:07:48 +00:00
|
|
|
api.add_resource(Gateway, '/gateway')
|
|
|
|
api.add_resource(Order, '/order')
|
|
|
|
api.add_resource(Trade, '/trades')
|
|
|
|
api.add_resource(Account, '/account')
|
|
|
|
api.add_resource(Position, '/position')
|
|
|
|
api.add_resource(Contract, '/contract')
|
|
|
|
api.add_resource(Log, '/log')
|
|
|
|
api.add_resource(Error, '/error')
|
2018-01-21 08:37:01 +00:00
|
|
|
api.add_resource(Tick, '/tick')
|
2018-01-21 07:36:48 +00:00
|
|
|
|
2018-01-22 14:52:47 +00:00
|
|
|
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')
|
2018-02-09 05:37:05 +00:00
|
|
|
api.add_resource(CtaStrategyName, '/ctastrategy/name')
|
2018-01-22 14:52:47 +00:00
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
|
2018-01-21 09:07:13 +00:00
|
|
|
# SocketIO
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
def handleEvent(event):
|
|
|
|
"""处理事件"""
|
|
|
|
eventType = event.type_
|
2018-02-09 09:09:18 +00:00
|
|
|
|
2018-02-08 04:35:28 +00:00
|
|
|
eventData = event.dict_['data']
|
|
|
|
|
|
|
|
if not isinstance(eventData, dict):
|
2018-02-09 09:12:03 +00:00
|
|
|
eventData = eventData.__dict__
|
2018-02-09 09:09:18 +00:00
|
|
|
|
2018-02-09 09:12:03 +00:00
|
|
|
if eventType == 'eTick.':
|
2018-02-09 09:09:18 +00:00
|
|
|
del eventData['datetime']
|
|
|
|
|
2018-01-22 09:34:49 +00:00
|
|
|
socketio.emit(eventType, eventData)
|
2018-01-21 09:07:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
ee.register(EVENT_TICK, handleEvent)
|
|
|
|
ee.register(EVENT_ORDER, handleEvent)
|
|
|
|
ee.register(EVENT_TRADE, handleEvent)
|
|
|
|
ee.register(EVENT_ACCOUNT, handleEvent)
|
|
|
|
ee.register(EVENT_POSITION, handleEvent)
|
|
|
|
ee.register(EVENT_CONTRACT, handleEvent)
|
|
|
|
ee.register(EVENT_LOG, handleEvent)
|
|
|
|
ee.register(EVENT_ERROR, handleEvent)
|
2018-02-07 02:12:47 +00:00
|
|
|
ee.register(EVENT_CTA_LOG, handleEvent)
|
|
|
|
ee.register(EVENT_CTA_STRATEGY, handleEvent)
|
2018-02-09 09:09:18 +00:00
|
|
|
|
2018-01-21 07:36:48 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-02-09 09:09:18 +00:00
|
|
|
socketio.run(app,debug=True,host='0.0.0.0',port=5000)
|