Skip to content
TradequixDocsOpen the terminal
OverviewStart here, search, progress
  • Introduction
  • Quickstart
  • How a script runs
  • Types and qualifiers
  • Operators, conditionals and loops
  • Functions and methods
  • Objects and enums
  • Arrays, matrices and maps
  • Strings and colours
  • Time and sessions
  • Bar states
  • Plotting
  • Inputs
  • Technical analysis
  • Drawings and tables
  • Multiple timeframes
  • Strategies
  • Alerts in scripts
  • Repainting
  • The Pine editor
  • Indicator settings
  • Coming from TradingView
  • The data
  • Publish and sell
  • Catalogue and community
  • Your dashboard and earnings
  • Errors and debugging
  • Limits
  • Coverage
  • Built-in catalogue
  • Cookbook
  • Write Pine with AI
  • FAQ
  • Glossary
  • Cheat sheet
  • Function reference
  • Will it run here?
  • Error index

Start

How a script runs

Pine runs your whole script once per bar, oldest to newest. Series, history references, var and na all follow from that.

View as MarkdownOpen in ClaudeOpen in ChatGPT

The single idea behind Pine: your script runs once for every bar on the chart, starting at the oldest and ending at the newest. On each run, close, open, high, low and volume hold that bar's values. Every variable you compute becomes a series: one value per bar.

Step through it yourself:

InteractiveStep through the bars. The script runs once on each.
1/12
//@version=6
indicator("Bar by bar")
up = close > open
var count = 0 // kept between bars
n = 0 // starts again each bar
if up
count += 1
n += 1
avg = ta.sma(close, 3)
VariableOn this barWhy
bar_index0which bar is executing
close1.0848this bar's close
close[1]naone bar back; na on the first bar
uptruerecomputed every bar
count1var: survives to the next bar
n1no var: back to 0 every bar
avgnana until 3 bars exist

History with []#

x[1] is the value x had one bar ago, x[2] two bars ago, and so on. It works on any series, including ones you compute.

RunnableBar-to-bar change
1//@version=62indicator("Bar-to-bar change")3change = close - close[1]4plot(change, "Change", style = plot.style_histogram, color = change >= 0 ? color.teal : color.red)

On the very first bar there is no previous bar, so close[1] is na and so is change.

na: no value yet#

na means "no value". It appears when you look further back than the chart goes, and while an average is still warming up: ta.sma(close, 50) is na for the first 49 bars. na spreads through arithmetic: na + 1 is na.

  • Test for it with na(x).
  • Replace it with nz(x) (gives 0) or nz(x, fallback).
  • A plot simply leaves a gap where the value is na.

var: keep a value between bars#

A plain declaration runs again on every bar, so the variable starts fresh. var declares it once, on the first bar, and keeps whatever you assign afterwards.

RunnableCounting up bars
1//@version=62indicator("Counting up bars")3var int ups = 04if close > open5 ups += 16plot(ups, "Up bars so far", color = color.teal)

Without var, ups would be 0 or 1 on every bar, never a running total.

The current bar#

On a live chart the newest bar is still forming. The script runs again as its price changes, so values on the last bar can move until it closes. Signals that must not change after they appear should check barstate.isconfirmed, which is true only once a bar has closed.

Watch out

Logic that looks ahead (for example close[-1], or request.security with lookahead) can look perfect on history and never happen live. If a backtest looks too good, check for this first.

PreviousQuickstartNext Types and qualifiers

On this page

  • History with []
  • na: no value yet
  • var: keep a value between bars
  • The current bar

Use with AI

View as MarkdownOpen in ClaudeOpen in ChatGPT