[Fix]bugs in cta strategy app

This commit is contained in:
vn.py 2019-03-27 14:44:48 +08:00
parent 4a1af72300
commit 1c601278ab
5 changed files with 68 additions and 85 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,7 @@
Defines constants and objects used in CtaStrategy App.
"""
from dataclasses import dataclass
from dataclasses import dataclass, field
from enum import Enum
from vnpy.trader.constant import Direction, Offset
@ -36,9 +36,9 @@ class StopOrder:
volume: float
stop_orderid: str
strategy_name: str
lock: bool = False
vt_orderids: list = field(default_factory=list)
status: StopOrderStatus = StopOrderStatus.WAITING
vt_orderids: list
lock: bool
EVENT_CTA_LOG = "eCtaLog"

View File

@ -45,13 +45,13 @@ class OffsetConverter:
holding = self.get_position_holding(order.vt_symbol)
holding.update_order(order)
def update_order_request(self, req: OrderRequest):
def update_order_request(self, req: OrderRequest, vt_orderid: str):
""""""
if not self.is_convert_required(req.vt_symbol):
return
holding = self.get_position_holding(req.vt_symbol)
holding.update_order_request(req)
holding.update_order_request(req, vt_orderid)
def get_position_holding(self, vt_symbol: str):
""""""
@ -76,12 +76,12 @@ class OffsetConverter:
else:
return [req]
@lru_cache
@lru_cache()
def is_convert_required(self, vt_symbol: str):
"""
Check if the contract needs offset convert.
"""
contract = self.main_engine.get(vt_symbol)
contract = self.main_engine.get_contract(vt_symbol)
# Only contracts with long-short position mode requires convert
if not contract.net_position:
@ -139,7 +139,10 @@ class PositionHolding:
def update_order_request(self, req: OrderRequest, vt_orderid: str):
""""""
gateway_name, orderid = vt_orderid.split(".")
ix = vt_orderid.index(".")
gateway_name = vt_orderid[:ix]
orderid = vt_orderid[ix + 1:]
order = req.create_order_data(orderid, gateway_name)
self.update_order(order)

View File

@ -282,9 +282,12 @@ class CtaEngine(BaseEngine):
price = tick.limit_down
else:
price = tick.bid_price_5
contract = self.main_engine.get_contract(stop_order.vt_symbol)
vt_orderids = self.send_limit_order(
strategy,
contract,
stop_order.direction,
stop_order.offset,
price,
@ -297,9 +300,9 @@ class CtaEngine(BaseEngine):
# Remove from relation map.
self.stop_orders.pop(stop_order.stop_orderid)
vt_orderids = self.strategy_orderid_map[strategy.strategy_name]
if stop_order.stop_orderid in vt_orderids:
vt_orderids.remove(stop_order.stop_orderid)
strategy_vt_orderids = self.strategy_orderid_map[strategy.strategy_name]
if stop_order.stop_orderid in strategy_vt_orderids:
strategy_vt_orderids.remove(stop_order.stop_orderid)
# Change stop order status to cancelled and update to strategy.
stop_order.status = StopOrderStatus.TRIGGERED
@ -308,6 +311,7 @@ class CtaEngine(BaseEngine):
self.call_strategy_func(
strategy, strategy.on_stop_order, stop_order
)
self.put_stop_order_event(stop_order)
def send_server_order(
self,
@ -341,11 +345,11 @@ class CtaEngine(BaseEngine):
vt_orderids = []
for req in req_list:
self.offset_converter.update_order_request(req)
vt_orderid = self.main_engine.send_order(
req, contract.gateway_name)
vt_orderids.append(vt_orderid)
self.offset_converter.update_order_request(req, vt_orderid)
# Save relationship between orderid and strategy.
self.orderid_strategy_map[vt_orderid] = strategy
@ -436,6 +440,7 @@ class CtaEngine(BaseEngine):
vt_orderids.add(stop_orderid)
self.call_strategy_func(strategy, strategy.on_stop_order, stop_order)
self.put_stop_order_event(stop_order)
return stop_orderid
@ -471,6 +476,7 @@ class CtaEngine(BaseEngine):
stop_order.status = StopOrderStatus.CANCELLED
self.call_strategy_func(strategy, strategy.on_stop_order, stop_order)
self.put_stop_order_event(stop_order)
def send_order(
self,
@ -493,7 +499,7 @@ class CtaEngine(BaseEngine):
if contract.stop_supported:
return self.send_server_stop_order(strategy, contract, direction, offset, price, volume, lock)
else:
return self.send_local_stop_order(strategy, contract, direction, offset, price, volume, lock)
return self.send_local_stop_order(strategy, direction, offset, price, volume, lock)
else:
return self.send_limit_order(strategy, contract, direction, offset, price, volume, lock)

View File

@ -302,23 +302,6 @@ class DataMonitor(QtWidgets.QTableWidget):
cell.setText(str(value))
class StrategyCell(BaseCell):
"""
Cell used for showing strategy name.
"""
def __init__(self, content: str, data: Any):
""""""
super(StrategyCell, self).__init__(content, data)
def set_content(self, content: Any, data: Any):
"""
Set text using enum.constant.value.
"""
if content:
super(StrategyCell, self).set_content(content.strategy_name, data)
class StopOrderMonitor(BaseMonitor):
"""
Monitor for local stop order.
@ -335,25 +318,16 @@ class StopOrderMonitor(BaseMonitor):
"update": False,
},
"vt_orderids": {"display": "限价委托号", "cell": BaseCell, "update": True},
"vt_symbol": {"display": "代码", "cell": BaseCell, "update": False},
"order_type": {"display": "类型", "cell": EnumCell, "update": False},
"vt_symbol": {"display": "本地代码", "cell": BaseCell, "update": False},
"direction": {"display": "方向", "cell": EnumCell, "update": False},
"offset": {"display": "开平", "cell": EnumCell, "update": False},
"price": {"display": "价格", "cell": BaseCell, "update": False},
"volume": {"display": "数量", "cell": BaseCell, "update": False},
"status": {"display": "状态", "cell": EnumCell, "update": True},
"lock": {"display": "锁仓", "cell": BaseCell, "update": False},
"strategy": {"display": "策略名", "cell": StrategyCell, "update": False},
"strategy_name": {"display": "策略名", "cell": BaseCell, "update": False},
}
def init_ui(self):
"""
Stretch columns.
"""
super(StopOrderMonitor, self).init_ui()
self.horizontalHeader().setSectionResizeMode(
QtWidgets.QHeaderView.Stretch
)
class LogMonitor(BaseMonitor):
"""