171 浏览
0

# 修改:获取小时线数据,并指定足够的数据长度
klines = api.get_kline_serial(SYMBOL, duration_seconds=60 * 60, data_length=data_length)
target_pos = TargetPosTask(api, SYMBOL)
 # 初始化交易状态变量
current_direction = 0  # 0: 无持仓, 1: 多头, -1: 空头
entry_price = 0  # 开仓价格
stop_loss_price = 0  # 止损价格
take_profit_price = 0  # 止盈价格
 try:
    while True:
        api.wait_update()
         if api.is_changing(klines.iloc[-1], "datetime"):  # 产生新k线:重新计算指标
            # 确保有足够的数据计算指标
            required_length = max(SHORT, LONG, ATR_PERIOD) + 10
            if len(klines) < required_length:
                print(f"数据不足,当前K线数量: {len(klines)},需要: {required_length},等待更多数据...")
                continue
             # ===== 技术指标计算 =====
            # 计算短期移动平均线(SHORT期)
            klines["short_ma"] = MA(klines, SHORT).ma
             # 计算长期移动平均线(LONG期)
            klines["long_ma"] = MA(klines, LONG).ma
             # 计算ATR指标(用于止损止盈)
            atr_data = ATR(klines, ATR_PERIOD)
            klines["atr"] = atr_data.atr
             # ===== 获取当前市场数据 =====
            current_price = float(klines.close.iloc[-1])  # 当前价格
            current_short_ma = float(klines.short_ma.iloc[-1])  # 当前短期均线
            prev_short_ma = float(klines.short_ma.iloc[-2])  # 前一根K线的短期均线
            current_long_ma = float(klines.long_ma.iloc[-1])  # 当前长期均线
            prev_long_ma = float(klines.long_ma.iloc[-2])  # 前一根K线的长期均线
            current_atr = float(klines.atr.iloc[-1])  # 当前ATR值
              # 获取当前时间
            current_datetime = klines.datetime.iloc[-1]
             # 输出调试信息 - 显示小时级别数据
            current_time_str = pd.to_datetime(current_datetime, unit='ns').strftime('%Y-%m-%d %H:%M')
            print(f"时间: {current_time_str}, "
                  f"价格: {current_price:.2f}, "
                  f"短均线: {current_short_ma:.2f}, "
                  f"长均线: {current_long_ma:.2f}, "
                  f"ATR: {current_atr:.2f}")
             # ===== 交易逻辑 =====
            # 情况1:当前无持仓
            if current_direction == 0:
                # 多头开仓条件:短期均线上穿长期均线(金叉)
                if prev_short_ma < prev_long_ma and current_short_ma > current_long_ma:
                    # 设置持仓方向和目标持仓
                    current_direction = 1
                    target_pos.set_target_volume(POSITION_SIZE)
                    # 记录开仓价格
                    entry_price = current_price
                    # 设置多头止损止盈:止损=开仓价-2*ATR,止盈=开仓价+4*ATR
                    stop_loss_price = entry_price - 2 * current_atr
                    take_profit_price = entry_price + 4 * current_atr
                    print(f"开多仓!时间: {current_time_str}, 价格: {entry_price:.2f}, "
                          f"止损: {stop_loss_price:.2f}, 止盈: {take_profit_price:.2f}")
                 # 空头开仓条件:短期均线下穿长期均线(死叉)
                elif prev_short_ma > prev_long_ma and current_short_ma < current_long_ma:
                    # 设置持仓方向和目标持仓
                    current_direction = -1
                    target_pos.set_target_volume(-POSITION_SIZE)
                    # 记录开仓价格
                    entry_price = current_price
                    # 设置空头止损止盈:止损=开仓价+2*ATR,止盈=开仓价-4*ATR
                    stop_loss_price = entry_price + 2 * current_atr
                    take_profit_price = entry_price - 4 * current_atr
                    print(f"开空仓!时间: {current_time_str}, 价格: {entry_price:.2f}, "
                          f"止损: {stop_loss_price:.2f}, 止盈: {take_profit_price:.2f}")


[/apcode]最简单的双均线代码,在小时线里回测,
时间: 2025-08-05 01:00, 价格: 3436.00, 短均线: 3509.00, 长均线: 3486.42, ATR: 18.36
时间: 2025-08-05 02:00, 价格: 3435.00, 短均线: 3506.37, 长均线: 3485.98, ATR: 18.29
时间: 2025-08-05 03:00, 价格: 3434.00, 短均线: 3503.27, 长均线: 3485.88, ATR: 18.29
时间: 2025-08-05 05:00, 价格: 3435.00, 短均线: 3500.33, 长均线: 3485.90, ATR: 18.21
时间: 2025-08-05 06:00, 价格: 3434.00, 短均线: 3497.40, 长均线: 3485.72, ATR: 18.21
时间: 2025-08-05 13:00, 价格: 3420.00, 短均线: 3493.90, 长均线: 3485.20, ATR: 16.36
时间: 2025-08-05 14:00, 价格: 3428.00, 短均线: 3490.93, 长均线: 3485.07, ATR: 14.93
时间: 2025-08-06 01:00, 价格: 3425.00, 短均线: 3487.13, 长均线: 3484.68, ATR: 12.57
时间: 2025-08-06 02:00, 价格: 3432.00, 短均线: 3483.57, 长均线: 3484.35, ATR: 12.43
开空仓!时间: 2025-08-06 02:00, 价格: 3432.00, 止损: 3456.86, 止盈: 3382.29
INFO – 模拟交易下单 TQSIM, PYSDK_target_4407609faef0477d9b4217da5fc8c3c8: 时间: 2025-08-06 10:00:00.000000, 合约: SHFE.bu2512, 开平: OPEN, 方向: SELL, 手数: 10, 价格: 3431.0
INFO – 模拟交易委托单 TQSIM, PYSDK_target_4407609faef0477d9b4217da5fc8c3c8: 全部成交
时间: 2025-08-06 03:00, 价格: 3437.00, 短均线: 3480.60, 长均线: 3484.12, ATR: 12.86

调试时间显示为时间: 2025-08-06 02:00,这个时间段不在交易时间内,而开空仓输出时间为:2025-08-06 10:00:00

请教一下,这个问题是什么原因,要如何解决?

chaos 已回答的问题 2025年12月1日
0

可能是代码中的时区转换不对,推荐用time_to_str来转换时间戳

chaos 发表新评论 2025年12月1日

有其他问题欢迎加入官方Q群748265037一起交流