Trend Slope Meter - Kaspricci
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kaspricci

//@version=5
indicator("Trend Slope Meter - Kaspricci", shorttitle = "Trend Slope Meter", overlay = false)

trendSource     = input.source(close, title = "Source")

headlineTrend   = "Trend Settings"

calcOwnTrend    = input.bool(true, "Calculate trend by moving average",   group = headlineTrend, tooltip = "In case you select an external source,you can disable moving average calculation.")
maType          = input.string("EMA", title = "      Type",               group = headlineTrend, options = ["EMA", "HMA", "SMA", "SWMA", "VWMA", "WMA"])
maLength        = input.int(50,       title = "      Length", minval = 1, group = headlineTrend)

headlineSlope   = "Slope Settings"
slLength        = input.int(defval = 10, minval = 1, title = "Length", group = headlineSlope, tooltip = "Number of bars back to calculate the slope")


float ma = switch maType 
    "EMA"  => ta.ema(trendSource,  maLength)
    "HMA"  => ta.hma(trendSource,  maLength)
    "SMA"  => ta.sma(trendSource,  maLength)
    "SWMA" => ta.swma(trendSource)
    "VWMA" => ta.vwma(trendSource, maLength)
    "WMA"  => ta.wma(trendSource,  maLength)


slope           =  calcOwnTrend ? ta.change(ma, slLength) / slLength / syminfo.mintick : ta.change(trendSource, slLength) / slLength / syminfo.mintick

upTH = hline( 45, "Upper Threshold", color.gray)
base = hline(  0, "Base Line",       color.black, hline.style_solid)
lwTH = hline(-45, "Lower Threshold", color.gray)

fill(upTH, lwTH, title = "Background", color = color.rgb(100, 100, 100, 90))

plot(slope, "Slope", color.purple, linewidth = 2)