Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

Variables, numbers and true or false

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

Advanced · Coding strategies in Pine Script

Variables, numbers and true or false

Wildflower Trading School · Lesson 7.4.1.3

Wildflower

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

  1. In the Pine Editor, write the Range in ticks script and add it to a 5-minute MES1! chart.
  2. Then add a variable, isUpCandle = close > open, and write in a comment line (starting with //) what it will be on the last candle.
  3. Check by looking at the candle.

Next: Reading price data.

Quick check

  1. 1. What is the difference between = and == ?

  2. 2. A candle closes below its open. What is close > open?

  3. 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

Make a free account and we'll remember where you are, so “Reading price data” is waiting next time.

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 ), float (numbers with decimals, like prices and a 4.5- 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 . syminfo.mintick is the market's tick size, filled in by TradingView: 0.25 on the . 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 numbers

Two questions about one candle

Made-up candle · open 5,010.00 · high 5,013.00 · low 5,009.50 · close 5,012.25

  1. isUpCandle = close > open: 5,012.25 > 5,010.00, so true
  2. isBig = high - low >= 4: 3.5 points, so false
  3. isUpCandle and isBig: false (both must be true)
  4. 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

Drill

Made-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.