blog · ~6 min read

TradingView Pine Script Introduction

Pine Script is TradingView's domain-specific language for building custom indicators and strategies, and this guide walks beginners from first line to a working EMA crossover.

T By tradernewbie · Curated for beginners
#platforms#tools
Este artigo está em inglês. Ver no seu idioma? Google Translate →

As ferramentas interativas podem não funcionar na vista traduzida.

TradingView Pine Script Introduction

Pine Script lets you turn a chart idea into code without leaving the browser — no Python environment, no IDE, no API keys.

What Pine Script is

Pine Script is a declarative language that runs inside TradingView. You describe what each bar should compute, and the engine plots it. There is no event loop, no memory management, and no manual loop over bars — the runtime handles the per-bar iteration for you.

Versions matter: Pine Script v5 is current. Always declare the version on line 1 to avoid silent behavior differences.

Your first indicator: an EMA ribbon

Open TradingView, click Pine Editor at the bottom, paste this, then click Add to chart:

//@version=5
indicator("EMA Ribbon", overlay = true)

fast = input.int(20, "Fast EMA")
slow = input.int(50, "Slow EMA")

emaFast = ta.ema(close, fast)
emaSlow = ta.ema(close, slow)

plot(emaFast, color = color.aqua, linewidth = 2)
plot(emaSlow, color = color.orange, linewidth = 2)

fill(plot1, plot2, color = emaFast > emaSlow ? color.new(color.green, 80) : color.new(color.red, 80))

Reading top to bottom:

  1. //@version=5 declares the language version.
  2. indicator() registers the script type and that it overlays the price chart.
  3. input.int() exposes a setting the user can change without editing code.
  4. ta.ema() is a built-in technical-analysis function.
  5. plot() draws a line; fill() shades the space between two plots.

Core concepts

Concept What it does
indicator() Plots a study (does not trade)
strategy() Backtests entries and exits
input.* User-configurable parameter
ta.* Built-in TA library (RSI, ATR, MACD)
[] operator Reference past bar values: close[1] is previous close
:= Reassign a variable
= Initialize a variable

Strategy example: EMA crossover

Replace indicator(...) with strategy(...) to enable backtesting:

//@version=5
strategy("EMA Cross", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)

fast = ta.ema(close, 20)
slow = ta.ema(close, 50)

longCondition = ta.crossover(fast, slow)
if longCondition
    strategy.entry("Long", strategy.long)

shortCondition = ta.crossunder(fast, slow)
if shortCondition
    strategy.entry("Short", strategy.short)

Click Add to chart, then the Strategy Tester tab to see trades, equity curve, and metrics.

Limitations to know

  • A script runs per-bar; you cannot pause or wait between bars.
  • Variable scoping is strict — var initializes once, varip per real-time update.
  • Free plans cannot create private scripts or use more than 40 functions in some categories.
  • Strategy backtests in Pine assume fills at the next bar open by default — set process_orders_on_close = true with care.

Learning path

  1. Read the official Pine Script v5 Reference — every function is documented with examples.
  2. Recreate a built-in indicator (RSI, MACD) to learn idioms.
  3. Add a strategy wrapper around it and watch the equity curve.
  4. Only then convert your discretionary setup into code.

Common pitfalls

  • Plotting inside an if block hides signals — always compute logic first, then plot.
  • Hardcoding lengths instead of input.* makes it impossible to optimize.
  • Ignoring commission and slippage in strategy() inflates backtest results.

Next: pair Pine Script strategies with a proper backtesting methodology so the equity curve is believable.

Related market data, powered by TradingView.

Educational content · Not financial advice · Trade at your own risk