Scan for 5EMA crossing Previous Day High low

kabira

Member
VIP
I tried this can someone tell me what is wrong on this please. I am not getting the desired results. I am trying to find the stocks 5ema crossing previous day high or low on a 5min tf


This is my study name PreviousDayHigh code is below

def regularSessionOpen = SecondsTillTime(930) == 0;
def regularSessionClose = SecondsTillTime(1559) == 0;

rec trackHighestHigh = if regularSessionOpen then high else if high > trackHighestHigh[1] then high else trackHighestHigh[1];

rec trackPreviousDayHigh = if regularSessionClose then trackHighestHigh else trackPreviousDayHigh[1];

plot signal = close > trackPreviousDayHigh;

This is my Scan code on a 5min tf
MovAvgExponential("length" = 5)."AvgExp" crosses PreviousDayHigh ()

@MerryDay or @BenTen or @rad14733 any advise to this script please.
 
Last edited:
I tried this can someone tell me what is wrong on this please. I am not getting the desired results. I am trying to find the stocks 5ema crossing previous day high or low on a 5min tf


This is my study name PreviousDayHigh code is below

def regularSessionOpen = SecondsTillTime(930) == 0;
def regularSessionClose = SecondsTillTime(1559) == 0;

rec trackHighestHigh = if regularSessionOpen then high else if high > trackHighestHigh[1] then high else trackHighestHigh[1];

rec trackPreviousDayHigh = if regularSessionClose then trackHighestHigh else trackPreviousDayHigh[1];

plot signal = close > trackPreviousDayHigh;

This is my Scan code on a 5min tf
MovAvgExponential("length" = 5)."AvgExp" crosses PreviousDayHigh ()

@MerryDay or @BenTen or @rad14733 any advise to this script please.


i don't scan, so i don't make scans.
this might work

if the 2nd agg time period doesn't overlap the comparing period , you can collect the 2nd agg data with other methods and hold it.

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

scan code
i made a watchlist study and changed it to be a possible scan code


Code:
# emaprev

# from
# scan_prev_day_ema5_00b_lower

# https://usethinkscript.com/threads/scan-for-5ema-crossing-previous-day-high-low.14370/
# Scan for 5EMA crossing Previous Day High low

#----------------------------------

def na = double.nan;
def bn = barnumber();
def big = 99999;

# last bar  ( most recent)
#def lastbar = !isnan(close[0]) and isnan(close[-1]);

def prevday = (getday() == (getlastday() - 1));
def currentday = (getday() == getlastday());


def phi = if bn == 1 then 0
 else if prevday and high > phi[1] then high
 else phi[1];

def plo = if bn == 1 then big
 else if prevday and low < plo[1] then low
 else plo[1];

#------------------------------
def price = close;
input ma1_len = 5;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

#------------------------------

# compare ma1 to prev day hi lo levels
def xup = (currentday and (ma1 > phi) and (ma1[1] < phi));
def xdwn = (currentday and (ma1 < plo) and (ma1[1] > plo));

#------------------------------

# bars since crossing
def upxbars = if bn == 1 then big
 else if isnan(close) then upxbars[1]
 else if xup then 1
 else if currentday and upxbars[1] > 0 then (upxbars[1] + 1)
 else upxbars[1];

def dwnxbars = if bn == 1 then big
 else if isnan(close) then dwnxbars[1]
 else if xdwn then 1
 else if currentday and dwnxbars[1] > 0 then (dwnxbars[1] + 1)
 else dwnxbars[1];

def m = min((if upxbars == 0 then big else upxbars), (if dwnxbars == 0 then big else dwnxbars));

def isrecentup = if upxbars == 0 and dwnxbars == 0 then 0
 else if upxbars < dwnxbars and upxbars > 0 then 1
 else if dwnxbars < upxbars and dwnxbars > 0 then -1
 else 0;

#------------------------------
#------------------------------

#  output for column
#addlabel(1, (if isrecentup == 1 then upxbars else if isrecentup == -1 then dwnxbars else 0), color.black);
#plot z = (if isrecentup == 1 then upxbars else if isrecentup == -1 then dwnxbars else 0);
#z.setdefaultcolor(color.black);

assignbackgroundcolor((if isrecentup == 1 then color.green else if isrecentup == -1 then color.red else color.gray));

#-----------------

# possible scan code
# disable the plot above
plot w = (if isrecentup != 0 then 1 else 0);
#


=====================



this is a watchlist study
that shows the quantity of bars since the ema crossed over the previous day high or low. if no crossing then it is 0.
if it crossed the high then the cell is green, if it crossed the low, it is red. no crossing is gray.

watchlist study

zemaprevd
http://tos.mx/WmQQZEP

Code:
# zemaprevd

# from
# scan_prev_day_ema5_00b_lower

# https://usethinkscript.com/threads/scan-for-5ema-crossing-previous-day-high-low.14370/
# Scan for 5EMA crossing Previous Day High low

#----------------------------------

def na = double.nan;
def bn = barnumber();
def big = 99999;

# last bar  ( most recent)
#def lastbar = !isnan(close[0]) and isnan(close[-1]);

def prevday = (getday() == (getlastday() - 1));
def currentday = (getday() == getlastday());


