Your first script had its numbers typed straight into the code. Today we give them names. By the end of this lesson, you'll know how to store a value in a variable, and the three kinds of value you'll use most. You'll also ask the chart a yes or no question, such as: did this candle close higher than it opened?
1 of 9
What you'll learn
- Store values in variables. A variable is a named box. Change the value in one place.
- Pick the right type. Int is whole numbers, float has decimals, bool is true or false.
- Ask yes or no on every candle. Use > or == to compare. One = stores a value.
- Combine answers. Use and, or and not to turn a written rule into code.
- Change a variable the right way. Use colon equals to update it after you make it.
Exercise
- In the Pine Editor, write the Range in ticks script and add it to a 5-minute MES1! chart.
- Then add a variable, isUpCandle = close > open, and write in a comment line (starting with //) what it will be on the last candle.
- Check by looking at the candle.
Quick check
1. What is the difference between = and == ?
2. A candle closes below its open. What is close > open?
3. isUpCandle is true and isBig is false. What is isUpCandle and isBig?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
In Pine Script, a variable is a named box for a value: an int, a float, a bool (true or false) or a string; comparisons such as close > open answer yes or no on every candle.
- A variable is a named box: length = 20 puts 20 in a box called length. Use the name anywhere below it, and change the value in one place.
- Four types of value: int (whole numbers, like a length of 20 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), float (numbers with decimals, like prices and a 4.5-One whole unit of a price. On the S&P 500, a move from 5,000 to 5,001 is one point, and each futures contract sets what a point is worth in dollars.More in the glossary An order that closes a trade if price reaches a level you set against it. It aims to limit the loss, but in a fast market it can fill at a worse price.More in the glossary distance), bool (true or false) and string (text in quotes). Type words such as int are optional but help readers.
- Comparisons answer yes or no on every candle: > greater than, >= greater than or equal to, < and <=, == exactly equal, != not equal. One = stores a value; == asks a question.
- Combine answers with and (both true), or (at least one true) and not (flips the answer).
- (high - low) / syminfo.mintick gives a candle's size in The smallest step a price can move. On many stock index futures that’s 0.25 of a point.More in the glossary. syminfo.mintick is the market's tick size, filled in by TradingView: 0.25 on the A small futures contract on the S&P 500, traded at CME Group. Each point is worth $5, and its smallest step of 0.25 is worth $1.25.More in the glossary. More: Futures tick value.
- To change a variable after making it, use := (colon equals). Using = twice with the same name is an error.
Worked example
Made-up numbersTwo questions about one candle
Made-up candle · open 5,010.00 · high 5,013.00 · low 5,009.50 · close 5,012.25
- isUpCandle = close > open: 5,012.25 > 5,010.00, so true
- isBig = high - low >= 4: 3.5 points, so false
- isUpCandle and isBig: false (both must be true)
- isUpCandle or isBig: true (one is enough)
And, or and not turn a written rule such as "up candle and big range" into code.
Your turn
DrillMade-up candle · high 5,013.00 · low 5,009.50 · syminfo.mintick = 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.