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
- On a 5-minute MES1! chart, add the Close and close[1] script.
- Hover over three candles in a row and check that the orange line on each candle matches the blue line on the candle before.
- Then write, as a comment, a rule from your own strategy sheet using close[1] or high[1].
Quick check
1. What does high[1] mean?
2. Which rule finds an inside bar?
3. On the live candle, which price is already fixed?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
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 How many contracts or shares traded in a stretch of time, such as one candle.More in the glossary. TradingView fills them in on every One bar on a price chart. It shows where price opened and closed, and the highest and lowest it reached, in one stretch of time.More in the glossary; they match the Open, high, low and close: the four prices a candle or bar shows for its stretch of time.More in the glossary 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 A candle whose high and low both stay within the high and low of the candle before it.More in the glossary).
- On a 5-minute chart, [1] means 5 minutes back; on any How much time each candle covers, such as 1 minute or 1 hour.More in the glossary, 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 numbersThree 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
- On 9:50: close > close[1] → 5,015.00 > 5,011.00, true
- On 9:50: high > high[1] → 5,015.50 > 5,013.75, true
- 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
DrillMade-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.