Back to TradingView PineScript for CFD Traders
Part 4

Drawing on charts: labels, lines, and boxes

Annotations that make a chart readable: trade markers, stop lines, target zones, and trade-management dashboards.

Advanced8 min readBeginner → AdvancedLesson 15 / 17
Try it in TradingView
Labels, boxes, and a live dashboard

Labels mark each golden cross, boxes shade the EMA zone, and the top-right table shows live values. Note the max_*_count caps in the declaration.

//@version=5
indicator("Lesson 15 - Drawing", overlay=true,
     max_labels_count=500, max_lines_count=500, max_boxes_count=500)

ema20 = ta.ema(close, 20)
ema50 = ta.ema(close, 50)

plot(ema20, "EMA 20", color=color.blue, linewidth=2)
plot(ema50, "EMA 50", color=color.orange, linewidth=2)

crossUp = ta.crossover(ema20, ema50)

// Label each golden cross.
if crossUp
    label.new(bar_index, low, "Golden cross",
         style=label.style_label_upper_left, color=color.new(color.green, 20),
         textcolor=color.white, size=size.small)

// Box the zone between the two EMAs.
box.new(left=bar_index - 10, top=math.max(ema20, ema50),
        right=bar_index,     bottom=math.min(ema20, ema50),
        border_color=color.new(color.gray, 50),
        bgcolor=color.new(color.blue, 92))

// Dashboard — only drawn once, on the last bar.
if barstate.islast
    var table dash = table.new(position.top_right, 2, 3, border_width=1)
    table.cell(dash, 0, 0, "EMA 20", text_color=color.white)
    table.cell(dash, 1, 0, str.tostring(ema20, "#.##"))
    table.cell(dash, 0, 1, "EMA 50", text_color=color.white)
    table.cell(dash, 1, 1, str.tostring(ema50, "#.##"))
    table.cell(dash, 0, 2, "Trend",  text_color=color.white)
    table.cell(dash, 1, 2, ema20 > ema50 ? "Up" : "Down",
               text_color=ema20 > ema50 ? color.green : color.red)
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

Why drawing matters

Lines that get plotted every bar are noise. A line drawn on entry, extended to exit, with the PnL labelled — that is information. Pine's drawing objects are how you turn a strategy tester into a chart you can read.

Labels: text annotations

if strategy.position_size == 0 and longCondition
    label.new(bar_index, high, "Long", style=label.style_label_down, color=color.green)

Labels are anchored at a specific bar and price. Use them for trade entries, exits, and signal markers.

Lines: trend and stop lines

var line stopLine = na
if strategy.position_size != 0 and strategy.position_size[1] == 0
    stopLine := line.new(bar_index, close, bar_index + 50, close - atrVal, color=color.red, width=2)

Lines can be horizontal, sloped, or extended. They auto-extend if you give them a future bar.

Boxes: zones

box.new(left, top, right, bottom) draws a rectangular zone. Use boxes for stop zones, target zones, session ranges, and consolidation zones.

Cleaning up old drawings

Drawing objects persist on the chart. Use label.delete, line.delete, box.delete to remove old ones, or use max_lines_count, max_labels_count, max_boxes_count in the indicator/strategy declaration to cap the total number Pine keeps.

A trade-management dashboard

if barstate.islast
    var table dash = table.new(position.top_right, 2, 4)
    table.cell(dash, 0, 0, "Direction", text_color=color.white)
    table.cell(dash, 1, 0, strategy.position_size > 0 ? "Long" : strategy.position_size < 0 ? "Short" : "Flat")
    table.cell(dash, 0, 1, "Entry", text_color=color.gray)
    table.cell(dash, 1, 1, str.tostring(strategy.position_avg_price, "#.##"))
    table.cell(dash, 0, 2, "Unrealized", text_color=color.gray)
    table.cell(dash, 1, 2, str.tostring(strategy.openprofit, "#.##"))
    table.cell(dash, 0, 3, "Risk", text_color=color.gray)
    table.cell(dash, 1, 3, str.tostring(equityRisk, "#.##") + "%")

What to take from this

A chart with annotations is a tool. A chart without annotations is a price line. Take the time to make the strategy legible at a glance — it changes how you react during live trading.

What you just did

Lesson 15 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.