def phi = if bn == 1 then 0
 else if prevday and high > phi[1] then high
 else phi[1];

def plo = if bn == 1 then big
 else if prevday and low < plo[1] then low
 else plo[1];

#------------------------------
def price = close;
input ma1_len = 5;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

#------------------------------

# compare ma1 to prev day hi lo levels
def xup = (currentday and (ma1 > phi) and (ma1[1] < phi));
def xdwn = (currentday and (ma1 < plo) and (ma1[1] > plo));

#------------------------------

# bars since crossing
def upxbars = if bn == 1 then big
 else if isnan(close) then upxbars[1]
 else if xup then 1
 else if currentday and upxbars[1] > 0 then (upxbars[1] + 1)
 else upxbars[1];

def dwnxbars = if bn == 1 then big
 else if isnan(close) then dwnxbars[1]
 else if xdwn then 1
 else if currentday and dwnxbars[1] > 0 then (dwnxbars[1] + 1)
 else dwnxbars[1];


def m = min((if upxbars == 0 then big else upxbars), (if dwnxbars == 0 then big else dwnxbars));


def isrecentup = if upxbars == 0 and dwnxbars == 0 then 0
 else if upxbars < dwnxbars and upxbars > 0 then 1
 else if dwnxbars < upxbars and dwnxbars > 0 then -1
 else 0;



#------------------------------
#------------------------------


#addlabel(1, (if isrecentup == 1 then upxbars else if isrecentup == -1 then dwnxbars else 0), color.black);

plot z = (if isrecentup == 1 then upxbars else if isrecentup == -1 then dwnxbars else 0);
z.setdefaultcolor(color.black);

assignbackgroundcolor((if isrecentup == 1 then color.green else if isrecentup == -1 then color.red else color.gray));

#

Y3N2JN5.jpg


=====================


this is an upper study i used for testing and verifying the formulas


Code:
# scan_prev_day_ema5_00b_upper

# https://usethinkscript.com/threads/scan-for-5ema-crossing-previous-day-high-low.14370/
# Scan for 5EMA crossing Previous Day High low


#----------------------------------


#declare lower;

def na = double.nan;
def bn = barnumber();
def big = 99999;

# last bar  ( most recent)
#def lastbar = !isnan(close[0]) and isnan(close[-1]);

def prevday = (getday() == (getlastday() - 1));
def currentday = (getday() == getlastday());


def phi = if bn == 1 then 0
 else if prevday and high > phi[1] then high
 else phi[1];

def plo = if bn == 1 then big
 else if prevday and low < plo[1] then low
 else plo[1];

#------------------------------
def price = close;
input ma1_len = 5;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

#------------------------------

#def xup = currentday and (ma1 crosses above phi);
#def xdwn = currentday and (ma1 crosses below plo);

# compare ma1 to prev day hi lo levels
def xup = (currentday and (ma1 > phi) and (ma1[1] < phi));
def xdwn = (currentday and (ma1 < plo) and (ma1[1] > plo));

#------------------------------

# bars since crossing
def upxbars = if bn == 1 then big
 else if isnan(close) then upxbars[1]
 else if xup then 1
 else if currentday and upxbars[1] > 0 then (upxbars[1] + 1)
 else upxbars[1];

def dwnxbars = if bn == 1 then big
 else if isnan(close) then dwnxbars[1]
 else if xdwn then 1
 else if currentday and dwnxbars[1] > 0 then (dwnxbars[1] + 1)
 else dwnxbars[1];


def m = min((if upxbars == 0 then big else upxbars), (if dwnxbars == 0 then big else dwnxbars));


def isrecentup = if upxbars == 0 and dwnxbars == 0 then 0
 else if upxbars < dwnxbars and upxbars > 0 then 1
 else if dwnxbars < upxbars and dwnxbars > 0 then -1
 else 0;


#------------------------------
#------------------------------


addlabel(1, (if isrecentup == 1 then upxbars else if isrecentup == -1 then dwnxbars else 0), (if isrecentup == 1 then color.green else if isrecentup == -1 then color.red else color.gray));

# prev hi lo
plot zphi = if phi > 0 then phi else na;
plot zplo = if plo > 0 and plo < big then plo else na;
zphi.setdefaultcolor(color.white);
zplo.setdefaultcolor(color.white);


#------------------------------


input show_avg_line = yes;
plot z1 = if show_avg_line then ma1 else na;
z1.setdefaultcolor(color.yellow);
z1.hidebubble();


#------------------------------


# plots


def t = upxbars > 0 or dwnxbars > 0;

addchartbubble(0, low*0.998,
upxbars + "\n" +
dwnxbars
, (if t then color.yellow else color.gray), no);



plot zup = if xup then phi else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zup.SetDefaultColor(Color.green);
zup.setlineweight(2);
zup.hidebubble();


plot zdwn = if xdwn then plo else na;
zdwn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zdwn.SetDefaultColor(Color.red);
zdwn.setlineweight(1);
zdwn.hidebubble();

#
 
Last edited:

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

Thread starter Similar threads Forum Replies Date
H Scan for RSI>60 and MACD crossover +more Questions 0
S scan help for relative volume Questions 0
A Engulfing Candle Scan Questions 0
D Power X Scan Questions 0
A Bull/Bear 180 Scan Questions 0

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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