Skip to content
Wildflower
Join free
Coding strategies in Pine Script

Free Trading School · Level 7 · Course 7.4

If statements and conditions

Lesson 7 of 16 in Coding strategies in Pine ScriptModule 2: Indicators and conditions

Advanced · Coding strategies in Pine Script

If statements and conditions

Wildflower Trading School · Lesson 7.4.2.2

Wildflower

You can already ask the chart a yes or no question. Today the script starts acting on the answer. By the end of this lesson, you'll know how to write an if statement, and why the spaces at the start of a line matter. You'll also turn a written rule into code that does one thing when it's true and another when it's false.

1 of 8

What you'll learn

  • Turn a rule into a yes or no. Write it as a comparison, like close > ma.
  • Run lines with if and else. If runs them when true. Else runs others when false.
  • Indent lines that belong. Use four spaces or a tab to put a line inside an if.
  • Decide inside, draw outside. Drawing commands can't go inside an if.
  • Keep indicators at the top. Inside an if, they skip candles and go wrong.

Exercise

  1. Add the Above or below script to a 5-minute MES1! chart.
  2. Then change the rule from close > ma to low > ma (the whole candle above the average) and save.
  3. Count how many green stretches you lose, and write one sentence on how the two rules differ.

Next: Crossovers and signals.

Quick check

  1. 1. How does Pine Script know a line belongs to an if?

  2. 2. Where should plot() go?

  3. 3. Why put ta.sma() at the top, not inside an if?

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 “Crossovers and signals” is waiting next time.

Read the full notes

In Pine Script, an if statement runs the lines indented under it only when its condition is true, and else runs other lines when it's false, so a written rule like close > ma can make the script act.

  • A written rule becomes a true-or-false question: aboveAvg = close > ma. if runs the lines under it only when the answer is true; else runs other lines when it's false.
  • Lines belong to an if when they're indented by four spaces or a tab. A line back at the left edge runs on every .
  • Use := inside the if to change a variable made before it. Drawing commands (plot, plotshape, bgcolor) can't go inside an if: decide inside, draw outside.
  • Work out (ta. lines) at the top, outside any if. Inside an if, they'd skip candles and their memory of past prices would go wrong; the editor warns about this.
  • Join conditions with and/or: longSetup = close > ma and > 50 is a made-up example. Mark a rule on the chart and check a few candles by hand before building on it. See Why vague rules fail and Turning a feeling into a rule.

Worked example

Made-up numbers

Which colour on each candle?

Made-up candles · 20-candle average at 5,018.25

  1. Close 5,021.50: close > ma is true, so lineColor := color.green
  2. Close 5,016.75: close > ma is false, so the else branch sets color.red
  3. A candle that dips to 5,017.00 but closes at 5,019.00 stays green: the rule says close, not touch

The code does exactly what the rule says, so the rule must say exactly what you mean.

Your turn

Drill

Made-up candle · close 5,016.75 · ma 5,018.25 · 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.