Plotting Functions#
Pine Script provides various functions for drawing visual elements on charts. This chapter covers the most commonly used: plot, hline, fill, and background color functions.
plot — Line Chart#
plot() is the most basic drawing function, rendering a series of values as a line:
plot(series, title, color, linewidth, style, display)Basic usage:
//@version=6
indicator("plot Demo", overlay=true)
ma5 = ta.sma(close, 5)
ma20 = ta.sma(close, 20)
plot(ma5, "MA5", color=color.blue, linewidth=1)
plot(ma20, "MA20", color=color.orange, linewidth=2)Line Styles#
The style parameter controls the line’s appearance:
| Constant | Description |
|---|---|
plot.style_line | Line (default) |
plot.style_stepline | Step line |
plot.style_histogram | Histogram bars |
plot.style_area | Area chart |
plot.style_areabr | Area chart (split positive/negative at 0) |
plot.style_columns | Column chart (similar to volume) |
plot.style_circles | Circles |
plot.style_cross | Crosses |
//@version=6
indicator("MACD Histogram")
[macdLine, signalLine, hist] = ta.macd(close, 12, 26, 9)
plot(hist, "Histogram", style=plot.style_histogram,
color=hist >= 0 ? color.green : color.red)
plot(macdLine, "MACD", color=color.blue)
plot(signalLine, "Signal", color=color.orange)Dynamic Colors#
The color parameter can be a conditional expression, allowing the line color to change based on value:
//@version=6
indicator("Dynamic Color", overlay=true)
ma = ta.sma(close, 20)
// Green when close is above MA, red when below
lineColor = close > ma ? color.green : color.red
plot(ma, "MA", color=lineColor, linewidth=2)color.new — Color Transparency#
color.new(baseColor, transparency) creates a color with a transparency level. The range is 0 (opaque) to 100 (fully transparent):
plot(close, color=color.new(color.blue, 50)) // 50% transparent bluehline — Horizontal Line#
hline() draws a horizontal line at a fixed price level, commonly used for RSI overbought/oversold levels:
//@version=6
indicator("RSI + Horizontal Lines")
rsiValue = ta.rsi(close, 14)
plot(rsiValue, "RSI", color=color.purple)
hline(70, "Overbought", color=color.red, linestyle=hline.style_dashed)
hline(50, "Midline", color=color.gray, linestyle=hline.style_dotted)
hline(30, "Oversold", color=color.green, linestyle=hline.style_dashed)linestyle options:
| Constant | Description |
|---|---|
hline.style_solid | Solid line (default) |
hline.style_dashed | Dashed line |
hline.style_dotted | Dotted line |
fill — Fill Between Two Lines#
fill() fills the color between two plot or hline objects, commonly used to display channels or indicator ranges:
//@version=6
indicator("Bollinger Band Fill", overlay=true)
[middle, upper, lower] = ta.bb(close, 20, 2)
upperPlot = plot(upper, "Upper", color=color.blue)
lowerPlot = plot(lower, "Lower", color=color.blue)
plot(middle, "Middle", color=color.orange)
// Fill between upper and lower bands
fill(upperPlot, lowerPlot, color=color.new(color.blue, 90))Filling between hline objects also works:
//@version=6
indicator("RSI Band Fill")
rsiValue = ta.rsi(close, 14)
plot(rsiValue, "RSI", color=color.purple)
h70 = hline(70, "Overbought", color=color.red)
h30 = hline(30, "Oversold", color=color.green)
// Fill between 30 and 70
fill(h70, h30, color=color.new(color.gray, 85))bgcolor — Bar Background Color#
bgcolor() colors the bar background under specific conditions, useful for marking special sessions or signals:
//@version=6
indicator("Background Color Demo", overlay=true)
rsiValue = ta.rsi(close, 14)
// Red background when RSI is overbought, green when oversold
bgcolor(rsiValue > 70 ? color.new(color.red, 85) : na)
bgcolor(rsiValue < 30 ? color.new(color.green, 85) : na)barcolor — Candle Color#
barcolor() changes the color of the candle itself (body and wicks):
//@version=6
indicator("Candle Color", overlay=true)
ma = ta.sma(close, 20)
// Show blue candles when close is above MA, otherwise keep default
barcolor(close > ma ? color.blue : na)Passing
nakeeps the default color unchanged.
Practical Example: Multiple Moving Averages Indicator#
//@version=6
indicator("Multiple MAs", overlay=true)
ma5 = ta.sma(close, 5)
ma10 = ta.sma(close, 10)
ma20 = ta.sma(close, 20)
ma60 = ta.sma(close, 60)
ma120 = ta.sma(close, 120)
plot(ma5, "MA5", color=color.new(color.red, 0), linewidth=1)
plot(ma10, "MA10", color=color.new(color.orange, 0), linewidth=1)
plot(ma20, "MA20", color=color.new(color.yellow, 0), linewidth=2)
plot(ma60, "MA60", color=color.new(color.green, 0), linewidth=2)
plot(ma120, "MA120", color=color.new(color.blue, 0), linewidth=3)
// Light green background when MAs are in bullish alignment
bullish = ma5 > ma10 and ma10 > ma20 and ma20 > ma60
bgcolor(bullish ? color.new(color.green, 92) : na)