Introduction to Pine Script
Introduction to Pine Script
What Is Pine Script?
Pine Script is a programming language created by TradingView. Its purpose is specific: to create custom technical analysis tools that run directly on TradingView charts. Think of it like a recipe. Instead of just using the standard dishes on the menu (like the RSI or Moving Average), you can write your own recipe to cook up a unique indicator that follows your exact trading rules.
Pine Script is TradingView’s proprietary programming language that allows traders to turn trading ideas directly into executable tools on charts.
With Pine Script, you can build two main types of tools:
- Indicators: These are visual tools that overlay on your chart to help you analyze price movements. You could, for example, create an indicator that highlights candles where the trading volume is unusually high.
- Strategies: These scripts can execute hypothetical buy and sell orders directly on your chart. This lets you backtest a trading idea to see how it would have performed historically without risking any real money.
The Pine Editor
You don't need any special software to start writing Pine Script. Everything is built directly into the TradingView platform. At the bottom of your TradingView chart, you'll see a panel with several tabs like "Stock Screener," "Text Notes," and "Strategy Tester." One of these is the Pine Editor.
Clicking on the Pine Editor tab opens up a workspace where you can write, edit, and test your code. This is your command center for creating custom tools. Once you write a script, you can add it to your chart to see it in action instantly.
Anatomy of a Simple Script
Every Pine Script follows a basic structure. It might look intimidating at first, but it's quite logical. Every script needs to do three things:
- Declare its version.
- Declare what kind of script it is (an indicator or a strategy).
- Tell TradingView what to do (like draw a line on the chart).
Let's look at the simplest possible script. This code just draws a line representing the closing price of each candle.
// 1. Declare the script version
//@version=5
// 2. Define the script as an indicator with a name
indicator("My First Script")
// 3. Plot the closing price of each bar
plot(close)
Let's break that down line by line.
//@version=5 This is a special comment that tells TradingView which version of Pine Script we're using. Version 5 is the latest, so we'll always start with this.
indicator("My First Script") This function declares the script as an indicator and gives it a name, which will appear on the chart. If it were a backtesting script, you would use strategy() instead.
plot(close) This is the action. The plot() function draws a line on the chart. We're telling it to plot the value of close, which is a built-in variable that holds the closing price of the current candle.
These three components—version, declaration, and action—form the foundation of every Pine Script you'll write.
Ready to test your understanding of these core concepts?
What is the primary purpose of Pine Script?
Where do you write and edit your Pine Script code within the TradingView platform?

