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

Pine Script v5 GMT+5:30时区15点后禁交易判断失效问题

Pine Script v5 尾盘交易限制逻辑失效问题

策略规则

在Pine Script v5中开发交易策略时,设定的交易规则如下:

  • 采用GMT+5:30时区,下午3点后不再执行任何交易
  • 仅当当前时间早于下午3点时,才允许执行入场/出场操作
  • 到达下午3点时,平掉所有未平仓头寸

原有实现代码

endOfDay = input.int(defval=1500, title="Close all trades, default is 3:00 PM, 1500 hours (integer)")

if (hour(timenow) < endOfDay)
    // Entry
    float sl = na
    if (sureBuyInTrend)
        strategy.entry("enter long", strategy.long, lotSize, limit=na, stop=na, comment="Long")
        sl = atrLow
    if (sureSellInTrend)
        strategy.entry("enter short", strategy.short, lotSize, limit=na, stop=na, comment="Short")
        sl = atrHigh

    // Exit: target or SL
    longExitComment = (close < sl) ? "Long SL hit" : sureSellInTrend ? "Long target hit" : "Long close"
    shortExitComment = (close > sl) ? "Short SL hit" : sureBuyInTrend ? "Short target hit" : "Short close"
    if ((sureSellInTrend or (close < sl)))
        strategy.close("enter long", comment=longExitComment)
    if (sureBuyInTrend or (close > sl))
        strategy.close("enter short", comment=shortExitComment)

lastBarOfDay = (hour(time_close)*60 + minute(time_close)==(60*(endOfDay/100)+endOfDay%100)) ? 1 : 0
if (lastBarOfDay)
    // Close all open position at the end
    strategy.close_all(comment = "Close all entries")

异常现象

代码中15点整平掉所有持仓的Close all entries逻辑运行正常,但hour(timenow) < endOfDay的时间判断未按预期生效,交易列表中可以看到15:15仍有订单发出。
交易记录异常截图

代码错误说明

  1. 数值比较逻辑完全错误:hour()函数返回值范围是0~23的整数(例如15点的返回值为15),但定义的endOfDay参数值为1500,直接执行hour(timenow) < endOfDay判断时,全天所有交易时段的小时返回值都远小于1500,判断条件永远成立,完全无法起到拦截15点后交易的作用,这是15点后依然有订单发出的核心原因。
  2. 时间源选择错误:timenow返回的是脚本运行时的实时当前时间,在回测、K线重放场景下不会跟随K线时间变化,会导致回测结果和实盘运行逻辑不一致。交易时间判断应该使用K线自身的时间属性,和尾盘平仓逻辑使用的time_close保持时间源统一。
  3. 时区未对齐:策略要求使用GMT+5:30时区,但hour()、minute()函数默认使用交易所时区,未传入指定时区参数时,时间计算会出现偏移。
  4. 时间计算逻辑不统一:尾盘平仓逻辑中已经将时间转换为分钟数做对比,但交易时段判断直接用小时值和1500比较,两套判断逻辑不一致,也是导致平仓正常但开仓限制失效的原因之一。

修正后参考代码

// 统一指定交易时区
tradeTimezone = "GMT+5:30"
endOfDay = input.int(defval=1500, title="收盘平仓时间(24小时制,如1500代表15:00)")
// 将平仓时间转换为当日分钟数,统一比较维度
endOfDayMinutes = 60 * math.floor(endOfDay / 100) + (endOfDay % 100)

// 计算当前K线时间对应的当日分钟数,指定时区
currentBarHour = hour(time, tradeTimezone)
currentBarMinute = minute(time, tradeTimezone)
currentBarMinutes = currentBarHour * 60 + currentBarMinute
allowTrade = currentBarMinutes < endOfDayMinutes

if (allowTrade)
    // 入场逻辑
    float sl = na
    if (sureBuyInTrend)
        strategy.entry("enter long", strategy.long, lotSize, limit=na, stop=na, comment="Long")
        sl = atrLow
    if (sureSellInTrend)
        strategy.entry("enter short", strategy.short, lotSize, limit=na, stop=na, comment="Short")
        sl = atrHigh

    // 止损止盈出场逻辑
    longExitComment = (close < sl) ? "Long SL hit" : sureSellInTrend ? "Long target hit" : "Long close"
    shortExitComment = (close > sl) ? "Short SL hit" : sureBuyInTrend ? "Short target hit" : "Short close"
    if (sureSellInTrend or (close < sl))
        strategy.close("enter long", comment=longExitComment)
    if (sureBuyInTrend or (close > sl))
        strategy.close("enter short", comment=shortExitComment)

// 尾盘平仓逻辑,统一时区、统一分钟数比较规则
closeBarHour = hour(time_close, tradeTimezone)
closeBarMinute = minute(time_close, tradeTimezone)
lastBarOfDay = (closeBarHour * 60 + closeBarMinute == endOfDayMinutes)
if (lastBarOfDay)
    strategy.close_all(comment = "Close all entries")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 00:06:25