#// Indicator for TOS
#// © BigBeluga
#indicator("Trend Counter [BigBeluga]", overlay = false, max_labels_count = 500)
# Converted by Sam4Cok@Samer800 - 01/2025
input TrendBarColor = no; # "Trend Bar Color", group = "Plot")
input showCounterLabel = yes;
input colorBackground = yes;
input TrendCounterLength = 20; #, "Trend Counter Length", group = "Settings")
input TrendCounterSmooth = 5; #, "Trend Counter Smooth", minval = 1, maxval = 10, group = "Settings")
input SignalsLevelValue = 10; #, "Signals Level Value", group = "Settings")
input PotentialTopLength = 200; #, "Potential Top Length", group = "Settings")
def na = Double.NaN;
def last = IsNaN(close);
def level = Min(Max(SignalsLevelValue, 0), 400);
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
DefineGlobalColor("bg1", CreateColor(0, 137, 119));
DefineGlobalColor("bg2", CreateColor(0, 98, 85));
DefineGlobalColor("bg3", CreateColor(0, 59, 51));
DefineGlobalColor("bg4", CreateColor(0, 20, 17));
##// ~~ Gradient Coloring {
Script gradient_color {
input value = 0;
input minVal = 10;
input maxVal = 400;
input loR = 0;
input loG = 59;
input loB = 51;
input hiR = 0;
input hiG = 255;
input hiB = 221;
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;
}
#// Count bars over length period above highest and lowest avg with offset loop
def counter;
def hh = Highest(high, TrendCounterLength);
def ll = Lowest(low, TrendCounterLength);
def mid = (hh + ll) / 2;
def cntr = fold offset = 0 to TrendCounterLength - 1 with p = counter[1] do
if hl2 > mid[offset] then p + 1 else 0;
#// Smooth Count and Round
counter = Round(ExpAverage(if cntr > 400 then 400 else cntr, TrendCounterSmooth), 0);
#// Avg Count and Round
def cnt = CompoundValue(1, cnt[1] + 1, 0);
def sumCntr = CompoundValue(1, sumCntr[1] + counter, 0);
def avg = if cnt!=0 then Round(sumCntr / cnt, 0) else 0;
#/ Trend Conditions based on counter bars
def cond1 = (counter > level) and (counter[1] <= level);
def cond2 = (counter < level) and (counter[1] >= level);
#// Colors
def col2G = gradient_color(counter, level, 400, 0, 51, 51, 0, 255, 255).g;
def col2B = gradient_color(counter, level, 400, 0, 51, 51, 0, 255, 255).b;
#// Potential Tops Plot
def highest = highest(counter, PotentialTopLength) * 0.99;
def crossDn = (counter < highest) and (counter[1] >= highest[1]);
plot topMarker = if crossDn[-1] then high else na; # "Top Marker"
topMarker.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
topMarker.SetDefaultColor(Color.LIGHT_GRAY);
plot sigUp = if cond1 then low else na;
plot sigDn = if cond2 then high else na;
sigUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
sigDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
sigUp.SetDefaultColor(Color.CYAN);
sigDn.SetDefaultColor(Color.MAGENTA);
#-- Lines
def top = if crossDn then high[1] else top[1];
def top_ = top != top[1];
plot topLine = if !top or (top_ - top_[1]) then na else top;
topLine.SetLineWeight(2);
topLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
topLine.AssignValueColor(CreateColor(0, col2G, col2B));
#-- BG Color
def count = if colorBackground then counter else na;
def lvl1 = count > (level + (400 - level) * 0.8);
def lvl2 = !lvl1 and (count > (level + (400 - level) * 0.6));
def lvl3 = !lvl1 and !lvl2 and (count > (level + (400 - level) * 0.4));
def lvl4 = !lvl1 and !lvl2 and !lvl3 and (count > level);
AddCloud(if lvl1 or lvl1[-1] then pos else na, neg, GlobalColor("bg1"));
AddCloud(if lvl2 or lvl2[-1] then pos else na, neg, GlobalColor("bg2"));
AddCloud(if lvl3 or lvl3[-1] then pos else na, neg, GlobalColor("bg3"));
AddCloud(if lvl4 or lvl4[-1] then pos else na, neg, GlobalColor("bg4"));
#// Trend Counter Value Plot
AddLabel(showCounterLabel, "Counter " + if counter > counter[1] then "Up(" else "Dn(" + counter + ")",
if counter > avg then Color.CYAN else CreateColor(0, 128, 128));
#-- Bar Color
AssignPriceColor(if !TrendBarColor then Color.CURRENT else
if counter > level then Color.CYAN else Color.WHITE);
#-- END of CODE