Back to TradingView PineScript for CFD Traders
Part 1

Series, types, and the basic data flow

Every Pine variable is implicitly a time series. Understanding this is what makes Pine feel strange to anyone coming from general-purpose languages.

Beginner6 min readBeginner → AdvancedLesson 02 / 17
Try it in TradingView
Bar-over-bar change from one line

close - close[1] gives you the change on EVERY bar at once — no loop. Watch the histogram flip red/green as bars alternate.

//@version=5
indicator("Lesson 2 - Series and history", overlay=false)

// Bar-over-bar change. One expression, evaluated across all bars.
change = close - close[1]

// Plot as a histogram — green up, red down.
plot(change, "Bar change", style=plot.style_columns,
     color=change >= 0 ? color.new(color.green, 30) : color.new(color.red, 30))

// And the 20-bar change, for comparison.
plot(close - close[20], "20-bar change", color=color.blue)

hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

The series data type

Most Pine values are series — values that change bar by bar across chart history. When you write close, Pine implicitly refers to the close of every bar on the chart, past and future. When you write close - close[1], you get the bar-over-bar change for every bar.

This is the single most important idea in Pine. It is what lets you write plot(close > close[1]) instead of looping over every bar in Python. Pine does that loop for you.

Simple types: int, float, bool, string, color

Pine has five primitive types: int, float, bool, string, color. A series of floats is what close is. A series of bools is what close > close[1] returns. A series of colors is what you pass to plot.

History references: [1], [2], [n]

Square brackets look up past bars. close[1] is the close one bar ago. close[20] is the close 20 bars ago. close[0] is the current bar (same as close). Reading history is the basic building block of every indicator.

na: Pine's "no value"

na means "no value". The first 20 bars of a 20-period moving average are na because there isn't enough history yet. Most plots handle na gracefully by leaving gaps on the chart, but be careful when doing math — close + na is na, not close.

Type mismatch errors

If you write plot(close > close[1]) you cannot plot a bool as a line — Pine will throw a type error. Either cast to a number (plot(close > close[1] ? 1 : 0)) or use plotshape / plotchar for booleans.

Why this matters

Every script you write is a vectorised operation across chart history. Once that clicks, the rest of Pine is just syntax.

What you just did

Lesson 02 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.