Draw a line from candle to candle on a reversal setup

Epavell

New member
upon a Candle reversal pattern - Draw a line between candles to identify the reversal

Hello,

Looking to see if a line can be drawn between the candles that shows a reversal is underway.

I did some basic script coding that shows a reversal is underway by a line study and wanted see if a line can be added the candle chart. see example attached

Here is my coding that I did. Not sure how to get a line created on a candle chart. Thank you for the help.

Create a Script in TOS to draw a line when a candle reverses up

low[2] < low[3] and high[3] < close [1] or high[3] < close ;

Draw a line from the high [3] to the candle that closes above high[3]

Paint the line in a blue color

Same when a candle reverses down with drawing a line in a pink color

high[2] > high[3] and low[3] > close [1] or low[3] > close ;

Draw a line from the low[3] to the next candle that closes below low[3]

Paint the line in a pink color

Example of a reversal down with pink line drawn on chart and a reversal up with a blue line


Appreciate the help in this
 
Solution
upon a Candle reversal pattern - Draw a line between candles to identify the reversal

Looking to see if a line can be drawn between the candles that shows a reversal is underway.
I did some basic script coding that shows a reversal is underway by a line study and wanted see if a line can be added the candle chart. see example attached

Here is my coding that I did. Not sure how to get a line created on a candle chart. Thank you for the help.
Create a Script in TOS to draw a line when a candle reverses up
low[2] < low[3] and high[3] < close [1] or high[3] < close ;
Draw a line from the high [3] to the candle that closes above high[3]
Paint the line in a blue color
Same when a candle reverses down with drawing a line in a pink...
upon a Candle reversal pattern - Draw a line between candles to identify the reversal

Looking to see if a line can be drawn between the candles that shows a reversal is underway.
I did some basic script coding that shows a reversal is underway by a line study and wanted see if a line can be added the candle chart. see example attached

Here is my coding that I did. Not sure how to get a line created on a candle chart. Thank you for the help.
Create a Script in TOS to draw a line when a candle reverses up
low[2] < low[3] and high[3] < close [1] or high[3] < close ;
Draw a line from the high [3] to the candle that closes above high[3]
Paint the line in a blue color
Same when a candle reverses down with drawing a line in a pink color
high[2] > high[3] and low[3] > close [1] or low[3] > close ;
Draw a line from the low[3] to the next candle that closes below low[3]
Paint the line in a pink color
Example of a reversal down with pink line drawn on chart and a reversal up with a blue line
Appreciate the help in this

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
O0cZXLq.jpg


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
X7bo8ms.jpg
 
Solution

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

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
O0cZXLq.jpg


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
X7bo8ms.jpg
Is there a basic code I could use to draw these lines if a certain condition is true?
 
halcyonguy: My apologies for not replying immediately but we had a death in the family and wasn't able to get to a PC to look at your response. I can't thank you enough for the coding and deep diving into my request. In looking at my charts I didn't really give you insight in what I wanted but you deciphered it and gave me exactly what I was looking for. So again - Thank you. My goal (as in probably everybody on this forum is looking for the holy grail set-up / indicator to bank as much money as possible. As a trader, I have found that there is no such thing. I look for an edge to raise my odds a bit more and you really helped me raise that with this code. So once again Thank you.

Monday I take a look at it live to see how its functioning.

As for the other coding you did by showing vertical lines- I feel like there is something of value that can be useful. Not sure what right now but I will keep that feather in my cap to analyze.

Really appreciate your help.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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