You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script问题:如何在周一开盘进场并在当日收盘/周二开盘平仓?

Pine Script 策略修正:周一开盘进场、当日收盘/次日开盘平仓

原代码问题分析

  1. 重复/错误开仓:原代码在周一的每一根K线(如小时线、分钟线)都执行strategy.entry,导致同一交易日多次开仓;若时间周期切换时(比如周一收盘到周二开盘),可能因时间判断误差出现周二开仓的异常。
  2. 平仓逻辑失效:平仓条件嵌套在周一的判断块内,dayofweek(time) != dayofweek.monday永远无法触发,只有止损条件生效,导致未触发止损的仓位会持续持有,甚至到隔周才意外平仓。

修正方案(两种平仓逻辑可选)

方案1:周一开盘进场,当日收盘平仓

//@version=5
strategy("Monday Open Close Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=1000)

// 自定义止损幅度(可按需调整)
mondayStop = 50

// 进场:仅在周一的第一根K线(开盘价)开多
if dayofweek == dayofweek.monday and barstate.isopening
    strategy.entry("Monday Buy", strategy.long)

// 平仓:触发止损 或 当日收盘平仓
if strategy.position_size > 0
    // 止损平仓
    if close <= (strategy.opentrades.entry_price - mondayStop)
        strategy.close("Monday Buy")
    // 当日收盘平仓(自动适配不同时间周期的收盘K线)
    elif barstate.isclosing and dayofweek == dayofweek.monday
        strategy.close("Monday Buy")

方案2:周一开盘进场,次日(周二)开盘平仓

//@version=5
strategy("Monday Open - Tuesday Close Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=1000)

// 自定义止损幅度(可按需调整)
mondayStop = 50

// 进场:仅在周一的第一根K线(开盘价)开多
if dayofweek == dayofweek.monday and barstate.isopening
    strategy.entry("Monday Buy", strategy.long)

// 平仓:触发止损 或 周二开盘平仓
if strategy.position_size > 0
    // 止损平仓
    if close <= (strategy.opentrades.entry_price - mondayStop)
        strategy.close("Monday Buy")
    // 周二开盘平仓
    elif dayofweek == dayofweek.tuesday and barstate.isopening
        strategy.close("Monday Buy")

关键修正点说明

  • 精准进场控制:用barstate.isopening确保仅在交易日的第一根K线(对应开盘价)执行进场,避免同一交易日重复开仓。
  • 独立平仓逻辑:将平仓条件从进场判断块中分离,只要持有仓位就检查止损或平仓时间条件,逻辑更清晰可靠。
  • 时间判断优化:用barstate.isclosing自动定位当日收盘K线,或直接指定周二开盘作为平仓点,彻底避免时间判断混乱。

内容的提问来源于stack exchange,提问作者DavidT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 06:16:06