Working with Multicharts and Tradestation – Lesson 06: Functions

Learn how to create and use functions in Multicharts and Tradestation

Let’s take a look at what functions are and how they are used. We can use this as foundation for another lesson when we take a look at how to operate the function that comes with our free Value Area Indicator.

In previous lessons we have looked at Moving Averages in various forms. The studies we wrote had all calculations within the main code block. This is fine to demonstrate things or when you only work with a few lines of code. However handling ten different moving averages within your code would require you to add the same code ten times. This would make your code much harder to read and the code maintenance would become harder, too. Think about a logic error you want to fix or a change in the average formula that you want to make. A mistake in the average logic is likely included in all ten code blocks. The change to the formula would have to be made at ten locations within the code.

What are functions?

Functions can be of help here. As the developer you can keep your code at one location, within a function. You can use this function now to provide the results of the computation to any other code. In case of a logic error in the code this can be a huge help. The error has to be corrected at one place only. At the same time this correction will effect all other codes that call your function. It doesn’t even have to be as serious as an error. If you simply want to change the logic, you would only have to do that in one place, too.

You also don’t have to add the same code block several times, as you can just call the function instead. This alone can help minimize your work. By breaking down complex logic into smaller parts it can also improve the readability of your code.

Minimized complexity, improved readability and less work sounds great, but what are functions?

Simply put, a function is a set of code instructions that return one or more values. Functions have a name and using this name, an indicator, signal or other function can call it.

Let’s take a look at how you create functions by turning the moving average logic from lesson 2 into one. This should make things a little clearer.

Create a new function

In case the PL Editor is not up and running already, please start it.

Go to -> “File” and click -> “New” to create a new function. Set the radio button to “Function” and click “OK”.

Study type options in Multicharts

In the next window, we have to give our new function a name. Following the naming from earlier lessons, I will call the function “ABC_MovingAverage_Lesson6”. I am using underscores within the name, as Function names may not contain any spaces and non-alphanumeric characters. The underscore is the only exception, that’s why I am using it instead of the space. Please feel free to give your function a different name. Just keep in mind that you will have to slightly adjust your code later.

Creating functions window

The “Return Type” specifies the type of result that the function returns to the caller. For the moving average we will return a numeric value. So we set it to “Numeric”.

In case your function returns a boolean result, the return type should be TrueFalse. Finally you would use the last option, in case the function returns a string.

Set the “Function Storage” to Auto-detect and have the PL Editor take care of the correct storage. In general a function would be “Simple”, unless you reference previous bar’s values in it (using square brackets) – in that case it would become “Series”.

Function Inputs

Our idea is to turn the moving average code from lesson 2 into a function. We also want to be able to easily call it with different length or using a different price for the average later.

Like the indicator, the function will have two inputs. These will allow you to set the length and price for the average. We will create similar inputs in the code that calls the function later. This way we can alter the results the function returns.

You have to specify a default input value in an indicator. In the function code on the other hand, you have to specify the type of each input. This will make sure that the program knows what input value to expect while the code is running.

Most of the time you will work with three types of function input parameters – Numeric, TrueFalse or String (text). Each parameter can have one of two main subtypes – series and simple. Although this might sound a bit more complex, it’s very easy when you keep the following in mind. If the particular input parameter is constant it’s “simple”. A moving average length for example would be “NumericSimple”. Inputs that refer to values that can change are of the subtype series. That’s why a price series input (like “Close” for the moving average) would be “NumericSeries”.

The rule of thumb is, when the input stays constant bar by bar it’s “Simple”, otherwise it’s “Series”.