Update engine.py

This commit is contained in:
1122455801 2019-05-30 15:11:17 +08:00
parent d76e842a73
commit a5dff8e948

View File

@ -1,6 +1,7 @@
"""""" """"""
from collections import defaultdict from collections import defaultdict
from vnpy.trader.object import OrderRequest from vnpy.trader.object import OrderRequest, LogData
from vnpy.event import Event, EventEngine, EVENT_TIMER from vnpy.event import Event, EventEngine, EVENT_TIMER
from vnpy.trader.engine import BaseEngine, MainEngine from vnpy.trader.engine import BaseEngine, MainEngine
from vnpy.trader.event import EVENT_TRADE, EVENT_ORDER, EVENT_LOG from vnpy.trader.event import EVENT_TRADE, EVENT_ORDER, EVENT_LOG
@ -12,14 +13,15 @@ APP_NAME = "RiskManager"
class RiskManagerEngine(BaseEngine): class RiskManagerEngine(BaseEngine):
"""风控引擎""" """"""
setting_filename = "risk_manager_setting.json" setting_filename = "risk_manager_setting.json"
def __init__(self, main_engine: MainEngine, event_engine: EventEngine): def __init__(self, main_engine: MainEngine, event_engine: EventEngine):
"""""" """"""
super().__init__(main_engine, event_engine, APP_NAME)
self.main_engine = main_engine self.main_engine = main_engine
self.event_engine = event_engine self.event_engine = event_engine
main_engine.rmEngine = self
self.active = False self.active = False
self.order_flow_count = 0 self.order_flow_count = 0
@ -33,20 +35,32 @@ class RiskManagerEngine(BaseEngine):
self.order_cancel_counts = defaultdict(int) self.order_cancel_counts = defaultdict(int)
self.active_order_limit = 20 self.active_order_limit = 20
# Patch send order function of MainEngine
self._send_order = self.main_engine.send_order
self.main_engine.send_order = self.send_order
self.load_setting() self.load_setting()
self.registerEvent() self.register_event()
def send_order(self, req: OrderRequest, gateway_name: str):
""""""
result = self.check_risk(req, gateway_name)
if not result:
return ""
return self._send_order(req, gateway_name)
def load_setting(self): def load_setting(self):
"""""" """"""
setting = load_json(self.setting_filename) setting = load_json(self.setting_filename)
self.active = setting["active"] self.active = setting.get("active", self.active)
self.order_flow_limit = setting["order_flow_limit"] self.order_flow_limit = setting.get("order_flow_limit", self.order_flow_count)
self.order_flow_clear = setting["order_flow_clear"] self.order_flow_clear = setting.get("order_flow_clear", self.order_flow_clear)
self.order_size_limit = setting["order_size_limit"] self.order_size_limit = setting.get("order_size_limit", self.order_size_limit)
self.trade_limit = setting["trade_limit"] self.trade_limit = setting.get("trade_limit", self.trade_limit)
self.active_order_limit = setting["active_order_limit"] self.active_order_limit = setting.get("active_order_limit", self.active_order_limit)
self.order_cancel_limit = setting["order_cancel_limit"] self.order_cancel_limit = setting.get("order_cancel_limit", self.order_cancel_limit)
def save_setting(self): def save_setting(self):
"""""" """"""
@ -90,10 +104,8 @@ class RiskManagerEngine(BaseEngine):
def write_risk_log(self, msg: str): def write_risk_log(self, msg: str):
"""""" """"""
event = Event( log = LogData(msg=msg, gateway_name="RiskManager")
EVENT_LOG, event = Event(type=EVENT_LOG, data=log)
msg
)
self.event_engine.put(event) self.event_engine.put(event)
def check_risk(self, req: OrderRequest, gateway_name: str): def check_risk(self, req: OrderRequest, gateway_name: str):
@ -117,13 +129,13 @@ class RiskManagerEngine(BaseEngine):
# Check flow count # Check flow count
if self.order_flow_count >= self.order_flow_limit: if self.order_flow_count >= self.order_flow_limit:
self.write_risk_log(f"委托流数量{self.order_flow_count},超过限制每{self.order_flow_clear}{self.order_flow_limit}") self.write_risk_log(f"委托流数量{self.order_flow_count},超过限制每{self.order_flow_clear}{self.order_flow_limit}")
return False return False
# Check all active orders # Check all active orders
active_order_count = len(self.main_engine.get_all_active_orders()) active_order_count = len(self.main_engine.get_all_active_orders())
if active_order_count >= self.active_order_limit: if active_order_count >= self.active_order_limit:
self.write_risk_log(f"当前活动委托{active_order_count},超过限制{self.active_order_limit}") self.write_risk_log(f"当前活动委托{active_order_count},超过限制{self.active_order_limit}")
return False return False
# Check order cancel counts # Check order cancel counts
@ -181,4 +193,3 @@ class RiskManagerEngine(BaseEngine):
def stop(self): def stop(self):
"""""" """"""
self.save_setting() self.save_setting()