风控模块增加单品种报撤单次数限制

This commit is contained in:
sunlit@home 2017-03-07 22:22:21 +08:00
parent 70151b0f16
commit 246dc4cba0
5 changed files with 105 additions and 58 deletions

View File

@ -4,5 +4,6 @@
"tradeLimit": 100,
"orderSizeLimit": 10,
"active": true,
"orderFlowLimit": 10
"orderCountLimit": 400,
"orderFlowLimit": 50
}

View File

@ -47,6 +47,10 @@ class RmEngine(object):
self.tradeCount = EMPTY_INT # 当日成交合约数量统计
self.tradeLimit = EMPTY_INT # 当日成交合约数量限制
# 单品种报撤
self.orderCountLimit = 450
self.orderCount = {}
# 活动合约相关
self.workingOrderLimit = EMPTY_INT # 活动合约最大限制
@ -71,6 +75,8 @@ class RmEngine(object):
self.workingOrderLimit = d['workingOrderLimit']
self.orderCountLimit = d['orderCountLimit']
#----------------------------------------------------------------------
def saveSetting(self):
"""保存风控参数"""
@ -89,6 +95,8 @@ class RmEngine(object):
d['workingOrderLimit'] = self.workingOrderLimit
d['orderCountLimit'] = self.orderCountLimit
# 写入json
jsonD = json.dumps(d, indent=4)
f.write(jsonD)
@ -98,6 +106,17 @@ class RmEngine(object):
"""注册事件监听"""
self.eventEngine.register(EVENT_TRADE, self.updateTrade)
self.eventEngine.register(EVENT_TIMER, self.updateTimer)
self.eventEngine.register(EVENT_ORDER, self.updateOrder)
def updateOrder(self, event):
"""更新成交数据"""
order = event.dict_['data']
if not self.orderCount.has_key(order.symbol):
self.orderCount[order.symbol] = 0
if order.status in [STATUS_NOTTRADED, STATUS_PARTTRADED, STATUS_ALLTRADED, STATUS_UNKNOWN]:
self.orderCount[order.symbol] += 1
elif order.status == STATUS_CANCELLED:
self.orderCount[order.symbol] += 2
#----------------------------------------------------------------------
def updateTrade(self, event):
@ -137,37 +156,42 @@ class RmEngine(object):
"""检查风险"""
# 如果没有启动风控检查,则直接返回成功
if not self.active:
return True
return RISK_OK
# 检查委托数量
if orderReq.volume > self.orderSizeLimit:
self.writeRiskLog(u'单笔委托数量%s,超过限制%s'
%(orderReq.volume, self.orderSizeLimit))
return False
return RISK_ERROR_ORDER_SIZE
# 检查成交合约量
if self.tradeCount >= self.tradeLimit:
self.writeRiskLog(u'今日总成交合约数量%s,超过限制%s'
%(self.tradeCount, self.tradeLimit))
return False
return RISK_ERROR_TRADE_COUNT
# 检查流控
if self.orderFlowCount >= self.orderFlowLimit:
self.writeRiskLog(u'委托流数量%s,超过限制每%s%s'
%(self.orderFlowCount, self.orderFlowClear, self.orderFlowLimit))
return False
return RISK_ERROR_ORDER_FLOW_COUNT
# 检查总活动合约
workingOrderCount = len(self.mainEngine.getAllWorkingOrders())
if workingOrderCount >= self.workingOrderLimit:
self.writeRiskLog(u'当前活动委托数量%s,超过限制%s'
%(workingOrderCount, self.workingOrderLimit))
return False
return RISK_ERROR_WORKING_ORDER
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
# 对于通过风控的委托,增加流控计数
self.orderFlowCount += 1
return True
return RISK_OK
#----------------------------------------------------------------------
def clearOrderFlowCount(self):
@ -206,6 +230,11 @@ class RmEngine(object):
"""设置活动合约限制"""
self.workingOrderLimit = n
#----------------------------------------------------------------------
def setOrderCountLimit(self, n):
"""设置单品种报撤次数上限"""
self.orderCountLimit = n
#----------------------------------------------------------------------
def switchEngineStatus(self):
"""开关风控引擎"""

View File

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

View File

@ -6,6 +6,18 @@ EMPTY_UNICODE = u''
EMPTY_INT = 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_LONG = u''

View File

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