# Set alert on reversal retrace

#### Gordon

##### New member
Usethinkscript is awesome!
I am trying to alert on reversal undercut retrace:

Code:
``````def body_vs_close_percent = Round((close - open) / close, 4)* 100;
def upsideReversal = if body_vs_close_percent[1] > .6 and (body_vs_close_percent[1] + body_vs_close_percent) <= -.01 then 1 else 0;
def reversal_low = if upsideReversal then low else reversal_low[1];
def undercut_percent = if Round((reversal_low - low) / reversal_low,4) *100 > .3;
def retrace_percent = Round((high - reversal_low) / reversal_low,4) *100 > .6;``````

????
plot signal = if upsideReversal occurs and
undercut_percent occurs after upsideReversal within 5 bars and
retrace_percent occurs after undercut_percent within 10 bars
then 1 else 0;
????
Any help is greatly appreciated.

Solution
Usethinkscript is awesome!
I am trying to alert on reversal undercut retrace:

Code:
``````def body_vs_close_percent = Round((close - open) / close, 4)* 100;
def upsideReversal = if body_vs_close_percent[1] > .6 and (body_vs_close_percent[1] + body_vs_close_percent) <= -.01 then 1 else 0;
def reversal_low = if upsideReversal then low else reversal_low[1];
def undercut_percent = if Round((reversal_low - low) / reversal_low,4) *100 > .3;
def retrace_percent = Round((high - reversal_low) / reversal_low,4) *100 > .6;``````

????
plot signal = if upsideReversal occurs and
undercut_percent occurs after upsideReversal within 5 bars and
retrace_percent occurs after undercut_percent within 10 bars...
Usethinkscript is awesome!
I am trying to alert on reversal undercut retrace:

Code:
``````def body_vs_close_percent = Round((close - open) / close, 4)* 100;
def upsideReversal = if body_vs_close_percent[1] > .6 and (body_vs_close_percent[1] + body_vs_close_percent) <= -.01 then 1 else 0;
def reversal_low = if upsideReversal then low else reversal_low[1];
def undercut_percent = if Round((reversal_low - low) / reversal_low,4) *100 > .3;
def retrace_percent = Round((high - reversal_low) / reversal_low,4) *100 > .6;``````

????
plot signal = if upsideReversal occurs and
undercut_percent occurs after upsideReversal within 5 bars and
retrace_percent occurs after undercut_percent within 10 bars
then 1 else 0;
????
Any help is greatly appreciated.

i created formulas for the last part that you have in ???

i'm not sure what you want, but guessing from the plot 0 or 1, you want a scanner.

i don't scan, so i don't make scanners.
i am making a lower study, that might work.
with a lower study, i can see what is going on and debug the math.

for debugging, i add bubbles to display some values.

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

i converted this text,

#plot signal = if upsideReversal occurs and
#undercut_percent occurs after upsideReversal within 5 bars and
#retrace_percent occurs after undercut_percent within 10 bars
#then 1 else 0;

into this,

plot signal = if upsideReversal and t2 and t4 then 1 else 0;

i added this bubble to display the variables for the plot formula.
i looked at many stocks and never saw a trigger , with 5 and 10 , for within bars.

input test1 = yes;
addchartbubble(test1 and !isnan(close), 0,
upsideReversal + "\n" +
t2 + "\n" +
t4 + "\n" +
signal
, (if signal then color.yellow else color.gray), no);

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

when i changed the defaults,
input undercut_bars = 5;
to 22

input retrace_bars = 10;
to 33

then i saw a couple of triggers on a AAPL 1hr chart

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

i am guessing you will have to experiment with all the constants to figure out viable settings.

there are 3 different test bubbles, that display different values, to help debug.

this is a lower chart study, that is 0 or 1.

Code:
``````# rev_undercut_retrace_00

#https://usethinkscript.com/threads/set-alert-on-reversal-retrace.14741/
#Set alert on reversal retrace
#Gordon  Mar 5, 2023

#I am trying to alert on reversal undercut retrace:

declare lower;

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

def body_vs_close_percent = Round((close - open) / close, 4) * 100;

input bodyclsper = 0.6;
def upsideReversal = if body_vs_close_percent[1] > bodyclsper and (body_vs_close_percent[1] + body_vs_close_percent) <= -.01 then 1 else 0;
def reversal_low = if upsideReversal then low else reversal_low[1];

input undercutper = 0.3;
def undercut_percent = if Round((reversal_low - low) / reversal_low, 4) *100 > undercutper then 1 else 0;

input retrace_per = 0.6;
def retrace_percent = Round((high - reversal_low) / reversal_low, 4) * 100 > retrace_per;

#---------------------------------
# make formulas for these rules

#plot signal = if upsideReversal occurs and
#undercut_percent occurs after upsideReversal within 5 bars and
#retrace_percent occurs after undercut_percent within 10 bars
#then 1 else 0;
#---------------------------------
#undercut_percent occurs after upsideReversal, within 5 bars and

input undercut_bars = 5;
def upside_revbn = if upsideReversal then bn else 0;
def upside_revbn_last = highestall(upside_revbn);

def undercut_perbn = if undercut_percent then bn else 0;
def underbn_last = highestall(undercut_perbn);

def t1 = (underbn_last - upside_revbn_last);
def t2 = if t1 >=0 and t1 <= undercut_bars then 1 else 0;

#---------------------------------
#retrace_percent occurs after undercut_percent, within 10 bars

input retrace_bars = 10;
def retrace_bn = if retrace_percent then bn else 0;
def retrace_bn_last = highestall(retrace_bn);

def t3 = (retrace_bn_last - underbn_last);
def t4 = if t3 >=0 and t3 <= retrace_bars then 1 else 0;
#---------------------------------

plot signal = if upsideReversal and t2 and t4 then 1 else 0;

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

input test1 = yes;
addchartbubble(test1 and !isnan(close), 0,
upsideReversal + "\n" +
t2 + "\n" +
t4 + "\n" +
signal
, (if signal then color.yellow else color.gray), no);

input test2 = no;
addchartbubble(test2 and !isnan(close), 0,
body_vs_close_percent + "\n" +
upsideReversal + "\n" +
reversal_low + "\n" +
undercut_percent + "\n" +
retrace_percent + "\n" +
"--"  + "\n" +
upside_revbn_last  + "\n" +
underbn_last + "\n" +
retrace_bn_last + "\n"
, (if signal then color.yellow else color.gray), no);

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

#def reversal_low = if upsideReversal then low else reversal_low[1];
#def undercutper = 0.3;
#def undercut_percent = if Round((reversal_low - low) / reversal_low, 4) *100 > undercutper then 1 else 0;

input test3 = no;
addchartbubble(test3 and !isnan(close), 0,
reversal_low + "\n" +
undercutper + "\n" +
(Round((reversal_low - low) / reversal_low, 4) *100) + "\n" +
undercut_percent
, color.yellow, no);

#``````

AAPL 1HR
input undercut_bars = 22
input retrace_bars = 33

Last edited:
Thank you, Great example of how to use bar numbers for ~pseusdo~ recursion. I couldn't figure it out. Expert programming on custom request. You deserve compensation for sharing your hard earned expertise! usethinkscript should have a tip jar....

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
501 Online

## 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.