All Articles
TradingView Pine Script Guide Indicators Strategy

Complete Guide to TradingView Pine Script: From Beginner to Advanced

Brokerlytic TeamApril 10, 2026
Key Takeaways:Master Pine Script on TradingView β€” learn to create custom indicators, strategies, and automated alerts from scratch with practical examples.

What is Pine Script?

Pine Script is TradingView's proprietary programming language, designed specifically for creating custom technical indicators, strategies, and alerts. It runs directly inside TradingView charts, making it the easiest way to code trading tools without any external setup.

Why Learn Pine Script?

  • No setup required β€” write and run code directly in your browser
  • Massive community β€” thousands of open-source scripts to learn from
  • Backtesting built-in β€” test strategies on historical data instantly
  • Real-time alerts β€” get notified when your conditions are met
  • Free to use β€” works on TradingView's free plan

Chapter 1: Getting Started

Your First Script

Open TradingView, click "Pine Editor" at the bottom. Paste this:

//@version=5
indicator("My First Indicator", overlay=true)

// Simple Moving Average
smaLen = input.int(20, "SMA Length")
smaVal = ta.sma(close, smaLen)

// Plot on chart
plot(smaVal, color=color.blue, linewidth=2, title="SMA")

Click "Add to Chart" β€” you now have a custom SMA indicator!

Understanding the Structure

Every Pine Script has these key parts:

  1. Version declaration: //@version=5 β€” always use version 5
  2. Type declaration: indicator() for indicators, strategy() for backtestable strategies
  3. Inputs: input.int(), input.float(), input.string() β€” user-configurable parameters
  4. Calculations: Your trading logic using built-in functions
  5. Output: plot(), plotshape(), bgcolor() β€” visual elements on chart

Chapter 2: Essential Built-in Functions

Moving Averages

//@version=5
indicator("MA Toolkit", overlay=true)

// Simple Moving Average
sma20 = ta.sma(close, 20)
sma50 = ta.sma(close, 50)

// Exponential Moving Average
ema12 = ta.ema(close, 12)
ema26 = ta.ema(close, 26)

// Weighted Moving Average
wma14 = ta.wma(close, 14)

plot(sma20, color=color.blue, title="SMA 20")
plot(sma50, color=color.red, title="SMA 50")
plot(ema12, color=color.green, title="EMA 12")

RSI (Relative Strength Index)

//@version=5
indicator("RSI with Zones")

rsiLen = input.int(14, "RSI Length")
rsiVal = ta.rsi(close, rsiLen)

overbought = 70
oversold = 30

plot(rsiVal, color=color.purple, linewidth=2)
hline(overbought, color=color.red, linestyle=hline.style_dashed)
hline(oversold, color=color.green, linestyle=hline.style_dashed)

// Color background when in extreme zones
bgcolor(rsiVal > overbought ? color.new(color.red, 90) : rsiVal < oversold ? color.new(color.green, 90) : na)

MACD

//@version=5
indicator("MACD Custom")

[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)

plot(macdLine, color=color.blue, title="MACD")
plot(signalLine, color=color.orange, title="Signal")
plot(histLine, style=plot.style_histogram, color=histLine >= 0 ? color.green : color.red, title="Histogram")

Bollinger Bands

//@version=5
indicator("Bollinger Bands", overlay=true)

length = input.int(20, "Length")
mult = input.float(2.0, "Multiplier")

[middle, upper, lower] = ta.bb(close, length, mult)

plot(middle, color=color.blue)
p1 = plot(upper, color=color.red)
p2 = plot(lower, color=color.green)
fill(p1, p2, color=color.new(color.blue, 95))

Chapter 3: Building a Complete Strategy

SMA Crossover Strategy with Risk Management

