9-20 sma multi timeframe indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Elimak07

// @version=5
indicator("9-20 multi timeframes indicator")

// Get user input
res1 = input.timeframe(title="Timeframe #1", defval="")
res2 = input.timeframe(title="Timeframe #2", defval="3")
res3 = input.timeframe(title="Timeframe #3", defval="5")

// set style
strong_green = input(#26A69A, "Above Grow", group="Sma", inline="Above")
weak_green = input(#8bc8c3, "Fall", group="Sma", inline="Above")
weak_red = input(#eab3b9, "Below Grow", group="Sma", inline="Below")
strong_red = input(#FF5252, "Fall", group="Sma", inline="Below")

// Define custom security function
f_sync(_res, _exp) => 
    request.security(syminfo.tickerid, _res, _exp)

// Function to find the closest previous value of the serie
// because of using differentr time frame, this could be several index back
f_findFirstPreviousValue(serie) =>
    found = serie
    for i = 0 to 20
        if serie[i] != serie
            found := serie[i]
            break
    found

// Function to get if the sma crossed in the timeframe selected
f_crossed(res, sma9, sma20) =>
    syncSma9 = f_sync(res,sma9)
    syncSma20 = f_sync(res,sma20)
    crossUp = ta.crossover(syncSma9, syncSma20)
    crossDown = ta.crossunder(syncSma9, syncSma20)
    crossUp or crossDown

// get the trend in the timeframe selected
f_uptrend(res, sma9, sma20) => 
    syncSma9 = f_sync(res, sma9)
    syncSma20 = f_sync(res, sma20)
    syncSma9 > syncSma20


// Function to find the status of the trade: started, not started
f_tradeStarted(res, sma9, sma20, tradeStarted) =>
    bool result = tradeStarted
    syncClose = f_sync(res, close) 
    syncOpen = f_sync(res, open)
    syncSma9 = f_sync(res, sma9)
    syncSma20 = f_sync(res, sma20)
    isGreen = syncClose > syncOpen
    isRed = syncClose < syncOpen
    crossed = f_sync(res, ta.crossover(syncSma9, syncSma20)) or f_sync(res, ta.crossunder(syncSma9, syncSma20))
    uptrend = f_uptrend(res, sma9, sma20)
    if crossed
        result := false
    else     
        isGreenCandleAbove9 = isGreen and syncClose > syncSma9
        isRedCandleBelow9 = isRed and syncClose < syncSma9
        if uptrend and not tradeStarted
            if isGreenCandleAbove9
                result := true
        if uptrend and tradeStarted
            if isRedCandleBelow9
                result := false
        if not uptrend and not tradeStarted        
            if isRedCandleBelow9
                result := true
        if not uptrend and tradeStarted
            if isGreenCandleAbove9
                result := false 
    log.info("> started "+ str.tostring(result))
    result

// when the trade is favorable, it will be green in the uptrend
// and red in the downtrend - when falling in uptrend and growing
// in downtrend, we wignal the weakness with a lighter color
f_colorFill(res, sma9, sma20) =>
    sma9Sync = f_sync(res1, sma9)
    previousValue = f_findFirstPreviousValue(sma9Sync)
    growing = sma9Sync >= previousValue
    falling = sma9Sync < previousValue
    colorUp = growing ? strong_green : weak_green
    colorDown = falling ? strong_red : weak_red
    uptrend = f_uptrend(res, sma9, sma20)
    uptrend ? colorUp : colorDown 

// Get SMA values
sma9 = ta.sma(close, 9)
sma20 = ta.sma(close, 20)

bool trade1Started = na
trade1Started := f_tradeStarted(res1, sma9, sma20, trade1Started[1])
showCross1 = f_crossed(res1, sma9, sma20)
colorFill1 = f_colorFill(res1, sma9, sma20)
color1 = trade1Started? colorFill1 : color.rgb(198, 202, 219)

bool trade2Started = na
trade2Started := f_tradeStarted(res2, sma9, sma20, trade2Started[1])
showCross2 = f_crossed(res2, sma9, sma20)
colorFill2 = f_colorFill(res2, sma9, sma20)
color2 = trade2Started? colorFill2 : color.rgb(198, 202, 219)

bool trade3Started = na
trade3Started := f_tradeStarted(res3, sma9, sma20, trade3Started[1])
showCross3 = f_crossed(res3, sma9, sma20)
colorFill3 = f_colorFill(res3, sma9, sma20)
color3 = trade3Started? colorFill3 : color.rgb(198, 202, 219)

plotshape(showCross1? 2.5:na,  title = "crossed #1",  text = '',  style = shape.xcross, location = location.absolute, color= color.rgb(0, 0, 0), textcolor = color.white, size = size.tiny)
plotshape(showCross2? 1.5:na,  title = "crossed #2",  text = '',  style = shape.xcross, location = location.absolute, color= color.rgb(0, 0, 0), textcolor = color.white, size = size.tiny)
plotshape(showCross3? 0.5:na,  title = "crossed #3",  text = '',  style = shape.xcross, location = location.absolute, color= color.rgb(0, 0, 0), textcolor = color.white, size = size.tiny)

// Change background color #3
r1 = hline(0, color=color.black)
r2 = hline(1, color=color.black)
fill(r1, r2, color3)

// Change background color #2
r3 = hline(1, color=color.black)
r4 = hline(2, color=color.black)
fill(r3, r4, color2)

// Change background color #1
r5 = hline(2, color=color.black)
r6 = hline(3, color=color.black)
fill(r5, r6, color1)

// Alert conditions
bool enter1 = na
if trade1Started != trade1Started[1] and trade1Started
    enter1 := true

bool exit1 = na
if trade1Started != trade1Started[1] and not trade1Started
    exit1 := true

alertcondition(enter1,  'Start trading timeframe #1', 'Start trading timeframe #1 - look at the chart for direction - {{interval}} {{ticker}}')
alertcondition(exit1,  'Exit trading timeframe #1', 'Exit trading timeframe #1 - {{interval}} {{ticker}}')

bool enter2 = na
if trade1Started != trade1Started[1] and trade1Started
    enter2 := true

bool exit2 = na
if trade2Started != trade2Started[2] and not trade2Started
    exit2 := true

alertcondition(enter2,  'Start trading timeframe #2', 'Start trading timeframe #2 - look at the chart for direction - {{interval}} {{ticker}}')
alertcondition(exit2,  'Exit trading timeframe #2', 'Exit trading timeframe #2 - {{interval}} {{ticker}}')

bool enter3 = na
if trade3Started != trade3Started[1] and trade3Started
    enter3 := true

bool exit3 = na
if trade3Started != trade3Started[2] and not trade3Started
    exit3 := true

alertcondition(enter3,  'Start trading timeframe #3', 'Start trading timeframe #3 - look at the chart for direction - {{interval}} {{ticker}}')
alertcondition(exit3,  'Exit trading timeframe #3', 'Exit trading timeframe #3 - {{interval}} {{ticker}}')

