Open Interest RSI
// This code is licensed under the Mozilla Public License 2.0
// © Demech

//@version=5
indicator(title = "Open Interest RSI", shorttitle = "OI RSI", format = format.volume, overlay = false)

// These two lines create user inputs that allow the user to override the default symbol
bool overwriteSymbolInput = input.bool(false, "Override symbol", inline = "Override symbol")
string tickerInput = input.symbol("", "", inline = "Override symbol")

// This checks if the user has chosen to override the default symbol. If they have, it uses the inputted symbol. If not, it uses the default.
string symbolOnly = syminfo.ticker(tickerInput)
string userSymbol = overwriteSymbolInput ? symbolOnly : syminfo.prefix + ":" + syminfo.ticker

// This creates the symbol for the open interest data and determines the timeframe for the data
string openInterestTicker = str.format("{0}_OI", userSymbol)
string timeframe = syminfo.type == "futures" and timeframe.isintraday ? "1D" : timeframe.period

// This line requests the open, high, low, and close of the open interest data
[oiOpen, oiHigh, oiLow, oiClose, oiColorCond] = request.security(openInterestTicker, timeframe, [open, high, low, close, close > close[1]], ignore_invalid_symbol = true)

// This checks if there's any missing open interest data, and if so, returns an error message
if barstate.islastconfirmedhistory and na(oiClose)
    runtime.error(str.format("No Open Interest data found for the `{0}` symbol.", userSymbol))

// These lines set the periods for the RSI and VWMA, then calculate the RSI and VWMA of the open interest data
oirsiPeriod = input(14, title="OI RSI Period")
oivwmaPeriode = input(14, title="OI VWMA Period")
oiRSI = ta.rsi(oiClose, oirsiPeriod)
oivwma = ta.vwma(oiRSI, oivwmaPeriode)

// Calculate Normal RSI
rsiPeriod = input(14, title = "RSI Period" )
rsi = ta.rsi(close, rsiPeriod)
vwmaPeriode = input(14, title="VWMA Period")
vwma = ta.vwma(rsi, vwmaPeriode)

// These two lines plot the Open Interest RSI and VWMA
plot(oiRSI, title="Open Interest RSI", color=color.rgb(0, 0, 0))
plot(oivwma, title="Open Interest RSI", color=color.orange)

// These two lines plot the RSI and VWMA
plot(rsi, title="Open Interest RSI", color=color.blue)
plot(vwma, title="Open Interest RSI", color=color.purple)

// Signal Logik
oib = (oiRSI > 70 and rsi < 30) or (oiRSI < 30 and rsi < 30)
ois = (oiRSI < 30 and rsi > 70) or (oiRSI > 70 and rsi > 70)

// Signals
bgcolor(oib ? color.new(color.green, 50) : na)
bgcolor(ois ? color.new(color.red, 50) : na)

// These lines create horizontal lines at various levels on the chart
u1 = hline(70, "Upper Level", color=color.red, linestyle= hline.style_solid)
u2 = hline(80, "Upper Level", color=color.red, linestyle= hline.style_solid)
l1 = hline(30, "Lower Level", color=color.green, linestyle= hline.style_solid)
l2 = hline(20, "Lower Level", color=color.green, linestyle= hline.style_solid)
m1 = hline(53, "upper Middl Level", color=color.gray, linestyle= hline.style_solid)
m2 = hline(47, "Lower Middl Level", color=color.gray, linestyle= hline.style_solid)

// These lines fill the areas between the horizontal lines with different colors
fill(u1, u2, color = color.new(color.red, 80))
fill(l1, l2, color = color.new(color.green, 80))
fill(m1, m2, color = color.new(color.gray, 80))

// Alerts
combined_alert = oib or ois
alertcondition(combined_alert, title='Combined Alert', message='A signal has triggered!')
alertcondition(oib, title='buy', message='Long Signal')
alertcondition(ois, title='sell', message='Short Signal')