1、新增SMCPureH1LongStrategy策略
2、修复实盘bug
This commit is contained in:
@@ -22,7 +22,7 @@ class SimpleLimitBuyStrategyLong(Strategy):
|
||||
def __init__(
|
||||
self,
|
||||
context: Any,
|
||||
symbol: str,
|
||||
main_symbol: str,
|
||||
enable_log: bool,
|
||||
trade_volume: int,
|
||||
range_factor: float,
|
||||
@@ -46,7 +46,7 @@ class SimpleLimitBuyStrategyLong(Strategy):
|
||||
stop_loss_points (float): 止损点数(例如,亏损达到此点数则止损)。
|
||||
take_profit_points (float): 止盈点数(例如,盈利达到此点数则止盈)。
|
||||
"""
|
||||
super().__init__(context, symbol, enable_log)
|
||||
super().__init__(context, main_symbol, enable_log)
|
||||
self.trade_volume = trade_volume
|
||||
self.range_factor = range_factor
|
||||
self.profit_factor = profit_factor
|
||||
@@ -57,6 +57,8 @@ class SimpleLimitBuyStrategyLong(Strategy):
|
||||
self.indicator = indicator
|
||||
self.lag = lag
|
||||
|
||||
self.main_symbol = main_symbol
|
||||
|
||||
self.order_id_counter = 0
|
||||
|
||||
self._last_order_id: Optional[str] = None # 用于跟踪上一根K线发出的订单ID
|
||||
@@ -71,7 +73,7 @@ class SimpleLimitBuyStrategyLong(Strategy):
|
||||
|
||||
def on_init(self):
|
||||
super().on_init()
|
||||
count = self.cancel_all_pending_orders()
|
||||
count = self.cancel_all_pending_orders(self.main_symbol)
|
||||
self.log(f"取消{count}笔订单")
|
||||
|
||||
def on_open_bar(self, open: float, symbol: str):
|
||||
@@ -138,12 +140,15 @@ class SimpleLimitBuyStrategyLong(Strategy):
|
||||
if avg_entry_price is not None:
|
||||
pnl_per_unit = open - avg_entry_price # 当前浮动盈亏(以收盘价计算)
|
||||
|
||||
stop_position_points = range_1_ago * self.profit_factor
|
||||
stop_position_points = 20
|
||||
|
||||
self.log(
|
||||
f"[{current_datetime}] PnL per unit: {pnl_per_unit:.2f}, 目标: {range_1_ago * self.profit_factor:.2f}"
|
||||
f"[{current_datetime}] PnL per unit: {pnl_per_unit:.2f}, 目标: {stop_position_points:.2f}"
|
||||
)
|
||||
|
||||
# 止盈条件
|
||||
if pnl_per_unit >= range_1_ago * self.profit_factor:
|
||||
if pnl_per_unit >= stop_position_points:
|
||||
|
||||
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
|
||||
self.order_id_counter += 1
|
||||
@@ -246,7 +251,7 @@ class SimpleLimitBuyStrategyLong(Strategy):
|
||||
# self.log(f"[{current_datetime}] 不满足开仓条件:持仓={current_pos_volume}, 待处理订单={len(pending_orders_after_cancel)}, K线历史长度={len(bar_history)}")
|
||||
|
||||
def on_close_bar(self, bar: Bar, next_bar_open: Optional[float] = None):
|
||||
self.cancel_all_pending_orders()
|
||||
self.cancel_all_pending_orders(self.main_symbol)
|
||||
|
||||
def on_rollover(self, old_symbol: str, new_symbol: str):
|
||||
"""
|
||||
@@ -270,7 +275,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
|
||||
def __init__(
|
||||
self,
|
||||
context: Any,
|
||||
symbol: str,
|
||||
main_symbol: str,
|
||||
enable_log: bool,
|
||||
trade_volume: int,
|
||||
range_factor: float,
|
||||
@@ -294,7 +299,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
|
||||
stop_loss_points (float): 止损点数(例如,亏损达到此点数则止损)。
|
||||
take_profit_points (float): 止盈点数(例如,盈利达到此点数则止盈)。
|
||||
"""
|
||||
super().__init__(context, symbol, enable_log)
|
||||
super().__init__(context, main_symbol, enable_log)
|
||||
self.trade_volume = trade_volume
|
||||
self.range_factor = range_factor
|
||||
self.profit_factor = profit_factor
|
||||
@@ -305,6 +310,8 @@ class SimpleLimitBuyStrategyShort(Strategy):
|
||||
self.indicator = indicator
|
||||
self.lag = lag
|
||||
|
||||
self.main_symbol = main_symbol
|
||||
|
||||
self.order_id_counter = 0
|
||||
|
||||
self._last_order_id: Optional[str] = None # 用于跟踪上一根K线发出的订单ID
|
||||
@@ -319,7 +326,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
|
||||
|
||||
def on_init(self):
|
||||
super().on_init()
|
||||
count = self.cancel_all_pending_orders()
|
||||
count = self.cancel_all_pending_orders(self.main_symbol)
|
||||
self.log(f"取消{count}笔订单")
|
||||
|
||||
def on_open_bar(self, open: float, symbol: str):
|
||||
@@ -489,7 +496,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
|
||||
# self.log(f"[{current_datetime}] 不满足开仓条件:持仓={current_pos_volume}, 待处理订单={len(pending_orders_after_cancel)}, K线历史长度={len(bar_history)}")
|
||||
|
||||
def on_close_bar(self, bar: Bar, next_bar_open: Optional[float] = None):
|
||||
self.cancel_all_pending_orders()
|
||||
self.cancel_all_pending_orders(self.main_symbol)
|
||||
|
||||
def on_rollover(self, old_symbol: str, new_symbol: str):
|
||||
"""
|
||||
@@ -513,7 +520,7 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
def __init__(
|
||||
self,
|
||||
context: Any,
|
||||
symbol: str,
|
||||
main_symbol: str,
|
||||
enable_log: bool,
|
||||
trade_volume: int,
|
||||
range_factor_l: float,
|
||||
@@ -527,6 +534,8 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
indicator_l: Indicator = None,
|
||||
indicator_s: Indicator = None,
|
||||
lag: int = 7,
|
||||
lag_l: int = None,
|
||||
lag_s: int = None,
|
||||
): # 新增:止盈点数
|
||||
"""
|
||||
初始化策略。
|
||||
@@ -540,7 +549,7 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
stop_loss_points (float): 止损点数(例如,亏损达到此点数则止损)。
|
||||
take_profit_points (float): 止盈点数(例如,盈利达到此点数则止盈)。
|
||||
"""
|
||||
super().__init__(context, symbol, enable_log)
|
||||
super().__init__(context, main_symbol, enable_log)
|
||||
self.trade_volume = trade_volume
|
||||
self.range_factor_l = range_factor_l
|
||||
self.profit_factor_l = profit_factor_l
|
||||
@@ -552,7 +561,16 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
self.use_indicator = use_indicator
|
||||
self.indicator_l = indicator_l
|
||||
self.indicator_s = indicator_s
|
||||
self.lag = lag
|
||||
|
||||
self.main_symbol = main_symbol
|
||||
|
||||
self.lag_l = lag_l
|
||||
self.lag_s = lag_s
|
||||
|
||||
if self.lag_l is None:
|
||||
self.lag_l = lag
|
||||
if self.lag_s is None:
|
||||
self.lag_s = lag
|
||||
|
||||
self.order_id_counter = 0
|
||||
|
||||
@@ -570,7 +588,7 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
|
||||
def on_init(self):
|
||||
super().on_init()
|
||||
count = self.cancel_all_pending_orders()
|
||||
count = self.cancel_all_pending_orders(self.main_symbol)
|
||||
self.log(f"取消{count}笔订单")
|
||||
|
||||
def on_open_bar(self, open, symbol):
|
||||
@@ -583,24 +601,30 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
self.symbol = symbol
|
||||
current_datetime = self.get_current_time()
|
||||
|
||||
self.log(
|
||||
f"[{current_datetime}] - 当前Open={open:.2f}, "
|
||||
)
|
||||
|
||||
# --- 1. 撤销上一根K线未成交的订单 ---
|
||||
count = self.cancel_all_pending_orders(self.main_symbol)
|
||||
self.log(f"取消{count}笔订单")
|
||||
# 检查是否记录了上一笔订单ID,并且该订单仍然在待处理列表中
|
||||
if self._last_order_id:
|
||||
pending_orders = self.get_pending_orders()
|
||||
if self._last_order_id in pending_orders:
|
||||
success = self.cancel_order(
|
||||
self._last_order_id
|
||||
) # 直接调用基类的取消方法
|
||||
if success:
|
||||
self.log(
|
||||
f"[{current_datetime}] 策略: 成功撤销上一根K线未成交订单 {self._last_order_id}"
|
||||
)
|
||||
else:
|
||||
self.log(
|
||||
f"[{current_datetime}] 策略: 尝试撤销订单 {self._last_order_id} 失败(可能已成交或不存在)"
|
||||
)
|
||||
# 无论撤销成功与否,既然我们尝试了撤销,就清除记录
|
||||
self._last_order_id = None
|
||||
# if self._last_order_id:
|
||||
# pending_orders = self.get_pending_orders()
|
||||
# if self._last_order_id in pending_orders:
|
||||
# success = self.cancel_order(
|
||||
# self._last_order_id
|
||||
# ) # 直接调用基类的取消方法
|
||||
# if success:
|
||||
# self.log(
|
||||
# f"[{current_datetime}] 策略: 成功撤销上一根K线未成交订单 {self._last_order_id}"
|
||||
# )
|
||||
# else:
|
||||
# self.log(
|
||||
# f"[{current_datetime}] 策略: 尝试撤销订单 {self._last_order_id} 失败(可能已成交或不存在)"
|
||||
# )
|
||||
# # 无论撤销成功与否,既然我们尝试了撤销,就清除记录
|
||||
# self._last_order_id = None
|
||||
# else:
|
||||
# self.log(f"[{current_datetime}] 策略: 无上一根K线未成交订单需要撤销。")
|
||||
|
||||
@@ -610,6 +634,7 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
# 获取当前持仓和未决订单(在取消之后获取,确保是最新的状态)
|
||||
current_positions = self.get_current_positions()
|
||||
current_pos_volume = current_positions.get(self.symbol, 0)
|
||||
self.log(f'current_pos_volume: {current_pos_volume}')
|
||||
pending_orders_after_cancel = (
|
||||
self.get_pending_orders()
|
||||
) # 再次获取,此时应已取消旧订单
|
||||
@@ -617,79 +642,21 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
range_1_ago = None
|
||||
|
||||
bar_history = self.get_bar_history()
|
||||
if len(bar_history) > 16:
|
||||
|
||||
# 获取前1根K线 (倒数第二根) 和前7根K线 (队列中最老的一根)
|
||||
bar_1_ago = bar_history[-self.lag]
|
||||
|
||||
# 计算历史 K 线的 Range
|
||||
# 持有多仓
|
||||
if (
|
||||
current_pos_volume > 0 and len(bar_history) > 16
|
||||
): # 假设只做多,所以持仓量 > 0
|
||||
|
||||
bar_1_ago = bar_history[-self.lag_l]
|
||||
range_1_ago = bar_1_ago.high - bar_1_ago.low
|
||||
|
||||
# --- 3. 平仓逻辑 (止损/止盈) ---
|
||||
# 只有当有持仓时才考虑平仓
|
||||
if (
|
||||
current_pos_volume < 0 and range_1_ago is not None
|
||||
): # 假设只做多,所以持仓量 > 0
|
||||
avg_entry_price = self.get_average_position_price(self.symbol)
|
||||
if avg_entry_price is not None:
|
||||
pnl_per_unit = avg_entry_price - open # 当前浮动盈亏(以收盘价计算)
|
||||
|
||||
self.log(
|
||||
f"[{current_datetime}] 止盈信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {self.take_profit_points:.2f}"
|
||||
)
|
||||
|
||||
# 止盈条件
|
||||
if pnl_per_unit >= range_1_ago * self.profit_factor_s:
|
||||
|
||||
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
|
||||
self.order_id_counter += 1
|
||||
|
||||
# 创建一个限价多单
|
||||
order = Order(
|
||||
id=order_id,
|
||||
symbol=self.symbol,
|
||||
direction="CLOSE_SHORT",
|
||||
volume=trade_volume,
|
||||
price_type="MARKET",
|
||||
# limit_price=limit_price,
|
||||
submitted_time=current_datetime,
|
||||
offset="CLOSE",
|
||||
)
|
||||
trade = self.send_order(order)
|
||||
return # 平仓后本K线不再进行开仓判断
|
||||
|
||||
# 止损条件
|
||||
elif pnl_per_unit <= -self.stop_loss_points:
|
||||
self.log(
|
||||
f"[{current_datetime}] 止损信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {-self.stop_loss_points:.2f}"
|
||||
)
|
||||
# 发送市价卖出订单平仓,确保立即成交
|
||||
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
|
||||
self.order_id_counter += 1
|
||||
|
||||
# 创建一个限价多单
|
||||
order = Order(
|
||||
id=order_id,
|
||||
symbol=self.symbol,
|
||||
direction="CLOSE_SHORT",
|
||||
volume=trade_volume,
|
||||
price_type="MARKET",
|
||||
# limit_price=limit_price,
|
||||
submitted_time=current_datetime,
|
||||
offset="CLOSE",
|
||||
)
|
||||
trade = self.send_order(order)
|
||||
return # 平仓后本K线不再进行开仓判断
|
||||
|
||||
if (
|
||||
current_pos_volume > 0 and range_1_ago is not None
|
||||
): # 假设只做多,所以持仓量 > 0
|
||||
avg_entry_price = self.get_average_position_price(self.symbol)
|
||||
if avg_entry_price is not None:
|
||||
pnl_per_unit = open - avg_entry_price # 当前浮动盈亏(以收盘价计算)
|
||||
|
||||
self.log(
|
||||
f"[{current_datetime}] 止盈信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {self.take_profit_points:.2f}"
|
||||
f"[{current_datetime}] 止盈信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {self.take_profit_points:.2f}, 止损: {-self.stop_loss_points:.2f}"
|
||||
)
|
||||
|
||||
# 止盈条件
|
||||
@@ -703,7 +670,7 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
id=order_id,
|
||||
symbol=self.symbol,
|
||||
direction="CLOSE_LONG",
|
||||
volume=trade_volume,
|
||||
volume=abs(current_pos_volume),
|
||||
price_type="MARKET",
|
||||
# limit_price=limit_price,
|
||||
submitted_time=current_datetime,
|
||||
@@ -714,9 +681,6 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
|
||||
# 止损条件
|
||||
elif pnl_per_unit <= -self.stop_loss_points:
|
||||
self.log(
|
||||
f"[{current_datetime}] 止损信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {-self.stop_loss_points:.2f}"
|
||||
)
|
||||
# 发送市价卖出订单平仓,确保立即成交
|
||||
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
|
||||
self.order_id_counter += 1
|
||||
@@ -726,7 +690,63 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
id=order_id,
|
||||
symbol=self.symbol,
|
||||
direction="CLOSE_LONG",
|
||||
volume=trade_volume,
|
||||
volume=abs(current_pos_volume),
|
||||
price_type="MARKET",
|
||||
# limit_price=limit_price,
|
||||
submitted_time=current_datetime,
|
||||
offset="CLOSE",
|
||||
)
|
||||
trade = self.send_order(order)
|
||||
return # 平仓后本K线不再进行开仓判断
|
||||
|
||||
# 持有空仓
|
||||
if (
|
||||
current_pos_volume < 0 and len(bar_history) > 16
|
||||
): # 假设只做多,所以持仓量 > 0
|
||||
|
||||
bar_1_ago = bar_history[-self.lag_s]
|
||||
range_1_ago = bar_1_ago.high - bar_1_ago.low
|
||||
|
||||
avg_entry_price = self.get_average_position_price(self.symbol)
|
||||
if avg_entry_price is not None:
|
||||
pnl_per_unit = avg_entry_price - open # 当前浮动盈亏(以收盘价计算)
|
||||
|
||||
self.log(
|
||||
f"[{current_datetime}] 止盈信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {self.take_profit_points:.2f}, 止损: {-self.stop_loss_points:.2f}"
|
||||
)
|
||||
|
||||
# 止盈条件
|
||||
if pnl_per_unit >= range_1_ago * self.profit_factor_s:
|
||||
|
||||
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
|
||||
self.order_id_counter += 1
|
||||
|
||||
# 创建一个限价多单
|
||||
order = Order(
|
||||
id=order_id,
|
||||
symbol=self.symbol,
|
||||
direction="CLOSE_SHORT",
|
||||
volume=abs(current_pos_volume),
|
||||
price_type="MARKET",
|
||||
# limit_price=limit_price,
|
||||
submitted_time=current_datetime,
|
||||
offset="CLOSE",
|
||||
)
|
||||
trade = self.send_order(order)
|
||||
return # 平仓后本K线不再进行开仓判断
|
||||
|
||||
# 止损条件
|
||||
elif pnl_per_unit <= -self.stop_loss_points:
|
||||
# 发送市价卖出订单平仓,确保立即成交
|
||||
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
|
||||
self.order_id_counter += 1
|
||||
|
||||
# 创建一个限价多单
|
||||
order = Order(
|
||||
id=order_id,
|
||||
symbol=self.symbol,
|
||||
direction="CLOSE_SHORT",
|
||||
volume=abs(current_pos_volume),
|
||||
price_type="MARKET",
|
||||
# limit_price=limit_price,
|
||||
submitted_time=current_datetime,
|
||||
@@ -739,7 +759,7 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
# 只有在没有持仓 (current_pos_volume == 0) 且没有待处理订单 (not pending_orders_after_cancel)
|
||||
# 且K线历史足够长时才考虑开仓
|
||||
|
||||
if current_pos_volume == 0 and range_1_ago is not None:
|
||||
if current_pos_volume == 0 and len(bar_history) > 16:
|
||||
|
||||
if not self.use_indicator or self.indicator_l.is_condition_met(
|
||||
np.array(self.get_price_history("close")),
|
||||
@@ -750,6 +770,9 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
):
|
||||
# 根据策略逻辑计算目标买入价格
|
||||
# 目标买入价 = 当前K线Open - (前1根Range * 因子1 + 前7根Range * 因子2)
|
||||
bar_1_ago = bar_history[-self.lag_l]
|
||||
range_1_ago = bar_1_ago.high - bar_1_ago.low
|
||||
|
||||
self.log(open, range_1_ago * self.range_factor_l)
|
||||
target_buy_price = open - (range_1_ago * self.range_factor_l)
|
||||
|
||||
@@ -757,9 +780,9 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
target_buy_price = max(0.01, target_buy_price)
|
||||
|
||||
self.log(
|
||||
f"[{current_datetime}] 开多仓信号 - 当前Open={open:.2f}, "
|
||||
f"[{current_datetime}] LONG信号 - 当前Open={open:.2f}, "
|
||||
f"前1Range={range_1_ago:.2f}, "
|
||||
f"计算目标买入价={target_buy_price:.2f}"
|
||||
f"计算目标LONG价={target_buy_price:.2f}"
|
||||
)
|
||||
|
||||
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
|
||||
@@ -780,14 +803,11 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
if new_order:
|
||||
self._last_order_id = new_order.id
|
||||
self.log(
|
||||
f"[{current_datetime}] 策略: 发送限价买入订单 {self._last_order_id} @ {target_buy_price:.2f}"
|
||||
f"[{current_datetime}] 策略: 发送限价LONG订单 {self._last_order_id} @ {target_buy_price:.2f}"
|
||||
)
|
||||
else:
|
||||
self.log(f"[{current_datetime}] 策略: 发送订单失败。")
|
||||
|
||||
indicator_value = RSI(5).get_latest_value(
|
||||
np.array(self.get_price_history("close")), None, None, None, None
|
||||
)
|
||||
if not self.use_indicator or self.indicator_s.is_condition_met(
|
||||
np.array(self.get_price_history("close")),
|
||||
np.array(self.get_price_history("open")),
|
||||
@@ -797,6 +817,9 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
):
|
||||
# 根据策略逻辑计算目标买入价格
|
||||
# 目标买入价 = 当前K线Open - (前1根Range * 因子1 + 前7根Range * 因子2)
|
||||
bar_1_ago = bar_history[-self.lag_s]
|
||||
range_1_ago = bar_1_ago.high - bar_1_ago.low
|
||||
|
||||
self.log(open, range_1_ago * self.range_factor_s)
|
||||
target_buy_price = open + (range_1_ago * self.range_factor_s)
|
||||
|
||||
@@ -804,9 +827,9 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
target_buy_price = max(0.01, target_buy_price)
|
||||
|
||||
self.log(
|
||||
f"[{current_datetime}] 开多仓信号 - 当前Open={open:.2f}, "
|
||||
f"[{current_datetime}] SHORT信号 - 当前Open={open:.2f}, "
|
||||
f"前1Range={range_1_ago:.2f}, "
|
||||
f"计算目标买入价={target_buy_price:.2f}"
|
||||
f"计算目标SHORT价={target_buy_price:.2f}"
|
||||
)
|
||||
|
||||
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
|
||||
@@ -827,15 +850,13 @@ class SimpleLimitBuyStrategy(Strategy):
|
||||
if new_order:
|
||||
self._last_order_id = new_order.id
|
||||
self.log(
|
||||
f"[{current_datetime}] 策略: 发送限价买入订单 {self._last_order_id} @ {target_buy_price:.2f}"
|
||||
f"[{current_datetime}] 策略: 发送限价SHORT订单 {self._last_order_id} @ {target_buy_price:.2f}"
|
||||
)
|
||||
else:
|
||||
self.log(f"[{current_datetime}] 策略: 发送订单失败。")
|
||||
# else:
|
||||
# self.log(f"[{current_datetime}] 不满足开仓条件:持仓={current_pos_volume}, 待处理订单={len(pending_orders_after_cancel)}, K线历史长度={len(bar_history)}")
|
||||
|
||||
def on_close_bar(self, bar: Bar, next_bar_open: Optional[float] = None):
|
||||
self.cancel_all_pending_orders()
|
||||
self.cancel_all_pending_orders(self.main_symbol)
|
||||
|
||||
def on_rollover(self, old_symbol: str, new_symbol: str):
|
||||
"""
|
||||
|
||||
@@ -112,7 +112,8 @@ class Strategy(ABC):
|
||||
|
||||
return self.context.cancel_order(order_id)
|
||||
|
||||
def cancel_all_pending_orders(self) -> int:
|
||||
|
||||
def cancel_all_pending_orders(self, main_symbol = None) -> int:
|
||||
"""取消当前策略的未决订单,仅限于当前策略关注的Symbol。"""
|
||||
# 注意:在换月模式下,引擎会自动取消旧合约的挂单,这里是策略主动取消
|
||||
if not self.trading:
|
||||
@@ -123,9 +124,14 @@ class Strategy(ABC):
|
||||
# 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 main_symbol is not None:
|
||||
orders_to_cancel = [
|
||||
order.id for order in pending_orders.values() if main_symbol in order.symbol
|
||||
]
|
||||
else:
|
||||
orders_to_cancel = [
|
||||
order.id for order in pending_orders.values()
|
||||
]
|
||||
for order_id in orders_to_cancel:
|
||||
if self.cancel_order(order_id):
|
||||
cancelled_count += 1
|
||||
|
||||
Reference in New Issue
Block a user