PatronsitoP
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © theinfamouspaul

//@version=5
indicator(title="PatronsitoP by Sr.Sombrero", shorttitle="PatronsitoP", overlay=true)

// ---- Inputs & Plotting
plotEma = input.bool(true, "EMA", group="Display in chart")
plotBB = input.bool(true, "Bollinger bands", group="Display in chart")
plotRSI = input.bool(true, "RSI", group="Display in chart")
plotOI = input.bool(true, "Open Interest", group="Display in chart")
plotRSIHeaderColor = input.color(defval=color.orange, title="RSI Header color", group = "Display in chart")
plotRSIValueColor = input.color(defval=color.white, title="RSI Value color", group = "Display in chart")
bbSource = input.source(close, "Source", group="Bollinger Bands")
bbLength = input.int(20, "Length", group="Bollinger Bands")
bbStdDev = input.int(2, "Dev", group="Bollinger Bands")

rsiSource = input.source(close, "Source", group="RSI")
rsiLength = input.int(14, "Length", 1, group="RSI")

rsiExtra1Timeframe = input.timeframe(defval = "60", title="Extra1 Timeframe", group="RSI")
rsiExtra2Timeframe = input.timeframe(defval = "240", title="Extra2 Timeframe", group="RSI")

rsiLongValue = input.int(20, "Long if RSI below", 1, 100, group="Alert signals")
rsiShortValue = input.int(80, "Short if RSI above", 1, 100, group="Alert signals")

emaSource = input.source(close, "Source", group="EMA")
emaLength = input.int(13, "Length", group="EMA")


// ---- Main
// EMA
emaLine = ta.ema(emaSource, emaLength)
// Bollinger Bands data
[bbMiddle, bbUpper, bbLower] = ta.bb(bbSource, bbLength, bbStdDev)

// Long condition
rsiValue = ta.rsi(rsiSource, rsiLength)
rsiLongCondition = ta.rsi(rsiSource, rsiLength) < rsiLongValue
bbLongCondition = close < bbLower

// Short condition
rsiShortCondition = ta.rsi(rsiSource, rsiLength) > rsiShortValue
bbShortCondition = close > bbUpper

// Long and short signals
bool longSignal = rsiLongCondition and bbLongCondition
bool shortSignal = rsiShortCondition and bbShortCondition
plotchar(longSignal, "Long", "▲", location.belowbar, color.lime, size = size.tiny)
plotchar(shortSignal, "Short", "▼", location.abovebar, color.red,  size = size.tiny)

// ---- Alerts
// Specific direction alerts
alertcondition(longSignal, "Long signal", "Long signal")
alertcondition(shortSignal, "Short signal", "Short signal")
// These ones to be used if you want alerts in any direction
if longSignal
    alert("Long signal")
if shortSignal
    alert("Short signal")

// ---- Plotting 
// Bollinger Bands
plot(plotBB ? bbUpper: na,"BB upper", color=color.gray)
plot(plotBB ? bbMiddle: na, "BB middle", color=color.teal)
plot(plotBB ? bbLower: na, "BB lower", color=color.gray)
plot(plotEma ? emaLine: na, "EMA", color=color.orange)

// Request 2 extra rsi info for higher timeframes
rsiExtra1Value = request.security(syminfo.ticker, rsiExtra1Timeframe, rsiValue[1], barmerge.gaps_off, barmerge.lookahead_on)
rsiExtra2Value = request.security(syminfo.ticker, rsiExtra2Timeframe, rsiValue[1], barmerge.gaps_off, barmerge.lookahead_on)

[oiOpen,oiClose] = request.security(syminfo.ticker + "_OI", timeframe.period, [open, close], ignore_invalid_symbol = true)

bool oiLong = oiClose > oiOpen
bool oiShort = oiOpen > oiClose

//plotchar(oiLong, "OI Long", "▲", location.belowbar, color.lime, size = size.tiny)
plotshape(plotOI ? oiLong: na, style=shape.circle, title="OI positive", location=location.abovebar, color=color.new(color.green,60), textcolor=color.new(color.black, 0))
plotshape(plotOI ? oiShort: na, style=shape.circle, title="OI negative", location=location.abovebar, color=color.new(color.red,60), textcolor=color.new(color.black, 0))

    

if plotRSI
    var rsiTable = table.new(position=position.bottom_left, columns=3, rows=2, border_color = color.blue, border_width = 1, frame_width = 1, bgcolor = plotRSIHeaderColor)
    table.cell(table_id = rsiTable, column = 0, row = 0, text = timeframe.period)
    table.cell(table_id = rsiTable, column = 1, row = 0, text = rsiExtra1Timeframe)
    table.cell(table_id = rsiTable, column = 2, row = 0, text = rsiExtra2Timeframe)
    table.cell(table_id = rsiTable, column = 0, row = 1, text = str.tostring(math.round(rsiValue, 2)), bgcolor=plotRSIValueColor)
    table.cell(table_id = rsiTable, column = 1, row = 1, text=  str.tostring(math.round(rsiExtra1Value, 2)), bgcolor=plotRSIValueColor)
    table.cell(table_id = rsiTable, column = 2, row = 1, text=  str.tostring(math.round(rsiExtra2Value, 2)), bgcolor=plotRSIValueColor)