Count how many times stock is above 200 SMA in thinkscript

sritrade

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];

AddLabel(1, if countdn < countup
            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);

xMj8hYr.png
 

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

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?

Thanks for your help again!
 
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);
AddLabel(yes,
         "No. of closes above " + avg_length + "MA in the past " + count_length + " bars = " + count_above,
         Color.ORANGE);

rNwUf6J.png
 
@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
# please keep code intact
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);

Capture.png
 
@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
# please keep code intact

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.

y322WKR.png
 
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?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
381 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