简体   繁体   中英

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

I'm trying to paint a label once two condition are met one after the other on multiple bars.

First the fourK-sma crosses the fiveK-sma and then the very next time the oneK-sma crosses the oneD-sma it paints a label.

ATM the script is saving fine but isn't painting the label and I've run out of ideas as to how to approach this.

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)

Problems were:

  • You declare your variables without the var keyword, so they're initialized on every bar.
  • Once fir is set to true, you never reset it to false again.

This will work:

//@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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM