Directional Slope Strength Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Mik3Christ3ns3n

//@version=5
indicator("Directional Slope Strength Index", shorttitle = "DSSI", format = format.price, precision = 1)
length = input.int(20, minval=1, title = "ROC Length")
source = input(hlc3, "ROC Source")
stdlength = input.int(1000, minval=1, title = "Sample Length")

roc = 100 * (source - source[length])/source[length]
rocstd = ta.stdev(roc, stdlength)
zscore = (roc - ta.sma(roc, stdlength)) / rocstd

zscore_color = zscore > 0 ? #4CAF50 : #FF5252

getHighest(dataSeries) =>
    var highest = dataSeries
    if dataSeries >= nz(highest, 0)
        highest := dataSeries
    highest

getLowest(dataSeries) =>
    var lowest = dataSeries
    if dataSeries < nz(lowest, 1e10)
        lowest := dataSeries
    lowest

plot(zscore, title="Slope Strength", style=plot.style_line, color=zscore_color, linewidth = 2)
hline(2, color=#787B86, linestyle = hline.style_dotted, title="2 STDEV")
hline(0, color=#787B86, title="Zero Line")
hline(-2, color=#787B86, linestyle = hline.style_dotted, title="-2 STDEV")
plot(series=getHighest(zscore), color=#4CAF50, linewidth=1, style=plot.style_line, title="Highest Score")
plot(series=getLowest(zscore), color=#FF5252, linewidth=1, style=plot.style_line, title="Lowest Score")
