Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Reading price data

Lesson 4 of 16 in Coding strategies in Pine ScriptModule 1: Pine Script basics

Advanced · Coding strategies in Pine Script

Reading price data

Wildflower Trading School · Lesson 7.4.1.4

Wildflower

Every rule you wrote in Level 3 talks about prices: a close above a line, a high broken, a candle bigger than the one before. Today you'll learn how a script reads those prices. By the end of this lesson, you'll know the five built-in price variables and how to reach back to the candle before this one. You'll also see why the candle that's still forming needs care.

1 of 8

What you'll learn

  • Read the five price values. They match the numbers the chart shows for each candle.
  • Reach back with square brackets. close[1] is the close one candle back.
  • Turn rules into comparisons. Compare this candle with the one before it.
  • Count back in candles. [1] means one candle back on any timeframe.
  • Wait for the close. On the live candle, only the open is fixed until it closes.

Exercise

  1. On a 5-minute MES1! chart, add the Close and close[1] script.
  2. Hover over three candles in a row and check that the orange line on each candle matches the blue line on the candle before.
  3. Then write, as a comment, a rule from your own strategy sheet using close[1] or high[1].

Next: Plotting lines and shapes.

Quick check

  1. 1. What does high[1] mean?

  2. 2. Which rule finds an inside bar?

  3. 3. On the live candle, which price is already fixed?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Plotting lines and shapes” is waiting next time.

Read the full notes

Pine Script reads prices with the built-in variables open, high, low, close and volume, and reaches back to earlier candles with square brackets: close[1] is the previous candle's close.

  • Five built-in price variables: open, high, low, close and . TradingView fills them in on every ; they match the numbers at the top left of the chart.
  • Square brackets reach back: close[1] is the close one candle back, close[2] two candles back, high[1] the previous high. On the chart's first candle there's nothing before it, so close[1] is na (not available).
  • Examples: close > close[1] (a higher close); high > high[1] (broke the last high); high < high[1] and low > low[1] (an ).
  • On a 5-minute chart, [1] means 5 minutes back; on any , it means one candle back.
  • On the live candle, close is the latest price and the high and low can still change; only the open is fixed. A rule that reads close is only settled when the candle closes. See Repainting indicators and hindsight.

Worked example

Made-up numbers

Three rules on three candles

Made-up 5-minute candles · 9:40 H 5,014.00 L 5,008.50 C 5,013.25 · 9:45 H 5,013.75 L 5,010.25 C 5,011.00 · 9:50 H 5,015.50 C 5,015.00

  1. On 9:50: close > close[1] → 5,015.00 > 5,011.00, true
  2. On 9:50: high > high[1] → 5,015.50 > 5,013.75, true
  3. On 9:45: high < high[1] and low > low[1] → 5,013.75 < 5,014.00 and 5,010.25 > 5,008.50, true: an inside bar

Each rule compares this candle with the one before, using [1].

Your turn

Drill

Made-up candles · close = 5,012.25 · close[1] = 5,010.50 · tick size 0.25

Trading is risky. You can lose money. Test results are hypothetical, and a good test doesn't mean future profits. This lesson explains how Pine Script works. It isn't advice.