How to count number of bars since below N-day average?

bitshift

New member
Trying to count the number of bars since the last time the 10 period avg crossed over the 20 period.
Sorry, tried to correct the title of this post, but not able?

What I have so far. Not working as expected. Plot value should be 0 while MovAvgFast < MovAvgSlow, but then grow by 1 for each bar since the last bar where MovAvgFast became > MovAvgSlow.

Code:
input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input lengthFast = 10;
input lengthSlow = 20;
input averageType = AverageType.SIMPLE;

def MovAvgFast = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod),lengthFast); 
def MovAvgSlow = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod),lengthSlow);
def Crossover = if (MovAvgFast > MovAvgSlow and MovAvgFast[1] < MovAvgSlow[1]) then 1 else 0;

# count number of bars since last time fast has crossed slow
def BarsSinceCrossUp = if (MovAvgFast > MovAvgSlow) then BarsSinceCrossUp[1] + Crossover else 0;
plot ShowBars = BarsSinceCrossUp;
 
Last edited:
Solution
Trying to count the number of bars since the last time the 10 period avg crossed over the 20 period.
Sorry, tried to correct the title of this post, but not able?

What I have so far. Not working as expected. Plot value should be 0 while MovAvgFast < MovAvgSlow, but then grow by 1 for each bar since the last bar where MovAvgFast became > MovAvgSlow.

Code:
input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input lengthFast = 10;
input lengthSlow = 20;
input averageType = AverageType.SIMPLE;

def MovAvgFast = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod),lengthFast);
def MovAvgSlow = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod),lengthSlow);
def...
Trying to count the number of bars since the last time the 10 period avg crossed over the 20 period.
Sorry, tried to correct the title of this post, but not able?

What I have so far. Not working as expected. Plot value should be 0 while MovAvgFast < MovAvgSlow, but then grow by 1 for each bar since the last bar where MovAvgFast became > MovAvgSlow.

Code:
input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input lengthFast = 10;
input lengthSlow = 20;
input averageType = AverageType.SIMPLE;

def MovAvgFast = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod),lengthFast);
def MovAvgSlow = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod),lengthSlow);
def Crossover = if (MovAvgFast > MovAvgSlow and MovAvgFast[1] < MovAvgSlow[1]) then 1 else 0;

# count number of bars since last time fast has crossed slow
def BarsSinceCrossUp = if (MovAvgFast > MovAvgSlow) then BarsSinceCrossUp[1] + Crossover else 0;
plot ShowBars = BarsSinceCrossUp;

EDIT - 22-11-30 8am , fixed
sometimes the label was black.

i added this,
if bn == 1 then 0 else

to these 2 formulas,
def xup = if bn == 1 then 0 else if ma1 crosses above ma2 then 1 else 0;
def xdwn = if bn == 1 then 0 else if ma1 crosses below ma2 then 1 else 0;


sorry about that. i think the 2nd agg had something to do with the error.
i've used formulas like those many times with no issues, but just goes to show, code should be tested before posting.


-------------------------

just wrote this on my cell. i think it will do what you want.
modified it to have 2 averages.
a colored label will show the quantity of bars since a crossing.

Code:
# bar_count_after_crossing
def bn = barnumber();
def na = double.nan;

input aggregationPeriod = AggregationPeriod.DAY;

input avg1_type = AverageType.SIMPLE;
#input avg1_price = close;
input avg1_len = 10;
def ma1 = MovingAverage(avg1_type, 
close(period = aggregationPeriod), avg1_len);

input avg2_type = AverageType.SIMPLE;
#input avg2_price = close;
input avg2_len = 20;
def ma2 = MovingAverage(avg2_type,
close(period = aggregationPeriod), avg2_len);

input show_average = yes;
plot z1 = if show_average then ma1 else na;
z1.SetDefaultColor(Color.YELLOW);
plot z2 = if show_average then ma2 else na;
z2.SetDefaultColor(Color.cyan);

def xup = if bn == 1 then 0 else if ma1 crosses above ma2 then 1 else 0;
def xdwn = if bn == 1 then 0 else if ma1 crosses below ma2 then 1 else 0;

def cnt = if bn == 1 then 0 
else if xup then 1
else if xdwn then -1
else if cnt[1] > 0 then cnt[1] + 1
else if cnt[1] < 0 then cnt[1] - 1
else cnt[1];

addlabel(1, " ", color.black);
addlabel(1,
absvalue(cnt) + " bars since crossing",
( if cnt > 0 then color.green
else if cnt < 0 then color.red
else color.gray));
#

#-------------------------------
input test_bubbles = no;
addchartbubble(test_bubbles and cnt > 0,
(ma2*0.999),
xup + "\n" +
xdwn + "\n" +
cnt
, (if absvalue(cnt) == 1 then color.yellow else color.magenta), no);

addchartbubble(test_bubbles and cnt < 0,
(ma2*1.001),
xup + "\n" +
xdwn + "\n" +
cnt
, (if absvalue(cnt) == 1 then color.yellow else color.magenta), yes);
#
 
Last edited:
Solution

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
236 Online
Create Post

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