Tutorial: Writing Custom Functions and Entry/Exit Systems: Write a Simple Function
Write a Simple Function
i Tutorial Task
Write a simply moving average function.
As an example of how you can write a simple function, let’s write our own simple Moving Average function. The Moving Average function comes with TradingSolutions. However, this exercise will help you to understand how to write your own functions by working with one that is useful and easily understandable.
A simple moving average is an average of the most recent n values. It can be calculated by taking a sum of the recent n prices and dividing by n, where n is the number of bars (trading days) to include in the calculation.
Here is what this formula will look like when we have completed it:
Div ( Sum ( Data , Period ) , Period )
Commentary
A simple moving average of a field is the sum of the last n values in that field, divided by n. In other words, an average of the last n values. If we use a period of 10, that’s the sum of the last 10 values divided by 10.
In this formula, “Sum” represents the Summation function, which adds the last Period values of Data. “Div” represents the Divide function, which divides one number by another.
Step-by-step Instructions
1. Press the Define Functions button on the toolbar. This will display the Define Functions Dialog.
Ä Note: If you cannot locate this icon, you can also select Define Functions… from the Tools menu.
2. On the Define Functions Dialog, press the New… button beneath the list of Function Groups.
Functions, entry/exit systems, and trading solutions are divided into groups to help you find the functionality you need quickly and easily.
To create a new group to contain our new function, press the New… button at the bottom of the list of function groups. This will display the Modify Function Group Dialog.
3. On the Modify Function Group Dialog, accept the default name “My Functions” and press OK.
This dialog will have the name “My Functions” entered as the default name. To create a group with this name, press the OK button.
Ä Note: If you have already created a group called “My Functions”, a different default name will appear.
“My Functions” will now appear selected in the list of function groups. The list of function definitions in this group will be empty.
4. On the Define Functions Dialog, press the New… button beneath the list of Function Definitions in the Selected Group.
Now, we want to add our first function to our new group. To create a new function, press the New… button at the bottom of the list of functions in the selected group. This will display the Create a Function Definition Wizard.
5. On the Create a Function Definition Wizard: Basic Information page, enter a Display Name of “My Moving Average” and a Short Name of “MyMA”. Press Next.
The first page of the Create a Function Definition Wizard allows you to enter basic naming information for your function. The Display Name and Description are used when displaying the list of available functions. The Short Name is used when a shorter name is desirable, such as when the function appears in a formula.
Since TradingSolutions already has a function named “Moving Average”, let’s call our function “My Moving Average” with a short name of “MyMA”.
Let’s leave the Description field empty for now. Press the Next button to continue to the Enter Formula page.
6. On the Enter Formula page, select the “Divide” function from the “Basic Math Functions” function group and press the Insert into Formula button to insert it in the formula.
The Enter Formula page allows you to enter the formula for a function definition or entry/exit system rule by selecting existing functions and putting them together.
The upper section of this page displays the current formula. The highlighted portion of this formula is what is currently being selected. To reinforce this information, a brief description of what is currently being selected is displayed above the formula. Since we have not defined any part of this formula yet, it is indicating to select a main function for this function definition.
The lower section of this page displays the selections that can be placed in the highlighted portion of the formula. In this case, we can specify a function definition. At other times, you can specify input parameters or numerical constants as inputs to functions in the formula.
Ä Note: We will cover the Special Processing tab in a later task.
The first step in defining our moving average function is to insert the Divide function into the formula.
If you are thinking of how the moving average is calculated, this may seem backwards. If you were going to calculate the value on paper, you would first sum together the last n values, then divide that sum by n. Why is our first step the divide function?
A formula in TradingSolutions is defined as a function taking parameters. Those parameters can be user-defined inputs, numeric constants, or other functions. Because of this, formulas in TradingSolutions are often defined from the outside in. The last step in the calculation is the outer-most function and it takes as a parameter the values of previous steps.
To see how this works, select the Divide function from the “Basic Math Functions” group and press Insert into Formula.
7. Notice how the formula has updated with your selection. Now that you have selected a function to perform, we need to specify the inputs to that function.
The formula should now appear as follows:
Div ( Value 1, Value 2 )
Ä Note: The blue text appears as reverse text in the formula display.
“Div” is bold because the parameters for this function are currently being entered. The parameters themselves are in italics because they have not been specified.
8. Inputs to functions can be fields, constants, or other functions. In this case, we want to divide the sum or recent values. Select the Function Definition sub-page and insert the “Summation” function from the “General Series Functions” group.
The value that we want to be divided is the sum of the most recent values. To do this, select the Function Definition tab and select the Summation function from the “General Series Functions” group, then press Insert into Formula.
The formula will update to the following:
Div ( Sum ( Data , Period ) , Value 2 )
“Value 1” was replaced with the Summation function and its parameters. The item being selected advances to the first parameter for the new function.
9. We want to let the function work with any field and period, so let’s make both of these values inputs that can be specified when we use the function. On the Input Field sub-page, press the Create New Input… button.
We want the value that we will sum to be a user-specified field. In other words, the user of the function “My Moving Average” will be able to specify which field they want a moving average for by specifying a field as a parameter.
To specify a value should be specified by the user of the function, insert an input field into the formula. Since we have not specified any input fields for this function, we will need to create a new one. To do this, press the Create New Input… button. This will display the Modify Input Dialog.
10. On the Modify Input Dialog, press the OK button to create and insert an input called “Data”.
The Modify Input Dialog allows you to specify the name and type of an input parameter to the function. By default, it uses the name and type of the parameter you are specifying. In this case, the default name is set to Data and the type is restricted to Data Series, since that is what is required for a summation.
Ä Note: The default name is used to select the default field that will be used when the user applies this function. If the default name is “Volume”, the Volume field will be used by default. If no matches for the default name are found, the Close field is used.
Press the OK button to accept these default values.
The formula will update to the following:
Div ( Sum ( Data , Period ) , Value 2 )
The undefined “Data” parameter has been replaced with the Data input. The difference between the two is that undefined values are in italics and inputs are underlined. The item being selected is now the second parameter to the Summation function.
11. Press Create New Input… and OK again to create and insert an input called “Period”.
We want the period of the moving average to be user-specified, as well. We could make our moving average always have a period of 10, but that would limit its usefulness. Instead of using a constant for this value, we can let the user of the function specify this value. We can even provide a default value if there is a preferred or typical value they should use.
Again, press the Create New Input… button. The Modify Input Dialog will appear with a default name of “Period”, a type of “Constant (integer only)”, and a default numeric value of 10. This means that the Period input can only be integer values and that the default value is 10.
If you would prefer a different default length for your moving average, enter it as the Default Numeric Value. When you are done, press the OK button.
The formula should now appear as follows:
Div ( Sum ( Data , Period ) , Value 2 )
12. We want to divide the sum by the period. Select the “Period” input and press the Insert into Formula button.
The final step is to specify the value to divide the summation by. In this case, we want to use the value in the “Period” input. To do this, select Period for the list of available input fields and press the Insert into Formula button.
The formula should now be the one we were trying to create:
Div ( Sum ( Data , Period ) , Period )
Ä Note: The entire formula will be selected. This allows you to change the divide function to another function.
13. Since the formula is now complete, the Next button should be available. Press Next.
We have finished entering the formula, press the Next button to continue.
14. On the Verify Inputs page, Press Finish.
The final page of the Create a Function Definition Wizard is the Verify Inputs page. This page allows you to verify that the inputs are in the order that you would expect to enter them when you use this function. For example, you may have created the Period input before the Data input in the formula. The Verify Inputs page would allow you to change the order so that the Data input is the first parameter, rather than the second.
Since our inputs are in the typical order of data followed by period, there is nothing that we need to change. Press the Finish button to create this function.
15. Indicate not to use the formula as the description by pressing No.
Since you did not enter a description for this function, TradingSolutions will ask if you would like to use the formula as the description. Many times this is useful for understanding what a function does without having to take the time to enter a meaningful description.
In this case, we are creating a simple moving average. Press the No button so that we can see what happens when a function does not have a description.
16. When you are finished with the Define Functions Dialog, press Close.
After you create a function, it will appear selected in the list of functions. In addition, the description of the newly selected function is displayed at the bottom of the list of functions. In this case, since we didn’t enter a description, a default description is given for the function.
17. (optional) You can now use this function definition like any other financial indicator. As an optional task, calculate a field using your new “My Moving Average” function.
You have now created your own function definition that you can use the same as you would use any other function in TradingSolutions. Add a new field to a data series in your portfolio to calculate a value using “My Moving Average”.
Ä Note: If you are not familiar with moving averages, you may also want to chart the results so that you can visualize the relationship between the original data and its moving average.