//@version=5
strategy("SMA Crossover Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// Inputs
fastLen = input.int(10, "Fast SMA")
slowLen = input.int(30, "Slow SMA")
stopLossPct = input.float(2.0, "Stop Loss %")
takeProfitPct = input.float(4.0, "Take Profit %")

// Calculations
fastSMA = ta.sma(close, fastLen)
slowSMA = ta.sma(close, slowLen)

// Signals
buySignal = ta.crossover(fastSMA, slowSMA)
sellSignal = ta.crossunder(fastSMA, slowSMA)

// Execute trades
if buySignal
    strategy.entry("Long", strategy.long)
    strategy.exit("TP/SL", "Long", profit=close * takeProfitPct / 100 / syminfo.mintick, loss=close * stopLossPct / 100 / syminfo.mintick)

if sellSignal
    strategy.close("Long")

// Plots
plot(fastSMA, color=color.green, linewidth=2, title="Fast SMA")
plot(slowSMA, color=color.red, linewidth=2, title="Slow SMA")
plotshape(buySignal, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, title="Buy")
plotshape(sellSignal, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Sell")

RSI + MACD Confluence Strategy

//@version=5
strategy("RSI + MACD Confluence", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=5)

// RSI
rsiLen = input.int(14, "RSI Length")
rsiOversold = input.int(30, "RSI Oversold")
rsiOverbought = input.int(70, "RSI Overbought")
rsiVal = ta.rsi(close, rsiLen)

// MACD
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)

// Confluence conditions
longCondition = rsiVal < rsiOversold and ta.crossover(macdLine, signalLine)
shortCondition = rsiVal > rsiOverbought and ta.crossunder(macdLine, signalLine)

if longCondition
    strategy.entry("Long", strategy.long)
if shortCondition
    strategy.close("Long")

plotshape(longCondition, style=shape.labelup, location=location.belowbar, color=color.green, text="BUY", textcolor=color.white)
plotshape(shortCondition, style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL", textcolor=color.white)

Chapter 4: Creating Alerts

//@version=5
indicator("Smart Alert System", overlay=true)

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

bullishCross = ta.crossover(ema20, ema50) and rsi > 50
bearishCross = ta.crossunder(ema20, ema50) and rsi < 50

// Alert conditions
alertcondition(bullishCross, title="Bullish Cross", message="EMA 20 crossed above EMA 50 with RSI > 50 on {{ticker}}")
alertcondition(bearishCross, title="Bearish Cross", message="EMA 20 crossed below EMA 50 with RSI < 50 on {{ticker}}")

plot(ema20, color=color.green)
plot(ema50, color=color.red)
plotshape(bullishCross, style=shape.diamond, location=location.abovebar, color=color.green, size=size.small)
plotshape(bearishCross, style=shape.diamond, location=location.belowbar, color=color.red, size=size.small)

Chapter 5: Advanced Techniques

Multi-Timeframe Analysis

//@version=5
indicator("MTF Dashboard", overlay=true)

// Get data from higher timeframes
dailyClose = request.security(syminfo.tickerid, "D", close)
weeklyClose = request.security(syminfo.tickerid, "W", close)
dailyRSI = request.security(syminfo.tickerid, "D", ta.rsi(close, 14))

// Table dashboard
var table dash = table.new(position.top_right, 2, 4, bgcolor=color.new(color.black, 80))
if barstate.islast
    table.cell(dash, 0, 0, "Timeframe", text_color=color.white, text_size=size.small)
    table.cell(dash, 1, 0, "Status", text_color=color.white, text_size=size.small)
    table.cell(dash, 0, 1, "Daily Close", text_color=color.gray, text_size=size.small)
    table.cell(dash, 1, 1, str.tostring(dailyClose, "#.##"), text_color=close > dailyClose ? color.green : color.red, text_size=size.small)
    table.cell(dash, 0, 2, "Weekly Close", text_color=color.gray, text_size=size.small)
    table.cell(dash, 1, 2, str.tostring(weeklyClose, "#.##"), text_color=close > weeklyClose ? color.green : color.red, text_size=size.small)
    table.cell(dash, 0, 3, "Daily RSI", text_color=color.gray, text_size=size.small)
    table.cell(dash, 1, 3, str.tostring(dailyRSI, "#.#"), text_color=dailyRSI > 50 ? color.green : color.red, text_size=size.small)

Custom Candlestick Patterns

//@version=5
indicator("Candlestick Patterns", overlay=true)

// Engulfing patterns
bullishEngulfing = close[1] < open[1] and close > open and close > open[1] and open < close[1]
bearishEngulfing = close[1] > open[1] and close < open and close < open[1] and open > close[1]

// Doji
bodySize = math.abs(close - open)
avgBody = ta.sma(bodySize, 20)
isDoji = bodySize < avgBody * 0.1

plotshape(bullishEngulfing, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, title="Bull Engulf")
plotshape(bearishEngulfing, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Bear Engulf")
plotshape(isDoji, style=shape.circle, location=location.abovebar, color=color.yellow, size=size.tiny, title="Doji")

Summary & Next Steps

Pine Script is one of the most accessible programming languages for traders. Start with simple indicators, progress to strategies with backtesting, and eventually build complex multi-timeframe systems.

Recommended learning path:

  1. Modify existing community scripts
  2. Build your own indicators
  3. Create backtestable strategies
  4. Add risk management rules
  5. Build multi-timeframe dashboards

Pro Tip: TradingView Pro gives you more indicators per chart, server-side alerts, and faster data. Get TradingView Pro β†’

Frequently Asked Questions

What is the main concept of to TradingView Pine Script: From Beginner to Advanced?

Master Pine Script on TradingView β€” learn to create custom indicators, strategies, and automated alerts from scratch with practical examples.

Who should read this guide?

This guide is perfect for both beginners looking to understand the basics and experienced traders wanting to refine their strategies in TradingView.