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

PineScript价格上穿200 SMA后指定K线数开多逻辑不生效排查

代码问题分析
  • 问题1:你使用var关键字声明了emaLongCondition变量,该类变量仅在指标首次加载时初始化一次,一旦被赋值为true后没有设置任何重置逻辑,只要历史上出现过一次满足上穿后阈值的情况,后续所有K线的emaLongCondition都会保持true,就会无限制触发开多信号。
  • 问题2:barssince(emaCrossOver) >= TrendBarsCount的逻辑仅判断了上穿200SMA后经过的K线数量,没有校验当前K线价格是否仍运行在200SMA上方,哪怕价格之后跌破了200SMA,barssince的返回值还是会随K线推进持续增大,依然会满足判断条件。
修正后基础代码
SMA200  = sma(close, 200)
//PRICE OVER EMA CHECKS
TrendBarsCount = input(5, "Price closes needed above/below 200 MA", minval=1)
emaCrossOver = crossover(close, SMA200)
// 取消var声明,每根K线重新计算条件,同时添加当前价格在SMA上方的校验
emaLongCondition = barssince(emaCrossOver) >= TrendBarsCount and close > SMA200
strategy.entry("Open Long", strategy.long, when = emaLongCondition)
连续符合条件的逻辑优化

如果你需要严格判断上穿后连续N根K线都收在200SMA上方,没有出现过跌破的情况,可以改用如下更严谨的判断逻辑,只要中间出现一根收盘价低于200SMA的情况,计数就会自动重置:

SMA200  = sma(close, 200)
TrendBarsCount = input(5, "Price closes needed above/below 200 MA", minval=1)
// 统计连续收盘价高于200SMA的K线数量,低于时计数重置为0
consecutiveAbove = barssince(close < SMA200)
emaLongCondition = consecutiveAbove >= TrendBarsCount
strategy.entry("Open Long", strategy.long, when = emaLongCondition)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 16:00:01