Using Advanced Features

Functions, Task 1

Write a Simple 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 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.

 

Here’s what this formula will look like when we are through with it:

Div ( Sum ( Data , Period ) , Period )

 

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.

þ    Select “Define Functions” from the toolbar.

The easiest way to define a new function is to click on the Define Functions button on the toolbar. That icon looks like this:

 

image\ebd_ebd110.gif

 

Ä    Note: If you cannot locate this icon, you can also select Define Functions… from the Tools menu.

 

This will display the Define Functions Dialog, where you can define and modify functions.

þ    Create a group called “My Functions” to contain your functions.

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 with 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.

þ    Press the “New…” button at the bottom of the list of functions 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.

þ    Enter the display name “My Moving Average” and the short name “MyMA”. Then, 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.

i   Understand the Enter Formula page.

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.

þ    Insert the Divide function into the formula.

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.

 

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.

þ    Insert the Summation function into the formula.

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.

þ    Create a new input called “Data”.

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.

 

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.

þ    Create a new 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 )

þ    Insert the “Period” input for Value 2 of the divide function.

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.

 

þ    Press the Next button.

We have finished entering the formula, press the Next button to continue.

þ    Verify the order of the inputs and 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.

þ    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.

þ    View the description of your new function.

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.

þ    Press the Edit button.

This function would probably be more useful if we entered a description for it. That way, if we were looking at the functions in the My Functions group, we would remember what it is used for or why it was created.

 

Press the Edit button to edit the selected function definition. This will display the Modify Function Definition Dialog.

þ    Enter a description and press OK.

The Modify Function Definition Dialog allows you to change the characteristics of existing functions. You can change the description, the formula, or the order of the inputs. You can even add notes concerning the parameters and usage of the function.

Ä    Note: If you change the order or the number of inputs into a function definition that is currently being used by a field, this may cause the field to become invalid.

 

Enter a description for “My Moving Average” that let’s you know why it was written. For example, a good description might be:

Simple moving average function written as part of the advanced tutorials.

 

When you have finished updating the description, press the OK button.

þ    Press the Close button.

When you are finished with the Define Functions Dialog, press the Close button.

þ    Calculate a field using “My Moving Average”.

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.

 

_    Return to the activity.

Ä    Write Custom Functions