修改风控模块关于撤单统计部分代码

This commit is contained in:
chenxy123 2017-03-08 22:27:08 +08:00
parent 7f2c47f425
commit cbcf781d20
6 changed files with 39 additions and 49 deletions

View File

@ -1,9 +1,9 @@
{ {
"orderFlowClear": 1, "orderFlowClear": 1,
"orderCancelLimit": 10,
"workingOrderLimit": 20, "workingOrderLimit": 20,
"tradeLimit": 100, "tradeLimit": 100,
"orderSizeLimit": 10, "orderSizeLimit": 10,
"active": true, "active": true,
"orderCountLimit": 400,
"orderFlowLimit": 50 "orderFlowLimit": 50
} }

View File

@ -47,9 +47,9 @@ class RmEngine(object):
self.tradeCount = EMPTY_INT # 当日成交合约数量统计 self.tradeCount = EMPTY_INT # 当日成交合约数量统计
self.tradeLimit = EMPTY_INT # 当日成交合约数量限制 self.tradeLimit = EMPTY_INT # 当日成交合约数量限制
# 单品种 # 单品种单统计
self.orderCountLimit = 450 self.orderCancelLimit = EMPTY_INT # 撤单总次数限制
self.orderCount = {} self.orderCancelDict = {} # 单一合约对应撤单次数的字典
# 活动合约相关 # 活动合约相关
self.workingOrderLimit = EMPTY_INT # 活动合约最大限制 self.workingOrderLimit = EMPTY_INT # 活动合约最大限制
@ -75,7 +75,7 @@ class RmEngine(object):
self.workingOrderLimit = d['workingOrderLimit'] self.workingOrderLimit = d['workingOrderLimit']
self.orderCountLimit = d['orderCountLimit'] self.orderCancelLimit = d['orderCancelLimit']
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def saveSetting(self): def saveSetting(self):
@ -95,7 +95,7 @@ class RmEngine(object):
d['workingOrderLimit'] = self.workingOrderLimit d['workingOrderLimit'] = self.workingOrderLimit
d['orderCountLimit'] = self.orderCountLimit d['orderCancelLimit'] = self.orderCancelLimit
# 写入json # 写入json
jsonD = json.dumps(d, indent=4) jsonD = json.dumps(d, indent=4)
@ -107,16 +107,19 @@ class RmEngine(object):
self.eventEngine.register(EVENT_TRADE, self.updateTrade) self.eventEngine.register(EVENT_TRADE, self.updateTrade)
self.eventEngine.register(EVENT_TIMER, self.updateTimer) self.eventEngine.register(EVENT_TIMER, self.updateTimer)
self.eventEngine.register(EVENT_ORDER, self.updateOrder) self.eventEngine.register(EVENT_ORDER, self.updateOrder)
#----------------------------------------------------------------------
def updateOrder(self, event): def updateOrder(self, event):
"""更新成交数据""" """更新成交数据"""
# 只需要统计撤单成功的委托
order = event.dict_['data'] order = event.dict_['data']
if not self.orderCount.has_key(order.symbol): if order.status != STATUS_CANCELLED:
self.orderCount[order.symbol] = 0 return
if order.status in [STATUS_NOTTRADED, STATUS_PARTTRADED, STATUS_ALLTRADED, STATUS_UNKNOWN]:
self.orderCount[order.symbol] += 1 if order.symbol not in self.orderCancelDict:
elif order.status == STATUS_CANCELLED: self.orderCancelDict[order.symbol] = 1
self.orderCount[order.symbol] += 2 else:
self.orderCancelDict[order.symbol] += 1
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def updateTrade(self, event): def updateTrade(self, event):
@ -156,42 +159,43 @@ class RmEngine(object):
"""检查风险""" """检查风险"""
# 如果没有启动风控检查,则直接返回成功 # 如果没有启动风控检查,则直接返回成功
if not self.active: if not self.active:
return RISK_OK return True
# 检查委托数量 # 检查委托数量
if orderReq.volume > self.orderSizeLimit: if orderReq.volume > self.orderSizeLimit:
self.writeRiskLog(u'单笔委托数量%s,超过限制%s' self.writeRiskLog(u'单笔委托数量%s,超过限制%s'
%(orderReq.volume, self.orderSizeLimit)) %(orderReq.volume, self.orderSizeLimit))
return RISK_ERROR_ORDER_SIZE return False
# 检查成交合约量 # 检查成交合约量
if self.tradeCount >= self.tradeLimit: if self.tradeCount >= self.tradeLimit:
self.writeRiskLog(u'今日总成交合约数量%s,超过限制%s' self.writeRiskLog(u'今日总成交合约数量%s,超过限制%s'
%(self.tradeCount, self.tradeLimit)) %(self.tradeCount, self.tradeLimit))
return RISK_ERROR_TRADE_COUNT return False
# 检查流控 # 检查流控
if self.orderFlowCount >= self.orderFlowLimit: if self.orderFlowCount >= self.orderFlowLimit:
self.writeRiskLog(u'委托流数量%s,超过限制每%s%s' self.writeRiskLog(u'委托流数量%s,超过限制每%s%s'
%(self.orderFlowCount, self.orderFlowClear, self.orderFlowLimit)) %(self.orderFlowCount, self.orderFlowClear, self.orderFlowLimit))
return RISK_ERROR_ORDER_FLOW_COUNT return False
# 检查总活动合约 # 检查总活动合约
workingOrderCount = len(self.mainEngine.getAllWorkingOrders()) workingOrderCount = len(self.mainEngine.getAllWorkingOrders())
if workingOrderCount >= self.workingOrderLimit: if workingOrderCount >= self.workingOrderLimit:
self.writeRiskLog(u'当前活动委托数量%s,超过限制%s' self.writeRiskLog(u'当前活动委托数量%s,超过限制%s'
%(workingOrderCount, self.workingOrderLimit)) %(workingOrderCount, self.workingOrderLimit))
return RISK_ERROR_WORKING_ORDER return False
if self.orderCount.has_key(orderReq.symbol) and self.orderCount[orderReq.symbol] > self.orderCountLimit:
self.writeRiskLog(u'%s当日报撤%s,超过限制%s'
% (orderReq.symbol, self.orderCount[orderReq.symbol], self.orderCountLimit))
return RISK_ERROR_ORDER_SEND
# 检查撤单次数
if orderReq.symbol in self.orderCancelDict and self.orderCancelDict[orderReq.symbol] >= self.orderCancelLimit:
self.writeRiskLog(u'当日%s撤单次数%s,超过限制%s'
%(orderReq.symbol, self.orderCancelDict[orderReq.symbol], self.orderCancelLimit))
return False
# 对于通过风控的委托,增加流控计数 # 对于通过风控的委托,增加流控计数
self.orderFlowCount += 1 self.orderFlowCount += 1
return RISK_OK return True
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def clearOrderFlowCount(self): def clearOrderFlowCount(self):
@ -230,10 +234,10 @@ class RmEngine(object):
"""设置活动合约限制""" """设置活动合约限制"""
self.workingOrderLimit = n self.workingOrderLimit = n
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def setOrderCountLimit(self, n): def setOrderCancelLimit(self, n):
"""设置单品种报撤次数上限""" """设置单合约撤单次数上限"""
self.orderCountLimit = n self.orderCancelLimit = n
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def switchEngineStatus(self): def switchEngineStatus(self):

