Skip to main content

Pine Script v6 Cheat Sheet: Operators, Functions, and Data Types

· 14 min read
Pineify Team
Pine Script and AI trading workflow research team

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

The Best Pine Script Generator

Math Operators

Just basic arithmetic:

SymbolWhat 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

SymbolWhat 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

WordWhat It Means
andBoth things must be true
orAt least one thing must be true
notFlip it -- if it was true, make it false
?:Shorthand for "if this, then that, otherwise this other thing"

Assignment Operators

SymbolWhat 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:

WordWhat It Does
importBorrow functions from someone else's script
exportLet others use your functions
methodCreate a function that belongs to your custom data type
typeMake your own custom container for data
matrixWork with data in rows and columns (like Excel)
varCreate a variable that remembers its value across bars
varipLike 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 TypeWhat It's Like
MatrixA spreadsheet with rows and columns
ArrayA shopping list -- items in order
String[]Older way to say "list of text items"
StringA single piece of text

Types of Information You Can Work With

TypeWhat It HoldsExample
StringText and words"Apple stock is rising"
Integer (int)Whole numbers42, 100, -5
FloatNumbers with decimals3.14, 99.9, -0.5
Boolean (bool)True or falsetrue, false
ColorColors for your chartsred, blue, custom colors
LineLines you can drawTrend lines, support lines
LinefillColored areas between linesShaded regions
BoxRectangles on your chartPrice ranges
LabelText labels on your chart"Buy here!"
TableOrganized data displayInfo tables

Creating Your Own Data Types

Sometimes you need to group related information -- like a trading setup with entry price, direction, and targets:

PartWhat It IsExample
Basic IdeaGroup related data togetherLike a trading setup with entry price, direction, and signals
How to Make OneUse the type keywordtype MyTradingSetup
What Goes InsideDifferent pieces of related informationPrice levels, trade direction, stop loss
Simple vs ComplexSimple types can have default values, complex ones can'tNumbers and text can have defaults, lists can't
Making a CopyUse .new() to create an instanceMyTradingSetup.new()
Getting Data OutUse a dot to access the informationmySetup.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:

ConceptWhat It Means
Loosely TypedYou don't have to declare what type of data you're storing
Type InferencePine Script figures it out from what you assign
Immutable TypeOnce determined, the type can't change later

Examples

What You WriteWhat 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 WantFunctionWhat It Does
RSI indicatorta.rsi()Shows if something is overbought or oversold -- learn how to code RSI from scratch
Moving averagesta.sma(), ta.ema()Smooths out price movements -- check out this EMA coding guide
Bollinger Bandsta.bb()Shows price channels -- here's a complete guide to coding them
Volume analysista.obv, ta.pvtTracks volume patterns -- master volume indicators in Pine Script
Money flowta.mfi()Shows if money is flowing in or out

Math Helpers

What You NeedFunctionWhat It Does
Remove negative signsmath.abs()Makes -5 become 5
Round numbersmath.round()Makes 3.7 become 4
Square rootmath.sqrt()Mathematical square root
Random numbersmath.random()Gives you random values
Find extremesmath.max(), math.min()Finds highest/lowest values

Time and Data

What You WantFunctionWhat It Does
Current yeartime.yearGets the current year
Current monthtime.monthGets the current month
Other market datarequest.security()Gets data from other symbols
Company financialsrequest.financial()Gets fundamental data

Arrays

What You WantFunctionWhat It Does
Make a new listarray.new()Creates an empty list
Add to listarray.push()Adds item to the end
Remove from listarray.pop()Takes item from the end
Find somethingarray.indexof()Finds where something is
Sort the listarray.sort()Puts items in order

Matrices

What You WantFunctionWhat It Does
Make a tablematrix.newCreates a new data table
Put data inmatrix.setAdds a value
Get data outmatrix.getRetrieves a value
Add up valuesmatrix.sumCalculates totals
Flip the tablematrix.transposeSwaps rows and columns

Things to Remember

TopicWhat You Need to Know
Storing DataSingle values are simple. Arrays are lists. Matrices are like spreadsheets.
Custom TypesMust have at least one field. Can't start names with numbers. Only simple types can have default values.
FunctionsNeed a name and at least one input. Can return values. Can be shared with others.
CommentsStart 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: buyPrice and sellPrice beat x and y every 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.
Pineify | Best Pine Script Editor

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.