Tutorial: Writing Custom Functions and Entry/Exit Systems: Use the Special Processing Sub-page
Use the Special Processing Sub-page
i Tutorial Task
Learn about the Special Processing sub-page.
The Special Processing sub-page of the Modify Formula Dialog contains some additional buttons and functionality to make definition of formulas and rules easier and more flexible. For this task, we are going to extend our “My Moving Average” function to be an “Average Absolute Difference” function.
Instead of averaging the current value and the most recent values, we will be averaging the absolute value of the difference between the current value and the recent values. By determining the average difference from recent values, we should get an idea of how volatile the price is.
Here is the formula we will be creating:
Div ( Sum ( Abs ( Sub ( Lag ( Data , 1) , Current: Ident ( Data ) ) ) , Period ) , Period )
Commentary
The Special Processing sub-page of the Modify Formula Dialog contains some additional buttons and functionality to make definition of formulas and rules easier and more flexible. This task will examine the use of some of these features.
Ä Note: The formula entered during this task is more complex than those in the previous tasks. This is to better show the features available on the Special Processing sub-page. However, the features on this sub-page are not typically required for basic function entry. If you have trouble understanding parts of this task, you may want to return to it at a later time after you are more familiar with writing functions in TradingSolutions.
For this task, we are going to extend our “My Moving Average” function to be an “Average Absolute Difference” function. Instead of averaging the current value and the most recent values, we will be averaging the absolute value of the difference between the current value and the recent values.
Conceptually, this is simple. Subtract the current value from each of the previous values, take the absolute value, and average them together. However, the difficulty arises when you need to specify to use both the current value and the previous values in the summation.
To handle this, TradingSolutions includes the ability to use the current value of a function inside of a summation function.
Here is the formula we will be creating:
Div ( Sum ( Abs ( Sub ( Lag ( Data , 1) , Current: Ident ( Data ) ) ) , Period ) , Period )
You may recognize the basic structure of this formula from a previous task. Here is our simple moving average:
Div ( Sum ( Data , Period ) , Period )
In this case, Data will be being replaced by:
Abs ( Sub ( Lag ( Data , 1) , Current: Ident ( Data ) ) )
Step-by-step Instructions
1. Select “Define Functions” from the toolbar.
2. On the Define Functions Dialog, select the “My Functions” group.
3. Press the New… button beneath the list of Function Definitions in the Selected Group.
Begin by creating a new function in the “My Functions” group. As before, this can be done by selecting Define Functions from the toolbar to display the Define Functions Dialog. Then, selecting the “My Functions” group and pressing the New… button at the bottom of the list of function in the selected group. Once again, this will display the Create a Function Definition Wizard.
4. On the Create a Function Definition Wizard: Basic Information page, enter a Display Name of “Average Absolute Difference” and a Short Name of “AvgAbsDiff”.
5. (optional) Enter the following description:…
This time, we will be creating the “Average Absolute Difference” function, for which we can use the short name “AvgAbsDiff”. An appropriate description for this function might be:
Average the absolute values of the differences between the current value and the most recent values.
6. Press the Copy another Function Definition… button.
7. On the Copy Function Definition Dialog, select the “My Moving Average” function from the “My Functions” group.
8. Deselect Notes & Description from the Elements To Copy section.
9. Press OK.
As was mentioned above, the function that we are creating has a similar basic structure to our “My Moving Average” function. This means that we could save some steps by copying our previous function and working from there.
Press the Copy another Function Definition… button. This will display the Copy Function Definition Dialog. Then, select “My Moving Average” from the “My Functions Group”. Since we only want to copy the formula and not the description or notes, deselect the option to copy the Descriptive Text. Now, press the OK button to perform the copy.
10. On the Create a Function Definition Wizard: Basic Information page, press Next.
We are now ready to enter the remainder of the formula. Press the Next button to advance to the Enter Formula page.
The formula that we used for our “My Moving Average” function should appear in the formula box.
11. On the Enter Formula page, the formula for “My Moving Average” should appear in the formula box. Let’s change what is being added together. Click on “Data” in the formula box so that only it is selected.
We want to replace the value being averaged with a more complex formula. Specifically, where it currently says Data, we want to add:
Abs ( Sub ( Lag ( Data , 1) , Current: Ident ( Data ) ) )
To do this, we could simply overwrite Data with this new formula. However, this would delete the current Data input, causing us to have to recreate it and change the order of the inputs. Let’s look at another way to do this.
First, click on Data in the formula so that it is the only text highlighted. We can now change this portion of the formula directly.
The formula should now appear as follows:
Div ( Sum ( Data , Period ) , Period )
Ä Note: The blue text appears as reverse text in the formula display.
12. We want to perform extra processing on the Data input. On the Special Processing sub-page, press the Add Level button.
The Special Processing sub-page includes buttons allowing you to add and remove processing levels from the current formula. Switch to this sub-page and press the Add Level button.
The formula should now appear as follows:
Div ( Sum ( Ident ( Data ) , Period ) , Period )
The Add Level button has added an Identity function around the selected section of the formula. As you may remember from previous tutorials, the Identity function returns the current value of its input parameter. Adding the Identity function does not change the overall value of the formula, but it does allow us to make additional changes.
13. On the Function Definition sub-page, select the “Absolute Value” function from the “Basic Math Functions” group and press the Change in Formula button.
Another thing that the Add Level button did for us was it left the new Identity function selected. This allows us to easily change it to a new function definition.
To change this to the Absolute Value function, change to the Function Definition tab and select the “Absolute Value” function from the “Basic Math Functions” group. Then, press the Change in Formula button. This will replace the currently selected Identity function to the Absolute Value function.
The formula should now appear as follows:
Div ( Sum ( Abs ( Data ) , Period ) , Period )
Using this same technique, we can add another level to subtract this value from itself.
14. On the Special Processing sub-page, press the Add Level button.
15. On the Function Definition sub-page, select the “Subtract” function from the “Basic Math Functions” group and press the Change in Formula button.
Add another level around the Data input and change it to perform the Subtract function. In this case, the Subtract function takes two parameters. The first parameter keeps Data as its value and the second parameter is left unresolved.
16. Click on “Value 2” in the formula box so that only it is selected.
17. We want the absolute value of the difference between a value and its previous value. On the Input Field sub-page, insert the existing “Data” input.
For now, let’s subtract the value of Data from itself. Select “Value 2” and insert the Data input into the formula by pressing the Insert into Formula button.
The formula should now appear as follows:
Div ( Sum ( Abs ( Sub ( Data , Data ) ) , Period ) , Period )
Technically, this is formula we want. We want to subtract the previous values of Data from the current value of Data. However, the current formula does not distinguish between the two. The present formula subtracts the value of Data from itself, so the result would always be 0.
18. We need to specify for one of these Data parameters to be the current value (as opposed to the previous values used in the summation). Click on the second “Data” parameter in the formula box so that only it is selected.
What we would like to do is use the current value of the second Data parameter, while allowing the Summation function to iterate through the previous values in the first Data parameter. The Special Processing sub-page includes functionality that allows us to do this.
The Special Array Processing section includes an option to use the current value of the selected function for all values in the surrounding array function. Array functions are functions, such as the Summation function, that operate on a buffer (or array) of recent values.
Note that this option specifically works on the selected function. We currently have an input value selected, so this option is not available. However, we have recently seen a feature that converts an input value to a function without changing its value.
19. On the Special Processing sub-page, press the Add Level button.
20. On the Special Processing sub-page, select Use the current value of the selected function for all values in the surrounding array function.
If we add the Identity function around the selected input value, we will have a function that has the same value as the input value. Press the Add Level button. Then, select to use Special Array Processing.
The formula should now appear as follows:
Div ( Sum ( Abs ( Sub ( Data , Current: Ident ( Data ) ) ) , Period ) , Period )
Now our formula will subtract each value in the summation from the current value, taking the absolute value of the subtraction before adding them together.
There is only one change left to make. The summation function includes the most recent values of the Data input, up to and including the current value. Because of this, one of the values currently included in the formula is the subtraction of the current value of Data from itself, yielding 0. A more useful function would probably begin with the previous value of Data and work backwards from there.
21. This is almost correct. However, we want to compare the current value of Data only to previous values. Click on the first “Data” parameter in the formula box so that only it is selected.
22. On the Special Processing sub-page, press the Add Level button.
23. On the Function Definition sub-page, select the “Lag” function from the “General Series Functions” group and press the Change in Formula button.
24. Click on the unresolved “Period” parameter in the formula box so that only it is selected.
25. On the Numeric Constant sub-page, press the Insert into Formula button to use the default period of 1.
So that the Summation uses the previous value of the Data input, we can use the Lag function to delay that input. To make this change, select the Data parameter and add a level around it. Then, change the added Identity function to the Lag function from the “General Series Functions” group.
The final step is to set the period of the lag. This is not the same as the period of the Summation function. The period of the Summation function indicates how many samples to include in the average. The period of the lag function should be set to delay the values going into the Summation function by one day. Therefore, this value should be set to 1.
To do this, click on the undefined Period value in the formula. Then, select the Numeric Constant page, accept the default value of 1, and press the Insert into Formula button.
The formula should now appear as follows:
Div ( Sum ( Abs ( Sub ( Lag ( Data , 1) , Current: Ident ( Data ) ) ) , Period ) , Period )
Ä Note: The “Current: Ident ( Data )” segment is selected because it is the next value after the one we just changed.
26. We are finished updating the function. Press Next.
We have now completed our formula, so advance through the remainder of the Create a Function Definition Wizard by pressing the Next button, followed by the Finish button. This will return you to the Define Functions Dialog.
27. On the Verify Inputs page, press Finish.
28. (optional) You can now use this function definition like any other financial indicator. As an optional task, calculate a field using the “Average Absolute Difference” function.
Congratulations, this advanced features task has shown you how you can add levels of processing while you are entering a formula. It has also shown you how to use the current value of an input in a Summation function.
You can see the results of this task by adding a new field and calculating the “Average Absolute Difference”. Displaying this value in a sub-chart with the original value will display a value that reduces as fluctuations in the price decrease.