Speed Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sumitmak

//@version=5
indicator("Speed Indicator", shorttitle="SI")

//Time and Speed Range
t = input.int(20, "Speed Period", 1)
mp = input.int(20, "Moving Average Period", 1)
h = input.int(20, "Channel Period", 1)
hl = input.bool(false, "High/Low", "To use either close values or high/low")
p = input.bool(false, "Speed Price Percentage", "Display Speed in Percentage to Closing Price")
ma = input.bool(false, "Speed EMA", "Applies an EMA line to the Indicator")
mm = input.bool(true, "Minimum Maximum Speed Bands", "Shows a min-max channel like the Donchian Channels on the Indicator")
bb = input.bool(false, "Triple Bollinger Bands", "Shows three different Bollinger Band channels")

//Distance
gu = low > high[1] ? (low - high[1]):na
gd = high < low[1] ? (low[1] - high):na
hlg = low > high[1] ? (high - low + gu) : high < low[1] ? (high - low + gd) : (high - low)
clc = math.abs(close - close[1])
dis = hl ? hlg : clc
d = math.sum(dis, t) 

//Speed
s = d/t
sp = s/close*100
sv = p ? sp:s

//Speedometer Min-Max Bands
max = ta.highest(sv, h)
min = ta.lowest(sv, h)
sr = (max - min)/3
hs = max - sr
ls = min + sr

//Speed Standard Deviation Bands
ema = ta.ema(sv, mp)
std = ta.stdev(sv, h)
bbu1 = ema+1*std
bbl1 = ema-1*std
bbu2 = ema+2*std
bbl2 = ema-2*std
bbu3 = ema+3*std
bbl3 = ema-3*std


//Plots and Fills
p1 = plot(mm ? max:na, "Max", color.red)
p2 = plot(mm ? hs:na, "High", color.yellow)
plot(mm ? ls:na, "Low", color.green)
plot(mm ? min:na, "Min", color.blue)

plot(ma ? ema:na, "Average Speed", color.orange)
plot(bb ? bbu1:na, "Upper 1 SD", color.green)
plot(bb ? bbl1:na, "Lower 1 SD", color.green)
bb1 = plot(bb ? bbu2:na, "Upper 2 SD", color.yellow)
bb2 = plot(bb ? bbl2:na, "Lower 2 SD", color.yellow)
bb3 = plot(bb ? bbu3:na, "Upper 3 SD", color.red)
bb4 = plot(bb ? bbl3:na, "Lower 3 SD", color.red)

plot(sv, "Speed", #FF0000, 2)
fill(p1, p2, color.new(color.red, 90),"Top Range")
fill(bb1, bb3, color.new(color.red, 90), "Upper 3 SD Range")
fill(bb2, bb4, color.new(color.red, 90), "Lower 3 SD Range")