Pine Script tutorial: from your first script to MT5 orders.
Pine Script is TradingView’s language for indicators and strategies. This tutorial walks the whole path: your first indicator, converting it into a strategy, testing it honestly, firing alerts — and then copying those alerts into MetaTrader 5 so the strategy trades without you at the keyboard.
What Pine Script is.
TRADINGVIEW’S LANGUAGE, IN THE CLOUD
Pine Script runs on TradingView’s servers: you write it in the Pine Editor, press compile, and the script draws on the chart or simulates trades. Two script types matter for automation. An indicator only draws or calculates. A strategy declares entries and exits, so TradingView can backtest them bar by bar and fire alerts when the conditions trigger live.
For automation, the strategy is the one you want: its alerts are the bridge between what you designed and what your broker executes.
Step 1: your first indicator.
TEN LINES, ONE LINE ON THE CHART
Open the Pine Editor at the bottom of any chart, paste this and press compile:
//@version=6
indicator("SMA 50", overlay=true)
plot(ta.sma(close, 50))
Three lines: declare the script, give it a name, draw the 50-period simple moving average over the price. From here, everything you will ever build is this same shape: inputs, a calculation, and something to show or to trade.
Step 2: from indicator to strategy.
DECLARING ENTRIES SO TRADINGVIEW CAN TEST THEM
Change the declaration to strategy() and add a rule that opens a position. A minimal crossover example:
//@version=6
strategy("SMA cross", overlay=true)
fast = ta.sma(close, 50)
slow = ta.sma(close, 200)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
Now the Strategy Tester tab fills with trades: every crossover bought, every crossunder closed. That tester is where you add costs and find out the strategy is not as good as it looked on the chart — which is the point.
Step 3: test it honestly.
COSTS, DEPTH AND THE REPAINTING TRAP
Three things turn a pretty backtest into an honest one. Commission and slippage in the strategy properties — without them every result is fiction. Enough history — a few months proves nothing. And the repainting trap: a script repaints when its past values change after the fact because it used information that did not exist at that bar’s close. A repainting strategy looks brilliant in the tester and dies live. Test the same logic with bar replay, and verify the signals on a demo before real money.
Step 4: fire alerts.
THE STRATEGY SPEAKS JSON
Once the strategy behaves, add alert messages: TradingView can send the order as JSON every time an entry or exit triggers. A webhook alert with a body like this is all a bridge needs:
{
"symbol": "{{ticker}}",
"action": "{{strategy.order.action}}",
"lot": 0.1
}
The placeholders fill in the symbol and the direction automatically. One alert now covers every chart the strategy runs on.
Step 5: the alerts become MT5 orders.
THE BRIDGE CLOSES THE LOOP
Pine cannot reach your broker: it lives inside TradingView. The last hop is a bridge — a webhook receiver plus an EA inside your MetaTrader 5 that executes each alert on your account. With SignalForge the setup is four steps (register the account, install the EA, paste the webhook URL, test on demo) and the median time from alert to confirmed order in our 19-23 September 2026 measurements was about 0.5 s. The full walkthrough: how to copy TradingView signals into MT5.
If writing the strategy itself is the part you would rather skip, the Script Builder turns plain-language rules into Pine and our AI Pine script generator guide covers that route in depth; converting an existing script into a tradeable one is covered in Pine Script to EA.
FAQ
Quick questions.
Email support@signalforge-ai.com if yours isn’t here.
Is Pine Script hard to learn?
+
What version of Pine Script should I use?
+
Can Pine Script place orders in my broker?
+
What is repainting in Pine Script?
+
Do I need to learn Pine to automate a TradingView strategy?
+
Benjamin SF is the founder of SignalForge and an expert in trading algorithm automation. He builds and operates the SignalForge bridge and the SFCloud hosted-MT5 fleet for prop-firm traders.
From Pine alerts to MT5 orders
SignalForge executes your TradingView strategy on MetaTrader 5 in milliseconds. 14-day free trial, no card required.
SignalForge AI is an order-execution tool. We do not provide investment advice. Trading involves risk of loss.