⬡ TECHNICAL TUTORIAL · WITH CODE · 13 MIN READ

Pine Script from scratch: indicators and strategies on TradingView.

The most accessible language for anyone who wants to program trading. We'll go from your first indicator to a complete strategy with backtest and alerts for automation — all inside TradingView, with nothing to install.

By the RoboTraderIA Team· updated May 2026· beginner level

If Python is the gateway to exchange bots, Pine Script is the gateway to chart automation. It's TradingView's native language, designed specifically for technical analysis, and it has a huge advantage for beginners: you program, plot and backtest on the same screen, with nothing to install. This tutorial uses the current version, Pine Script v5.

01What Pine Script is (and why to start with it)

Pine Script is a scripting language created by TradingView for two purposes: building custom indicators (which plot on the chart) and building strategies (which generate buy/sell signals and can be tested on history). It's simple, specific and runs in TradingView's cloud — you don't need a server or any installation.

Why it's great for learning: the write→plot→test cycle is instant and visual. You see the result on the chart right away. Compared to building an entire data pipeline in Python, it's much faster for validating a strategy idea.

02Your first indicator

In TradingView, open the Pine Editor (tab at the bottom of the chart). Every script begins by declaring the version and type. Let's plot a simple moving average:

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

// input: makes the period adjustable from the interface
period = input.int(20, "MA Period")

// compute the simple moving average of the close
ma = ta.sma(close, period)

// plot it on the chart
plot(ma, "MA", color=color.aqua, linewidth=2)

Click Add to chart. Done — your average appears over the candles. The overlay=true is what makes the indicator draw on top of price (instead of in a separate panel below).

03The concepts you need to understand

The time series

In Pine, variables like close, open, high, low aren't a single number — they're series. The script runs once for each candle on the chart, and close always refers to the current candle's close on that pass. To access earlier candles, use brackets: close[1] is the previous candle's close, close[2] the one two bars back.

The ta library functions

The ta (technical analysis) library has everything ready: ta.sma() (simple average), ta.ema() (exponential), ta.rsi(), ta.macd(), ta.crossover() (cross up), ta.crossunder() (cross down). You rarely need to compute an indicator by hand.

04From indicator to strategy

The crucial difference: indicator() only draws; strategy() simulates trades and generates a backtest report. Let's turn the average into a crossover system that trades:

//@version=5
strategy("Moving Average Crossover", overlay=true,
         initial_capital=10000, default_qty_type=strategy.percent_of_equity,
         default_qty_value=10)

// adjustable parameters
fast = input.int(9,  "Fast MA")
slow = input.int(21, "Slow MA")

fast_ma = ta.ema(close, fast)
slow_ma = ta.ema(close, slow)

// detect the crossovers
crossUp   = ta.crossover(fast_ma, slow_ma)
crossDown = ta.crossunder(fast_ma, slow_ma)

// trading rules
if crossUp
    strategy.entry("Buy", strategy.long)
if crossDown
    strategy.close("Buy")

// visualize the averages
plot(fast_ma, "Fast", color=color.lime)
plot(slow_ma, "Slow", color=color.orange)

When you add it to the chart, TradingView automatically opens the Strategy Tester below — with net profit, number of trades, win rate, maximum drawdown and the equity curve. You just ran a backtest without writing a single line of test code.

Want ready strategies to adapt?

See our collection of classic strategies with the logic explained — ready to turn into code.

See strategies →

05Adding stop loss and take profit

A strategy without risk management is incomplete. In Pine, you define the stop and target at the moment of entry:

// inside the entry block
if crossUp
    strategy.entry("Buy", strategy.long)
    // stop 2% below, target 4% above (risk:reward 1:2)
    strategy.exit("Exit", "Buy",
                   stop  = close * 0.98,
                   limit = close * 1.04)

Tip: the Strategy Tester recalculates everything when you change these values. Use it to see how different stop/target levels affect the result — but watch out for overfitting (over-optimizing to the past).

06Alerts: the bridge to automation

Pine Script runs on TradingView and doesn't execute orders at your broker on its own. The bridge to automation is alerts. You add an alert in the code, and when the condition fires, TradingView can send a webhook (an HTTP request) to a server that executes the order at the broker.

// fire an alert on the crossover
if crossUp
    alert("BUY BTCUSDT", alert.freq_once_per_bar_close)
if crossDown
    alert("SELL BTCUSDT", alert.freq_once_per_bar_close)

Two ways to turn the alert into a real order: (1) native integration — brokers that support TradingView (like Pepperstone) execute directly from the chart; (2) webhook + server — the alert calls your server (e.g. a Python script that receives the webhook and sends the order via API, as we show in the Binance tutorial).

Caution: automation via webhook adds points of failure (the server goes down, the request is lost, latency). For time-sensitive strategies, the broker's native integration is more reliable. Test exhaustively on a demo account before automating with real money.

07Next steps to master it

  • Study the whole ta library — RSI, MACD, Bollinger, ATR. Each one opens up new strategies.
  • Learn arrays and var to keep state between candles (e.g. tracking a support level).
  • Detect SMC patterns — order blocks and fair value gaps can be coded in Pine (see our Smart Money Concepts guide).
  • Read the official Pine v5 manual — it's the best reference and has examples for everything.

08Frequently asked questions

What is Pine Script?

It's TradingView's programming language, for creating custom indicators and strategies with a backtest right on the platform. It runs in the cloud — nothing to install.

Is Pine Script hard?

No. It's simple and specific to chart analysis. Those who have programmed learn it in hours; beginners pick up the basics in a few days. The visual write→plot→test cycle greatly speeds up learning.

Can you actually trade with Pine Script?

Pine generates signals and alerts. To execute real orders automatically, you connect the alerts to a webhook (which sends the order to the broker via API) or use the native integration of brokers that support TradingView.

Pine Script or Python for automation?

Pine is better for prototyping and quick visual backtests. Python gives full control and runs independently of TradingView, ideal for robust execution. Many use Pine to design the strategy and Python to execute it.

Do you need to pay for TradingView to use Pine?

The free plan lets you create and test scripts with limitations (number of simultaneous indicators, alerts). For serious automation with many alerts, the paid plans unlock more resources.