Opening Price Change
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © marceloelgringo

//@version=5
indicator("Opening Price Change", overlay=true)


closePreviousDay = request.security(syminfo.tickerid, "D", close[1], barmerge.gaps_off, barmerge.lookahead_on)
openCurrentDay = request.security(syminfo.tickerid, "D", open, barmerge.gaps_off, barmerge.lookahead_on)


calculateChange(previousDay, currentDay) =>
    isPositive = currentDay > previousDay
    if isPositive
        increase = currentDay - previousDay
        increase / previousDay * 100
    else
        decrease = previousDay - currentDay
        decrease / previousDay * 100


if session.isfirstbar_regular
    isOpenPositive = openCurrentDay > closePreviousDay
    isChangeZero = openCurrentDay == closePreviousDay
    openMarketChangeLabel = label.new(bar_index, na)
    label.set_color(openMarketChangeLabel, isChangeZero ? color.silver : (isOpenPositive ? color.green : color.red))
    label.set_text(openMarketChangeLabel, (isChangeZero ? "" : isOpenPositive ? "+" : "-") + str.tostring(calculateChange(closePreviousDay, openCurrentDay), format.percent))
    label.set_yloc(openMarketChangeLabel, yloc.abovebar)
