If/Else

Let's implement a function which accepts 2 parameters and returns whichever one of them is larger than the other.

To achieve this, we'll need to use the if/else expression, like this:

1 function Max(x, y):
2 x if x > y else y

Let's analyze the syntax of an if/else expression:

We begin with the positive outcome expression, followed by the

if
keyword. Then comes the condition expression, followed by the
else
keyword. Lastly, we have the negative outcome expression.

The result of an if/else expression is its positive outcome expression if the condition was equal to

true
or else the negative outcome, if the condition was equal to
false
.

So, in our example, we're returning

x
if it is larger than
y
. Otherwise, we return
y
.

As with all other expressions, if we were to pass, for example

close
and
close[1]
, to the Max function, we could access the previous value of its result, using the
[]
time travel operator, like this:

1 function Max(x, y):
2 x if x > y else y
3
4 max = Max(close, close[1]) # the larger of close and close[1]
5 maxPreviousBar = max[1] # the larger of close[1] and close[2]

This pretty much covers if/else expressions.

Next, let's learn about debugging QuantScript code.