Trade Tool VDWMA + OI RSI Based
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Demech

//@version=5
indicator("Trade Tool VDWMA + OI RSI Based", shorttitle = "VDWMA Tool", overlay=true, format=format.price, precision = 2, timeframe = "", timeframe_gaps = false)
import PineCoders/Time/3

// Parameters
resistance_length = input(50, title = "Resistance")
support_length = input(50, title = "Support")
vdwmalength = input(200, title = "VDWMA Slow" )
vdwmalengthfast = input(50, title='VDWMA Fast')
rsi1_period = input(title='RSI Period', defval=14)
oirsiPeriod = input(7, title="OI RSI Period")

// Barcolor Input
upper_color = input(color.new (color.rgb(1, 250, 10),0),"bull")
lower_color = input(color.new (color.rgb(255, 0, 0),0),"bear")
middle_color = input(color.new(color.gray,0), "neutral")

// Calculation of the volume delta (buy volume - sell volume)
upWick = high - (open < close ? open : close)
downWick = (open > close ? open : close) - low
wickSum = upWick + downWick
buyVolume = wickSum != 0 ? volume * (upWick / wickSum) : volume / 2
sellVolume = wickSum != 0 ? volume * (downWick / wickSum) : volume / 2
volDelta = buyVolume - sellVolume

// Use the absolute value of the volume delta as weight
absVolDelta = math.abs(volDelta)

// VDWMA
vdwma = ta.vwma(close * absVolDelta, vdwmalength) / ta.vwma(absVolDelta, vdwmalength)
vdwmac = ta.vwma(close * absVolDelta, vdwmalengthfast) / ta.vwma(absVolDelta, vdwmalengthfast)

// Barcolor Logik
up = vdwmac > vdwma and close > vdwmac
down = vdwmac < vdwma and close < vdwmac
barcolor(up ? upper_color : down ? lower_color : middle_color)

// Resistance and Support
resistance = ta.highest(close, resistance_length)
support = ta.lowest(close, support_length)
resistanceh = ta.highest(high, resistance_length)
supportl = ta.lowest(low, support_length)

// RSI
rsiValue = ta.rsi(close, rsi1_period)

// Search for highest/lowest points
priceHigh = ta.highest(high, rsi1_period)
priceLow = ta.lowest(low, rsi1_period)
rsiHigh = ta.highest(rsiValue, rsi1_period)
rsiLow = ta.lowest(rsiValue, rsi1_period)

// Find the divergences
bearishDiv = high == priceHigh and rsiValue < rsiHigh
bullishDiv = low == priceLow and rsiValue > rsiLow

// OI RSI Code

// 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))

// Creates the OI RSI
oiRSI = ta.rsi(oiClose, oirsiPeriod)

// Signal Logic
oib = ((oiRSI > 70 and rsiValue < 30) or (oiRSI < 30 and rsiValue < 30)) and not (oiOpen > oiClose)
ois = ((oiRSI < 30 and rsiValue > 70) or (oiRSI > 70 and rsiValue > 70)) and not (oiOpen < oiClose)

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

// Buy and Sell Signals Logic
newsell = resistance[1] == resistance and high >= resistance and close < resistance and bearishDiv
newbull = support[1] == support and low <= support and close > support and bullishDiv

// Draw support and resistance lines and signals
r1 = plot(resistance, color=color.new(color.rgb(253, 0, 241), 100), title = "Resistance Close")
s1 = plot(support, color=color.new(color.rgb(253, 0, 241), 100), title = "Support Close")
r2 = plot(resistanceh, color=color.new(color.rgb(253, 0, 241), 100), title = "Resistance High")
s2 = plot(supportl, color=color.new(color.rgb(253, 0, 241), 100), title = "Support Low")
fill(r1, r2, color = color.new(color.purple, 80), title = "Resistance Fill Color")
fill(s1, s2, color = color.new(color.purple, 80), title = "Support Fill Color")


// Draws VDWMA Fast and slow
plot(vdwmac, color=color.new(color.blue, 0), title = "VDWMA Fast")
plot(vdwma, color=color.new(#000000, 0), title = "VDWMA Slow" )

// Draws signals
plotshape(newbull, title="Buy", location=location.belowbar, color=color.green, style=shape.labelup, text="b", textcolor = color.white)
plotshape(newsell, title="Sell", location=location.abovebar, color=color.red, style=shape.labeldown, text="s", textcolor = color.white)
plotshape(bearishDiv, title="Bearish Divergence", location=location.abovebar, color=color.red, style=shape.triangledown, size = size.tiny)
plotshape(bullishDiv, title="Bullish Divergence", location=location.belowbar, color=color.green, style=shape.triangleup, size = size.tiny)


// Alerts
alertcondition(condition = newbull, title = "buy", message = "Buy")
alertcondition(condition = newsell, title = "sell", message = "Sell")
alertcondition(condition = bullishDiv, title = "Bullisch Divergence", message = "Bullisch Divergence")
alertcondition(condition = bearishDiv, title = "Bearisch Divergence", message = "Bearisch Divergence")
alertcondition(condition = oib, title = "Bullisch OI RSI Signal", message = "Bullisch OI RSI Signal")
alertcondition(condition = ois, title = "Bearisch OI RSI Signal", message = "Bearisch OI RSI Signal")

combined_alert = newbull or newsell or bullishDiv or bearishDiv or oib or ois
alertcondition(combined_alert, title='Combined Alert', message='A signal has triggered!')