Trend Step - Trailing
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © ProValueTrader

//@version=5
indicator('Trend Step - Trailing', overlay=true)

matype = input.string("WMA",  title="MA Type", options=["SMA", "EMA", "WMA", "RMA", "HMA"])
len = input.int(50, minval=2, title='Length')
Step = input.int(50, minval=0, title='Step Size [Ticks]') * syminfo.mintick  // The syminfo.mintick variable returns the minimum tick value for the instrument that the script calculates on (TradingView, n.d.). That value is the instrument’s smallest possible price movement. We can use the syminfo.mintick variable in indicator (study) and strategy scripts.
switchcol = input(false, title="Switch color based on the Average?")
col = input.color(color.aqua, title="Color")
width = input.int(2, minval=1, title="Linewidth")

ma(type)=>
    float ma = switch type
        "EMA" => ta.ema(close, len)
        "SMA" => ta.sma(close, len)
        "RMA" => ta.rma(close, len)
        "WMA" => ta.wma(close, len)
        "HMA" => ta.hma(close, len)
        // Default 
        => ta.sma(close, len)


set_ma = ma(matype)
varip MA = close
if set_ma > MA + Step
    MA := set_ma
    MA
else if set_ma < MA - Step
    MA := set_ma
    MA


col_switch = set_ma[1]<set_ma?color.lime:color.red
plot(MA, title="Trend Step - Trailing", color=switchcol?col_switch:col,style=plot.style_stepline, linewidth=width)
plot(set_ma, title="Average", color=col_switch)


licence = input.string("Licence", group="This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/",tooltip="This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/")
