What Is Pine Script? TradingView's Chart Language Explained

Beginner6 min read
Tall pine trees above a mountain lake with distant peaks
Image by Anne Kaercher on Wikimedia Commons, CC0

What Is Pine Script?

Pine Script is the programming language of TradingView, the charting website. You write a short script, and TradingView runs it on its own servers and draws the result on your chart. Scripts can draw indicators, simulate trades to test a strategy, and trigger alerts. You install nothing on your computer.

Traders use it because it is small and built for charts. A moving average that takes many lines in other languages takes one line in Pine. This guide explains what the language is, what it can and cannot do, and where to go next. If you want to paste a script right away, see how to add a Pine Script to TradingView.

What Does a Pine Script Look Like?

Here is a complete script. It draws a 20-bar simple moving average, the average of the last 20 closing prices, as a blue line on the price chart:

//@version=6
indicator("Simple Moving Average", overlay=true)
length = input.int(20, "Length", minval=1)
avg = ta.sma(close, length)
plot(avg, "SMA", color=color.blue, linewidth=2)

Read it line by line:

  • Line 1://@version=6 tells TradingView which version of the language the script uses. The next guide, Pine Script v4, v5 and v6, explains it.
  • Line 2:indicator() is the declaration. It says this script is an indicator and gives it a name. overlay=true means “draw on the price chart, not in a separate pane”.
  • Line 3: an input is a setting you can change without editing the code. Here it is the length, 20 by default, and it cannot go below 1.
  • Line 4:ta.sma(close, length) works out the simple moving average. With a length of 20 and closes that add up to 2,000 over 20 bars, the result is 2,000 / 20 = 100.
  • Line 5:plot() draws the value on the chart, one point per bar.

Notice that there is no loop over the bars. Pine runs the script once for every bar on the chart, from the oldest to the newest, and then again on each new price tick of the live bar. That one idea explains most of how the language behaves.

Where Does Pine Script Run?

Pine Script is cloud-based. TradingView’s documentation says it lets traders “create their own trading tools and run them on our servers”. Your script is saved in your TradingView account and runs inside the chart in your browser or app, so it is there on any device you sign in to.

Diagram: a chart leads to the Pine Editor, then the script runs on the platform servers, which draw on the chart, backtest and send alerts; a crossed-out box shows it cannot send broker orders alone
A Pine script is written in the browser editor but runs on TradingView's servers. It can draw, backtest and raise alerts. It cannot place broker orders by itself; that needs a separate link.

This is different from a program you install. Nothing is compiled on your machine. TradingView also sets limits on things such as how much data a script requests, how long it may run, its memory use and its size.

What Can Pine Script Do?

  • Draw indicators: lines, histograms, shapes, coloured bars and background shading. The indicators in the Pine Script Library are examples.
  • Draw objects: lines, labels, boxes and tables, for example to mark the previous day’s high and low.
  • Backtest a strategy: a strategy script places simulated trades on past bars, and the Strategy Tester shows the results. See how to backtest a Pine strategy.
  • Fire alerts: a script can define conditions that trigger a TradingView alert, when a rule you wrote becomes true. The Pine Script alerts guide covers this.
  • Use other symbols and timeframes: a script can request data from another chart symbol or a higher timeframe, within TradingView’s limits.

What Can’t Pine Script Do?

Knowing the limits saves time and money.

  • It cannot send orders to your broker on its own. A strategy trades only inside TradingView’s simulator. To trade for real from a script, you need alerts plus another program that receives them. How to send TradingView alerts to MT4 or MT5 shows that chain and why it can fail.
  • It cannot predict the market. An indicator describes past prices. A good-looking line on history says nothing about the next bar.
  • It cannot be a general-purpose program. It is a chart language, not a tool for building apps or programs that run on your own computer. The documentation itself says it is not likely to become a full-fledged language, so it stays focused on charts and trading tools.
  • It cannot make a backtest honest. A strategy test can look better than live trading, especially if the script repaints, which means its signals change after the fact. Repainting in TradingView explains the trap.

What Are the Three Kinds of Pine Script?

Every script declares what it is on its first lines. An indicator shows information, a strategy also simulates trades, and a library holds functions that other scripts can import. The full comparison, with a table, is in Pine Script indicators, strategies and libraries, so we will not repeat it here.

Which Pine Script Versions Exist?

TradingView has released several versions of the language, and the current one is version 6. Older scripts written for versions 4 and 5 still run, and many scripts you find online use them. The version changes some names and rules, so code written for one version can fail in another. The ForexR library lets you filter by version 6 if you want only the newest syntax.

Who Is Pine Script For?

It suits traders who already use TradingView charts and want a custom tool without learning a large programming language. It is a reasonable first language, because you see the result on a chart straight away. It is less suited to a trader who wants a robot that places orders at a broker without extra tools. For that job, many people look at MetaTrader instead; Pine Script vs MQL5 compares the two.

Where Can You Get Pine Scripts?

You can write your own, copy one from a book or forum, or use open-source code from GitHub. The ForexR Pine Script Library is a free directory of open-source TradingView scripts published on GitHub, with thousands of indicators, strategies and libraries. It links to each original repository. ForexR does not test or recommend any script, so treat every script as unproven until you have checked it. The library has a search box, filters and a Copy script button on every page, so you can find a script by type, version or category.

