0

I have a series myLine, which I fill with value na

myLine = 1==1 ? na : na // Series with na

Now I want to create a function that updates the myLine series to have a value only on certain bars (intraday).

isDate(y,m,d) => y==year and m==month and d==dayofmonth ? true:false // Is the date of the current bar equal to the date provided by the parameters?

setMyData(y,m,d,lineValue) =>
    if timeframe.isintraday and isDate(y,m,d)
        myLine := lineValue

setMyData(2020,03,31,1234)
setMyData(2020,04,01,2345)

However, this doesn't seem to work, and I get this error

Cannot modify global variable 'myLine' in function.

I also tried using myLine[bar_index] := lineValue but that doesn't seem to work either.

Does anyone know how to update values of a series only for certain datapoints?
I'm trying to plot horizontal lines only on certain dates (intraday).

I specifically want to use a series (instead of a line object) because that allows me to change to color in the styles tab.

Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42

1 Answers1

0

Pine does not allow global variables to be modified from a function's local scope. These 2 ways should get the job done, with #2 being the most robust because it will not be constrained by compiler limits on the number of nested if blocks in the ternary:

//@version=4
study("")

// ————— #1
isDate(y,m,d) => y==year and m==month and d==dayofmonth // Is the date of the current bar equal to the date provided by the parameters?

float myLine1 = na
myLine1 := 
  isDate(2020,03,31) ? 1234 :
  isDate(2020,04,01) ? 2345 : na

plot(myLine1, "myLine1", color.silver, 10, plot.style_circles, transp = 50)


// ————— #2
initOnDate(y,m,d, prev, init) => 
    if y==year and m==month and d==dayofmonth
        init
    else
        prev

float myLine2 = na
myLine2 := initOnDate(2020,03,31,myLine2,1234)
myLine2 := initOnDate(2020,04,01,myLine2,2345)

plot(myLine2, "myLine2", color.orange, 3, plot.style_circles)
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21