SMT Pair (Nephew_Sam_)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Nephew_Sam_

//@version=5
indicator(title='SMT Pair (Nephew_Sam_)')

// ----- INPUTS -----
var GRP1 = "SMT settings"
chartType = input.string(title="Line Chart or Candlesticks", defval="CandleSticks", options=["CandleSticks", "Lines"], group = GRP1, tooltip="Should the SMT chart show as a line chart or candlesticks?")
leftbars = input.int(title="Pivot Lookback Left", defval=7, group = GRP1, tooltip="The pivot should be the lowest/highest point in a minimum of x previous candles")
rightBars = input.int(title="Pivot Lookback Right", defval=1, group = GRP1, tooltip="The pivot should be the lowest/highest point in a minium of x next candles (the higher the value, the delayed the confirmation)")
hideLines = input.bool(true, title='Hide Divergence Lines?', group = GRP1)

var GRP2 = "Pair-SMT-Inverse Settings"

pair1 = input.symbol(title='(1) Pair', defval="EURUSD", inline = "1", group = GRP2)
smt1 = input.symbol(title='SMT', defval="DXY", inline = "1", group = GRP2)
smt1_i = input.bool(title=' ', defval=true, inline = "1", group = GRP2)

pair2 = input.symbol(title='(2) Pair', defval="GBPUSD", inline = "2", group = GRP2)
smt2 = input.symbol(title='SMT', defval="EURUSD", inline = "2", group = GRP2)
smt2_i = input.bool(title=' ', defval=false, inline = "2", group = GRP2)

pair3 = input.symbol(title='(3) Pair', defval="AUDUSD", inline = "3", group = GRP2)
smt3 = input.symbol(title='SMT', defval="NZDUSD", inline = "3", group = GRP2)
smt3_i = input.bool(title=' ', defval=false, inline = "3", group = GRP2)

pair4 = input.symbol(title='(4) Pair', defval="XAUUSD", inline = "4", group = GRP2)
smt4 = input.symbol(title='SMT', defval="XAGUSD", inline = "4", group = GRP2)
smt4_i = input.bool(title=' ', defval=false, inline = "4", group = GRP2)

pair5 = input.symbol(title='(5) Pair', defval="BTCUSD", inline = "5", group = GRP2)
smt5 = input.symbol(title='SMT', defval="ALTPERP", inline = "5", group = GRP2)
smt5_i = input.bool(title=' ', defval=false, inline = "5", group = GRP2)

pair6 = input.symbol(title='(6) Pair', defval="USOIL", inline = "6", group = GRP2)
smt6 = input.symbol(title='SMT', defval="UKOIL", inline = "6", group = GRP2)
smt6_i = input.bool(title=' ', defval=false, inline = "6", group = GRP2)

pair7 = input.symbol(title='(7) Pair', defval="SPX500USD", inline = "7", group = GRP2)
smt7 = input.symbol(title='SMT', defval="US30USD", inline = "7", group = GRP2)
smt7_i = input.bool(title=' ', defval=false, inline = "7", group = GRP2)

pair8 = input.symbol(title='(8) Pair', defval="GBPJPY", inline = "8", group = GRP2)
smt8 = input.symbol(title='SMT', defval="USDJPY", inline = "8", group = GRP2)
smt8_i = input.bool(title=' ', defval=false, inline = "8", group = GRP2)

pair9 = input.symbol(title='(9) Pair', defval="ETHUSD", inline = "9", group = GRP2)
smt9 = input.symbol(title='SMT', defval="BTCUSD", inline = "9", group = GRP2)
smt9_i = input.bool(title=' ', defval=false, inline = "9", group = GRP2)

pair10 = input.symbol(title='(10) Pair', defval="NAS100USD", inline = "10", group = GRP2)
smt10 = input.symbol(title='SMT', defval="SPX500USD", inline = "10", group = GRP2)
smt10_i = input.bool(title=' ', defval=false, inline = "10", group = GRP2)

defaultSmt = input.symbol(title='Default SMT', defval="DXY", inline="11", group = GRP2, tooltip="The default SMT divergence pair for all other symbols")
defaultSmt_i = input.bool(title=' ', defval=true, inline="11", group = GRP2)


showdebug = input.bool(false, title='Hover for tips ->', group = GRP2,
          tooltip="1. Use checkmark to get the inverse of a pair \n\n" +
              "2. For crypto pairs, make sure your pair/smt symbol is correct\n" +
              "(BTCUSD / BTCUSDT / BTCUSDTPERP ...) \n\n" +
              "3. You can get an index of FX base pairs by averaging the majors (divide JPY by 100):\n" + 
              "(EURUSD+EURGBP+EURCHF+EURJPY/100+EURAUD+EURNZD+EURCAD)/7\n" +
              "(GBPUSD+GBPEUR+GBPCHF+GBPJPY/100+GBPAUD+GBPCAD+GBPNZD)/7 etc..."
          )
