20 EMA break retrace scan

jo40788

New member
Plus
Hi
I was wondering if there was a way to script the following setup on the 20 EMA. I am looking for candles that break the 20 then ride higher with a retrace to the 20 but do not break it. On the move higher it breaks above those highs from when it crossed. I am trying to find a way to scan where it breaks a previous high from when it crossed over the 20 day emea. Thanks



Screenshot 2024-05-21 at 6.06.05 PM.png
 
Solution
Hi
I was wondering if there was a way to script the following setup on the 20 EMA. I am looking for candles that break the 20 then ride higher with a retrace to the 20 but do not break it. On the move higher it breaks above those highs from when it crossed. I am trying to find a way to scan where it breaks a previous high from when it crossed over the 20 day emea. Thanks



View attachment 21934


Hey, here is a script that seems a good match to the criteria that were given. A couple of notes above the code explain the details. It appears to make good signals on daily futures charts.

4C6GbBt.png

Code:
# EMAcross_PB_Breakout
# question from jo40788 on 5-21-24
# Plots arrow when price crosses above the EMA within...
Hi
I was wondering if there was a way to script the following setup on the 20 EMA. I am looking for candles that break the 20 then ride higher with a retrace to the 20 but do not break it. On the move higher it breaks above those highs from when it crossed. I am trying to find a way to scan where it breaks a previous high from when it crossed over the 20 day emea. Thanks



View attachment 21934


Hey, here is a script that seems a good match to the criteria that were given. A couple of notes above the code explain the details. It appears to make good signals on daily futures charts.

4C6GbBt.png

Code:
# EMAcross_PB_Breakout
# question from jo40788 on 5-21-24
# Plots arrow when price crosses above the EMA within the last N bars, then forms a low that comes back down to touch or get near the EMA line, and then afterward rises to a new relative high. The length and type of moving average are adjustable in the inputs.

# Note: extra rules are that the current bar forming a new high is a bullish candle (close > open), the pullback to the EMA must get within a distance from the line that is an adjustable fraction of the ATR (ATRfraction * ATR), and the cross up above the EMA can’t have been in the last 5 bars; these adjustments eliminated some bad signals. The number of past candles used for the different steps described above (the Lookbacks) are adjustable in the inputs; default values were set through observation of daily futures charts. The script strongly identifies signals where the order of events is as described above, but sequential order is not perfectly defined in the math. Empirically, it appears to work well to identify the desired breakouts.

input price = close;
input length = 20;
input DistantLookback = 14;
input MediumLookback = 12;
input NearbyLookback = 10;
input averageType = AverageType.EXPONENTIAL;
input ATRlength = 14;
input averageType2 = AverageType.WILDERS;
input ATRfraction = 0.1;

def ATR = MovingAverage(averageType2, TrueRange(high, close, low), ATRlength);
def MA = movingaverage(AverageType, price, length);
def MAcross = price crosses above MA within DistantLookback bars;
def MAcross0 = price crosses above MA;
def MAcross1 = price[1] crosses above MA[1];
def MAcross2 = price[2] crosses above MA[2];
def MAcross3 = price[3] crosses above MA[3];
def MAcross4 = price[4] crosses above MA[4];

def AboveMA = low > MA within MediumLookback bars;
def MAtouch = low <= MA + (ATRfraction * ATR) within NearbyLookback bars;
def BullishCandle = close > open;
def NewHigh = high crosses above Highest(high[1], MediumLookback);

plot CrossPBNewHigh = NewHigh and MAcross and AboveMA and MAtouch and BullishCandle and !MAcross0 and !MACross1 and !MACross2 and !MACross3 and !MACross4;

CrossPBNewHigh.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
CrossPBNewHigh.SetDefaultColor(Color.BLUE);
CrossPBNewHigh.SetLineWeight(5);
 
Last edited by a moderator:
Solution

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

