Search code examples
pine-scriptpine-script-v5

Why does this Pine Script indicator work for some ticker symbols and not others?


I have a Pine Script indicator that displays correctly for the SP:SPX ticker but not the CBOE:SPX ticker. These are the same ticker but are coming from different feeds.

Correct indicator display

Incorrect indicator display

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © 2023, Vincent Dentice

//@version=5
indicator("Opening Range", "OpeningRange", overlay = true)

bool showOnlyCurrentDay = input.bool(true, "Show only current day")
color defaultColor = color.new(color.yellow, 30)
color lineColorInput = input.color(defaultColor, "Color")
string sessionInput = input.session("0930-1000", "Session")
string timeframeInput = input.timeframe("D", "Timeframe")

sessionBegins(sess) =>
    t = time("", sess)
    timeframe.isintraday and (not barstate.isfirst) and na(t[1]) and not na(t)

isInSession(sess) =>
    t = time("", sess)
    timeframe.isintraday and (not barstate.isfirst) and not na(t)

var float hi = na
var float mid = na
var float lo = na
var line hiLine = na
var line midLine = na
var line loLine = na

if sessionBegins(sessionInput)
    hi := high
    lo := low
    mid := (hi + lo) / 2.0

if isInSession(sessionInput)
    hi := math.max(hi, high)
    lo := math.min(lo, low)
    mid := (hi + lo) / 2.0

if ta.change(time(timeframeInput))
    if showOnlyCurrentDay
        if na(hiLine)
            hiLine := line.new(bar_index - 1, hi, bar_index, hi, color = lineColorInput, width = 2, extend = extend.right)
            int(na)
        else
            line.set_x1(hiLine, bar_index - 1)
            line.set_y1(hiLine, hi)
            line.set_x2(hiLine, bar_index)
            line.set_y2(hiLine, hi)
            int(na)

        if na(midLine)
            midLine := line.new(bar_index - 1, mid, bar_index, mid, color = lineColorInput, width = 1, extend = extend.right, style = line.style_dashed)
            int(na)
        else
            line.set_x1(midLine, bar_index - 1)
            line.set_y1(midLine, mid)
            line.set_x2(midLine, bar_index)
            line.set_y2(midLine, mid)
            int(na)

        if na(loLine)
            loLine := line.new(bar_index - 1, lo, bar_index, lo, color = lineColorInput, width = 2, extend = extend.right)
            int(na)
        else
            line.set_x1(loLine, bar_index - 1)
            line.set_y1(loLine, lo)
            line.set_x2(loLine, bar_index)
            line.set_y2(loLine, lo)
            int(na)
    else
        line.set_extend(hiLine, extend.none)
        line.set_extend(midLine, extend.none)
        line.set_extend(loLine, extend.none)
        hiLine := line.new(bar_index - 1, hi, bar_index, hi, color = lineColorInput, width = 2, extend = extend.right)
        midLine := line.new(bar_index - 1, mid, bar_index, mid, color = lineColorInput, width = 1, extend = extend.right, style = line.style_dashed)
        loLine := line.new(bar_index - 1, lo, bar_index, lo, color = lineColorInput, width = 2, extend = extend.right)
        int(na)
else
    line.set_y1(hiLine, hi)
    line.set_y1(midLine, mid)
    line.set_y1(loLine, lo)
    line.set_x2(hiLine, bar_index)
    line.set_x2(midLine, bar_index)
    line.set_x2(loLine, bar_index)
    line.set_y2(hiLine, hi)
    line.set_y2(midLine, mid)
    line.set_y2(loLine, lo)
    int(na)

xUp = close > hiLine.get_y1() and close[1] <= hiLine.get_y1()
xDn = close < loLine.get_y1() and close[1] >= loLine.get_y1()

alertcondition(xUp, "Above Opening Range", "Current price is above opening range.")
alertcondition(xDn, "Below Opening Range", "Current price is below opening range.")

I think the problem has something to do with the timeframe calls near the top of the script, but I can't figure it out.

Thanks.

I am expecting the indicator to display the same results for both data feeds.


Solution

  • SP is in UTC-4 timezone and CBOE is in UTC-5. That's why the results are different because the sessions are different.

    The time() function has a third argument called timezone.

    timezone (series string) Timezone of the session argument. Can only be used when a session is specified. Optional. The default is syminfo.timezone. Can be specified in GMT notation (e.g. "GMT-5") or as an IANA time zone database name (e.g. "America/New_York").

    You can pass GMT-4 or whatever timezone you want to use to this and then they will be in sync.