What is Pine Script?
Pine Script is TradingView's proprietary scripting language designed specifically for creating chart indicators, strategy backtests, and alert conditions. It runs entirely inside TradingView's platform — no installation, no external runtime, no dependencies to manage.
The syntax is purpose-built for financial data. Variables like close, open, high, low, and volume are built-in. Functions like ta.sma() (simple moving average), ta.rsi(), and ta.macd() are available out of the box. You're writing logic on top of a financial data structure that TradingView manages — not building data access from scratch.
Pine Script is currently on version 5. Scripts from older versions (v1-v4) still work but may use deprecated syntax. Any new script you write should start with @version=5.
Why use Pine Script?
The built-in indicator library covers most common tools — RSI, MACD, Bollinger Bands, moving averages. Pine Script becomes valuable when you need something that doesn't exist in the default library or in the community scripts.
Primary use cases:
- Custom indicators: Build an indicator that combines multiple signals in a specific way — for example, an RSI filtered by volume, or a moving average with custom alert conditions built in.
- Strategy backtesting: Define entry and exit rules, run them against historical data, and TradingView generates a performance report showing win rate, drawdown, and equity curve.
- Alert automation: Build complex alert conditions that aren't possible through the standard alert interface — for example, fire an alert only when multiple conditions are true simultaneously.
- Community sharing: Publish scripts to the public library for other traders to use.
Getting started without coding
You do not need to write any code to benefit from Pine Script. TradingView's community library contains over 100,000 free scripts published by other users. This library is available to all accounts, including free plans.
To browse and add community scripts:
- Click "Indicators" in the top toolbar of any chart
- Select "Community Scripts" from the left panel
- Search by name, type (indicator or strategy), or category
- Click any script to preview it — you'll see a description, the script code, and comments from other users
- Click "Add to chart" to apply it immediately
Quality filtering tips: sort by "Most liked" or "Most used." Look for scripts with descriptions that clearly explain what the indicator does and how to interpret it. Check the publication date — Pine Script v5 scripts are more current and better maintained than older versions.
Your first Pine Script (beginner example)
Open the Pine Script editor by clicking "Pine Editor" in the bottom panel of any TradingView chart. The editor opens with a template script. Here's the basic structure of a simple indicator script:
Breaking this down line by line:
@version=5— declares Pine Script version 5indicator()— declares this as an indicator (not a strategy).overlay=truemeans it plots on the price chart, not in a separate pane below.input.int()— creates a user-configurable input. The number 20 is the default. Users can change it from the indicator settings panel without touching the code.ta.sma(close, length)— calculates a simple moving average of the closing price over the specified lengthplot()— draws the result on the chart with an orange color and line width of 2
Click "Add to chart" in the Pine Editor to see it render live. Change the length value and the MA updates instantly.
Strategy backtesting with Pine Script
To backtest a trading strategy, replace indicator() with strategy() and add entry/exit logic using strategy.entry() and strategy.close().
When you add a strategy script to your chart, TradingView automatically generates a "Strategy Tester" panel showing: net profit, number of trades, win rate, maximum drawdown, and an equity curve. This lets you evaluate whether a set of rules has historically worked on a given asset and timeframe.
Important caveat: backtesting has limitations. It assumes perfect fills at the signal candle close, doesn't account for slippage or order book depth, and historical performance is not a guarantee of future results. Use backtesting to eliminate obviously bad ideas — not to confirm that a strategy will work going forward.
Pine Script resources
The best places to learn Pine Script:
- TradingView Pine Script manual: The official documentation at pine-script-docs.tradingview.com. Comprehensive reference for every function, type, and operator.
- TradingView community forums: The "Pine Script Q&A" section on TradingView has answers to most beginner questions already posted.
- YouTube Pine Script tutorials: Search "Pine Script v5 tutorial" for step-by-step video walkthroughs. The official TradingView YouTube channel has beginner-focused content.
- PineCoders community: A community of Pine Script developers who publish guides, best practices, and reusable code libraries.
Limitations
Pine Script is powerful within TradingView but has real constraints to understand before investing heavily in it:
- Platform lock-in: Pine Script runs only on TradingView. You cannot export a Pine Script indicator to run on another platform or in your own trading system without rewriting it in a different language.
- No external API access: Pine Script cannot make HTTP requests to external services or pull data from outside TradingView's own data infrastructure.
- Backtesting simplifications: Strategy tester uses simplified order fills (bar close, market orders). It doesn't simulate order book behavior, partial fills, or realistic spread conditions.
- Execution limits: Scripts have computation time limits. Complex scripts with many lookback calculations can hit these limits on lower timeframes with large history ranges.