draw a vertical line and horizontal line at a specific place on chart

halcyonguy

Moderator - Expert
VIP
Lifetime
this draws a giant plus sign, a vertical line and a horizontal line, on the chart.
default center is $1 below the close on bar # 80.
it has a height of $2 and a width of 9 bars.


related to this question
https://usethinkscript.com/threads/vertical-horizontal-lines.21035/


Code:
# addchart_hline_vline

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

input vertical_line_ht = 2.0;
#hint line_ht: Total height of the vertical line, in dollars 

input line_yoffset = -1.0;
#hint yoffset: This is the price difference, from the close, to the top of the line. a negative number moves the line below the candle. positive number moves line above candle

input line_barnum = 80;
#hint barnum: Draw a vertical line at a specific barnumber


addlabel(1, "vertical line ht " +  vertical_line_ht, color.yellow);
addlabel(1, "line y offset " + line_yoffset , color.yellow);
addlabel(1, "line barnum " + line_barnum , color.yellow);

addchartbubble(bn == line_barnum, high*1.002,
 "Bar# " + bn + "\n" +
 "close " + close
, color.yellow, yes);


#-------------------
# vert line 
def vlinetop = close + line_yoffset;
def vlinemid = vlinetop - (vertical_line_ht/2);
def linebot = vlinetop - vertical_line_ht;

def ht = if bn == line_barnum then vlinetop else na;
def lt = ht - vertical_line_ht;
def ct = vlinemid;
def ot = vlinemid;

# uses the grow color
input type = ChartType.CANDLE;
AddChart(high = ht,low = lt,open = ot,close = ct, growColor = Color.cyan, fallColor = color.magenta, type = type);


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

addlabel(1, " ", color.black);
input horz_line_bar_length = 9;
addlabel(1, "horizontal line length, bars " + horz_line_bar_length , color.yellow);

def hhalf = floor(horz_line_bar_length/2);
def hline_first_bn = line_barnum - hhalf;
def hline_last_bn = hline_first_bn + horz_line_bar_length - 1;
def hline = (bn >= hline_first_bn and bn <= hline_last_bn);

def hline2 = if bn == hline_first_bn then vlinemid[-hhalf]
 else if hline then hline2[1]
 else na;

plot z1 = hline2;
z1.setdefaultcolor(color.cyan);
#
 

Attachments

  • img1.JPG
    img1.JPG
    72.1 KB · Views: 37

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

this draws a giant plus sign, a vertical line and a horizontal line, on the chart.
default center is $1 below the close on bar # 80.
it has a height of $2 and a width of 9 bars.


related to this question
https://usethinkscript.com/threads/vertical-horizontal-lines.21035/


Code:
# addchart_hline_vline

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

input vertical_line_ht = 2.0;
#hint line_ht: Total height of the vertical line, in dollars

input line_yoffset = -1.0;
#hint yoffset: This is the price difference, from the close, to the top of the line. a negative number moves the line below the candle. positive number moves line above candle

input line_barnum = 80;
#hint barnum: Draw a vertical line at a specific barnumber


addlabel(1, "vertical line ht " +  vertical_line_ht, color.yellow);
addlabel(1, "line y offset " + line_yoffset , color.yellow);
addlabel(1, "line barnum " + line_barnum , color.yellow);

addchartbubble(bn == line_barnum, high*1.002,
 "Bar# " + bn + "\n" +
 "close " + close
, color.yellow, yes);


#-------------------
# vert line
def vlinetop = close + line_yoffset;
def vlinemid = vlinetop - (vertical_line_ht/2);
def linebot = vlinetop - vertical_line_ht;

def ht = if bn == line_barnum then vlinetop else na;
def lt = ht - vertical_line_ht;
def ct = vlinemid;
def ot = vlinemid;

# uses the grow color
input type = ChartType.CANDLE;
AddChart(high = ht,low = lt,open = ot,close = ct, growColor = Color.cyan, fallColor = color.magenta, type = type);


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

addlabel(1, " ", color.black);
input horz_line_bar_length = 9;
addlabel(1, "horizontal line length, bars " + horz_line_bar_length , color.yellow);

def hhalf = floor(horz_line_bar_length/2);
def hline_first_bn = line_barnum - hhalf;
def hline_last_bn = hline_first_bn + horz_line_bar_length - 1;
def hline = (bn >= hline_first_bn and bn <= hline_last_bn);

def hline2 = if bn == hline_first_bn then vlinemid[-hhalf]
 else if hline then hline2[1]
 else na;

plot z1 = hline2;
z1.setdefaultcolor(color.cyan);
#
Thank you, @halcyonguy! I will study the code.
 
Thread starter Similar threads Forum Replies Date
halcyonguy experimental code: draw 2 rectangles, with start and stop dates/times, and price levels Playground 0

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
431 Online
Create Post

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