#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © sumitmak
#indicator("Speed Indicator", shorttitle="SI")
# converted by Sam4COK@Samer800 - 10/2023 - request from UseThinkScript.com memeber
declare lower;
#//Time and Speed Range
input SpeedLength = 20; # "Speed Period"
input MovAvgLength = 20; # "Moving Average Period"
input ChannelLength = 20; # "Channel Period"
input PriceCalculation = {Default "Close", "High/Low"};#, "To use either close values or high/low")
input usePricePercentage = no; # "Speed Price Percentage", "Display Speed in Percentage to Closing Price"
input showSpeedMovAvg = no; # "Speed EMA", "Applies an EMA line to the Indicator")
input showMaxMinBand = yes; # "Shows a min-max channel like the Donchian Channels on the Indicator"
input showTripleBollingerBands = no;# "Triple Bollinger Bands", "Shows three different Bollinger Band channels"
def na = Double.NaN;
def hl = PriceCalculation==PriceCalculation."High/Low";
def bb = showTripleBollingerBands;
#//Distance
def gu = if low > high[1] then (low - high[1]) else na;
def gd = if high < low[1] then (low[1] - high) else na;
def hlg = if low > high[1] then (high - low + gu) else
if high < low[1] then (high - low + gd) else (high - low);
def clc = AbsValue(close - close[1]);
def dis = if hl then hlg else clc;
def d = Sum(dis, SpeedLength);
#//Speed
def s = d / SpeedLength;
def sp = s / close * 100;
def sv = if usePricePercentage then sp else s;
#//Speedometer Min-Max Bands
def max = Highest(sv, ChannelLength);
def min = Lowest(sv, ChannelLength);
def sr = (max - min) / 3;
def hs = max - sr;
def ls = min + sr;
#//Speed Standard Deviation Bands
def ema = ExpAverage(sv, MovAvgLength);
def std = stdev(sv, ChannelLength);
def bbu1 = ema + 1 * std;
def bbl1 = ema - 1 * std;
def bbu2 = ema + 2 * std;
def bbl2 = ema - 2 * std;
def bbu3 = ema + 3 * std;
def bbl3 = ema - 3 * std;
#//Plots and Fills
plot speed = sv;#, "Speed", #FF0000, 2)
speed.SetLineWeight(2);
speed.SetDefaultColor(Color.MAGENTA);
plot p1 = if showMaxMinBand then max else na;#, "Max", color.red)
plot p2 = if showMaxMinBand then hs else na;#, "High", color.yellow)
p1.SetDefaultColor(Color.RED);
p2.SetDefaultColor(Color.YELLOW);
plot LowLine = if showMaxMinBand then ls else na;#, "Low", color.green)
plot minLine = if showMaxMinBand then min else na;#, "Min", color.blue)
LowLine.SetDefaultColor(Color.GREEN);
minLine.SetDefaultColor(Color.CYAN);
plot AvgSpeed = if showSpeedMovAvg then ema else na;#, "Average Speed", color.orange)
plot UpBB1 = if bb then bbu1 else na;#, "Upper 1 SD", color.green)
plot LoBB1 = if bb then bbl1 else na;#, "Lower 1 SD", color.green)
plot bb1 = if bb then bbu2 else na;#, "Upper 2 SD", color.yellow)
plot bb2 = if bb then bbl2 else na;#, "Lower 2 SD", color.yellow)
plot bb3 = if bb then bbu3 else na;#, "Upper 3 SD", color.red)
plot bb4 = if bb then bbl3 else na;#, "Lower 3 SD", color.red)
AvgSpeed.SetDefaultColor(Color.ORANGE);
UpBB1.SetDefaultColor(Color.DARK_GREEN);
LoBB1.SetDefaultColor(Color.DARK_GREEN);
BB1.SetDefaultColor(Color.DARK_ORANGE);
BB2.SetDefaultColor(Color.DARK_ORANGE);
BB3.SetDefaultColor(Color.DARK_RED);
BB4.SetDefaultColor(Color.DARK_RED);
AddCloud(p1, p2, Color.DARK_RED);#,"Top Range")
AddCloud(bb3, bb1, Color.DARK_RED);#, "Upper 3 SD Range")
AddCloud(bb2, bb4, Color.DARK_RED);#, "Lower 3 SD Range")
#-- END of CODE