Skip to main content

Plotting and Visualization 📈

Basic Plotting

Simple Line Plots

//@version=5
indicator("Basic Plotting")

// Simple line plot
sma20 = ta.sma(close, 20)
plot(sma20, "SMA 20", color=color.blue, linewidth=2)

// Plot with dynamic color
plot(close, "Price",
color = close > open ? color.green : color.red)

Plot Styles

//@version=5
indicator("Plot Styles")

// Different plot styles
plot(high, "Circles", style=plot.style_circles)
plot(low, "Cross", style=plot.style_cross)
plot(close, "Line", style=plot.style_line)
plot(volume, "Columns", style=plot.style_columns)
plot(open, "Stepline", style=plot.style_stepline)

Advanced Plotting

Fill Between Lines

//@version=5
indicator("Fill Example")

// Create two moving averages
fastMA = ta.sma(close, 20)
slowMA = ta.sma(close, 50)

// Plot MAs
p1 = plot(fastMA, "Fast MA", color=color.blue)
p2 = plot(slowMA, "Slow MA", color=color.red)

// Fill between
fill(p1, p2,
color = fastMA > slowMA ? color.green : color.red,
transp = 70)

Candlestick Plotting

//@version=5
indicator("Custom Candlesticks", overlay=true)

// Plot candlesticks with custom colors
plotcandle(open, high, low, close,
title="Custom Candles",
color = close > open ? color.green : color.red,
wickcolor = color.gray)

Shapes and Labels

Plotting Shapes

//@version=5
indicator("Shapes Example", overlay=true)

// Plot arrows for crossovers
ma1 = ta.sma(close, 10)
ma2 = ta.sma(close, 20)

crossOver = ta.crossover(ma1, ma2)
crossUnder = ta.crossunder(ma1, ma2)

plotshape(crossOver, "Buy",
style=shape.triangleup,
location=location.belowbar,
color=color.green,
size=size.normal)

plotshape(crossUnder, "Sell",
style=shape.triangledown,
location=location.abovebar,
color=color.red,
size=size.normal)

Adding Labels

//@version=5
indicator("Labels Example", overlay=true)

// Create label on new highs
if ta.highest(high, 20)[1] < high
label.new(bar_index, high, "New High!",
color=color.green,
style=label.style_label_down,
textcolor=color.white)

// Create label on new lows
if ta.lowest(low, 20)[1] > low
label.new(bar_index, low, "New Low!",
color=color.red,
style=label.style_label_up,
textcolor=color.white)

Tables and Charts

Creating Tables

//@version=5
indicator("Table Example")

// Create a table
var table myTable = table.new(position.top_right, 2, 2)

// Update table content
if barstate.islast
table.cell(myTable, 0, 0, "Price", bgcolor=color.blue, text_color=color.white)
table.cell(myTable, 1, 0, str.tostring(close), bgcolor=color.blue, text_color=color.white)
table.cell(myTable, 0, 1, "Volume", bgcolor=color.green, text_color=color.white)
table.cell(myTable, 1, 1, str.tostring(volume), bgcolor=color.green, text_color=color.white)

Boxes and Lines

Drawing Boxes

//@version=5
indicator("Box Example", overlay=true)

// Draw box between high and low of last 5 bars
if barstate.islast
box.new(bar_index[5], high, bar_index, low,
border_color=color.blue,
bgcolor=color.blue.lighten(70))

Drawing Lines

//@version=5
indicator("Line Example", overlay=true)

// Draw trend line between pivots
pivotHigh = ta.pivothigh(high, 5, 5)
pivotLow = ta.pivotlow(low, 5, 5)

if not na(pivotHigh)
line.new(bar_index[10], pivotHigh, bar_index, high,
color=color.red,
width=2)

if not na(pivotLow)
line.new(bar_index[10], pivotLow, bar_index, low,
color=color.green,
width=2)

Practice Examples

1. Multi-Indicator Display

//@version=5
indicator("Multi-Indicator")

// Calculate indicators
sma = ta.sma(close, 14)
rsi = ta.rsi(close, 14)
bb_middle = ta.sma(close, 20)
bb_upper = bb_middle + ta.stdev(close, 20) * 2
bb_lower = bb_middle - ta.stdev(close, 20) * 2

// Plot with different styles
plot(sma, "SMA", color.blue)
plot(rsi, "RSI", color.purple, style=plot.style_circles)
p1 = plot(bb_upper, "BB Upper", color.green)
p2 = plot(bb_lower, "BB Lower", color.green)
fill(p1, p2, color=color.green.lighten(80))

2. Advanced Signal Visualization

//@version=5
indicator("Signal Visualization", overlay=true)

// Calculate signals
ma_fast = ta.ema(close, 9)
ma_slow = ta.ema(close, 21)
signal = ta.crossover(ma_fast, ma_slow)

// Plot moving averages
plot(ma_fast, "Fast EMA", color.blue)
plot(ma_slow, "Slow EMA", color.red)

// Plot signals with labels and arrows
if signal
label.new(bar_index, low, "BUY",
color=color.green,
style=label.style_label_up,
textcolor=color.white)
plotshape(true, "Buy Signal",
shape.triangleup,
location.belowbar,
color.green,
size=size.small)
Pro Tips
  • Use transparency for better visibility
  • Combine different plot types
  • Keep visuals clean and meaningful
  • Use consistent color schemes

Common Pitfalls

Watch Out For

❌ Too many overlapping plots ❌ Excessive use of labels ❌ Poor color contrast ❌ Performance impact of complex visuals

Next Steps

Ready to create complete indicators? Move on to the next chapter! 🚀