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
 

BenTen

Administrative
Staff
VIP
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
 

sritrade

New member
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!
 

OBW

Member
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
 

XeoNoX

Well-known member
VIP
@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
 

BenTen

Administrative
Staff
VIP
@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
 

raghavag2004

New member
2019 Donor
VIP
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

Top