
Author Message:
The SMA Trend Spectrum indicator is designed to visually represent market trends and momentum by using a series of Simple Moving Averages (SMAs) to create a color-coded spectrum or heatmap. This tool helps traders identify the strength and direction of market trends across various time frames within one chart.
More Details: https://www.tradingview.com/script/vgnEQWlK-SMA-Trend-Spectrum-InvestorUnknown/
CODE:
CSS:
#// Indicator for TOS
#// © InvestorUnknown | TRW: @Andrej S.
#indicator("SMA Trend Spectrum [InvestorUnknown]","SMA Trend Spectrum")
# Converted by Sam4Cok@Samer800 - 01/2025
Declare lower;
input timeframe = AggregationPeriod.MIN;
input colorBars = yes; # "Color Bars?"
input showSignalLine = yes; # "Plot Signal?"
input showLabel = yes;
input StartingPeriod = 2; # "Starting Period"
input PeriodIncrement = 1; # "Period Increment"
input Source = FundamentalType.CLOSE; # "SMA Source"
input smoothingOption = {Default "Smooth Signal Line", "No Smoothing"}; # "Signal Smoothing"
input smoothMovAvgType = AverageType.SIMPLE;
input SmoothingLength = 14; # "Smoothing Length"
def na = Double.NaN;
def last = isNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def from = Max(StartingPeriod, 2);
def by_ = Max(PeriodIncrement, 1);
def src = Fundamental(Source, Period = tf);
#-- Color
DefineGlobalColor("hi", Color.DARK_GREEN);
DefineGlobalColor("mid", Color.DARK_ORANGE);
DefineGlobalColor("lo", Color.DARK_RED);
#-- Functions
Script f_trend {
input type = AverageType.SIMPLE;
input src = close;
input x1 = 1;
input x2 = 2;
input x3 = 3;
def min = MovingAverage(type, src, x1);
def mid = MovingAverage(type, src, x2);
def max = MovingAverage(type, src, x3);
def t = if src > min and src > mid and src > max then 1 else
if src > min or src > mid or src > max then 0.5 else 0;
plot out = if isNaN(t) then 0 else t;
}
##// ~~ Gradient Coloring {
Script gradient_color {
input src = close;
input minVal = 10;
input maxVal = 400;
input loR = 173;
input loG = 216;
input loB = 230;
input hiR = 41;
input hiG = 98;
input hiB = 255;
def value = if isNaN(src) then 0 else src;
def clamped_value = max(min(value, maxVal), minVal);
def normalized_value = (clamped_value - minVal) / (maxVal - minVal);
def re = floor(loR + (hiR - loR) * normalized_value);
def gr = floor(loG + (hiG - loG) * normalized_value);
def bl = floor(loB + (hiB - loB) * normalized_value);
plot r = re;
plot g = gr;
plot b = bl;
}
def x0 = from + by_ * 0 * 3;
def x1 = from + by_ * 1 * 3;
def x2 = from + by_ * 2 * 3;
def x3 = from + by_ * 3 * 3;
def x4 = from + by_ * 4 * 3;
def x5 = from + by_ * 5 * 3;
def x6 = from + by_ * 6 * 3;
def x7 = from + by_ * 7 * 3;
def x8 = from + by_ * 8 * 3;
def x9 = from + by_ * 9 * 3;
def x10 = from + by_ * 10 * 3;
def x11 = from + by_ * 11 * 3;
def x12 = from + by_ * 12 * 3;
def x13 = from + by_ * 13 * 3;
def x14 = from + by_ * 14 * 3;
def x15 = from + by_ * 15 * 3;
def x16 = from + by_ * 16 * 3;
def x17 = from + by_ * 17 * 3;
def x18 = from + by_ * 18 * 3;
def x19 = from + by_ * 19 * 3;
def x20 = from + by_ * 20 * 3;
def x21 = from + by_ * 21 * 3;
def x22 = from + by_ * 22 * 3;
def x23 = from + by_ * 23 * 3;
def x24 = from + by_ * 24 * 3;
def x25 = from + by_ * 25 * 3;
def x26 = from + by_ * 26 * 3;
def x27 = from + by_ * 27 * 3;
def x28 = from + by_ * 28 * 3;
def x29 = from + by_ * 29 * 3;
def x30 = from + by_ * 30 * 3;
def trend0 = f_trend(smoothMovAvgType, src, x0, x0 + 1, x0 + 2);
def trend1 = f_trend(smoothMovAvgType, src, x1, x1 + 1, x1 + 2);
def trend2 = f_trend(smoothMovAvgType, src, x2, x2 + 1, x2 + 2);
def trend3 = f_trend(smoothMovAvgType, src, x3, x3 + 1, x3 + 2);
def trend4 = f_trend(smoothMovAvgType, src, x4, x4 + 1, x4 + 2);
def trend5 = f_trend(smoothMovAvgType, src, x5, x5 + 1, x5 + 2);
def trend6 = f_trend(smoothMovAvgType, src, x6, x6 + 1, x6 + 2);
def trend7 = f_trend(smoothMovAvgType, src, x7, x7 + 1, x7 + 2);
def trend8 = f_trend(smoothMovAvgType, src, x8, x8 + 1, x8 + 2);
def trend9 = f_trend(smoothMovAvgType, src, x9, x9 + 1, x9 + 2);
def trend10 = f_trend(smoothMovAvgType, src, x10, x10 + 1, x10 + 2);
def trend11 = f_trend(smoothMovAvgType, src, x11, x11 + 1, x11 + 2);
def trend12 = f_trend(smoothMovAvgType, src, x12, x12 + 1, x12 + 2);
def trend13 = f_trend(smoothMovAvgType, src, x13, x13 + 1, x13 + 2);
def trend14 = f_trend(smoothMovAvgType, src, x14, x14 + 1, x14 + 2);
def trend15 = f_trend(smoothMovAvgType, src, x15, x15 + 1, x15 + 2);
def trend16 = f_trend(smoothMovAvgType, src, x16, x16 + 1, x16 + 2);
def trend17 = f_trend(smoothMovAvgType, src, x17, x17 + 1, x17 + 2);
def trend18 = f_trend(smoothMovAvgType, src, x18, x18 + 1, x18 + 2);
def trend19 = f_trend(smoothMovAvgType, src, x19, x19 + 1, x19 + 2);
def trend20 = f_trend(smoothMovAvgType, src, x20, x20 + 1, x20 + 2);
def trend21 = f_trend(smoothMovAvgType, src, x21, x21 + 1, x21 + 2);
def trend22 = f_trend(smoothMovAvgType, src, x22, x22 + 1, x22 + 2);
def trend23 = f_trend(smoothMovAvgType, src, x23, x23 + 1, x23 + 2);
def trend24 = f_trend(smoothMovAvgType, src, x24, x24 + 1, x24 + 2);
def trend25 = f_trend(smoothMovAvgType, src, x25, x25 + 1, x25 + 2);
def trend26 = f_trend(smoothMovAvgType, src, x26, x26 + 1, x26 + 2);
def trend27 = f_trend(smoothMovAvgType, src, x27, x27 + 1, x27 + 2);
def trend28 = f_trend(smoothMovAvgType, src, x28, x28 + 1, x28 + 2);
def trend29 = f_trend(smoothMovAvgType, src, x29, x29 + 1, x29 + 2);
def trend30 = f_trend(smoothMovAvgType, src, x30, x30 + 1, x30 + 2);
def col1 = f_trend(smoothMovAvgType, src, from, from + by_, from + (2 * by_));
def col2 = f_trend(smoothMovAvgType, src, from + (3 * by_), from + (4 * by_), from + (5 * by_));
def col3 = f_trend(smoothMovAvgType, src, from + (6 * by_), from + (7 * by_), from + (8 * by_));
def col4 = f_trend(smoothMovAvgType, src, from + (9 * by_), from + (10 * by_), from + (1 * by_));
def col5 = f_trend(smoothMovAvgType, src, from + (12 * by_), from + (13 * by_), from + (14 * by_));
def col6 = f_trend(smoothMovAvgType, src, from + (15 * by_), from + (16 * by_), from + (17 * by_));
def col7 = f_trend(smoothMovAvgType, src, from + (18 * by_), from + (19 * by_), from + (20 * by_));
def col8 = f_trend(smoothMovAvgType, src, from + (21 * by_), from + (22 * by_), from + (23 * by_));
def col9 = f_trend(smoothMovAvgType, src, from + (24 * by_), from + (25 * by_), from + (26 * by_));
def col10 = f_trend(smoothMovAvgType, src, from + (27 * by_), from + (28 * by_), from + (29 * by_));
def col11 = f_trend(smoothMovAvgType, src, from + (30 * by_), from + (31 * by_), from + (32 * by_));
def col12 = f_trend(smoothMovAvgType, src, from + (33 * by_), from + (34 * by_), from + (35 * by_));
def col13 = f_trend(smoothMovAvgType, src, from + (36 * by_), from + (37 * by_), from + (38 * by_));
def col14 = f_trend(smoothMovAvgType, src, from + (39 * by_), from + (40 * by_), from + (41 * by_));
def col15 = f_trend(smoothMovAvgType, src, from + (42 * by_), from + (43 * by_), from + (44 * by_));
def col16 = f_trend(smoothMovAvgType, src, from + (45 * by_), from + (46 * by_), from + (47 * by_));
def col17 = f_trend(smoothMovAvgType, src, from + (48 * by_), from + (49 * by_), from + (50 * by_));
def col18 = f_trend(smoothMovAvgType, src, from + (51 * by_), from + (52 * by_), from + (53 * by_));
def col19 = f_trend(smoothMovAvgType, src, from + (54 * by_), from + (55 * by_), from + (56 * by_));
def col20 = f_trend(smoothMovAvgType, src, from + (57 * by_), from + (58 * by_), from + (59 * by_));
def col21 = f_trend(smoothMovAvgType, src, from + (60 * by_), from + (61 * by_), from + (62 * by_));
def col22 = f_trend(smoothMovAvgType, src, from + (63 * by_), from + (64 * by_), from + (65 * by_));
def col23 = f_trend(smoothMovAvgType, src, from + (66 * by_), from + (67 * by_), from + (68 * by_));
def col24 = f_trend(smoothMovAvgType, src, from + (69 * by_), from + (70 * by_), from + (71 * by_));
def col25 = f_trend(smoothMovAvgType, src, from + (72 * by_), from + (73 * by_), from + (74 * by_));
def col26 = f_trend(smoothMovAvgType, src, from + (75 * by_), from + (76 * by_), from + (77 * by_));
def col27 = f_trend(smoothMovAvgType, src, from + (78 * by_), from + (79 * by_), from + (80 * by_));
def col28 = f_trend(smoothMovAvgType, src, from + (81 * by_), from + (82 * by_), from + (83 * by_));
def col29 = f_trend(smoothMovAvgType, src, from + (84 * by_), from + (85 * by_), from + (86 * by_));
def col30 = f_trend(smoothMovAvgType, src, from + (87 * by_), from + (88 * by_), from + (89 * by_));
def trendSum = trend0 + trend1 + trend2 + trend3 + trend4 + trend5 + trend6 + trend7 + trend8 + trend9 + trend10 +
trend11 + trend12 + trend13 + trend14 + trend15 + trend16 + trend17 + trend18 + trend19 + trend20 +
trend21 + trend22 + trend23 + trend24 + trend25 + trend26 + trend27 + trend28 + trend29 + trend30;
def avg_trend = Average(trendSum / 31, 1);
#def col = if avg_trend > 1 then 255 else
# if avg_trend < 0 then 0 else avg_trend * 255;
def signal;
Switch (smoothingOption) {
Case "No Smoothing" :
signal = avg_trend;
Default :
signal = MovingAverage(smoothMovAvgType, avg_trend, SmoothingLength);
}
#// Scale Signal
def scaled_signal = from + (signal * (88 * by_));
#-- Col
def colR = gradient_color(scaled_signal, if from - by_ > 0 then from - by_ else 0, from + (88 * by_),
255, 0, 255, 255, 255, 255).r;
def colG = gradient_color(scaled_signal, if from - by_ > 0 then from - by_ else 0, from + (88 * by_),
255, 0, 255, 255, 255, 255).g;
def colB = gradient_color(scaled_signal, if from - by_ > 0 then from - by_ else 0, from + (88 * by_),
255, 0, 255, 255, 255, 255).b;
# plots
plot sigAvg = if !last and showSignalLine then scaled_signal else na; # "Average Signal"
plot sigMid = if !last and showSignalLine then (from + (45 * by_)) else na; # "Signal Midline"
sigAvg.SetLineWeight(3);
sigMid.SetLineWeight(2);
sigAvg.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
sigMid.SetPaintingStrategy(PaintingStrategy.DASHES);
sigAvg.AssignValueColor(CreateColor(colR, colG, colB));
sigMid.SetDefaultColor(Color.CYAN);
#// Heatmap
plot h0 = if last then na else if from - by_ > 0 then from - by_ else 0;
plot h1 = if last then na else from + by_;
plot h2 = if last then na else from + (4 * by_);
plot h3 = if last then na else from + (7 * by_);
plot h4 = if last then na else from + (10 * by_);
plot h5 = if last then na else from + (13 * by_);
plot h6 = if last then na else from + (16 * by_);
plot h7 = if last then na else from + (19 * by_);
plot h8 = if last then na else from + (22 * by_);
plot h9 = if last then na else from + (25 * by_);
plot h10 = if last then na else from + (28 * by_);
plot h11 = if last then na else from + (31 * by_);
plot h12 = if last then na else from + (34 * by_);
plot h13 = if last then na else from + (37 * by_);
plot h14 = if last then na else from + (40 * by_);
plot h15 = if last then na else from + (43 * by_);
plot h16 = if last then na else from + (46 * by_);
plot h17 = if last then na else from + (49 * by_);
plot h18 = if last then na else from + (52 * by_);
plot h19 = if last then na else from + (55 * by_);
plot h20 = if last then na else from + (58 * by_);
plot h21 = if last then na else from + (61 * by_);
plot h22 = if last then na else from + (64 * by_);
plot h23 = if last then na else from + (67 * by_);
plot h24 = if last then na else from + (70 * by_);
plot h25 = if last then na else from + (73 * by_);
plot h26 = if last then na else from + (76 * by_);
plot h27 = if last then na else from + (79 * by_);
plot h28 = if last then na else from + (82 * by_);
plot h29 = if last then na else from + (85 * by_);
plot h30 = if last then na else from + (88 * by_);
h0.AssignValueColor(if col1 == 1 then GlobalColor("hi") else if col1 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h1.AssignValueColor(if col1 == 1 then GlobalColor("hi") else if col1 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h2.AssignValueColor(if col2 == 1 then GlobalColor("hi") else if col2 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h3.AssignValueColor(if col3 == 1 then GlobalColor("hi") else if col3 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h4.AssignValueColor(if col4 == 1 then GlobalColor("hi") else if col4 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h5.AssignValueColor(if col5 == 1 then GlobalColor("hi") else if col5 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h6.AssignValueColor(if col6 == 1 then GlobalColor("hi") else if col6 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h7.AssignValueColor(if col7 == 1 then GlobalColor("hi") else if col7 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h8.AssignValueColor(if col8 == 1 then GlobalColor("hi") else if col8 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h9.AssignValueColor(if col9 == 1 then GlobalColor("hi") else if col9 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h10.AssignValueColor(if col10 == 1 then GlobalColor("hi") else if col10 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h11.AssignValueColor(if col11 == 1 then GlobalColor("hi") else if col11 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h12.AssignValueColor(if col12 == 1 then GlobalColor("hi") else if col12 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h13.AssignValueColor(if col13 == 1 then GlobalColor("hi") else if col13 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h14.AssignValueColor(if col14 == 1 then GlobalColor("hi") else if col14 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h15.AssignValueColor(if col15 == 1 then GlobalColor("hi") else if col15 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h16.AssignValueColor(if col16 == 1 then GlobalColor("hi") else if col16 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h17.AssignValueColor(if col17 == 1 then GlobalColor("hi") else if col17 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h18.AssignValueColor(if col18 == 1 then GlobalColor("hi") else if col18 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h19.AssignValueColor(if col19 == 1 then GlobalColor("hi") else if col19 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h20.AssignValueColor(if col20 == 1 then GlobalColor("hi") else if col20 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h21.AssignValueColor(if col21 == 1 then GlobalColor("hi") else if col21 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h22.AssignValueColor(if col22 == 1 then GlobalColor("hi") else if col22 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h23.AssignValueColor(if col23 == 1 then GlobalColor("hi") else if col23 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h24.AssignValueColor(if col24 == 1 then GlobalColor("hi") else if col24 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h25.AssignValueColor(if col25 == 1 then GlobalColor("hi") else if col25 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h26.AssignValueColor(if col26 == 1 then GlobalColor("hi") else if col26 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h27.AssignValueColor(if col27 == 1 then GlobalColor("hi") else if col27 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h28.AssignValueColor(if col28 == 1 then GlobalColor("hi") else if col28 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h29.AssignValueColor(if col29 == 1 then GlobalColor("hi") else if col29 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h30.AssignValueColor(if col30 == 1 then GlobalColor("hi") else if col30 == 0.5 then GlobalColor("mid") else GlobalColor("lo"));
h0.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h1.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h2.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h3.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h4.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h5.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h6.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h7.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h8.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h9.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h10.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h11.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h12.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h13.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h14.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h15.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h16.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h17.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h18.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h19.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h20.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h21.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h22.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h23.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h24.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h25.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h26.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h27.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h28.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h29.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
h30.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
#-- bar Color
def R = gradient_color(signal, 0, 1, 255, 32, 32, 22, 255, 22).r;
def G = gradient_color(signal, 0, 1, 255, 32, 32, 22, 255, 22).g;
def B = gradient_color(signal, 0, 1, 255, 32, 32, 22, 255, 22).b;
AssignPriceColor(if !colorBars then Color.CURRENT else CreateColor(r, g, b));
#CreateColor(255 - col, col, 0));
#-- Label
AddLabel(showLabel, "Trend (" + AsPercent(avg_trend) + ")", CreateColor(r, g, b));
#-- END of CODE