每个交易时段仅1笔交易脚本故障:每根K线触发警报求助
解决Pine Script交易时段内重复触发警报的问题
你的脚本核心问题有两个:
- 时区参数未生效:定义了
timeZone1但未在会话判断函数中使用,导致会话时间判断基于图表时区而非你指定的GMT+10,可能引发会话识别错误。 - 缺少K线确认逻辑:未限制仅在K线收盘后触发条件,盘中价格波动可能导致同一K线内多次触发(虽使用了
alert.freq_once_per_bar,但结合条件逻辑可进一步加固)。
以下是修正后的代码:
var bool traded_today = false // 交易会话设置 sessionInput1 = input.session("1800-2200", group = "Trading Session") timeZone1 = input.string("GMT+10", title="Time Zone", group = "Trading Session") // 修正会话判断函数,加入时区参数 InSession1(sess, tz) => na(time(timeframe.period, sess, tz)) == false IsInSession1 = InSession1(sessionInput1, timeZone1) is_new_session1 = not IsInSession1[1] and IsInSession1 // 重置交易标记:新会话开始时重置为false traded_today := is_new_session1 ? false : traded_today // 买入条件:加入barstate.isconfirmed确保仅在K线收盘后判断 Buy = barstate.isconfirmed and IsInSession1 and BuyCond1 and BuyCond2 and BuyCond3 and not traded_today if (Buy) traded_today := true alert("Buy Limit " + syminfo.ticker + " at " + str.tostring(sessionHighPrice, "0.000") + "\n" + "TP " + str.tostring(Buy_TP, "0.000") + "\n" + "SL " + str.tostring(Buy_SL, "0.000") + "\n" + "TimeStamp " + str.tostring(timenow + 10), alert.freq_once_per_bar)
关键修正点说明:
- 时区参数传递:修改
InSession1函数,加入时区参数并在调用时传入timeZone1,确保会话时间判断符合你指定的时区。 - K线确认:添加
barstate.isconfirmed,仅在K线完全收盘后判断买入条件,避免盘中价格波动导致的重复触发。 - 变量类型明确:将
traded_today声明为bool类型,提升代码可读性。
修改后,每个交易时段内只会在第一个满足买入条件的收盘K线触发一次警报,后续K线因traded_today标记为true而不会重复触发。
内容的提问来源于stack exchange,提问作者slipery
相关产品推荐
相关产品推荐

