Pine Script:当两个条件相继满足时绘制标签

Pine Script: Paint label when two conditions are met one after the other

我正在尝试在多个条上一个接一个满足两个条件后绘制标签。

首先,fourK-sma 穿过 fiveK-sma,然后下一次 oneK-sma 穿过 oneD-sma 时,它绘制了一个标签。

ATM 脚本保存良好,但没有绘制标签,我 运行 不知道如何处理这个问题。

oneK=sma(stoch(close, high, low, 11), 10)
oneD=sma(oneK, 4)
fourK=sma(stoch(close, high, low, 176), 160)
fiveK=sma(stoch(close, high, low, 352), 320)

test=bool(na)
fir=bool(na)
if (crossover(fourK,fiveK))
    test:=true
if (test==true) and crossover(oneK,oneD)
    fir:=true
    test:=false

plotshape(fir, style = shape.arrowup, location = location.belowbar, color=#ff4545, size=size.small)

问题是:

  • 您在声明变量时没有使用 var 关键字,所以它们是 在每个柱上初始化。
  • 一旦 fir 设置为 true,您就再也不会将其重置为 false。

这会起作用:

//@version=4
study("SO 64706821", overlay=true, shorttitle="SO")

oneK  = sma(stoch(close, high, low, 11), 10)
oneD  = sma(oneK, 4)
fourK = sma(stoch(close, high, low, 176), 160)
fiveK = sma(stoch(close, high, low, 352), 320)

var bool test = na // Initializion occurs once, on first bar
bool fir = false   // Initializion occurs on every bar

if crossover(fourK,fiveK)
    test := true
    
if test and crossover(oneK,oneD)
    fir  := true
    test := false

plotshape(fir, style = shape.arrowup, location = location.belowbar, color=color.red, size=size.small)

// Background color to make it more clear where the signals occur, because there are so few.
bgcolor(fir ? color.yellow : na, transp=80)