Consensio V2 - Relativity 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 Relativity Indicator", overlay=true, max_bars_back=500, max_labels_count=500)

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

//Creates SMA's
price = ta.sma(close, p)
stma = ta.sma(close, s)
ltma = ta.sma(close, l)


//Plot SMA's
plot(price, "Price", color.lime, linewidth=1)
plot(stma, "STMA", color.orange, linewidth=1)
plot(ltma, "LTMA", color.blue, linewidth=1)

//Label function
relativity_label(txt, label_color, y_location) =>
    _label = label.new(bar_index, na)
    label.set_textcolor(_label, label_color)
    label.set_yloc(_label, y_location)
    label.set_text(_label, txt)
    label.set_style(_label, label.style_none)

//Relativity conditions
relativity(price, stma, ltma) =>  
    if price > stma and price > ltma and stma > ltma
        relativity_label("A", color.green, yloc.abovebar) 
    
    if price == stma and price > ltma and stma > ltma
        relativity_label("B", color.gray,  yloc.abovebar) 
    
    if price < stma and price > ltma and stma > ltma
        relativity_label("C", color.yellow,  yloc.abovebar)

    if price < stma and price == ltma and stma > ltma 
        relativity_label("D", color.gray, yloc.abovebar)

    if price < stma and price < ltma and stma > ltma
        relativity_label("E", color.orange, yloc.abovebar)

    if price < stma and price < ltma and stma == ltma
        relativity_label("F", color.gray, yloc.abovebar)

    if price < stma and price < ltma and stma < ltma
        relativity_label("G", color.red, yloc.abovebar)

    if price == stma and price < ltma and stma < ltma
        relativity_label("H", color.gray, yloc.belowbar)
    
    if price > stma and price < ltma and stma < ltma
        relativity_label("I", color.yellow, yloc.belowbar)
    
    if price > stma and price == ltma and stma < ltma
        relativity_label("J", color.gray, yloc.belowbar)
    
    if price > stma and price > ltma and stma < ltma
        relativity_label("K", color.orange, yloc.belowbar)
    
    if price > stma and price > ltma and stma == ltma 
        relativity_label("L", color.gray, yloc.belowbar)
    
    if price == stma and price == ltma and stma == ltma
        relativity_label("M", color.gray, yloc.belowbar)
        
//Detect when SMA's price are crossing
if ta.crossover(price, stma) or ta.crossunder(price, stma)
    relativity(price, stma, ltma)
if ta.crossover(ltma,stma) or ta.crossunder(ltma, stma)
    relativity(price, stma, ltma)
if ta.crossover(price, ltma) or ta.crossunder(price, ltma)
    relativity(price, stma, ltma)
if price==ltma or price==stma or stma==ltma
    relativity(price, stma, ltma)
