Pine Script v6 Cheat Sheet: Operators, Functions, and Data Types
Pine Script v6 is TradingView's scripting language for building custom indicators and trading strategies. I've been writing Pine Script for about two years, and this cheat sheet collects the operators, data types, and built-in functions I reference most. Whether I'm coding an RSI divergence scanner for AAPL or a trailing stop for ES futures, these are the fundamentals.
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 |
|---|---|
| = | Store this 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 | Borrow functions from someone else's script |
| export | Let others use your functions |
| method | Create a function that belongs to your custom data type |
| 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 | Like var, but updates on every tick |
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 |
| Simple vs Complex | Simple types can have default values, complex ones can't | Numbers and text can have defaults, lists can't |
| Making a Copy | Use .new() to create an instance | MyTradingSetup.new() |
| Getting Data Out | Use a dot to access the information | mySetup.entryPrice |
Here's a simple example:
type TradingIdea
float buyPrice = 0.0 // This can have a default
string direction // This can too
array<float> targets // This can't have a default
Variables and Constants
Pine Script is pretty good at figuring types out on its own:
| Concept | What It Means |
|---|---|
| Loosely Typed | You don't have to declare what type of data you're storing |
| 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. Here are the ones I actually use:
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 -- here's a complete guide to coding them |
| Volume analysis | ta.obv, ta.pvt | Tracks volume patterns -- master volume indicators in Pine Script |
| Money flow | ta.mfi() | Shows if money is flowing in or out |
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 |
|---|---|---|
| Current year | time.year | Gets the current year |
| Current month | time.month | Gets the current 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() | Creates an empty list |
| 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 table | matrix.new | Creates a new data table |
| Put data in | matrix.set | Adds a value |
| Get data out | matrix.get | Retrieves a value |
| Add up values | matrix.sum | Calculates totals |
| 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. Can't start names with numbers. Only simple types can have default values. |
| Functions | Need a name and at least one input. Can return values. Can be shared with others. |
| Comments | Start with "//" for notes. Use "//@" for documentation. |
My Tips for Writing Pine Script
After working with Pine Script for a while, here's what I've learned:
- Start simple: I tried building a multi-timeframe strategy on day one and it was a mess. Start with basic indicators. If you're new, this beginner's tutorial walks through the fundamentals step by step.
- Use descriptive names:
buyPriceandsellPricebeatxandyevery time. Your future self will thank you. - Comment everything: I regularly forget what my own code does two weeks later. Leave yourself notes.
- Test with simple examples: Before running complex logic on live data, backtest with basic scenarios. Learn how to backtest properly to validate your ideas.
- One thing at a time: Don't try to learn arrays, matrices, and custom types all in one session. I spread it across a weekend.
Website: Pineify
I code most of my Pine Scripts by hand, but I've also used Pineify for quick experiments. It's a visual tool that generates Pine Script from dropdowns and forms -- no syntax to memorize. I've found it useful when I need to prototype an idea fast, especially for standard indicators like moving average crosses or RSI-based signals. That said, the generated scripts can include more boilerplate than I'd write manually, and I wouldn't rely on it for very custom logic or niche indicator combinations. It's best as a starting point, not a replacement for understanding the language.
Check out all of Pineify's features here
Frequently Asked Questions
▶What is Pine Script v6 and how is it different from earlier versions?
Pine Script v6 is TradingView's latest scripting language for creating custom indicators and strategies. The key additions are custom data types with the type keyword, improved matrix and array operations, and var/varip declarations for memory management. It's mostly backward-compatible with v5 but offers more flexibility for complex trading logic.
▶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?
It's designed to be accessible -- loosely typed variables, clear syntax, and plenty of built-in functions for technical analysis. 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. One limitation: only simple types (int, float, string, bool) can have default values -- arrays and nested types can't.
▶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.


