Consensio V2 - Directionality Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bneumann

//@version=5
indicator("Consensio Directionality Indicator")

//Inputs
p = input(1, "Price")
s = input(7, "STMA")
l = input(30, "LTMA")

//Create SMA's
price = ta.sma(close, p)
stma = ta.sma(close, s)
ltma = ta.sma(close, l)
 
//Get Moving Average Direction
get_direction(ma, length) =>
    highest = ma[1]
    lowest = ma[1]
    for i = 1 to length
        if highest < ma[i]
            highest := ma[i]
        if lowest > ma[i]
            lowest := ma[i]
    if ma > highest
        2
    else if ma < lowest
        0
    else
        1

//Calculate directionality
directionality = (get_direction(ltma, l)*9)+(get_direction(stma, s)*3)+(get_direction(price, p))-13

//Plot Directionality   
plot(directionality, "Dirctionality")

//Background
greenTop = hline(13,  "Upper Band")
greenBottom = hline(5, "Lower Band")
fill(greenTop, greenBottom, color.new(color.green, 70), "Background")
yellowTop = hline(5,  "Upper Band")
yellowBottom = hline(0, "Lower Band")
fill(yellowTop, yellowBottom, color.new(color.yellow, 70), "Background")
orangeTop = hline(0,  "Upper Band")
orangeBottom = hline(-5, "Lower Band")
fill(orangeTop, orangeBottom, color.new(color.orange, 70), "Background")
redTop = hline(-5,  "Upper Band")
redBottom = hline(-13, "Lower Band")
fill(redTop, redBottom, color.new(color.red, 70), "Background")
