Pine Script - Complete Programming Guide

Welcome to the most comprehensive Pine Script tutorial. This guide will take you from absolute beginner to building professional-grade trading indicators and automated strategies.


Table of Contents

  1. introduction
  2. variables
  3. operators
  4. functions
  5. inputs
  6. plotting
  7. moving-averages
  8. rsi
  9. macd
  10. bollinger-bands
  11. atr
  12. volume
  13. basics
  14. entries-exits
  15. stops
  16. backtesting
  17. timeframes
  18. custom-functions
  19. arrays
  20. optimization

What is Pine Script?

Pine Script is TradingView’s proprietary programming language designed specifically for financial chart analysis. Unlike general-purpose programming languages, Pine Script is optimized for:

  • Time series data - Built-in support for OHLCV (Open, High, Low, Close, Volume) data
  • Indicator development - Easy-to-use functions for technical analysis
  • Strategy backtesting - Test your trading ideas on historical data
  • Real-time alerts - Get notified when market conditions match your criteria
  • Multi-timeframe analysis - Analyze data across different timeframes

Why Learn Pine Script?

BenefitDescription
Free to useNo additional software needed
Large communityAccess thousands of free scripts
Fast developmentSimple syntax, quick prototyping
Powerful featuresBuild complex indicators
Career opportunitiesPine Script developers are in demand

Version History

Pine Script has evolved significantly since its introduction:

VersionYearKey Features
v12011Basic plotting, limited functions
v22015Functions, security(), better debugging
v32019Arrays, var, line operations
v42020Objects, charts, drawing tools
v52022Matrix, enhanced debugging
v62024Latest features, performance improvements

Always use the latest version (v5 or v6) for your scripts.


Your First Pine Script

Let’s start with the simplest possible indicator:

//@version=6
indicator("My First Script")
plot(close)

What this does:

  • //@version=6 - Uses Pine Script version 6
  • indicator("My First Script") - Names your script
  • plot(close) - Plots closing prices on the chart

Step-by-Step Explanation

//@version=6
indicator("Simple Price Plot")
 
// Calculate 20-period Simple Moving Average
sma20 = ta.sma(close, 20)
 
// Plot the SMA
plot(sma20, color=color.blue, title="20 SMA")

Breaking down each element:

//@version=6                           // 1. Version declaration
indicator("Simple Price Plot")         // 2. Script name
 
// INPUT: User-configurable parameter
length = input.int(20, "Length")      // 3. User input
 
// CALCULATION: Calculate SMA
smaValue = ta.sma(close, length)      // 4. Indicator calculation
 
// OUTPUT: Draw on chart
plot(smaValue,                        // 5. What to plot
     color=color.blue,                // 6. Color
     title="20 SMA")                  // 7. Label

Understanding the Code Structure

Every Pine Script follows this structure:

┌─────────────────────────────────────────┐
│  1. VERSION DECLARATION                 │
│     //@version=6                       │
├─────────────────────────────────────────┤
│  2. SCRIPT TYPE                         │
│     indicator() or strategy()          │
├─────────────────────────────────────────┤
│  3. INPUTS (Optional)                   │
│     input.*()                          │
├─────────────────────────────────────────┤
│  4. CALCULATIONS                        │
│     Variables, functions, logic        │
├─────────────────────────────────────────┤
│  5. OUTPUT                              │
│     plot(), plotshape(), fill()        │
└─────────────────────────────────────────┘

Script Types

There are two main types of Pine Scripts:

// INDICATOR - For analysis only (no trading)
indicator("My Indicator", overlay=true)
 
// STRATEGY - For backtesting and trading
strategy("My Strategy", overlay=true)

Key Concepts Explained

1. Series vs Int vs Float

//@version=6
indicator("Data Types Demo")
 
// SERIES - Changes every bar (most common)
seriesPrice = close
seriesMA = ta.sma(close, 20)
 
// INT - Whole number
intCount = 10
 
// FLOAT - Decimal number
floatPrice = 1234.56
 
plot(seriesPrice)

2. Bar Context

//@version=6
indicator("Bar Context")
 
// Current bar
currentClose = close
 
// Previous bar (1 bar ago)
prevClose = close[1]
 
// 5 bars ago
fiveBarsAgo = close[5]
 
// Check if current bar is green
isGreen = close > open

3. Built-in Variables

// Price data
open    // Open price
high    // High price
low     // Low price
close   // Close price
volume  // Trading volume
 
// Bar information
bar_index     // Current bar number
time          // Bar timestamp
ta.sma()      // Moving average
ta.ema()      // Exponential moving average
ta.rsi()      // Relative Strength Index

Common Errors and How to Fix Them

Error: Compilation Error

Problem: Code won’t compile Solution: Check for:

  • Missing closing brackets )
  • Missing quotes ""
  • Typo in function names
  • Duplicate variable names

Error: Loop Not Allowed

Problem: Traditional for-loops aren’t supported in v2 Solution: Use vector operations:

// Wrong
for i = 0 to 10
    sum := sum + close[i]
 
// Correct
sum = math.sum(close, 11)

Error: Reference Undeclared Variable

Problem: Using variable before defining it Solution: Always define variables before use


Next Steps

Now that you understand the basics, continue with:

  1. variables - Deep dive into data types and variables
  2. operators - Mathematical and logical operations
  3. functions - Using and creating functions
  4. moving-averages - Build your first indicator

Quick Reference

// Basic structure
//@version=6
indicator("Name", overlay=true)
 
// Inputs
input.int(14, "Period")
input.float(2.0, "Multiplier")
input.color(color.blue, "Color")
 
// Calculations
sma = ta.sma(close, 20)
ema = ta.ema(close, 20)
rsi = ta.rsi(close, 14)
 
// Output
plot(sma)
plotshape(condition)
hline(50)

Start your Pine Script journey here. Complete all tutorials to become proficient.

5 items under this folder.