saltyeggs
saltyeggs

Reputation: 93

Pinescript: How to plot simple text instead of lines?

I have not been able to find the method or sample code that will do this. Could someone please tell me how it is done? text labels instead of lines

Upvotes: 1

Views: 10804

Answers (2)

user3200254
user3200254

Reputation: 31

Have to create the labels and lines, then update them on the last bar (barstate.islast).

Adjust the "bar_index + X" number to move the lines to your liking. enter image description here

ema1Len = 10
ema1 = ta.ema(close, ema1Len)
ema1Color = color.red

ema2Len = 21
ema2 = ta.ema(close, ema2Len)
ema2Color = color.yellow

ema3Len = 50
ema3 = ta.ema(close, ema3Len)
ema3Color = color.green

ema4Len = 89
ema4 = ta.ema(close, ema4Len)
ema4Color = color.blue


var line ema1Line = line.new(bar_index, high, bar_index + 3, high, xloc.bar_index, extend.none, na, line.style_solid, 1) 
var line ema2Line = line.copy(ema1Line)
var line ema3Line = line.copy(ema1Line)
var line ema4Line = line.copy(ema1Line)

var label ema1Label = label.new(bar_index, high, "Test", style=label.style_none)
var label ema2Label = label.copy(ema1Label)
var label ema3Label = label.copy(ema1Label)
var label ema4Label = label.copy(ema1Label)

EmaDisplay(float ema, int len, string maType, color _color, line _line, label _label) =>
    line.set_xy1(_line, bar_index, ema)
    line.set_xy2(_line, bar_index + 4, ema)
    line.set_color(_line, _color)
    label.set_text(_label, str.format("{0} {1} {2}", str.tostring(len), maType, str.tostring(ema, "0.00")))
    label.set_xy(_label, bar_index + 10, ema)
    label.set_textcolor(_label, _color)


if(barstate.islast)
    EmaDisplay(ema1, ema1Len, "EMA", ema1Color, ema1Line, ema1Label)
    EmaDisplay(ema2, ema2Len, "EMA", ema2Color, ema2Line, ema2Label)
    EmaDisplay(ema3, ema3Len, "EMA", ema3Color, ema3Line, ema3Label)
    EmaDisplay(ema4, ema4Len, "EMA", ema4Color, ema4Line, ema4Label)

Upvotes: 0

Mahome
Mahome

Reputation: 132

Use label.new, fill out the text=section with string, and for style=label.style_none

Basically, it would be like this.

label= label.new( bar_index, high, 
                  text="XYXYXYXYXYX",  
                  color=color.white, 
                  textcolor= color.green,
                  style =  label.style_none,
                  yloc = yloc.abovebar)

If you have a variable and you would like to convert it to a string, use text = tostring(x).

Upvotes: 6

Related Questions