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