# EMAcross_PB_Breakout # question from jo40788 on 5-21-24 # Plots arrow when price crosses above the EMA within the last N bars, then forms a low that comes back down to touch or get near the EMA line, and then afterward rises to a new relative high. The length and type of moving average are adjustable in the inputs. # Note: extra rules are that the current bar forming a new high is a bullish candle (close > open), the pullback to the EMA must get within a distance from the line that is an adjustable fraction of the ATR (ATRfraction * ATR), and the cross up above the EMA can’t have been in the last 5 bars; these adjustments eliminated some bad signals. The number of past candles used for the different steps described above (the Lookbacks) are adjustable in the inputs; default values were set through observation of daily futures charts. The script strongly identifies signals where the order of events is as described above, but sequential order is not perfectly defined in the math. Empirically, it appears to work well to identify the desired breakouts. input price = close; input length = 20; input DistantLookback = 14; input MediumLookback = 12; input NearbyLookback = 10; input averageType = AverageType.EXPONENTIAL; input ATRlength = 14; input averageType2 = AverageType.WILDERS; input ATRfraction = 0.1; def ATR = MovingAverage(averageType2, TrueRange(high, close, low), ATRlength); def MA = movingaverage(AverageType, price, length); def MAcross = price crosses above MA within DistantLookback bars; def MAcross0 = price crosses above MA; def MAcross1 = price[1] crosses above MA[1]; def MAcross2 = price[2] crosses above MA[2]; def MAcross3 = price[3] crosses above MA[3]; def MAcross4 = price[4] crosses above MA[4]; def AboveMA = low > MA within MediumLookback bars; def MAtouch = low <= MA + (ATRfraction * ATR) within NearbyLookback bars; def BullishCandle = close > open; def NewHigh = high crosses above Highest(high[1], MediumLookback); plot CrossPBNewHigh = NewHigh and MAcross and AboveMA and MAtouch and BullishCandle and !MAcross0 and !MACross1 and !MACross2 and !MACross3 and !MACross4; CrossPBNewHigh.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); CrossPBNewHigh.SetDefaultColor(Color.BLUE); CrossPBNewHigh.SetLineWeight(5);
Thanks @traderLK ! Its amazing how you guys just translate and whip stuff up like this! Its definitely an art. I think there are some mixed results but does give me something to work with. I looked at the following and ebay is perfect setup but the others are either closing below or not defined enough that I see (meaning me as I relate to the ebay setup which is very clear).
 

Attachments

  • SATS20day.png
    SATS20day.png
    72.4 KB · Views: 128
  • Ebay20Day.png
    Ebay20Day.png
    53.1 KB · Views: 111
  • CCCS20Day.png
    CCCS20Day.png
    53.4 KB · Views: 118
Hi
I was wondering if there was a way to script the following setup on the 20 EMA. I am looking for candles that break the 20 then ride higher with a retrace to the 20 but do not break it. On the move higher it breaks above those highs from when it crossed. I am trying to find a way to scan where it breaks a previous high from when it crossed over the 20 day emea. Thanks

i made a chart study
read the comments in the code to change it to be a scan.
can turn off the signal shapes


this checks for 4 conditions.
when all 4 are in the correct sequence, a cyan up arrow is drawn, for a buy.

sequence of events,
1. close crosses above ema20
2. find a peak and save the high. (peakline)
3. retrace , low drops below ema and the close stays above the ema.
4. look for close crossing above the peakline price.
buy = when all rules happen in sequence


Code:
#ema20_xup_dip

#https://usethinkscript.com/threads/20-ema-break-retrace-scan.18806/
#20 EMA break retrace scan
#jo40788  5/22

#i was wondering if there was a way to script the following setup on the 20 EMA. I am looking for candles that break the 20 then ride higher with a retrace to the 20 but do not break it. On the move higher it breaks above those highs from when it crossed. I am trying to find a way to scan where it breaks a previous high from when it crossed over the 20 day emea. Thanks


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

#sequence of events,
#1. close crosses above ema20
#2. find a peak and save the high. (peakline)
#3. retrace , low drops below ema and the close stays above the ema. (have a 2nd code line to compare body bottom)
#4. look for close crossing above the peakline price.

# buy = all rules happen in seq

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

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

def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));

#input bar_limit = 20;
#input retrace_near_per = 0.2;

def bodytop = max(open,close);
def bodybot = min(open,close);

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

input avg1_type = AverageType.exponential;
#input avg1_type = AverageType.Simple;
input avg1_length = 20;
def avg1 = MovingAverage(avg1_type, data, avg1_length );

def xup = close crosses above avg1;
def xdwn = close crosses below avg1;

#---------------------------------------
# peaks
#peaksvalleys_robert_03_updated
#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
#post10  robert payne

def highx = high;
def lowx = low;
input length = 4;
def offset = Min(length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], length - 1) and highx == GetValue(Highest(highx, length), -offset);
def valley = lowx < Lowest(lowx[1], length - 1) and lowx == GetValue(Lowest(lowx, length), -offset);

def peakline = if bn == 1 then 0 else if peak then high else peakline[1];

#---------------------------------------
# retrace , (drop to near ema) , lower wick crosses ema and close > ema 
#def retrace = if low < avg1 and bodybot > avg1 then 1 else 0;
def retrace = if low < avg1 and close > avg1 then 1 else 0;

#---------------------------------------
# close crosses above peak line
def abovepeakline = close crosses above peakline;

#---------------------------------------
# save the barnumbers of signals
def r1 = xup;
def r1bn = if bn == 1 then 0 else if r1 then bn else r1bn[1];
#---------------------------------------
def r2 = peak;
def r2bn = if bn == 1 then 0 else if r2 then bn else r2bn[1];
#---------------------------------------
def r3 = retrace;
def r3bn = if bn == 1 then 0 else if r3 then bn else r3bn[1];
#---------------------------------------
def r4 = abovepeakline;
def r4bn = if bn == 1 then 0 else if r4 then bn else r4bn[1];
#---------------------------------------
# are the signals in sequence?  bn's will be in order
def rulesseq = r1bn < r2bn and r2bn < r3bn and r3bn < r4bn;

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

# for a chart study, use this
def buy = r4 and rulesseq;

# for a scan study, use this
#plot buy = r4 and rulesseq;

#---------------------------------------
#---------------------------------------
# for a scan study , delete everything after this line
#---------------------------------------
#---------------------------------------

# avg line
input show_average_lines = yes;
plot zavg1 = if show_average_lines then avg1 else na;
zavg1.SetDefaultColor(Color.cyan);
zavg1.setlineweight(1);
zavg1.hidebubble();

def y = 0.0006;
# buy signal - up arrow
plot zbuy = if buy then low*(1-(2*y)) else na;
#zr4.SetPaintingStrategy(PaintingStrategy.triangles);
zbuy.SetPaintingStrategy(PaintingStrategy.arrow_up);
zbuy.SetDefaultColor(Color.cyan);
zbuy.setlineweight(4);
zbuy.hidebubble();

input show_signal_shapes = yes;
plot z1 = if show_signal_shapes and r1 then 1 else 0;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
z1.SetDefaultColor(Color.magenta);
z1.setlineweight(2);
z1.hidebubble();

plot zr2 = if show_signal_shapes and r2 then high*(1+y) else na;
zr2.SetPaintingStrategy(PaintingStrategy.points);
zr2.SetDefaultColor(Color.green);
zr2.setlineweight(2);
zr2.hidebubble();

plot zr3 = if show_signal_shapes and r3 then low*(1-y) else na;
zr3.SetPaintingStrategy(PaintingStrategy.triangles);
zr3.SetDefaultColor(Color.light_gray);
zr3.setlineweight(2);
zr3.hidebubble();

plot zr4 = if show_signal_shapes and r4 then low*(1-y) else na;
zr4.SetPaintingStrategy(PaintingStrategy.squares);
zr4.SetDefaultColor(Color.yellow);
zr4.setlineweight(3);
zr4.hidebubble();

#---------------------------------------
#---------------------------------------
# test data

# yellow during buy trade
input test1 = no;
addchartbubble(test1, low*0.997,
r1bn + "\n" +
r2bn + "\n" +
r3bn + "\n" +
r4bn + "\n" +
rulesseq
, (if rulesseq then color.yellow else color.gray), no);

input test2 = no;
addchartbubble(test2, low*0.998,
r1 + "\n" +
r2 + "\n" +
r3 + "\n" +
r4 + "\n"
, (if rulesseq then color.yellow else color.gray), no);
#

C 5min chart 5/21
buy signal , up arrow
 

Attachments

  • img1- c 5-21.JPG
    img1- c 5-21.JPG
    98.9 KB · Views: 121
i made a chart study
read the comments in the code to change it to be a scan.
can turn off the signal shapes


this checks for 4 conditions.
when all 4 are in the correct sequence, a cyan up arrow is drawn, for a buy.

sequence of events,
1. close crosses above ema20
2. find a peak and save the high. (peakline)
3. retrace , low drops below ema and the close stays above the ema.
4. look for close crossing above the peakline price.
buy = when all rules happen in sequence


Code:
#ema20_xup_dip

#https://usethinkscript.com/threads/20-ema-break-retrace-scan.18806/
#20 EMA break retrace scan
#jo40788  5/22

#i was wondering if there was a way to script the following setup on the 20 EMA. I am looking for candles that break the 20 then ride higher with a retrace to the 20 but do not break it. On the move higher it breaks above those highs from when it crossed. I am trying to find a way to scan where it breaks a previous high from when it crossed over the 20 day emea. Thanks


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

#sequence of events,
#1. close crosses above ema20
#2. find a peak and save the high. (peakline)
#3. retrace , low drops below ema and the close stays above the ema. (have a 2nd code line to compare body bottom)
#4. look for close crossing above the peakline price.

# buy = all rules happen in seq

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

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

def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));

#input bar_limit = 20;
#input retrace_near_per = 0.2;

def bodytop = max(open,close);
def bodybot = min(open,close);

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

input avg1_type = AverageType.exponential;
#input avg1_type = AverageType.Simple;
input avg1_length = 20;
def avg1 = MovingAverage(avg1_type, data, avg1_length );

def xup = close crosses above avg1;
def xdwn = close crosses below avg1;

#---------------------------------------
# peaks
#peaksvalleys_robert_03_updated
#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
#post10  robert payne

def highx = high;
def lowx = low;
input length = 4;
def offset = Min(length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], length - 1) and highx == GetValue(Highest(highx, length), -offset);
def valley = lowx < Lowest(lowx[1], length - 1) and lowx == GetValue(Lowest(lowx, length), -offset);

def peakline = if bn == 1 then 0 else if peak then high else peakline[1];

#---------------------------------------
# retrace , (drop to near ema) , lower wick crosses ema and close > ema
#def retrace = if low < avg1 and bodybot > avg1 then 1 else 0;
def retrace = if low < avg1 and close > avg1 then 1 else 0;

#---------------------------------------
# close crosses above peak line
def abovepeakline = close crosses above peakline;

#---------------------------------------
# save the barnumbers of signals
def r1 = xup;
def r1bn = if bn == 1 then 0 else if r1 then bn else r1bn[1];
#---------------------------------------
def r2 = peak;
def r2bn = if bn == 1 then 0 else if r2 then bn else r2bn[1];
#---------------------------------------
def r3 = retrace;
def r3bn = if bn == 1 then 0 else if r3 then bn else r3bn[1];
#---------------------------------------
def r4 = abovepeakline;
def r4bn = if bn == 1 then 0 else if r4 then bn else r4bn[1];
#---------------------------------------
# are the signals in sequence?  bn's will be in order
def rulesseq = r1bn < r2bn and r2bn < r3bn and r3bn < r4bn;

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

# for a chart study, use this
def buy = r4 and rulesseq;

# for a scan study, use this
#plot buy = r4 and rulesseq;

#---------------------------------------
#---------------------------------------
# for a scan study , delete everything after this line
#---------------------------------------
#---------------------------------------

# avg line
input show_average_lines = yes;
plot zavg1 = if show_average_lines then avg1 else na;
zavg1.SetDefaultColor(Color.cyan);
zavg1.setlineweight(1);
zavg1.hidebubble();

def y = 0.0006;
# buy signal - up arrow
plot zbuy = if buy then low*(1-(2*y)) else na;
#zr4.SetPaintingStrategy(PaintingStrategy.triangles);
zbuy.SetPaintingStrategy(PaintingStrategy.arrow_up);
zbuy.SetDefaultColor(Color.cyan);
zbuy.setlineweight(4);
zbuy.hidebubble();

input show_signal_shapes = yes;
plot z1 = if show_signal_shapes and r1 then 1 else 0;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
z1.SetDefaultColor(Color.magenta);
z1.setlineweight(2);
z1.hidebubble();

plot zr2 = if show_signal_shapes and r2 then high*(1+y) else na;
zr2.SetPaintingStrategy(PaintingStrategy.points);
zr2.SetDefaultColor(Color.green);
zr2.setlineweight(2);
zr2.hidebubble();

plot zr3 = if show_signal_shapes and r3 then low*(1-y) else na;
zr3.SetPaintingStrategy(PaintingStrategy.triangles);
zr3.SetDefaultColor(Color.light_gray);
zr3.setlineweight(2);
zr3.hidebubble();

plot zr4 = if show_signal_shapes and r4 then low*(1-y) else na;
zr4.SetPaintingStrategy(PaintingStrategy.squares);
zr4.SetDefaultColor(Color.yellow);
zr4.setlineweight(3);
zr4.hidebubble();

#---------------------------------------
#---------------------------------------
# test data

# yellow during buy trade
input test1 = no;
addchartbubble(test1, low*0.997,
r1bn + "\n" +
r2bn + "\n" +
r3bn + "\n" +
r4bn + "\n" +
rulesseq
, (if rulesseq then color.yellow else color.gray), no);

input test2 = no;
addchartbubble(test2, low*0.998,
r1 + "\n" +
r2 + "\n" +
r3 + "\n" +
r4 + "\n"
, (if rulesseq then color.yellow else color.gray), no);
#

C 5min chart 5/21
buy signal , up arrow
Thanks @halcyonguy. I am still doing something incorrect. My scan does not show any results. Is this the right code for scans? I also only swing trade so where do you change for 1,4,day, and week? I see it says 5min. Thanks again
 
Thanks @halcyonguy. I am still doing something incorrect. My scan does not show any results. Is this the right code for scans? I also only swing trade so where do you change for 1,4,day, and week? I see it says 5min. Thanks again

#ema20_xup_dip

#https://usethinkscript.com/threads/20-ema-break-retrace-scan.18806/
#20 EMA break retrace scan
#jo40788 5/22

#i was wondering if there was a way to script the following setup on the 20 EMA. I am looking for candles that break the 20 then ride higher with a retrace to the 20 but do not break it. On the move higher it breaks above those highs from when it crossed. I am trying to find a way to scan where it breaks a previous high from when it crossed over the 20 day emea. Thanks


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

#sequence of events,
#1. close crosses above ema20
#2. find a peak and save the high. (peakline)
#3. retrace , low drops below ema and the close stays above the ema. (have a 2nd code line to compare body bottom)
#4. look for close crossing above the peakline price.

# buy = all rules happen in seq

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

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

def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));

#input bar_limit = 20;
#input retrace_near_per = 0.2;

def bodytop = max(open,close);
def bodybot = min(open,close);

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

input avg1_type = AverageType.exponential;
#input avg1_type = AverageType.Simple;
input avg1_length = 20;
def avg1 = MovingAverage(avg1_type, data, avg1_length );

def xup = close crosses above avg1;
def xdwn = close crosses below avg1;

#---------------------------------------
# peaks
#peaksvalleys_robert_03_updated
#https://usethinkscript.com/threads/...y-demand-zones-for-thinkorswim.172/#post-7048
#post10 robert payne

def highx = high;
def lowx = low;
input length = 4;
def offset = Min(length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], length - 1) and highx == GetValue(Highest(highx, length), -offset);
def valley = lowx < Lowest(lowx[1], length - 1) and lowx == GetValue(Lowest(lowx, length), -offset);

def peakline = if bn == 1 then 0 else if peak then high else peakline[1];

#---------------------------------------
# retrace , (drop to near ema) , lower wick crosses ema and close > ema
#def retrace = if low < avg1 and bodybot > avg1 then 1 else 0;
def retrace = if low < avg1 and close > avg1 then 1 else 0;

#---------------------------------------
# close crosses above peak line
def abovepeakline = close crosses above peakline;

#---------------------------------------
# save the barnumbers of signals
def r1 = xup;
def r1bn = if bn == 1 then 0 else if r1 then bn else r1bn[1];
#---------------------------------------
def r2 = peak;
def r2bn = if bn == 1 then 0 else if r2 then bn else r2bn[1];
#---------------------------------------
def r3 = retrace;
def r3bn = if bn == 1 then 0 else if r3 then bn else r3bn[1];
#---------------------------------------
def r4 = abovepeakline;
def r4bn = if bn == 1 then 0 else if r4 then bn else r4bn[1];
#---------------------------------------
# are the signals in sequence? bn's will be in order
def rulesseq = r1bn < r2bn and r2bn < r3bn and r3bn < r4bn;

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

# for a scan study, use this
plot buy = r4 and rulesseq;
 
#ema20_xup_dip

#https://usethinkscript.com/threads/20-ema-break-retrace-scan.18806/
#20 EMA break retrace scan
#jo40788 5/22

#i was wondering if there was a way to script the following setup on the 20 EMA. I am looking for candles that break the 20 then ride higher with a retrace to the 20 but do not break it. On the move higher it breaks above those highs from when it crossed. I am trying to find a way to scan where it breaks a previous high from when it crossed over the 20 day emea. Thanks


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

#sequence of events,
#1. close crosses above ema20
#2. find a peak and save the high. (peakline)
#3. retrace , low drops below ema and the close stays above the ema. (have a 2nd code line to compare body bottom)
#4. look for close crossing above the peakline price.

# buy = all rules happen in seq

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

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

def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));

#input bar_limit = 20;
#input retrace_near_per = 0.2;

def bodytop = max(open,close);
def bodybot = min(open,close);

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

input avg1_type = AverageType.exponential;
#input avg1_type = AverageType.Simple;
input avg1_length = 20;
def avg1 = MovingAverage(avg1_type, data, avg1_length );

def xup = close crosses above avg1;
def xdwn = close crosses below avg1;

#---------------------------------------
# peaks
#peaksvalleys_robert_03_updated
#https://usethinkscript.com/threads/...y-demand-zones-for-thinkorswim.172/#post-7048
#post10 robert payne

def highx = high;
def lowx = low;
input length = 4;
def offset = Min(length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], length - 1) and highx == GetValue(Highest(highx, length), -offset);
def valley = lowx < Lowest(lowx[1], length - 1) and lowx == GetValue(Lowest(lowx, length), -offset);

def peakline = if bn == 1 then 0 else if peak then high else peakline[1];

#---------------------------------------
# retrace , (drop to near ema) , lower wick crosses ema and close > ema
#def retrace = if low < avg1 and bodybot > avg1 then 1 else 0;
def retrace = if low < avg1 and close > avg1 then 1 else 0;

#---------------------------------------
# close crosses above peak line
def abovepeakline = close crosses above peakline;

#---------------------------------------
# save the barnumbers of signals
def r1 = xup;
def r1bn = if bn == 1 then 0 else if r1 then bn else r1bn[1];
#---------------------------------------
def r2 = peak;
def r2bn = if bn == 1 then 0 else if r2 then bn else r2bn[1];
#---------------------------------------
def r3 = retrace;
def r3bn = if bn == 1 then 0 else if r3 then bn else r3bn[1];
#---------------------------------------
def r4 = abovepeakline;
def r4bn = if bn == 1 then 0 else if r4 then bn else r4bn[1];
#---------------------------------------
# are the signals in sequence? bn's will be in order
def rulesseq = r1bn < r2bn and r2bn < r3bn and r3bn < r4bn;

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

# for a scan study, use this
plot buy = r4 and rulesseq;
yes
its correct
your formulas rarely have a signal, so the chances of a signal happening on the current bar are extremely rare.

here is a mod,
i added a formula to look and see if a signal happened in the past 100 bars.
run it as a lower chart study. experiment with different numbers for this,
input past_bars = 100;
find a symbol that is 1 on last bar. remember chart time setting.
copy this code in scanner. set scan to same time. its a drop down at the top of window. turn off ext hours.
the same symbol that was 1 in lower should show up in scan.

after verifying the symbol is in a scan , change past_bars to what you want


Code:
#ema20_xup_dip_scan

#ema20_xup_dip
#https://usethinkscript.com/threads/20-ema-break-retrace-scan.18806/
#20 EMA break retrace scan
#jo40788 5/22

#i was wondering if there was a way to script the following setup on the 20 EMA. I am looking for candles that break the 20 then ride higher with a retrace to the 20 but do not break it. On the move higher it breaks above those highs from when it crossed. I am trying to find a way to scan where it breaks a previous high from when it crossed over the 20 day emea. Thanks


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

#sequence of events,
#1. close crosses above ema20
#2. find a peak and save the high. (peakline)
#3. retrace , low drops below ema and the close stays above the ema. (have a 2nd code line to compare body bottom)
#4. look for close crossing above the peakline price.

# buy = all rules happen in seq

#-----------------------
declare lower;

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

def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));

#input bar_limit = 20;
#input retrace_near_per = 0.2;

def bodytop = max(open,close);
def bodybot = min(open,close);

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

input avg1_type = AverageType.exponential;
#input avg1_type = AverageType.Simple;
input avg1_length = 20;
def avg1 = MovingAverage(avg1_type, data, avg1_length );

def xup = close crosses above avg1;
def xdwn = close crosses below avg1;

#---------------------------------------
# peaks
#peaksvalleys_robert_03_updated
#https://usethinkscript.com/threads/...y-demand-zones-for-thinkorswim.172/#post-7048
#post10 robert payne

def highx = high;
def lowx = low;
input length = 4;
def offset = Min(length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], length - 1) and highx == GetValue(Highest(highx, length), -offset);
def valley = lowx < Lowest(lowx[1], length - 1) and lowx == GetValue(Lowest(lowx, length), -offset);

def peakline = if bn == 1 then 0 else if peak then high else peakline[1];

#---------------------------------------
# retrace , (drop to near ema) , lower wick crosses ema and close > ema
#def retrace = if low < avg1 and bodybot > avg1 then 1 else 0;
def retrace = if low < avg1 and close > avg1 then 1 else 0;

#---------------------------------------
# close crosses above peak line
def abovepeakline = close crosses above peakline;

#---------------------------------------
# save the barnumbers of signals
def r1 = xup;
def r1bn = if bn == 1 then 0 else if r1 then bn else r1bn[1];
#---------------------------------------
def r2 = peak;
def r2bn = if bn == 1 then 0 else if r2 then bn else r2bn[1];
#---------------------------------------
def r3 = retrace;
def r3bn = if bn == 1 then 0 else if r3 then bn else r3bn[1];
#---------------------------------------
def r4 = abovepeakline;
def r4bn = if bn == 1 then 0 else if r4 then bn else r4bn[1];
#---------------------------------------
# are the signals in sequence? bn's will be in order
def rulesseq = r1bn < r2bn and r2bn < r3bn and r3bn < r4bn;

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

# for a scan study, use this
# not many signals occur. so stretch a signal over many bars.
# add a formula to find a signal within the past 100 bars
input past_bars = 100;
def b1 = if sum((r4 and rulesseq), past_bars) > 0 then 1 else 0;
plot buy = b1;
#
 

Attachments

  • scan1-c.JPG
    scan1-c.JPG
    24.9 KB · Views: 98

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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