Let's look at our double-SMA strategy again:
Lines 5-10 and 16-21 are practically indentical. They work differently though, because they're using the variablesand , which have different values on line 5 and on line 16.
Two things would help here:
- A way to mark a part of our code as reusable; name it, so that we could refer to it; and, optionally, declare a list of variables (called parameters), values for which must be provided in order for the code to work. That's what a function is.
- A way to execute that code and provide values for its parameters, if any. That's what a function call is.
Essentially, functions are a way to write code once and execute it many times, customising each execution according to our needs.
Let's rework our strategy, using a function:
Let's analyse what's happening here:
Lines 1-5 comprise the function definition.
Taking a closer look at line 1, we see the keyword
Lines 2-5 make up the function's body.
This code is not executed yet, only saved for future use. Notice how the body is indented by 2 spaces - this marks it as part of the function. Any variable created in the function body is not accessible outside of it .
Lines 7 and 8 are function calls. A function call begins with the name of the function and an open parenthesis:. Then, values are passed for each of the function's parameters. Each value is preceded by the parameter's name, and a colon, like this: . We top things off with a closing parenthesis.
However, in addition to the function call, we also have assignments on lines 7 and 8 - how does that work?
Whenever you call a function, that function's body is executed, and a value is returned. That value is whatever the function body's last line evaluates to. In the case of our function, the last line is line 5, so we get the average as the resulting value. Then, we assign that value toon line 7 and on line 8.
In essence, on lines 7 and 8, we're telling QuantScript, "Hey, go execute that code for me. Give it these values to work with. Then, assign whatever it returns, to this variable".
Let's remove even more code duplication by creating two functions which return whether some value has risen or fallen, compared to the previous bar.
Here are the functions:
Now let's use them in our strategy's signal condition statements:
Did you notice? When we called, we did not specify the parameter name, only the value. This is perfectly okay in QuantScript - it's up to you whether you want to specify the name or not. However, if you don't, you have to make sure to pass in the values in the exact order in which the function expects them to be.
That about wraps up how functions work in QuantScript. I hope you agree that our code is much more readable now.
Next, let's implement a function which returns the larger of two numbers.