Acta Non Verba
Acta Non Verba

Reputation: 1

Series string used but simple string expected Pinescript

Goal was to take the users session input, split it, add 1 candle time onto both ends of the session and put it back together. Thought I had it but when trying to pass the newly created session into the time function to check if I am in that session I get the "Series string was used but simple string is expected" I tried using str.tostring() but didn't change anything

Code below to test out, I put in a label so you can see the session time is being display the same way the input is

//@version=5
ses             = input.session("0800-1700", "Trade Session")

timeinrange(res, sess) =>
    time(res, sess) != 0

splitSession    = str.split(ses, "-")

firstTime       = array.get(splitSession,0)
seconTime       = array.get(splitSession,1)

firstInt        = str.tonumber(firstTime)/1000
seconInt        = str.tonumber(seconTime)/1000
t               = timeframe.multiplier / 1000

newSesStart     = firstInt + t
newSesEnd       = seconInt + t
newSesSString   = newSesStart < 1 ? "0" + str.tostring(newSesStart*1000) : str.tostring(newSesStart*1000)
newSesEString   = newSesEnd < 1 ? "0" + str.tostring(newSesEnd*1000) : str.tostring(newSesEnd*1000)
newSession      = newSesSString + "-" + newSesEString

l = label.new(bar_index,high, text=str.tostring(newSession))
label.delete(l[1])

inSession   = timeinrange(timeframe.period, newSession)

Thanks in advance!

Upvotes: 0

Views: 1479

Answers (1)

e2e4
e2e4

Reputation: 3828

As the error says, the time() function supports only simple/constant values. Note that working with arrays will always return the series form.

Thinking of a workaround, str.replace() function can modify a string and return a simple form, the result could be used later in the script, however, the values should be hardcoded. On a lower that 1h chart (30-minute for ex), where you have to modify only the minutes, it would look like this:

//@version=5
indicator("")

ses             = input.session("0800-1700", "Trade Session")

candleRange = str.tostring(timeframe.multiplier, "00")

step1 = str.replace(ses, "00", candleRange)
step2 = str.replace(step1, "00", candleRange)

l = label.new(bar_index,high, text= "original session is : " + ses + "\n Shifted Session is : " + step2)
label.delete(l[1])

bgcolor(time == time(timeframe.period, ses) ? color.new(color.red, 80) : na, title = "Original Session")
bgcolor(time == time(timeframe.period, step2) ? color.new(color.green, 80) : na, title = "Shifted Session")

enter image description here

Upvotes: 0

Related Questions