// ----- INPUTS -----


// ----- SYMBOL -----
symbol = syminfo.ticker // Current Symbol
_i(_pair) => str.contains(_pair, symbol)

smt = _i(pair1) ? smt1 : 
          _i(pair2) ? smt2 :
          _i(pair3) ? smt3 :
          _i(pair4) ? smt4 :
          _i(pair5) ? smt5 :
          _i(pair6) ? smt6 :
          _i(pair7) ? smt7 :
          _i(pair8) ? smt8 :
          _i(pair9) ? smt9 :
          _i(pair10) ? smt10 :
          _i(smt1) ? pair1 :
          _i(smt2) ? pair2 :
          _i(smt3) ? pair3 :
          _i(smt4) ? pair4 :
          _i(smt5) ? pair5 :
          _i(smt6) ? pair6 :
          _i(smt7) ? pair7 :
          _i(smt8) ? pair8 :
          _i(smt9) ? pair9 :
          _i(smt10) ? pair10 :
          defaultSmt

smt_i = _i(pair1) ? smt1_i : 
          _i(pair2) ? smt2_i :
          _i(pair3) ? smt3_i :
          _i(pair4) ? smt4_i :
          _i(pair5) ? smt5_i :
          _i(pair6) ? smt6_i :
          _i(pair7) ? smt7_i :
          _i(pair8) ? smt8_i :
          _i(pair9) ? smt9_i :
          _i(pair10) ? smt10_i :
          defaultSmt_i

// ----- SYMBOL -----

// ----- OHLC -----
smtH(i = 0) => smt_i ? request.security(smt, timeframe.period, 1 / high[i]) : request.security(smt, timeframe.period, high[i])
smtL(i = 0) => smt_i ? request.security(smt, timeframe.period, 1 / low[i]) : request.security(smt, timeframe.period, low[i])
smtC(i = 0) => smt_i ? request.security(smt, timeframe.period, 1 / close[i]) : request.security(smt, timeframe.period, close[i])
smtO(i = 0) => smt_i ? request.security(smt, timeframe.period, 1 / open[i]) : request.security(smt, timeframe.period, open[i])
// ----- OHLC -----

// ----- Plot -----
typeC = chartType == "CandleSticks"

// plot(typeC ? na : smtC(), color=color.new(color.orange, 0), linewidth=2, title='SMT Close')
plot(typeC ? na : smtH(), color=color.new(color.blue, 0), linewidth=2, title='SMT High')
plot(typeC ? na : smtL(), color=color.new(color.red, 0), linewidth=2, title='SMT Low')

plotcandle(typeC ? smtO() : na, typeC ? smtH() : na, typeC ? smtL() : na, typeC ? smtC() : na,
     color=smtC() >= smtO() ? color.lime : color.red, 
     bordercolor=smtC() >= smtO() ? color.lime : color.red,
     wickcolor=smtC() >= smtO() ? color.lime : color.red)
// ----- Plot -----


// ----- SMT Divergence -----
pivotL = ta.pivotlow(low, leftbars, rightBars)
pivotH = ta.pivothigh(high, leftbars, rightBars)

plFound = na(pivotL) ? false : true
phFound = na(pivotH) ? false : true


// Bullish Divergence
priceLL = low[rightBars] < ta.valuewhen(plFound, low[rightBars], 1)
smtHL = smtL(rightBars) > ta.valuewhen(plFound, smtL(rightBars), 1)
bullCond = priceLL and smtHL and plFound
plot(not hideLines and plFound ? smtL(rightBars) : na, offset=-rightBars, title='Regular Bullish', linewidth=2, color=bullCond ? color.lime : na)


// Bearish Divergence
priceHH = high[rightBars] > ta.valuewhen(phFound, high[rightBars], 1)
smtLH = smtH(rightBars) < ta.valuewhen(phFound, smtH(rightBars), 1)
bearCond = priceHH and smtLH and phFound
plot(not hideLines and phFound ? smtH(rightBars) : na, offset=-rightBars, title='Regular Bullish', linewidth=2, color=bearCond ? color.red : na)

// ----- SMT Divergence -----


// DEBUG
if(showdebug)
    label.new(bar_index, smtH(), "Chart Pair: " + str.tostring(symbol) + "\n" + "SMT Pair: " + str.tostring(smt) + "\n" + "Inverse: " + str.tostring(smt_i), style = label.style_none, textcolor=color.blue)