Brendan Little
Brendan Little

Reputation: 75

EMA line won't stay fixed when scrolling - Pinescript

I'm having an issue where I'm trying to lock in place a EMA Line for the current EMA, how ever when I scroll vertical the value and line changes position, this does not have when scrolling horizontally.

//@version=6
indicator("Right-Side EMA", overlay=true)

// Define the EMA length
emaLength = 50

// Calculate the EMA value
emaValue = ta.ema(close, emaLength)

// Define how far to extend the EMA line to the right (in bar index units)
extensionLength = 20

// Declare a persistent variable for the line
var line emaLine = na

// Delete the previous line when a new one is created
if (not na(emaLine))
    line.delete(emaLine)

// Draw the line at the current EMA value, extending it to the right and keeping it fixed vertically at that value
emaLine := line.new(x1=bar_index, y1=emaValue, x2=bar_index + extensionLength, y2=emaValue, width=2, color=color.blue, xloc=xloc.bar_index, extend=extend.none)

Upvotes: 0

Views: 22

Answers (0)

Related Questions