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

//@version=4
study("Confluence Candles", overlay=true)

includeSymbol1 = input(true, "Include Symbol 1?", group = "Symbols")
useCurrentChartAsSymbol1 = input(true, "Use current chart's symbol as Symbol 1", group = "Symbols")
symbol1 = input("", "Symbol 1", group = "Symbols")
includeSymbol2 = input(false, "Include Symbol 2?", group = "Symbols")
symbol2 = input("", "Symbol 2", group = "Symbols")
includeSymbol3 = input(false, "Include Symbol 3?", group = "Symbols")
symbol3 = input("", "Symbol 3", group = "Symbols")
includeSymbol4 = input(false, "Include Symbol 4?", group = "Symbols")
symbol4 = input("", "Symbol 4", group = "Symbols")

// Kill Switch for if no instruments were selected
killSwitch =  not includeSymbol1 and not includeSymbol2 and not includeSymbol3 and not includeSymbol4

includeRsi = input(true, "Include RSI", group = "RSI")
rsiLength = input(7, "RSI Length", group = "RSI")
rsiBullThreshold = input(50, "RSI Bull Threshold", group = "RSI")
rsiBearThreshold = input(50, "RSI Bear Threshold", group = "RSI")
includeStoch = input(true, "Include Stochastic", group = "Stochastic")

periodK = input(14, title="%K Length", minval=1, group = "Stochastic")
smoothK = input(1, title="%K Smoothing", minval=1, group = "Stochastic")
periodD = input(3, title="%D Smoothing", minval=1, group = "Stochastic")
useStochD = input(false, "Use Stochastic D instead of K", group = "Stochastic")
stochBullThreshold = input(50, "Stochastic Bull Threshold", group = "Stochastic")
stochBearThreshold = input(50, "Stochastic Bear Threshold", group = "Stochastic")

includeMacdHist = input(true, "Include MACD Histogram", group = "MACD")
macdFastLength = input(12, "MACD Fast Length", group = "MACD")
macdSlowLength = input(26, "MACD Slow Length", group = "MACD")
macdSignalLength = input(9, "MACD Signal Length", group = "MACD")

// Kill Switch for if no indicators are selected
confluenceKillSwitch = not includeMacdHist and not includeRsi and not includeStoch

stochastic(periodK, smoothK, periodD) => 
    k = sma(stoch(close, high, low, periodK), smoothK)
    d = sma(k, periodD)
    output = useStochD ? d : k

confluence() =>
    [_, _, histLine] = macd(close, macdFastLength, macdSlowLength, macdSignalLength)
    rsiLine = rsi(close, rsiLength)
    stochLine = stochastic(periodK, smoothK, periodD)
    histBull = includeMacdHist ? histLine > 0 : true
    histBear = includeMacdHist ? histLine <= 0 : true
    rsiBull = includeRsi ? rsiLine > rsiBullThreshold : true
    rsiBear = includeRsi ? rsiLine < rsiBearThreshold : true
    stochBull = includeStoch ? stochLine > stochBullThreshold : true
    stochBear = includeStoch ? stochLine < stochBearThreshold : true
    signal = histBull and rsiBull and stochBull and not confluenceKillSwitch ? 1 : histBear and rsiBear and stochBear and not confluenceKillSwitch ? -1 : 0

confluenceSymbol1 = useCurrentChartAsSymbol1 ? confluence() : security(symbol1, "", confluence())
confluenceSymbol2 = security(symbol2, "", confluence())
confluenceSymbol3 = security(symbol3, "", confluence())
confluenceSymbol4 = security(symbol4, "", confluence())

symbol1BullFilter = includeSymbol1 ? confluenceSymbol1 == 1 : true
symbol1BearFilter = includeSymbol1 ? confluenceSymbol1 == -1 : true

symbol2BullFilter = includeSymbol2 ? confluenceSymbol2 == 1 : true
symbol2BearFilter = includeSymbol2 ? confluenceSymbol2 == -1 : true

symbol3BullFilter = includeSymbol3 ? confluenceSymbol3 == 1 : true
symbol3BearFilter = includeSymbol3 ? confluenceSymbol3 == -1 : true

symbol4BullFilter = includeSymbol4 ? confluenceSymbol4 == 1 : true
symbol4BearFilter = includeSymbol4 ? confluenceSymbol4 == -1 : true

bullish = symbol1BullFilter and symbol2BullFilter and symbol3BullFilter and symbol4BullFilter and not killSwitch
bearish = symbol1BearFilter and symbol2BearFilter and symbol3BearFilter and symbol4BearFilter and not killSwitch

barcolor(bullish ? #00FF00 : bearish ? #FF0000 : color.gray)

// Alerts 
greenCandleAlerts = input(true, "Alerts for candle color changing to GREEN", group="Alerts")
redCandleAlerts = input(true, "Alerts for candle color changing to RED", group="Alerts")
grayCandleAlerts = input(true, "Alerts for candle color changing to GRAY", group="Alerts")

if (greenCandleAlerts and bullish and not bullish[1])
    alert("Candle color changed to GREEN", alert.freq_once_per_bar_close)

if (redCandleAlerts and bearish and not bearish[1])
    alert("Candle color changed to RED", alert.freq_once_per_bar_close)

if (grayCandleAlerts and not bullish and not bearish and (bullish[1] or bearish[1]))
    alert("Candle color changed to GRAY", alert.freq_once_per_bar_close)