0
votes

The script below always gives back the color red for my label colors. What am I doing wrong here? Any feedback appreciated (pretty new to pine).

study("Info Labels", overlay = true)

// Variables and Conditions
ema8 = ema(close, 8)
ema13 = ema(close, 13)
ema21 = ema(close, 21)
ema34 = ema(close, 34)
ema34h = ema(high, 34)

sc1 = ema8 >= ema13
sc2 = ema13 > ema21
sc3 = ema21 > ema34
sc4 = close > ema34h

colorlabel1 = sc1 ? color.green : color.red
colorlabel2 = sc4 ? color.green : color.red
var label1 = label.new(bar_index, high, text = "8:13:21", style = label.style_label_lower_right, color = 
colorlabel1, size = size.small)


var label2 = label.new(bar_index, low, text = "Wave   ", style = label.style_label_lower_left, color = colorlabel2, size = size.small)


label.set_xy(label1, bar_index[1], high[1] + atr(21))
label.set_xy(label2, bar_index[1], high[1] + atr(21))
1
Your question title is too unspecific. - Drewdavid

1 Answers

0
votes

You declared your labels with the var keyword.
That means that those variables are only initialized once, on the first bar.
On that first bar, both sc1 and sc4 conditions are false, so the label is created with color red and never changes again.

The conditions colorlabel1 and colorlabel2 change correctly.
You just have to update the color of the label to reflect that.
Just like you're setting the position of the label with label.set_xy(), you can also set the color with label.set_color().

You can read more about how var works in the manual, under Expressions, declarations and statements

This will work as you intended.

//@version=4
study("Info Labels", overlay = true)

// Variables and Conditions
var label1 = label.new(bar_index, high, text = "8:13:21", style = label.style_label_lower_right, size = size.small)
var label2 = label.new(bar_index, low,  text = "Wave   ", style = label.style_label_lower_left,  size = size.small)

ema8 = ema(close, 8)
ema13 = ema(close, 13)
ema21 = ema(close, 21)
ema34 = ema(close, 34)
ema34h = ema(high, 34)

sc1 = ema8 >= ema13
sc2 = ema13 > ema21
sc3 = ema21 > ema34
sc4 = close > ema34h

colorlabel1 = sc1 ? color.green : color.red
colorlabel2 = sc4 ? color.green : color.red

label.set_xy(label1, bar_index[1], high[1] + atr(21))
label.set_xy(label2, bar_index[1], high[1] + atr(21))

label.set_color(label1, colorlabel1)
label.set_color(label2, colorlabel2)