Count how many times stock is above 200 SMA in thinkscript

New member
Anyone has a thinkscript to identify "how many times a stock is closed above SMA 200 line within the last 30 days"?

Code:
``````# Bars Since MA Crossover
# blt
# 6.21.2016

# Displays bar number counting from 1) a moving average cross up and 2) cross down

input length1 = 8;
input length2 = 21;
input avgtyp  = AverageType.EXPONENTIAL;

plot  ma1     = MovingAverage(avgtyp, close, length1);
plot  ma2     = MovingAverage(avgtyp, close, length2);

def countdn = if ma1 crosses below ma2
then BarNumber()
else countdn[1];
def countup = if  ma1 crosses above ma2
then BarNumber()
else countup[1];

then "Bars since last cross of MA1 above MA2 = " + (BarNumber() - countup)
else "Bars since last cross of MA1 below MA2 = " + (BarNumber() - countdn) ,
if countup > countdn
then Color.GREEN
else Color.RED);
# End Study``````

Here you go:
• Label added to show the total count.
• Down arrows (signals) to point out candles closing above 200 SMA.
Code:
``````# Count total candles closing above a specific moving average
# Assembled by BenTen at UseThinkScript.com

input price = close;
input length = 200;
input displace = 0;

def SMA = Average(price[-displace], length);
def aboveMA = close > SMA;

#def count = if aboveMA[1] then 1 else count[1] + 1;

plot signal = aboveMA;
signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

def countCondition = aboveMA;
rec countMA = if countCondition then countMA[1] + 1 else countMA[1];
AddLabel(yes, Concat("Total Close Above 200 SMA: ", countMA), Color.WHITE);``````

Hey BenTen,

Thank you so much and you are wonderful! Solution is almost reached, but not fully, as I want to limit the scan for last 30 days only and I wan that value as the label. I would like to display the label with the number of times a stock price is closed above SMA 200 line with in the last 30 days. It looks it is showing me the total count. Can you please help me with this?

Hi,

I would like to count the bars that are above the 20 MA for the past 21 bars.

Thanks

Ruby:
``````# Example: count number of candles closing above moving average in the past N bars.
# Robert Payne
# funtwiththinkscript.com

input avg_length = 13;
input count_length = 30;
def avg = Average(close, avg_length);
def count_above = Sum(close > avg, count_length);
"No. of closes above " + avg_length + "MA in the past " + count_length + " bars = " + count_above,
Color.ORANGE);``````

I want to be able to count the number of day's since the price crossed the SMA.

Or since the last time it cross above the SMA, how many candle there is.
Thanks

Last edited:
@CapitaineFlam88 Remember to hit the thumbs up button if you found this post useful.

Number of Consecutive bars since Price last crossed the Moving Average

Code:
``````# Number of Consecutive bars since Price last crossed the Moving Average
# by XeoNoX via usethinkscript.com
def price = close;
input length = 9;
def displace = 0;
plot SMA = Average(price[-displace], length);
def nan = Double.NaN;
def barUp = (high>sma) and (low>sma);
def barDown = (high<sma) and ( low< sma);
def barUpCount = CompoundValue(1, if barUp then barUpCount[1] + 1 else 0, 0);
def pBarUpCount = if barUpCount > 0 then barUpCount else nan;
def barDownCount = CompoundValue(1, if barDown then barDownCount[1] - 1 else 0, 0);
def pBarDownCount = if barDownCount < 0 then barDownCount else nan;
AddLabel(yes, "Consecutive Bars Since Crossing SMA" + length + ": "+(" " + (Round(barDownCount + barUpCount, 1))), if barUpCount >= 1 then Color.GREEN else if barDownCount <= -1 then Color.RED else Color.GRAY);``````

@XeoNoX Thanks. Is there a way to transform it into a scan?

I want to be able to choose the number of days since the SMA (green) was crossed.

@CapitaineFlam88 Add the following code as a new indicator

Code:
``````# Number of Consecutive bars since Price last crossed the Moving Average
# by XeoNoX via usethinkscript.com

declare lower;

def price = close;
input length = 9;
def displace = 0;
def SMA = Average(price[-displace], length);
def nan = Double.NaN;
def barUp = (high>sma) and (low>sma);
def barDown = (high<sma) and ( low< sma);
def barUpCount = CompoundValue(1, if barUp then barUpCount[1] + 1 else 0, 0);
def pBarUpCount = if barUpCount > 0 then barUpCount else nan;
def barDownCount = CompoundValue(1, if barDown then barDownCount[1] - 1 else 0, 0);
def pBarDownCount = if barDownCount < 0 then barDownCount else nan;
plot data = (Round(barDownCount + barUpCount, 1));``````

Then, set you a condition like this for your scanner. In this example, I'm looking for stocks with more than 5 bars since last crossover.

Can we use it as scanner? when candle above 20EMA in 15min or 5min & 20ema cross 50ema kind of filer?

Another Requirement is candle hold above 20ema,50ema,200 ema above into watch list column for 5min or 15min kind of time frame just True to false or candle count like BTD?

@raghavag2004 yes you can and its possible, you can just stack studies in the conditional wizard. learn to use the conditional wizard similar to what benten posted above.

Not the exact question you're looking for?

87k+ Posts
381 Online

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?