tqsdk实盘
This commit is contained in:
@@ -33,6 +33,7 @@ class Strategy(ABC):
|
||||
self.symbol = symbol # 策略操作的合约Symbol
|
||||
self.params = params
|
||||
self.enable_log = enable_log
|
||||
self.trading = False
|
||||
|
||||
def on_init(self):
|
||||
"""
|
||||
@@ -78,6 +79,9 @@ class Strategy(ABC):
|
||||
发送订单的辅助方法。
|
||||
会在 BaseStrategy 内部构建 Order 对象,并通过 context 转发给模拟器。
|
||||
"""
|
||||
if not self.trading:
|
||||
return None
|
||||
|
||||
if self.context.is_rollover_bar:
|
||||
self.log(f"当前是换月K线,禁止开仓订单")
|
||||
return None
|
||||
@@ -103,16 +107,24 @@ class Strategy(ABC):
|
||||
取消指定ID的订单。
|
||||
通过 context 调用模拟器的 cancel_order 方法。
|
||||
"""
|
||||
if not self.trading:
|
||||
return False
|
||||
|
||||
return self.context.cancel_order(order_id)
|
||||
|
||||
def cancel_all_pending_orders(self) -> int:
|
||||
"""取消当前策略的未决订单,仅限于当前策略关注的Symbol。"""
|
||||
# 注意:在换月模式下,引擎会自动取消旧合约的挂单,这里是策略主动取消
|
||||
if not self.trading:
|
||||
return 0
|
||||
|
||||
pending_orders = self.get_pending_orders()
|
||||
cancelled_count = 0
|
||||
# orders_to_cancel = [
|
||||
# order.id for order in pending_orders.values() if order.symbol == self.symbol
|
||||
# ]
|
||||
orders_to_cancel = [
|
||||
order.id for order in pending_orders.values() if order.symbol == self.symbol
|
||||
order.id for order in pending_orders.values()
|
||||
]
|
||||
for order_id in orders_to_cancel:
|
||||
if self.cancel_order(order_id):
|
||||
@@ -179,3 +191,7 @@ class Strategy(ABC):
|
||||
|
||||
def get_bar_history(self):
|
||||
return self.context.get_bar_history()
|
||||
|
||||
|
||||
def get_price_history(self, key: str):
|
||||
return self.context.get_price_history(key)
|
||||
|
||||
Reference in New Issue
Block a user