003 — Price data and series
Trading charts are built from bars. Pine Script gives you direct access to each bar’s market data.
Core bar values
Section titled “Core bar values”| Built-in | Meaning |
|---|---|
open |
Opening price |
high |
Highest price |
low |
Lowest price |
close |
Closing or current price |
volume |
Reported volume |
These values are series. Their value can change from one bar to the next.
Current and previous values
Section titled “Current and previous values”Use square brackets to access an earlier bar:
close // Current bar's closeclose[1] // Previous bar's closeclose[2] // Close two bars agoThe number inside the brackets is the historical offset.
Calculate close-to-close change in basis points
Section titled “Calculate close-to-close change in basis points”One basis point is 0.01%, so 100 basis points equal 1%.
//@version=6indicator("Close Change in BPS", overlay = false)
float changeBps = (close / close[1] - 1.0) * 10000.0color barColor = changeBps >= 0 ? color.green : color.red
plot( changeBps, "Close change (bps)", style = plot.style_columns, color = barColor)
hline(0, "Zero")Read the formula
Section titled “Read the formula”(close / close[1] - 1.0) * 10000.0Suppose the previous close was 100 and the current close is 101:
(101 / 100 - 1) × 10,000 = 100 bpsThe price increased by 1%, which equals 100 basis points.
Conditional color
Section titled “Conditional color”color barColor = changeBps >= 0 ? color.green : color.redThis uses the ternary operator:
condition ? value_when_true : value_when_falseGreen is selected when the change is zero or positive; otherwise red is selected.
First-bar behavior
Section titled “First-bar behavior”On the first available chart bar, close[1] does not exist. Calculations depending on it return na, meaning “not available.” This is normal. The plot begins when enough historical data exists.