From e296a8098cc09792babdcb4940a1e75d45d095f0 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 22 Mar 2019 23:07:20 +0800 Subject: [PATCH] [Add]call strategy on_stop_order when receives server stop order event update --- vnpy/app/cta_strategy/engine.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/vnpy/app/cta_strategy/engine.py b/vnpy/app/cta_strategy/engine.py index 9eefcf2a..63c8c707 100644 --- a/vnpy/app/cta_strategy/engine.py +++ b/vnpy/app/cta_strategy/engine.py @@ -22,7 +22,9 @@ from vnpy.trader.object import ( ContractData ) from vnpy.trader.event import EVENT_TICK, EVENT_ORDER, EVENT_TRADE -from vnpy.trader.constant import Direction, OrderType, Interval, Exchange, Offset +from vnpy.trader.constant import ( + Direction, OrderType, Interval, Exchange, Offset, Status +) from vnpy.trader.utility import load_json, save_json from vnpy.trader.database import DbTickData, DbBarData from vnpy.trader.setting import SETTINGS @@ -39,6 +41,16 @@ from .base import ( from .template import CtaTemplate +STOP_STATUS_MAP = { + Status.SUBMITTING: StopOrderStatus.WAITING, + Status.NOTTRADED: StopOrderStatus.WAITING, + Status.PARTTRADED: StopOrderStatus.TRIGGERED, + Status.ALLTRADED: StopOrderStatus.TRIGGERED, + Status.CANCELLED: StopOrderStatus.CANCELLED, + Status.REJECTED: StopOrderStatus.CANCELLED +} + + class CtaEngine(BaseEngine): """""" @@ -183,6 +195,22 @@ class CtaEngine(BaseEngine): if order.vt_orderid in vt_orderids and not order.is_active(): vt_orderids.remove(order.vt_orderid) + # For server stop order, call strategy on_stop_order function + if order.type == OrderType.STOP: + so = StopOrder( + vt_symbol=order.vt_symbol, + direction=order.direction, + offset=order.offset, + price=order.price, + volume=order.volume, + stop_orderid=order.vt_orderid, + strategy_name=strategy.strategy_name, + status=STOP_STATUS_MAP[order.status], + vt_orderid=order.vt_orderid, + ) + self.call_strategy_func(strategy, strategy.on_stop_order, so) + + # Call strategy on_order function self.call_strategy_func(strategy, strategy.on_order, order) def process_trade_event(self, event: Event):