Gedhusek UltraTrend
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gedhusek1

//@version=5
indicator("Gedhusek UltraTrend",overlay = true)


firstPeriod = input.int(125,"First Period")
secondPeriod = input.int(225,"Second Period")
thirdPeriod = input.int(325,"Third Period")
AtrPeriod =  input.int(6,"ATR Period")

float firstMa = ta.sma(open,firstPeriod)
float secondMa = ta.sma(open,secondPeriod)
float thirdMa = ta.sma(open,thirdPeriod)
float AtrValue = ta.atr(AtrPeriod)

GreenShade(value) =>
    value >= 15 ? #00b300: value >12 ? #00cc00 :value > 10 ? #00e600: value >8 ? #00ff00 :value > 6? #1aff1a: value > 4 ? #33ff33 :#4dff4d

             

RedShade(value) =>
    value >= 15 ? #b30000 : value > 12 ? #cc0000 :  value > 10 ? #e60000 : value > 8 ? color.rgb(221, 59, 59) :  value > 6? #ff1a1a : value > 4 ? #ff3333 :#ff3333
  

TrendFunction(int period, float value, float atr) =>
    float multiplier = 0.5
    float shiftValue = value[period]
    float strength = 0
    
    string returnTrend = "None"

    float difference = value-shiftValue
    strength := math.abs(difference)/atr

    if(difference>atr*multiplier)
        returnTrend := "BULL"
    else if(difference<(atr*-1)*multiplier)
        returnTrend :="BEAR"
 

    [returnTrend,strength]
plottingLine = secondMa
string currentTrend = "None"
color plotColor = color.gray



[firstTrend,firstStr] = TrendFunction(firstPeriod,firstMa,AtrValue)
[secondTrend,secondStr] = TrendFunction(secondPeriod,secondMa,AtrValue)
[thirdTrend,thirdStr] = TrendFunction(thirdPeriod,thirdMa,AtrValue)

float sumofStrength = (firstStr+secondStr+thirdStr)/3


if(firstTrend == "BEAR" and secondTrend=="BEAR" and thirdTrend=="BEAR" and high<secondMa)
    currentTrend := "BEAR"
else if(firstTrend == "BULL" and secondTrend=="BULL" and thirdTrend=="BULL" and low>secondMa)
    currentTrend := "BULL"


if(currentTrend=="BULL")
    plotColor := GreenShade(sumofStrength)
    
if(currentTrend=="BEAR")
    plotColor :=  RedShade(sumofStrength)

plotcandle(open,high,low,close,color = plotColor,wickcolor = plotColor,bordercolor=plotColor)

plot(plottingLine,"Trend Line",color = plotColor,linewidth = 3)



    
