Trend Counter [BigBeluga] For ThinkOrSwim

Author states: The Trend Counter indicator is designed to help traders identify trend conditions and potential reversals by counting the number of bars within a specified period that are above or below an average price level. By smoothing and averaging these counts, the indicator provides a clear visual representation of market trends and highlights key trend changes.
hONx2Jl.png


Here is the original Tradingview code:
https://www.tradingview.com/script/J6PkxvTK-Trend-Counter-BigBeluga/


For the new ThinkOrSwim code, you must scroll down to the next post
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@samer800 would you mind taking a crack at converting this.
https://www.tradingview.com/script/J6PkxvTK-Trend-Counter-BigBeluga/

I use it on my tradingview charts it would be a good for thinkscript. Thanks for any assistance you can provide.

View attachment 23656
the code contains upper and lower study. Find them below:

Upper Study :

CSS:
#// 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

Lower Study:

CSS:
#// Indicator for TOS
#// © BigBeluga
#indicator("Trend Counter [BigBeluga]", overlay = false, max_labels_count = 500)
# Converted by Sam4Cok@Samer800    - 01/2025
declare lower;

input showCounterLabel = 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")
input TrendBarColor = no; # "Trend Bar Color", group = "Plot")

def na = Double.NaN;
def last = IsNaN(close);
def level = Min(Max(SignalsLevelValue, 0), 400);
##// ~~ 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 last[-1] then Round(sumCntr / cnt, 0) else avg[1];
#// Colors
def col1G = gradient_color(counter, level, 400, 0, 20, 17, 0, 255, 221).g;
def col1B = gradient_color(counter, level, 400, 0, 20, 17, 0, 255, 221).b;
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;

plot lvlLine = if last then na else level;
plot avgLine = if last then na else highestAll(avg);
plot trendCounter = if last then na else counter;
trendCounter.SetLineWeight(2);
trendCounter.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
trendCounter.AssignValueColor(CreateColor(0, col1G, col1B));
lvlLine.SetDefaultColor(Color.WHITE);
avgLine.SetDefaultColor(Color.GRAY);
avgLine.SetStyle(Curve.SHORT_DASH);

#// 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 counter[1] else na; # "Top Marker"
topMarker.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
topMarker.SetDefaultColor(Color.LIGHT_GRAY);

plot hiLine = if highest then highest else na;
hiLine.AssignValueColor(CreateColor(0, col2G, col2B));

#// 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
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
168 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top