What Is a Good Learning Path?

  1. Run other people’s scripts first. Add two or three from the library and change their settings.
  2. Read before you write. Open the code of a short indicator and match each line to what you see on the chart.
  3. Write the first indicator. Pine Script basics: write your first indicator takes you through it step by step.
  4. Add inputs, then alerts, then a simple strategy. Test each one on a demo chart before you trust it.

Pine Script is a tool for drawing and testing ideas, not a source of profit. Backtests flatter, and indicators fail in ranging markets and around news. Leveraged forex and CFD trading carries a high risk of losing money quickly, so only risk money you can afford to lose.

FAQ

Is Pine Script free to use?

You need a TradingView account to save and run scripts, and a free account is enough to start. Some limits, such as how many indicators sit on one chart, depend on your plan, so check TradingView’s own pages for current limits. Reading and learning the language costs nothing.

Is Pine Script hard to learn for a beginner?

It is one of the easier languages to begin with, because scripts are short and you see the result on the chart at once. You still need to learn how it runs once per bar and how history is read. Start by changing small things in working scripts, then write a simple indicator.

Do I need to know programming before I learn Pine Script?

No. Many traders learn Pine as their first language. It helps to understand what a variable and a function are, but guides and examples explain these as you go. Basic chart knowledge, such as what a moving average is, matters more than prior coding.

Can Pine Script trade automatically for me?

Not by itself. A Pine strategy only simulates trades inside TradingView. A script can send an alert, and another program can read that alert and place an order, but that adds delay and more points of failure. Test any such setup on a demo account first.

Related on ForexR

Next lesson How to Use the ForexR Pine Script Library: Find & Copy Scripts Continue

More Beginner Guides

What Is Forex Trading? A Beginner's Guide to the FX Market How to Start Forex Trading: A Step-by-Step Beginner Guide Currency Pairs Explained: Majors, Minors & Exotics Best Currency Pairs to Trade for Beginners: Where to Start Forex Trading Terms: A Plain-English Glossary for Beginners Going Long vs Going Short in Forex: Buy and Sell Explained Forex Spreads and Pips: What They Cost You Forex Lot Sizes and Order Types Explained for Beginners Leverage and Margin in Forex: A Beginner's Guide Margin Call and Stop Out in Forex: How They Work What Is Swap in Forex? Overnight Fees Explained Simply How Much Money Do You Need to Start Forex Trading? How to Choose a Forex Broker: Regulation, Costs & Checklist How Forex Brokers Make Money: Spreads, Swaps, IBs and B-Books Forex Rebates and Cashback Explained: Who Pays and What You Get Is Forex Trading Legal? How Forex Regulation Works Worldwide Forex Demo Account: How to Practise Trading Properly MT4 vs MT5: Key Differences and Which One to Choose How to Use MT4 and MT5: MetaTrader Tutorial for Beginners Types of Expert Advisors: Robots, Trade Managers, Copiers & More How to Use the ForexR EA Library: Find, Check & Download EAs How to Install an Expert Advisor in MT4 and MT5 (Step by Step) How to Use the ForexR Pine Script Library: Find & Copy Scripts How to Add a Pine Script to TradingView: Copy, Paste & Run Pine Script v4, v5 and v6: What the Version Line Means Pine Script vs MQL5: TradingView or MetaTrader for Trading? How to Read a Forex Chart: Candlesticks & Trends Forex Trading Sessions: Hours and Best Times Best Time to Trade Forex: Best Hours, Days and Sessions How to Use an Economic Calendar for Forex How to Use Forex Market Analysis and News to Trade Forex vs Stocks: Which Is Better for Beginners to Trade? Forex vs Crypto Trading: Key Differences and Which Suits You How to Trade Gold (XAU/USD): A Beginner’s Six-Step Guide Why Does Gold Have Value? A Plain-English History of Money What Moves the Gold Price? 7 Drivers Explained in Plain English Physical Gold vs Gold CFDs, ETFs and Futures: 10 Ways to Own Gold Gold Lot Size, Pip Value and Margin Explained (XAU/USD) Best Time to Trade Gold: Sessions, Hours and the Daily Break Gold Spread, Swap Fees and Weekend Gaps: What Gold Costs Troy Ounces, Grams and Karats: How Gold Is Priced and Weighed Gold Trading Glossary: 40 Terms Explained in Plain English How to Read a Gold Price Forecast (and Why They Miss) 10 Gold Trading Mistakes That Empty Small Accounts Fast What Is Copy Trading in Forex? How It Works and What It Costs Forex Signals Explained: How They Work and How to Judge Them What Is AI Trading? A Plain-English Guide for Beginners AI Trading vs Algorithmic Trading: The Real Difference How to Use AI Chatbots for Trading Research Safely Forex Scams: How to Spot Them and Keep Your Money Safe AI Trading Scams: How to Spot Fake Bots and Deepfakes 12 Common Forex Trading Mistakes and How to Fix Them Is Forex Trading Profitable? What the Numbers Say Is Forex Trading Halal or Haram? A Neutral Explainer Is Gold Trading Halal? Spot, CFDs and Swap-Free Accounts Zakat on Gold, Crypto and Trading Accounts: A Simple Guide

Choose a Regulated Broker

Put what you’ve learned to work with a regulated broker. Compare costs, platforms and licences side by side.

Compare top brokers