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.
Les outils interactifs peuvent ne pas fonctionner dans la vue traduite.
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:
//@version=5declares the language version.indicator()registers the script type and that it overlays the price chart.input.int()exposes a setting the user can change without editing code.ta.ema()is a built-in technical-analysis function.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 —
varinitializes once,varipper 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 = truewith care.
Learning path
- Read the official Pine Script v5 Reference — every function is documented with examples.
- Recreate a built-in indicator (RSI, MACD) to learn idioms.
- Add a strategy wrapper around it and watch the equity curve.
- Only then convert your discretionary setup into code.
Common pitfalls
- Plotting inside an
ifblock 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.
Live Chart
Open full chart →Related market data, powered by TradingView.