View File

@ -68,7 +68,7 @@ class RmEngineManager(QtGui.QWidget):
self.spinOrderSizeLimit = RmSpinBox(self.rmEngine.orderSizeLimit) self.spinOrderSizeLimit = RmSpinBox(self.rmEngine.orderSizeLimit)
self.spinTradeLimit = RmSpinBox(self.rmEngine.tradeLimit) self.spinTradeLimit = RmSpinBox(self.rmEngine.tradeLimit)
self.spinWorkingOrderLimit = RmSpinBox(self.rmEngine.workingOrderLimit) self.spinWorkingOrderLimit = RmSpinBox(self.rmEngine.workingOrderLimit)
self.spinOrderCountLimit = RmSpinBox(self.rmEngine.orderCountLimit) self.spinOrderCancelLimit = RmSpinBox(self.rmEngine.orderCancelLimit)
buttonClearOrderFlowCount = QtGui.QPushButton(u'清空流控计数') buttonClearOrderFlowCount = QtGui.QPushButton(u'清空流控计数')
buttonClearTradeCount = QtGui.QPushButton(u'清空总成交计数') buttonClearTradeCount = QtGui.QPushButton(u'清空总成交计数')
@ -92,8 +92,9 @@ class RmEngineManager(QtGui.QWidget):
grid.addWidget(RmLine(), 8, 0, 1, 2) grid.addWidget(RmLine(), 8, 0, 1, 2)
grid.addWidget(Label(u'活动订单上限'), 9, 0) grid.addWidget(Label(u'活动订单上限'), 9, 0)
grid.addWidget(self.spinWorkingOrderLimit, 9, 1) grid.addWidget(self.spinWorkingOrderLimit, 9, 1)
grid.addWidget(Label(u'单品种报撤次数上限'), 10, 0) grid.addWidget(RmLine(), 10, 0, 1, 2)
grid.addWidget(self.spinOrderCountLimit, 10, 1) grid.addWidget(Label(u'单合约撤单上限'), 11, 0)
grid.addWidget(self.spinOrderCancelLimit, 11, 1)
hbox = QtGui.QHBoxLayout() hbox = QtGui.QHBoxLayout()
hbox.addWidget(buttonClearOrderFlowCount) hbox.addWidget(buttonClearOrderFlowCount)
@ -112,7 +113,7 @@ class RmEngineManager(QtGui.QWidget):
self.spinOrderSizeLimit.valueChanged.connect(self.rmEngine.setOrderSizeLimit) self.spinOrderSizeLimit.valueChanged.connect(self.rmEngine.setOrderSizeLimit)
self.spinTradeLimit.valueChanged.connect(self.rmEngine.setTradeLimit) self.spinTradeLimit.valueChanged.connect(self.rmEngine.setTradeLimit)
self.spinWorkingOrderLimit.valueChanged.connect(self.rmEngine.setWorkingOrderLimit) self.spinWorkingOrderLimit.valueChanged.connect(self.rmEngine.setWorkingOrderLimit)
self.spinOrderCountLimit.valueChanged.connect(self.rmEngine.setOrderCountLimit) self.spinOrderCancelLimit.valueChanged.connect(self.rmEngine.setOrderCancelLimit)
self.buttonSwitchEngineStatus.clicked.connect(self.switchEngineSatus) self.buttonSwitchEngineStatus.clicked.connect(self.switchEngineSatus)
buttonClearOrderFlowCount.clicked.connect(self.rmEngine.clearOrderFlowCount) buttonClearOrderFlowCount.clicked.connect(self.rmEngine.clearOrderFlowCount)

