Skip to main content
In this tutorial we draw a horizontal line at the highest high of the last N bars and a label that names its price. We draw them once, on the last bar of the chart, and then keep them and move them as the chart grows, instead of drawing new ones. Every step is a whole script: replace what is in the editor, and check it. Unlike plot, the drawing calls (line.new, label.new, box.new, …) may be called inside an if, a loop or a function. They return a handle you can keep, change and delete.
1

find the level

talib.highest(source, length) is the highest value of source over the last length bars. We plot it first to see what we are going to draw.
A stepped orange line follows the highest high of the last 50 bars.
2

draw the line on the last bar

line.new(x1, y1, x2, y2) draws a line between two points. The x values are bar numbers (bar_index is the number of the bar the script is on), the y values are prices. barstate.islast is true only on the last bar, so the line is drawn once the script reaches the end of the history. extend=extend.right carries it past the last bar.
The line starts at the first bar of the window and runs to the last bar at the level of the highest high. math.max keeps the start from going below bar 0 on a short chart.
3

add the label

label.new(x, y, text) puts text at a point. The text is joined from strings, and numbers are converted with str.tostring.
4

keep the drawings and move them

A drawing stays on the chart until the script deletes it or the limit for its kind trims it (50 of each kind unless the declaration raises it). A script that calls line.new every time it reaches the last bar adds a line each time. Instead, create the line and the label once, hold them in static variables, and move them afterwards.
  • static line levelLine = na and static label levelLabel = na declare empty handles that last across bars.
  • na(levelLine) is true until a line has been assigned to it, so the first branch runs once.
  • line.set_xy1, line.set_xy2, label.set_xy and label.set_text change the existing drawings.
Name the string variable something other than text: text is the name of a built-in namespace, and a variable called text compiles with the warning Variable `text` hides the built-in namespace `text`.

What we built

An indicator with one setting that keeps a single line and a single label on the chart and moves them to the current highest high of the last N bars.

Next