SSL Channel [MoonKoder]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MoonKoder


//@version=5

indicator(title = "SSL Channel [MoonKoder]", overlay = true)

// ======================================================
// ==                                                  ==
// ==         I N P U T    P A R A M E T E R S         ==
// ==                                                  ==
// ======================================================

// {

// Get User Inputs
Period_High = input.int(title = "Period High", defval = 13)
Color_High  = input.color(title = "Color High", defval = color.teal)
Period_Low  = input.int(title = "Period Low", defval=13)
Color_Low   = input.color(title = "Color Low", defval = color.red)
Resolution  = input.timeframe(title = "TimeFrame", defval = "15")
CandleType  = input.string(title = "Candle Type", defval = "01 - Traditional", options = ["01 - Traditional", "02 - Renko", "03 - Line Break", "04 - Kagi"])
UseGap      = input.bool(title = "Use Gap", defval = true)

// }



// ======================================================
// ==                                                  ==
// ==    S U P P O R T     C A L C U L A T I O N S     ==
// ==                                                  ==
// ======================================================

// {


// Function to Calculate the SSL Channel
Function_SslChannel(_Period_High, _Period_Low) =>
    MovAvg_High = ta.sma(high, _Period_High)
    MovAvg_Low  = ta.sma(low, _Period_Low)
    
    Direction   = 0
    Direction   := close > MovAvg_High ? 1 : close < MovAvg_Low ? -1 : Direction[1]
    
    SSL_Below   = Direction < 0 ? MovAvg_High: MovAvg_Low
    SSL_Above   = Direction < 0 ? MovAvg_Low : MovAvg_High
    
    [SSL_Below, SSL_Above]


// Function to Determine The Choosen Ticker Types
// {

Function_TickerType(_TickerType) =>
    if _TickerType == "01 - Current"
        syminfo.tickerid
    else if _TickerType == "02 - Heikin Ashi"
        ticker.heikinashi(syminfo.tickerid) 
    else if _TickerType == "03 - Renko"
        ticker.renko(syminfo.tickerid, "ATR", 10)  
    else if _TickerType == "04 - Line Break"
        ticker.linebreak(syminfo.tickerid, 3)
    else if _TickerType == "05 - Kagi"
        ticker.kagi(syminfo.tickerid, 3) 

// }


// }



// ======================================================
// ==                                                  ==
// ==       R E Q U E S T S     &     P L O T S        ==
// ==                                                  ==
// ======================================================

// {


[_SSL_Below, _SSL_Above]  = Function_SslChannel(Period_High, Period_Low)


SSL_Below_GapOn     = request.security(Function_TickerType(CandleType), Resolution, _SSL_Below, barmerge.gaps_on)
SSL_Below_GapOff    = request.security(Function_TickerType(CandleType), Resolution, _SSL_Below, barmerge.gaps_off)
SSL_Below           = UseGap ? SSL_Below_GapOn : SSL_Below_GapOff

SSL_Above_GapOn     = request.security(Function_TickerType(CandleType), Resolution, _SSL_Above, barmerge.gaps_on)
SSL_Above_GapOff    = request.security(Function_TickerType(CandleType), Resolution, _SSL_Above, barmerge.gaps_off)
SSL_Above           = UseGap ? SSL_Above_GapOn : SSL_Above_GapOff


plot(SSL_Below, linewidth = 2, color = Color_High)
plot(SSL_Above, linewidth = 2, color = Color_Low)


// }



// ======================================================
// ==                                                  ==
// ==             P R O G R A M     E N D              ==
// ==                                                  ==
// ======================================================