Multi Timeframe Support & Resistance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jordanfray

//@version=5
indicator(title="Multi-Timeframe Support & Resistance",  overlay=true, max_bars_back=5000)
import jordanfray/threengine_global_automation_library/74 as bot

// Colors - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
green                                   = color.new(#2DBD85,0)
lightGreen                              = color.new(#2DBD85,80)
red                                     = color.new(#E02A4A,0)
lightRed                                = color.new(#E02A4A,80)
white                                   = color.new(#ffffff,0)
transparent                             = color.new(#ffffff,100)


//  Strategy Settings - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
supportLookBack                         = input.int(defval=22, title = "Support Look Back", group="Support & Resistance Settings")
supportLookAhead                        = input.int(defval=26, title = "Support Look Ahead", group="Support & Resistance Settings")
resistanceLookBack                      = input.int(defval=30, title = "Reistance Look Back", group="Support & Resistance Settings")
resistanceLookAhead                     = input.int(defval=27, title = "Reistance Look Ahead", group="Support & Resistance Settings")

timeframe1                              = input.timeframe(defval="60", title="Timeframe 1", group="Support & Resistance 1")
enableSrRanges1                         = input.bool(defval=true, title="Enable Support & Resistance Ranges", group="Support & Resistance 1")
supportRange1                           = input.float(defval=.25, title="     Support Range (%)       ", inline="support1", group="Support & Resistance 1")
supportRange1Color                      = input.color(defval=green, title="", inline="support1", group="Support & Resistance 1")
resistanceRange1                        = input.float(defval=.25, title="     Resistance Range (%)    ", inline="resistance1", group="Support & Resistance 1")
resistanceRange1Color                   = input.color(defval=red, title="", inline="resistance1", group="Support & Resistance 1")

timeframe2                              = input.timeframe(defval="120", title="Timeframe 2", group="Support & Resistance 2")
enableSrRanges2                         = input.bool(defval=true, title="Enable Support & Resistance Ranges", group="Support & Resistance 2")
supportRange2                           = input.float(defval=.25, title="     Support Range (%)       ", inline="support2", group="Support & Resistance 2")
supportRange2Color                      = input.color(defval=green, title="", inline="support2", group="Support & Resistance 2")
resistanceRange2                        = input.float(defval=.25, title="     Resistance Range (%)    ", inline="resistance2", group="Support & Resistance 2")
resistanceRange2Color                   = input.color(defval=red, title="", inline="resistance2", group="Support & Resistance 2")

//  Support and Resistance Functions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
getSupport(lookback, lookahead) =>
    support = fixnan(ta.pivotlow(lookback, lookahead)[1])

    support

getResistance(lookback, lookahead) =>
    resistance = fixnan(ta.pivothigh(lookback, lookahead)[1])

    resistance


//  Support and Resistance 1
support1                                = request.security(syminfo.ticker, timeframe1, getSupport(supportLookBack, supportLookAhead))
resistance1                             = request.security(syminfo.ticker, timeframe1, getResistance(resistanceLookBack, resistanceLookAhead))
var float mostRecentResistance1         = na
var float mostRecentSupport1            = na
mostRecentResistance1                   := ta.change(resistance1) ? resistance1 : mostRecentResistance1
mostRecentSupport1                      := ta.change(support1) ? support1 : mostRecentSupport1
var line resistance1Line                = na
var line support1Line                   = na
line.delete(resistance1Line)
line.delete(support1Line)
resistance1Line                         := line.new(x1=bar_index[10], y1=mostRecentResistance1, x2=bar_index, y2=mostRecentResistance1, color=resistanceRange1Color, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
support1Line                            := line.new(x1=bar_index[10], y1=mostRecentSupport1, x2=bar_index, y2=mostRecentSupport1, color=supportRange1Color, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
resistance1Price                        = plot(series=mostRecentResistance1, title="Resistance Price", color=red, editable=false, display=display.price_scale)
support1Price                           = plot(series=mostRecentSupport1, title="Support Price", color=green, editable=false, display=display.price_scale)

var label support1label                 = na
var label resistance1label                 = na
label.delete(support1label)
label.delete(resistance1label)
support1label                           := label.new(x=bar_index+30, y=mostRecentSupport1, text=bot.convertTimeframeToString(timeframe1), color=supportRange1Color, textcolor=white, style=label.style_label_down)
resistance1label                        := label.new(x=bar_index+30, y=mostRecentResistance1, text=bot.convertTimeframeToString(timeframe1), color=resistanceRange1Color, textcolor=white, style=label.style_label_down)


//  Support and Resistance 2
support2                                = request.security(syminfo.ticker, timeframe2, getSupport(supportLookBack, supportLookAhead))
resistance2                             = request.security(syminfo.ticker, timeframe2, getResistance(resistanceLookBack, resistanceLookAhead))
var float mostRecentResistance2         = na
var float mostRecentSupport2            = na
mostRecentResistance2                   := ta.change(resistance2) ? resistance2 : mostRecentResistance2
mostRecentSupport2                      := ta.change(support2) ? support2 : mostRecentSupport2
var line resistance2Line                = na
var line support2Line                   = na
line.delete(resistance2Line)
line.delete(support2Line)
resistance2Line                         := line.new(x1=bar_index[10], y1=mostRecentResistance2, x2=bar_index, y2=mostRecentResistance2, color=resistanceRange2Color, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
support2Line                            := line.new(x1=bar_index[10], y1=mostRecentSupport2, x2=bar_index, y2=mostRecentSupport2, color=supportRange2Color, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
resistance2Price                        = plot(series=mostRecentResistance2, title="Resistance Price", color=red, editable=false, display=display.price_scale)
support2Price                           = plot(series=mostRecentSupport2, title="Support Price", color=green, editable=false, display=display.price_scale)

var label support2label                 = na
var label resistance2label              = na
label.delete(support2label)
label.delete(resistance2label)
support2label                           := label.new(x=bar_index+30, y=mostRecentSupport2, text=bot.convertTimeframeToString(timeframe2), color=supportRange2Color, textcolor=white, style=label.style_label_down)
resistance2label                        :=  label.new(x=bar_index+30, y=mostRecentResistance2, text=bot.convertTimeframeToString(timeframe2), color=resistanceRange2Color, textcolor=white, style=label.style_label_down)


if enableSrRanges1
    support1RangeTop                    = support1 + (support1 * (supportRange1/100))
    support1RangeBottom                 = support1 - (support1 * (supportRange1/100))
    resistance1RangeTop                 = resistance1 + (resistance1 * (resistanceRange1/100))
    resistance1RangeBottom              = resistance1 - (resistance1 * (resistanceRange1/100))
    
    var line support1RangeTopLine       = na
    var line support1RangeBottomLine    = na
    var line resistance1RangeTopLine    = na
    var line resistance1RangeBottomLine = na
    
    line.delete(support1RangeTopLine)
    line.delete(support1RangeBottomLine)
    line.delete(resistance1RangeTopLine)
    line.delete(resistance1RangeBottomLine)

    support1RangeTopLine                := line.new(x1=bar_index[10], y1=support1RangeTop, x2=bar_index, y2=support1RangeTop, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
    support1RangeBottomLine             := line.new(x1=bar_index[10], y1=support1RangeBottom, x2=bar_index, y2=support1RangeBottom, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
    linefill.new(line1=support1RangeTopLine, line2=support1RangeBottomLine, color=lightGreen)
    
    resistance1RangeTopLine             := line.new(x1=bar_index[10], y1=resistance1RangeTop, x2=bar_index, y2=resistance1RangeTop, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
    resistance1RangeBottomLine          := line.new(x1=bar_index[10], y1=resistance1RangeBottom, x2=bar_index, y2=resistance1RangeBottom, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
    linefill.new(line1=resistance1RangeTopLine, line2=resistance1RangeBottomLine, color=lightRed)
    

if enableSrRanges2
    support2RangeTop                    = support2 + (support2 * (supportRange2/100))
    support2RangeBottom                 = support2 - (support2 * (supportRange2/100))
    resistance2RangeTop                 = resistance2 + (resistance2 * (resistanceRange2/100))
    resistance2RangeBottom              = resistance2 - (resistance2 * (resistanceRange2/100))
    
    var line support2RangeTopLine       = na
    var line support2RangeBottomLine    = na
    var line resistance2RangeTopLine    = na
    var line resistance2RangeBottomLine = na
    
    line.delete(support2RangeTopLine)
    line.delete(support2RangeBottomLine)
    line.delete(resistance2RangeTopLine)
    line.delete(resistance2RangeBottomLine)

    support2RangeTopLine                := line.new(x1=bar_index[10], y1=support2RangeTop, x2=bar_index, y2=support2RangeTop, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
    support2RangeBottomLine             := line.new(x1=bar_index[10], y1=support2RangeBottom, x2=bar_index, y2=support2RangeBottom, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
    linefill.new(line1=support2RangeTopLine, line2=support2RangeBottomLine, color=lightGreen)
    
    resistance2RangeTopLine             := line.new(x1=bar_index[10], y1=resistance2RangeTop, x2=bar_index, y2=resistance2RangeTop, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
    resistance2RangeBottomLine          := line.new(x1=bar_index[10], y1=resistance2RangeBottom, x2=bar_index, y2=resistance2RangeBottom, color=transparent, width=1, xloc=xloc.bar_index, style=line.style_solid, extend=extend.both)
    linefill.new(line1=resistance2RangeTopLine, line2=resistance2RangeBottomLine, color=lightRed)
    
