Multicharts Tutorial – Lesson 04: If statements and conditional branching

Learn how to execute code expressions based on conditions

In today’s lesson you will learn how to control your program flow and make it execute parts only when certain conditions are met. This is where if statements are used. You can find them in basically every high level programming language and they are the engine that drives each program. If statements can for example be used in coloring a moving average differently based on its relation to the close of a bar. If you want to close all open positions after a certain time, an if statement will come into play. In case you want to trigger an alert when a predefined condition is met, you will also use an if statement for that. This list could go on for quite some time, but I think you already understand that if statements are not only very useful, but also very important. No programming tutorial could be complete without going over them and a good understanding is essential before we can move to more complex things.

if…then…

The “if…then…” statement is the simplest form of a conditional statement. The condition is tested and if it’s true the following code statement will be executed. If the test is false nothing will be done as the following code statement will not be executed. When I say the test is true, don’t get confused and think you are limited to testing conditions that include “true” only. In case “ii” is a numeric variable and “MyCondition1” and “MyCondition2” are boolean variables these are three valid “if…then…” statements.

Easylanguage if statements image

In case of the first statement the code checks for “MyCondition1 = false”. If “MyCondition1” indeed is false the test result will in fact be “true” (because the expression to check is matched). This can be a bit confusing at first, but if you recall we have done similar checks in the last lesson working with the “while loop”. You can print the result for a test to the PL Editor output bar using the print command.

test condition print image

The code statement following the “if…then…” statement will only get executed when the check condition is matched. In case the test returns false the code will continue with the next part. If you want your code to perform one statement if the test condition is true and another one if it’s false, you could use two “if…then…” statements or use another type of statement.

if…then…else…

The “if…then…else…” statement will execute one code statement if the check condition test is true and a second statement if the test is false. Going back to our moving average example we can change the color of the average according to the relation of the closing price to the average. If the close is above the average then the average should be colored in green, if it’s not it should be red. Note that this means the average would also be colored red in case the close matches the average. Using the “if…then…else” statement and the reserved word “SetPlotColor” will do the trick here. SetPlotColor has two parameters: The first is the number of the plot you want to change the color for (it’s 1 for Plot1 and 5 for Plot5 etc.) and the second is for the color you want the plot to use.

average with color change code imagecolor changing average image

As planned the average will change its color now according to the relation of the bar close to the average. As I mentioned the case where the close is equal to the average would also be colored in red. One very useful feature for “if…” statements is that you can combine or nest them to create more complex logic trees. We could slightly alter the “if…then…else” statement used in the code above and add a third color for the case where the close matches the average.

grouped if statements image

The code piece groups one “if…then…” and one “if…then…else…” statement to perform the task. Please note that only the last code line in a single “if” or multiple grouped “if” statements has to be followed by a semicolon.

moving average with three colors image

if…then begin…end

The “if…then” and “if…then…else…” statements are great if you only have one code expression that should be executed. For more complex code blocks you will have to use block statements. The “if…then begin…end” block statement is similar to the “if…then” statement, but allows for multiple code expressions between the “begin” and “end”. The “begin” and “end” are common for block statements, this is how they start and end. When comparing them to the regular “if…then…” or “if…then…else” statements in a “if…then begin…end” block all complete statements within the “begin…end” have to be followed by a semicolon.

Let’s add a simple block statement to our average that plots a cross (for this you need to change the plot style to cross in the properties) and gives us an alert when the full bar is below the average.