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
- Add the Above or below script to a 5-minute MES1! chart.
- Then change the rule from close > ma to low > ma (the whole candle above the average) and save.
- Count how many green stretches you lose, and write one sentence on how the two rules differ.
Quick check
1. How does Pine Script know a line belongs to an if?
2. Where should plot() go?
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
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 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.
- 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 A line or set of bars worked out from a market’s prices or volume and drawn on or under the chart, such as a moving average.More in the glossary (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 An indicator from 0 to 100 that compares the size of recent gains with recent losses.More in the glossary > 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 numbersWhich colour on each candle?
Made-up candles · 20-candle average at 5,018.25
- Close 5,021.50: close > ma is true, so lineColor := color.green
- Close 5,016.75: close > ma is false, so the else branch sets color.red
- 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
DrillMade-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.