Pine Script v6 Cheat Sheet: Operators, Functions, and Data Types
Pine Script v6 is TradingView's language for indicators, strategies, and libraries. This reference separates syntax, types, and execution behavior. Function names in the tables are lookup entries, not complete executable calls; consult the linked reference for arguments and return types.
If you're wondering what language is Pine Script, it's TradingView's domain-specific scripting language for financial analysis and automated strategies.
The Basic Building Blocks
Math Operators
Just basic arithmetic:
| Symbol | What It Does |
|---|---|
| + | Adds numbers (2 + 3 = 5) |
| - | Subtracts numbers (5 - 2 = 3) |
| * | Multiplies numbers (3 * 4 = 12) |
| / | Divides numbers (10 / 2 = 5) |
| % | Returns the remainder (10 % 3 = 1) |
Comparison Operators
| Symbol | What It's Asking |
|---|---|
| == | Are these exactly the same? |
| != | Are these different? |
| > | Is the first one bigger? |
| < | Is the first one smaller? |
| >= | Is the first one bigger or equal? |
| <= | Is the first one smaller or equal? |
Logic Operators
| Word | What It Means |
|---|---|
| and | Both things must be true |
| or | At least one thing must be true |
| not | Flip it: if it was true, make it false |
| ?: | Shorthand for "if this, then that, otherwise this other thing" |
Assignment Operators
| Symbol | What It Does |
|---|---|
| = | Declare a variable and assign its initial value |
| := | Update the value |
| += | Add to what's already there |
| -= | Subtract from what's already there |
| *= | Multiply what's already there |
| /= | Divide what's already there |
| %= | Store the remainder |
Words That Mean Something Special
The Important Ones
Pine Script has reserved keywords that do specific things:
| Word | What It Does |
|---|---|
| import | Import an explicitly published Pine library |
| export | Expose supported declarations from a library |
| method | Define a function callable with method notation on its typed first parameter |
| type | Make your own custom container for data |
| matrix | Work with data in rows and columns (like Excel) |
| var | Create a variable that remembers its value across bars |
| varip | Preserve assigned state across realtime updates by escaping rollback |
Reserved Words (Don't Use These)
These are set aside for future use:
catch, class, do, ellipse, in, is, polygon, range, return, struct, text, throw, try
Just don't name your variables after any of them and you'll be fine.
How to Store Your Data
Think of these as different types of containers:
| Container Type | What It's Like |
|---|---|
| Matrix | A spreadsheet with rows and columns |
| Array | A shopping list: items in order |
| string[] | Older way to say "list of text items" |
| string | A single piece of text |
Types of Information You Can Work With
| Type | What It Holds | Example |
|---|---|---|
| string | Text and words | "Apple stock is rising" |
| Integer (int) | Whole numbers | 42, 100, -5 |
| float | Numbers with decimals | 3.14, 99.9, -0.5 |
| Boolean (bool) | True or false | true, false |
| color | Colors for your charts | red, blue, custom colors |
| line | Lines you can draw | Trend lines, support lines |
| linefill | Colored areas between lines | Shaded regions |
| box | Rectangles on your chart | Price ranges |
| label | Text labels on your chart | "Buy here!" |
| table | Organized data display | Info tables |
Creating Your Own Data Types
Sometimes you need to group related information: like a trading setup with entry price, direction, and targets:
| Part | What It Is | Example |
|---|---|---|
| Basic Idea | Group related data together | Like a trading setup with entry price, direction, and signals |
| How to Make One | Use the type keyword | type MyTradingSetup |
| What Goes Inside | Different pieces of related information | Price levels, trade direction, stop loss |
| Field defaults | Defaults have restrictions; runtime function calls cannot initialize a field declaration | Pass a newly created array to the constructor |
| Creating an instance | Use .new(); copying an existing object is a separate operation | MyTradingSetup.new() |
| Getting Data Out | Use a dot to access the information | mySetup.entryPrice |
This complete indicator illustrates a user-defined type and constructor. It plots a stored reference price, not a trade signal. The array is created in the constructor call rather than in a field default.
//@version=6
indicator("UDT reference example", overlay = true)
type TradingIdea
float buyPrice = na
string direction = "unassigned"
array<float> targets
var TradingIdea idea = TradingIdea.new(close, "example", array.new<float>())
plot(idea.buyPrice, "Stored reference price")
The first confirmed value persists through var; this example does not update an entry or calculate returns. See the type-system documentation for field defaults and object references.
Variables and Constants
Pine Script is pretty good at figuring types out on its own:
| Concept | What It Means |
|---|---|
| Statically Typed | Types are checked even when an explicit type keyword is omitted |
| Type Inference | Pine Script figures it out from what you assign |
| Immutable Type | Once determined, the type can't change later |
Examples
| What You Write | What Pine Script Thinks |
|---|---|
price = 100 | "This is a whole number" |
price = 100.50 | "This is a decimal number" |
symbol = "AAPL" | "This is text" |
isRising = true | "This is true/false" |
Built-in Functions
Pine Script ships with dozens of pre-built functions so you don't have to code everything from scratch. These are common lookup entries:
Technical Analysis
| What You Want | Function | What It Does |
|---|---|---|
| RSI indicator | ta.rsi() | Shows if something is overbought or oversold: learn how to code RSI from scratch |
| Moving averages | ta.sma(), ta.ema() | Smooths out price movements: check out this EMA coding guide |
| Bollinger Bands | ta.bb() | Shows price channels |
| Volume analysis | ta.obv, ta.pvt | Tracks volume patterns |
| Money flow | ta.mfi() | Price-and-volume oscillator; not a record of investor cash flows |
Math Helpers
| What You Need | Function | What It Does |
|---|---|---|
| Remove negative signs | math.abs() | Makes -5 become 5 |
| Round numbers | math.round() | Makes 3.7 become 4 |
| Square root | math.sqrt() | Mathematical square root |
| Random numbers | math.random() | Gives you random values |
| Find extremes | math.max(), math.min() | Finds highest/lowest values |
Time and Data
| What You Want | Function | What It Does |
|---|---|---|
| Bar year | year(time) | Calendar year of the bar timestamp, using the exchange timezone by default |
| Bar month | month(time) | Calendar month of the bar timestamp, not necessarily the current wall-clock month |
| Other market data | request.security() | Gets data from other symbols |
| Company financials | request.financial() | Gets fundamental data |
Arrays
| What You Want | Function | What It Does |
|---|---|---|
| Make a new list | array.new<float>() | Creates an empty float array when size is omitted |
| Add to list | array.push() | Adds item to the end |
| Remove from list | array.pop() | Takes item from the end |
| Find something | array.indexof() | Finds where something is |
| Sort the list | array.sort() | Puts items in order |
Matrices
| What You Want | Function | What It Does |
|---|---|---|
| Make a matrix | matrix.new<float>() | Creates a matrix; a chart table is a different object |
| Put data in | matrix.set | Adds a value |
| Get data out | matrix.get | Retrieves a value |
| Add matrices or a scalar | matrix.sum() | Returns element-wise addition; does not reduce all cells to one total |
| Flip the table | matrix.transpose | Swaps rows and columns |
Things to Remember
| Topic | What You Need to Know |
|---|---|
| Storing Data | Single values are simple. Arrays are lists. Matrices are like spreadsheets. |
| Custom Types | Must have at least one field. Field defaults cannot contain arbitrary runtime expressions. |
| Functions | Can have zero or more parameters. Return the final expression or supported result from their body. |
| Comments | Start with "//" for notes. Use supported annotations such as "//@variable" for documentation. |
Execution details that syntax alone does not show
varpreserves state across bars;varipadditionally preserves intrabar changes across rollback. Neither keyword causes a strategy to execute on every tick by itself. See variable declarations.- A historical offset reads earlier values; it does not request future prices. Multi-timeframe requests need a separate check for confirmation timing and lookahead.
- Pine v6 changes include lazy
and/orevaluation and removal of thenastate for booleans. User-defined types, arrays, andvarwere available before v6. Use the v6 migration guide when converting a script. - Check a small reproducible calculation before adding strategy orders. Compilation checks syntax and types; it does not validate a trading hypothesis.
Website: Pineify
Pineify's visual editor generates Pine Script from supported inputs and conditions. Its Coding Agent can help draft custom logic. Review the resulting code and behavior in TradingView; a generated script still needs the same timing and data checks as handwritten code. See the Visual Pine Script Editor manual.
Primary reference pages
Use TradingView's v6 reference manual for exact overloads, the time guide for timestamps, the matrices guide for matrix operations, and user-defined functions for parameter and return rules.
Frequently Asked Questions
What is Pine Script v6 and how is it different from earlier versions?
Pine Script v6 supports indicators, strategies, and libraries. Its migration guide documents behavior changes such as lazy boolean evaluation and booleans no longer accepting na. Types, arrays, and var are not new v6 features; older scripts require migration checks.
How do I use comparison operators in Pine Script v6?
Same as standard math. == for equality, != for inequality, > and < for comparisons, >= and <= for inclusive checks. Combine them with and, or, and not to build multi-condition signals.
Is Pine Script v6 beginner-friendly for traders with no coding experience?
Type inference can reduce explicit declarations, but Pine is statically typed and has a bar-based execution model. If coding's not your thing, Pineify's visual script generator creates indicators through a point-and-click interface without writing code.
How can I create custom data types in Pine Script v6?
Use the type keyword. For example, type TradingIdea groups fields like entry price, direction, and targets into one structure. Instantiate with .new() and access fields with dot notation. Field defaults have restrictions. For a newly created array, pass the array into the constructor instead of calling its constructor in the field declaration.
What built-in functions does Pine Script v6 offer for technical analysis?
Pine Script v6 includes ta.rsi() for RSI, ta.sma() and ta.ema() for moving averages, ta.bb() for Bollinger Bands, ta.obv for volume analysis, and ta.mfi() for money flow. Plus math helpers (math.abs(), math.round(), math.sqrt()) and full array and matrix manipulation libraries.
What words and keywords are reserved in Pine Script v6?
Reserved for future use: catch, class, do, ellipse, in, is, polygon, range, return, struct, text, throw, try. Active keywords you shouldn't use as variable names: import, export, method, type, matrix, var, varip.
If you're curious about other approaches to Pine Script development, check out how AI is changing Pine Script coding and some real Pine Script v6 strategy examples to see what's possible.


