修复未来函数bug

This commit is contained in:
2025-07-15 22:45:51 +08:00
parent 5de1a43b02
commit 5a2d0fb984
19 changed files with 15019 additions and 9388 deletions

View File

@@ -2,6 +2,7 @@
from tkinter import N
import numpy as np
from src.indicators.base_indicators import Indicator
from src.indicators.indicators import RSI, HistoricalRange
from .base_strategy import Strategy
from ..core_data import Bar, Order
@@ -30,6 +31,8 @@ class SimpleLimitBuyStrategyLong(Strategy):
stop_loss_points: float = 10, # 新增:止损点数
take_profit_points: float = 10,
use_indicator: bool = False,
indicator: Indicator = None,
lag: int = 7,
): # 新增:止盈点数
"""
初始化策略。
@@ -51,6 +54,8 @@ class SimpleLimitBuyStrategyLong(Strategy):
self.stop_loss_points = stop_loss_points
self.take_profit_points = take_profit_points
self.use_indicator = use_indicator
self.indicator = indicator
self.lag = lag
self.order_id_counter = 0
@@ -67,18 +72,17 @@ class SimpleLimitBuyStrategyLong(Strategy):
def on_init(self):
super().on_init()
count = self.cancel_all_pending_orders()
self.log(f'取消{count}笔订单')
self.log(f"取消{count}笔订单")
def on_open_bar(self, bar: Bar, next_bar_open: Optional[float] = None):
def on_open_bar(self, open: float, symbol: str):
"""
每当新的K线数据到来时调用。
Args:
bar (Bar): 当前的K线数据对象。
next_bar_open (Optional[float]): 下一根K线的开盘价此处策略未使用。
"""
current_datetime = bar.datetime # 获取当前K线时间
self.symbol = bar.symbol
current_datetime = self.get_current_time() # 获取当前K线时间
self.symbol = symbol
# --- 1. 撤销上一根K线未成交的订单 ---
# 检查是否记录了上一笔订单ID并且该订单仍然在待处理列表中
@@ -112,18 +116,15 @@ class SimpleLimitBuyStrategyLong(Strategy):
) # 再次获取,此时应已取消旧订单
range_1_ago = None
range_7_ago = None
bar_history = self.get_bar_history()
if len(bar_history) > 16:
# 获取前1根K线 (倒数第二根) 和前7根K线 (队列中最老的一根)
bar_1_ago = bar_history[-8]
bar_7_ago = bar_history[-15]
bar_1_ago = bar_history[-self.lag]
# 计算历史 K 线的 Range
range_1_ago = bar_1_ago.high - bar_1_ago.low
range_7_ago = bar_7_ago.high - bar_7_ago.low
# for i in range(1, 9, 1):
# print(bar_history[-i].datetime)
@@ -135,18 +136,16 @@ class SimpleLimitBuyStrategyLong(Strategy):
): # 假设只做多,所以持仓量 > 0
avg_entry_price = self.get_average_position_price(self.symbol)
if avg_entry_price is not None:
pnl_per_unit = (
bar.open - avg_entry_price
) # 当前浮动盈亏(以收盘价计算)
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}, 目标: {range_1_ago * self.profit_factor:.2f}"
)
# 止盈条件
if pnl_per_unit >= range_1_ago * self.profit_factor:
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -157,7 +156,7 @@ class SimpleLimitBuyStrategyLong(Strategy):
volume=trade_volume,
price_type="MARKET",
# limit_price=limit_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
offset="CLOSE",
)
trade = self.send_order(order)
@@ -169,7 +168,7 @@ class SimpleLimitBuyStrategyLong(Strategy):
f"[{current_datetime}] 止损信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {-self.stop_loss_points:.2f}"
)
# 发送市价卖出订单平仓,确保立即成交
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -180,7 +179,7 @@ class SimpleLimitBuyStrategyLong(Strategy):
volume=trade_volume,
price_type="MARKET",
# limit_price=limit_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
offset="CLOSE",
)
trade = self.send_order(order)
@@ -190,36 +189,37 @@ class SimpleLimitBuyStrategyLong(Strategy):
# 只有在没有持仓 (current_pos_volume == 0) 且没有待处理订单 (not pending_orders_after_cancel)
# 且K线历史足够长时才考虑开仓
# rsi = RSI(5).get_latest_value(np.array(self.get_price_history('close')), None, None, None, None)
indicator_value = HistoricalRange(21).get_latest_value(
None,
None,
np.array(self.get_price_history("high")),
np.array(self.get_price_history("low")),
None,
)
if (
current_pos_volume == 0
and range_1_ago is not None
and (not self.use_indicator or 10 < indicator_value < 25)
and (
not self.use_indicator
or self.indicator.is_condition_met(
np.array(self.get_price_history("close")),
np.array(self.get_price_history("open")),
np.array(self.get_price_history("high")),
np.array(self.get_price_history("low")),
np.array(self.get_price_history("volume")),
)
)
):
# if current_pos_volume == 0 and range_1_ago is not None:
# 根据策略逻辑计算目标买入价格
# 目标买入价 = 当前K线Open - (前1根Range * 因子1 + 前7根Range * 因子2)
self.log(bar.open, range_1_ago * self.range_factor)
target_buy_price = bar.open - (range_1_ago * self.range_factor)
self.log(open, range_1_ago, self.range_factor)
target_buy_price = open - (range_1_ago * self.range_factor)
# 确保目标买入价格有效,例如不能是负数
target_buy_price = max(0.01, target_buy_price)
self.log(
f"[{current_datetime}] 开多仓信号 - 当前Open={bar.open:.2f}, "
f"[{current_datetime}] 开多仓信号 - 当前Open={open:.2f}, "
f"前1Range={range_1_ago:.2f}, "
f"计算目标买入价={target_buy_price:.2f}"
)
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -230,7 +230,7 @@ class SimpleLimitBuyStrategyLong(Strategy):
volume=trade_volume,
price_type="LIMIT",
limit_price=target_buy_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
)
new_order = self.send_order(order)
# 记录下这个订单的ID以便在下一根K线开始时进行撤销
@@ -279,6 +279,8 @@ class SimpleLimitBuyStrategyShort(Strategy):
stop_loss_points: float = 10, # 新增:止损点数
take_profit_points: float = 10,
use_indicator: bool = False,
indicator: Indicator = None,
lag: int = 7,
): # 新增:止盈点数
"""
初始化策略。
@@ -300,6 +302,8 @@ class SimpleLimitBuyStrategyShort(Strategy):
self.stop_loss_points = stop_loss_points
self.take_profit_points = take_profit_points
self.use_indicator = use_indicator
self.indicator = indicator
self.lag = lag
self.order_id_counter = 0
@@ -313,15 +317,20 @@ class SimpleLimitBuyStrategyShort(Strategy):
f"止损点={self.stop_loss_points}, 止盈点={self.take_profit_points}"
)
def on_open_bar(self, bar: Bar, next_bar_open: Optional[float] = None):
def on_init(self):
super().on_init()
count = self.cancel_all_pending_orders()
self.log(f"取消{count}笔订单")
def on_open_bar(self, open: float, symbol: str):
"""
每当新的K线数据到来时调用。
Args:
bar (Bar): 当前的K线数据对象。
next_bar_open (Optional[float]): 下一根K线的开盘价此处策略未使用。
"""
current_datetime = bar.datetime # 获取当前K线时间
self.symbol = bar.symbol
current_datetime = self.get_current_time() # 获取当前K线时间
self.symbol = symbol
# --- 1. 撤销上一根K线未成交的订单 ---
# 检查是否记录了上一笔订单ID并且该订单仍然在待处理列表中
@@ -355,18 +364,15 @@ class SimpleLimitBuyStrategyShort(Strategy):
) # 再次获取,此时应已取消旧订单
range_1_ago = None
range_7_ago = None
bar_history = self.get_bar_history()
if len(bar_history) > 16:
# 获取前1根K线 (倒数第二根) 和前7根K线 (队列中最老的一根)
bar_1_ago = bar_history[-8]
bar_7_ago = bar_history[-15]
bar_1_ago = bar_history[-self.lag]
# 计算历史 K 线的 Range
range_1_ago = bar_1_ago.high - bar_1_ago.low
range_7_ago = bar_7_ago.high - bar_7_ago.low
# --- 3. 平仓逻辑 (止损/止盈) ---
# 只有当有持仓时才考虑平仓
@@ -375,9 +381,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
): # 假设只做多,所以持仓量 > 0
avg_entry_price = self.get_average_position_price(self.symbol)
if avg_entry_price is not None:
pnl_per_unit = (
avg_entry_price - bar.open
) # 当前浮动盈亏(以收盘价计算)
pnl_per_unit = avg_entry_price - open # 当前浮动盈亏(以收盘价计算)
self.log(
f"[{current_datetime}] 止盈信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {self.take_profit_points:.2f}"
@@ -386,7 +390,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
# 止盈条件
if pnl_per_unit >= range_1_ago * self.profit_factor:
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -397,7 +401,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
volume=trade_volume,
price_type="MARKET",
# limit_price=limit_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
offset="CLOSE",
)
trade = self.send_order(order)
@@ -409,7 +413,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
f"[{current_datetime}] 止损信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {-self.stop_loss_points:.2f}"
)
# 发送市价卖出订单平仓,确保立即成交
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -420,7 +424,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
volume=trade_volume,
price_type="MARKET",
# limit_price=limit_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
offset="CLOSE",
)
trade = self.send_order(order)
@@ -430,28 +434,35 @@ class SimpleLimitBuyStrategyShort(Strategy):
# 只有在没有持仓 (current_pos_volume == 0) 且没有待处理订单 (not pending_orders_after_cancel)
# 且K线历史足够长时才考虑开仓
# rsi = RSI(5).get_latest_value(np.array(self.get_price_history('close')), None, None, None, None)
indicator_value = RSI(5).get_latest_value(np.array(self.get_price_history('close')), None, None, None, None)
if (
current_pos_volume == 0
and range_1_ago is not None
and (not self.use_indicator or 20 < indicator_value < 60)
and (
not self.use_indicator
or self.indicator.is_condition_met(
np.array(self.get_price_history("close")),
np.array(self.get_price_history("open")),
np.array(self.get_price_history("high")),
np.array(self.get_price_history("low")),
np.array(self.get_price_history("volume")),
)
)
):
# 根据策略逻辑计算目标买入价格
# 目标买入价 = 当前K线Open - (前1根Range * 因子1 + 前7根Range * 因子2)
self.log(bar.open, range_1_ago * self.range_factor)
target_buy_price = bar.open + (range_1_ago * self.range_factor)
target_buy_price = open + (range_1_ago * self.range_factor)
# 确保目标买入价格有效,例如不能是负数
target_buy_price = max(0.01, target_buy_price)
self.log(
f"[{current_datetime}] 开多仓信号 - 当前Open={bar.open:.2f}, "
f"[{current_datetime}] 开多仓信号 - 当前Open={open:.2f}, "
f"前1Range={range_1_ago:.2f}, "
f"计算目标买入价={target_buy_price:.2f}"
)
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_SELL_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -462,7 +473,7 @@ class SimpleLimitBuyStrategyShort(Strategy):
volume=trade_volume,
price_type="LIMIT",
limit_price=target_buy_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
)
new_order = self.send_order(order)
# 记录下这个订单的ID以便在下一根K线开始时进行撤销
@@ -513,6 +524,9 @@ class SimpleLimitBuyStrategy(Strategy):
stop_loss_points: float = 10, # 新增:止损点数
take_profit_points: float = 10,
use_indicator: bool = False,
indicator_l: Indicator = None,
indicator_s: Indicator = None,
lag: int = 7,
): # 新增:止盈点数
"""
初始化策略。
@@ -536,6 +550,9 @@ class SimpleLimitBuyStrategy(Strategy):
self.stop_loss_points = stop_loss_points
self.take_profit_points = take_profit_points
self.use_indicator = use_indicator
self.indicator_l = indicator_l
self.indicator_s = indicator_s
self.lag = lag
self.order_id_counter = 0
@@ -551,15 +568,20 @@ class SimpleLimitBuyStrategy(Strategy):
f"止损点={self.stop_loss_points}, 止盈点={self.take_profit_points}"
)
def on_open_bar(self, bar: Bar, next_bar_open: Optional[float] = None):
def on_init(self):
super().on_init()
count = self.cancel_all_pending_orders()
self.log(f"取消{count}笔订单")
def on_open_bar(self, open, symbol):
"""
每当新的K线数据到来时调用。
Args:
bar (Bar): 当前的K线数据对象。
next_bar_open (Optional[float]): 下一根K线的开盘价此处策略未使用。
"""
current_datetime = bar.datetime # 获取当前K线时间
self.symbol = bar.symbol
self.symbol = symbol
current_datetime = self.get_current_time()
# --- 1. 撤销上一根K线未成交的订单 ---
# 检查是否记录了上一笔订单ID并且该订单仍然在待处理列表中
@@ -593,18 +615,15 @@ class SimpleLimitBuyStrategy(Strategy):
) # 再次获取,此时应已取消旧订单
range_1_ago = None
range_7_ago = None
bar_history = self.get_bar_history()
if len(bar_history) > 16:
# 获取前1根K线 (倒数第二根) 和前7根K线 (队列中最老的一根)
bar_1_ago = bar_history[-8]
bar_7_ago = bar_history[-15]
bar_1_ago = bar_history[-self.lag]
# 计算历史 K 线的 Range
range_1_ago = bar_1_ago.high - bar_1_ago.low
range_7_ago = bar_7_ago.high - bar_7_ago.low
# --- 3. 平仓逻辑 (止损/止盈) ---
# 只有当有持仓时才考虑平仓
@@ -613,9 +632,7 @@ class SimpleLimitBuyStrategy(Strategy):
): # 假设只做多,所以持仓量 > 0
avg_entry_price = self.get_average_position_price(self.symbol)
if avg_entry_price is not None:
pnl_per_unit = (
avg_entry_price - bar.open
) # 当前浮动盈亏(以收盘价计算)
pnl_per_unit = avg_entry_price - open # 当前浮动盈亏(以收盘价计算)
self.log(
f"[{current_datetime}] 止盈信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {self.take_profit_points:.2f}"
@@ -624,7 +641,7 @@ class SimpleLimitBuyStrategy(Strategy):
# 止盈条件
if pnl_per_unit >= range_1_ago * self.profit_factor_s:
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -635,7 +652,7 @@ class SimpleLimitBuyStrategy(Strategy):
volume=trade_volume,
price_type="MARKET",
# limit_price=limit_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
offset="CLOSE",
)
trade = self.send_order(order)
@@ -647,7 +664,7 @@ class SimpleLimitBuyStrategy(Strategy):
f"[{current_datetime}] 止损信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {-self.stop_loss_points:.2f}"
)
# 发送市价卖出订单平仓,确保立即成交
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -658,7 +675,7 @@ class SimpleLimitBuyStrategy(Strategy):
volume=trade_volume,
price_type="MARKET",
# limit_price=limit_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
offset="CLOSE",
)
trade = self.send_order(order)
@@ -669,9 +686,7 @@ class SimpleLimitBuyStrategy(Strategy):
): # 假设只做多,所以持仓量 > 0
avg_entry_price = self.get_average_position_price(self.symbol)
if avg_entry_price is not None:
pnl_per_unit = (
bar.open - avg_entry_price
) # 当前浮动盈亏(以收盘价计算)
pnl_per_unit = open - avg_entry_price # 当前浮动盈亏(以收盘价计算)
self.log(
f"[{current_datetime}] 止盈信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {self.take_profit_points:.2f}"
@@ -680,7 +695,7 @@ class SimpleLimitBuyStrategy(Strategy):
# 止盈条件
if pnl_per_unit >= range_1_ago * self.profit_factor_l:
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -691,7 +706,7 @@ class SimpleLimitBuyStrategy(Strategy):
volume=trade_volume,
price_type="MARKET",
# limit_price=limit_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
offset="CLOSE",
)
trade = self.send_order(order)
@@ -703,7 +718,7 @@ class SimpleLimitBuyStrategy(Strategy):
f"[{current_datetime}] 止损信号 - PnL per unit: {pnl_per_unit:.2f}, 目标: {-self.stop_loss_points:.2f}"
)
# 发送市价卖出订单平仓,确保立即成交
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -714,7 +729,7 @@ class SimpleLimitBuyStrategy(Strategy):
volume=trade_volume,
price_type="MARKET",
# limit_price=limit_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
offset="CLOSE",
)
trade = self.send_order(order)
@@ -723,35 +738,31 @@ class SimpleLimitBuyStrategy(Strategy):
# --- 4. 开仓逻辑 (只考虑做多 BUY 方向) ---
# 只有在没有持仓 (current_pos_volume == 0) 且没有待处理订单 (not pending_orders_after_cancel)
# 且K线历史足够长时才考虑开仓
if (
current_pos_volume == 0
and range_1_ago is not None
):
indicator_value = HistoricalRange(21).get_latest_value(
None,
None,
np.array(self.get_price_history("high")),
np.array(self.get_price_history("low")),
None,
)
if (not self.use_indicator or 10 < indicator_value < 25):
if current_pos_volume == 0 and range_1_ago is not None:
if not self.use_indicator or self.indicator_l.is_condition_met(
np.array(self.get_price_history("close")),
np.array(self.get_price_history("open")),
np.array(self.get_price_history("high")),
np.array(self.get_price_history("low")),
np.array(self.get_price_history("volume")),
):
# 根据策略逻辑计算目标买入价格
# 目标买入价 = 当前K线Open - (前1根Range * 因子1 + 前7根Range * 因子2)
self.log(bar.open, range_1_ago * self.range_factor_l)
target_buy_price = bar.open - (range_1_ago * self.range_factor_l)
self.log(open, range_1_ago * self.range_factor_l)
target_buy_price = open - (range_1_ago * self.range_factor_l)
# 确保目标买入价格有效,例如不能是负数
target_buy_price = max(0.01, target_buy_price)
self.log(
f"[{current_datetime}] 开多仓信号 - 当前Open={bar.open:.2f}, "
f"[{current_datetime}] 开多仓信号 - 当前Open={open:.2f}, "
f"前1Range={range_1_ago:.2f}, "
f"计算目标买入价={target_buy_price:.2f}"
)
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -762,7 +773,7 @@ class SimpleLimitBuyStrategy(Strategy):
volume=trade_volume,
price_type="LIMIT",
limit_price=target_buy_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
)
new_order = self.send_order(order)
# 记录下这个订单的ID以便在下一根K线开始时进行撤销
@@ -774,24 +785,31 @@ class SimpleLimitBuyStrategy(Strategy):
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 20 < indicator_value < 60):
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")),
np.array(self.get_price_history("high")),
np.array(self.get_price_history("low")),
np.array(self.get_price_history("volume")),
):
# 根据策略逻辑计算目标买入价格
# 目标买入价 = 当前K线Open - (前1根Range * 因子1 + 前7根Range * 因子2)
self.log(bar.open, range_1_ago * self.range_factor_s)
target_buy_price = bar.open + (range_1_ago * self.range_factor_s)
self.log(open, range_1_ago * self.range_factor_s)
target_buy_price = open + (range_1_ago * self.range_factor_s)
# 确保目标买入价格有效,例如不能是负数
target_buy_price = max(0.01, target_buy_price)
self.log(
f"[{current_datetime}] 开多仓信号 - 当前Open={bar.open:.2f}, "
f"[{current_datetime}] 开多仓信号 - 当前Open={open:.2f}, "
f"前1Range={range_1_ago:.2f}, "
f"计算目标买入价={target_buy_price:.2f}"
)
order_id = f"{self.symbol}_BUY_{bar.datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
order_id = f"{self.symbol}_BUY_{current_datetime.strftime('%Y%m%d%H%M%S')}_{self.order_id_counter}"
self.order_id_counter += 1
# 创建一个限价多单
@@ -802,7 +820,7 @@ class SimpleLimitBuyStrategy(Strategy):
volume=trade_volume,
price_type="LIMIT",
limit_price=target_buy_price,
submitted_time=bar.datetime,
submitted_time=current_datetime,
)
new_order = self.send_order(order)
# 记录下这个订单的ID以便在下一根K线开始时进行撤销

View File

@@ -54,7 +54,7 @@ class Strategy(ABC):
pass # 默认不执行任何操作,具体策略可覆盖
@abstractmethod
def on_open_bar(self, bar: "Bar"):
def on_open_bar(self, open: float, symbol: str):
"""
每当新的K线数据到来时调用此方法。
Args: