hello and welcome
my answer to this simple post turned into something interesting, so i decided to write out a longer post to explain how i decided to do what i did.
--------------------
i am not sure what i am supposed to look at in that image of 5 charts.
next time, try to use the snipping tool , to grab just a portion of the screen and to draw lines around the important section.
https://support.clio.com/hc/en-us/a...ow-to-Find-and-use-the-Snipping-Tool-on-a-PC-
do you want a horizontal line or a diagonal? i'm guessing a horizontal line
not sure if you want to keep finding instances of your formulas being true, during a continious rise, or just on the first bar?
i'm basing this on just the first bar.
you say you made a code , but didn't post it? go ahead and post what you have.
it is easier and quicker for others, to modify a code, than to recreate from scratch, what you already have.
when making a post, there is an icon in the header, </> click on that to open a code window and paste in the code.
-------------------
...Draw a line from the high [3] to the candle that closes above high[3]...
if you want to draw lines from a [3] offset, you will have to use a [-3] offset to check when that bar has the desired condition.
lines are plotted during the current bar. you can't say, go back 4 bars and plot a line.
it may be less confusing if the line starts on a condition with with an offset of 0.
i changed all of the offsets, so the primary condition ( high[3] ) is changed to be high[0] and all the others changed too, by -3.
orig
low[2] < low[3] and high[3] < close [1] or high[3] < close ;
changed so high[3] becomes high[0] , by subtracting 3 from all the offsets
low[-1] < low[0] and high[0] < close [-2] or high[0] < close[-3];
now a line can be drawn from high[0], and you don't need extra offsets when comparing the line level to the current bar.
i made 2 variables for your formulas,
def rev_up = low[-1] < low[0] and high[0] < close [-2] or high[0] < close[-3];
def rev_down = high[-1] > high[0] and low[0] > close [-2] or low[0] > close[-3];
-------------------
if i'm not sure what the values are in some variables, i add bubbles and/or vertical lines.
i wasn't sure what your formulas were detecting. by your words i expected 1 signal near a peak or valley.
so i added vertical lines to see how often your signals are true...
one of them is true on almost every bar...
so the formulas aren't finding reversals, they are detecting a price direction, up or down.
vertical lines
so then i knew i needed to find the first bar in a series of up bars (or down bars).
then from the first bar, i could find the high (or low), and use it to draw a line.
-------------------
this formula finds and holds a value for a line.
it has has several conditions. the order of the last 4 conditions is important, stop, read data, stop, and retain.
def up_line =
if bn == 1 then na #<-- since it uses [1] on bar 1 it could cause an error. so this skips bar 1.
else if down_firstbar then na #<-- a down signal will cancel the up line, if no candle has crossed the line.
else if up_firstbar then high #<-- if a first bar , read a value
else if close[1] > up_line[1] then na #<-- if the close on previous bar closed above the line, then stop drawing the line.
else up_line[1]; #<-- keep the previous value
-------------------
changed the line colors to green and red. i like the contrast better.
i left the other code lines in, for cyan and pink. just change # in front of some lines to enable/disable them.
added wedges, to show on the first bar of a series.
added arrows, to show when price crosses a line.
it is the weekend, so i didn't test the alert code. i did copy it from a working study.
Ruby:
# candle_rev_lines_01
# concept by: Epavell
# code by: halcyonguy
#https://usethinkscript.com/threads/draw-a-line-from-candle-to-candle-on-a-reversal-setup.11016/
# Draw a line from candle to candle on a reversal setup
# Epavell Apr 20, 2022
# when a candle reverses up, draw a horizontal blue line,
# from the high[3], to the candle that crosses above the line
# do the opposite for reversal down
# -------------------------------
def na = double.nan;
def bn = barnumber();
#----------------
# reversal up , rising , original
# def rev_up = low[2] < low[3] and high[3] < close [1] or high[3] < close;
# change offsets so high[3] is high[0] , so line starts on a 0 offset, the current candle
def rev_up = low[-1] < low[0] and high[0] < close [-2] or high[0] < close[-3];
# reversal down , dropping , original
# def rev_down = high[2] > high[3] and low[3] > close [1] or low[3] > close;
# change offsets so low[3] is low[0] , so line starts on a 0 offset, the current candle
def rev_down = high[-1] > high[0] and low[0] > close [-2] or low[0] > close[-3];
#----------------
# find the first bar of a series of similar signals
def up_firstbar = if bn == 1 then 0 else if !rev_up[1] and rev_up[0] then 1 else 0;
def down_firstbar = if bn == 1 then 0 else if !rev_down[1] and rev_down[0] then 1 else 0;
#----------------
# create a price level from the high[0] to the candle with a close above the price level
# used [1] so the line would draw onto the candle that crosses, and not stop before it.
def up_line = if bn == 1 then na
else if down_firstbar then na
else if up_firstbar then high
# else if isnan(up_line[1]) then na
else if close[1] > up_line[1] then na
else up_line[1];
def up_cross = if (!isnan(up_line[0]) and close[0] > up_line[1]) then 1 else 0;
# draw a blue line from the high
plot h_up = up_line;
#h_up.SetDefaultColor(Color.cyan);
h_up.SetDefaultColor(Color.green);
#h_up.setlineweight(1);
h_up.hidebubble();
#----------------
# create a price level from the low[0] to the candle with a close below the price level
# used [1] so the line would draw onto the candle that crosses, and not stop before it.
def down_line = if bn == 1 then na
else if up_firstbar then na
else if down_firstbar then low
# else if isnan(down_line[1]) then na
else if close[1] < down_line[1] then na
else down_line[1];
def down_cross = if (!isnan(down_line[0]) and close[0] < down_line[1]) then 1 else 0;
# draw a horizontal pink line from the low
plot h_down = down_line;
#h_down.SetDefaultColor(Color.pink);
h_down.SetDefaultColor(Color.red);
#h_down.setlineweight(1);
h_down.hidebubble();
#----------------
# draw wedges on 1st bar of up or down series
input show_wedge_on_first_bar = yes;
plot w_up = if show_wedge_on_first_bar and up_firstbar then 1 else 0;
w_up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
w_up.SetDefaultColor(Color.green);
w_up.setlineweight(2);
w_up.hidebubble();
plot w_down = if show_wedge_on_first_bar and down_firstbar then 1 else 0;
w_down.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_down);
w_down.SetDefaultColor(Color.red);
w_down.setlineweight(2);
w_down.hidebubble();
#----------------
# draw arrow when price crosses a line
#plot up_arrow = if !isnan(up_line2[0]) and close[0] > up_line2[1] then 1 else 0;
plot up_arrow = if up_cross then 1 else 0;
up_arrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
up_arrow.SetDefaultColor(Color.green);
up_arrow.setlineweight(3);
up_arrow.hidebubble();
#plot down_arrow
plot down_arrow = if down_cross then 1 else 0;
down_arrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
down_arrow.SetDefaultColor(Color.red);
down_arrow.setlineweight(3);
down_arrow.hidebubble();
#----------------
# enable alerts when a candle crosses a line
input enable_alerts_on_crossings = yes;
# up sound ding
Alert(up_cross and enable_alerts_on_crossings , "cross above line " + (close), alert.bar, Sound.ding);
# down sound bell
Alert(down_cross and enable_alerts_on_crossings , "cross below line " + (close), alert.bar, Sound.bell);
# ----------------------------------------------
# test stuff
input test1_vert_lines = no;
addverticalline(rev_up and test1_vert_lines, "up", color.green);
addverticalline(rev_down and test1_vert_lines, "down", color.red);
input test2_bubbles = no;
addchartbubble(test2_bubbles, low*0.99,
up_firstbar + " H\n" +
down_firstbar + " L\n" +
up_line + " $"
, color.yellow, no);
#
this may look great, but remember, the rules use an offset of [3], so some signals (arrows) won't appear until 3 bars later.
Z 5min