View File

@ -6,18 +6,6 @@ EMPTY_UNICODE = u''
EMPTY_INT = 0 EMPTY_INT = 0
EMPTY_FLOAT = 0.0 EMPTY_FLOAT = 0.0
# 风控常量
RISK_OK = 0 # 风控正常
RISK_ERROR_ORDER_SIZE = 1 # 单笔委托数量超限
RISK_ERROR_TRADE_COUNT = 2 # 今日总成交合约数量超限
RISK_ERROR_ORDER_FLOW_COUNT = 3 # 委托流数量超限
RISK_ERROR_WORKING_ORDER = 4 # 当前活动委托数量超限
RISK_ERROR_ORDER_SEND = 5 # 当日单品种报撤次数超限
RISK_MESSAGE = {RISK_ERROR_ORDER_SIZE: u'单笔委托数量超限', RISK_ERROR_TRADE_COUNT: u'今日总成交合约数量超限',
RISK_ERROR_ORDER_FLOW_COUNT: u'委托流数量超限', RISK_ERROR_WORKING_ORDER: u'当前活动委托数量超限',
RISK_ERROR_ORDER_SEND: u'当日单品种报撤次数超限'}
# 方向常量 # 方向常量
DIRECTION_NONE = u'无方向' DIRECTION_NONE = u'无方向'
DIRECTION_LONG = u'' DIRECTION_LONG = u''

View File

@ -188,9 +188,8 @@ class MainEngine(object):
def sendOrder(self, orderReq, gatewayName): def sendOrder(self, orderReq, gatewayName):
"""对特定接口发单""" """对特定接口发单"""
# 如果风控检查失败则不发单 # 如果风控检查失败则不发单
riskResult = self.rmEngine.checkRisk(orderReq) if not self.rmEngine.checkRisk(orderReq):
if riskResult != RISK_OK: return ''
return riskResult
if gatewayName in self.gatewayDict: if gatewayName in self.gatewayDict:
gateway = self.gatewayDict[gatewayName] gateway = self.gatewayDict[gatewayName]

View File

@ -291,8 +291,6 @@ class VtPositionData(VtBaseData):
self.frozen = EMPTY_INT # 冻结数量 self.frozen = EMPTY_INT # 冻结数量
self.price = EMPTY_FLOAT # 持仓均价 self.price = EMPTY_FLOAT # 持仓均价
self.vtPositionName = EMPTY_STRING # 持仓在vt系统中的唯一代码通常是vtSymbol.方向 self.vtPositionName = EMPTY_STRING # 持仓在vt系统中的唯一代码通常是vtSymbol.方向
# 20151020添加
self.ydPosition = EMPTY_INT # 昨持仓 self.ydPosition = EMPTY_INT # 昨持仓