Double Supertrend HTF Filter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Harrocop

////////////////////////////////////////////////////////////////////////////////////////
// Double HTF Supertrend Filter:
// - The filter consists of two supertrend indicators of different higther time frames
// - Option to change the ATR and Factor of the Supertrends_Align_Down
// - Option to plot (or not) supertrend on the chart
// - The indicator is displayed as a horizontal line at the bottom of the chart:
//    1) Green: Both Supertrends are Up
//    2) Red: Both Supertrends are Down
//    3) Yellow: 1 supertrend is Up and the other id Down
// - The main idea is to combine the two supertrends as a filter in addition to a signal indicator to your liking.
////////////////////////////////////////////////////////////////////////////////////////

//@version=5
indicator(title = "Double Supertrend HTF Filter", shorttitle = "Supertrend HTF Filter", overlay = true)

////////////////////////////////////////////
//////       Supertrend HTF 1         //////
////////////////////////////////////////////
SUPER = "Supertrend HTF 1 Settings"
Use_supertrend1 = input.bool(true, title = "Plot Supertrend?", inline = "start", group = SUPER)
TimeFrame_supertrend1 = input.timeframe(title='Higher Time Frame', defval='30', inline = "start", group = SUPER)
ATR = input.int(defval = 10, title = "ATR length", minval = 1, maxval = 1000, step = 1, inline = "super1", group = SUPER)
FACTOR = input.float(defval = 3, title = "ATR length", minval = 0.1, maxval = 10, step = 0.1, inline = "super1", group = SUPER)

[Supertrend_1, direction_1] = ta.supertrend(FACTOR, ATR)
Supertrend_HTF1 = request.security(syminfo.tickerid, TimeFrame_supertrend1, Supertrend_1)
Direction_HTF1 = request.security(syminfo.tickerid, TimeFrame_supertrend1, direction_1)

////////////////////////////////////////////
//////       Supertrend HTF 2         //////
////////////////////////////////////////////
SUPER2 = "Supertrend HTF 2 Settings"
Use_supertrend2 = input.bool(true, title = "Plot Supertrend?", inline = "start2", group = SUPER2)
TimeFrame_supertrend2 = input.timeframe(title='Higher Time Frame', defval='240', inline = "start2", group = SUPER2)
ATR2 = input.int(defval = 10, title = "ATR length", minval = 1, maxval = 1000, step = 1, inline = "super2", group = SUPER2)
FACTOR2 = input.float(defval = 3, title = "ATR length", minval = 0.1, maxval = 10, step = 0.1, inline = "super2", group = SUPER2)

[Supertrend_2, direction_2] = ta.supertrend(FACTOR2, ATR2)
Supertrend_HTF2 = request.security(syminfo.tickerid, TimeFrame_supertrend2, Supertrend_2)
Direction_HTF2 = request.security(syminfo.tickerid, TimeFrame_supertrend2, direction_2)


// Define colors based on the Supertrend direction for the higher timeframe
color color_HTF1 = Direction_HTF1 < 0 ? color.green : color.red
plot(Use_supertrend1 ? Supertrend_HTF1 : na, title = "Supertrend HTF 1", color = color_HTF1)

color color_HTF2 = Direction_HTF2 < 0 ? color.green : color.red
plot(Use_supertrend2 ? Supertrend_HTF2 : na, title = "Supertrend HTF 2", color = color_HTF2)

// plot horizontal indicator at bottom of the chart
Supertrends_Align_Up = Direction_HTF1 < 0 and Direction_HTF2 < 0
Supertrends_Align_Down = Direction_HTF1 > 0 and Direction_HTF2 > 0
Suptertrends_Indecisive = Direction_HTF1 < 0 and Direction_HTF2 > 0 or Direction_HTF1 > 0 and Direction_HTF2 < 0
plotshape(Supertrends_Align_Up, title = "Supertrend are Up", style = shape.square, location = location.bottom, color = color.green)
plotshape(Supertrends_Align_Down, title = "Supertrend are Down", style = shape.square, location = location.bottom, color = color.red)
plotshape(Suptertrends_Indecisive, title = "Supertrend are Indecisive", style = shape.square, location = location.bottom, color = color.yellow)