Tutorial: Writing Custom Functions and Entry/Exit Systems: Write a Basic Entry/Exit System
Write a Basic Entry/Exit System
i Tutorial Task
Write a basic rule-based entry/exit system.
Let’s build on what we know about writing a function to build a rule-based entry/exit system.
As an example of how you can write a basic entry/exit system, let’s compare today’s closing price to the high and low prices of yesterday. Specifically, we will enter long when the close is lower than yesterday’s low and enter short when the close is higher than yesterday’s high.
Here are the rules we will be using:
Enter Long
LT ( Close , Lag ( Low , 1 ) )
Enter Short
GT ( Close , Lag ( High , 1 ) )
The LT and GT are the “is less than” and “is greater than” functions. The “Lag” function returns a previous value of a field.
Commentary
This may seem backward. We will be entering long when the price is declining and entering short when then price increasing. However, rapid price movements like this are often corrected with a move in the opposite direction on the next day. In trading terms, yesterday’s trading range can often make an effective overbought/oversold indicator.
In these rules, the Lag function returns the value for a previous day. The LT and GT functions are short for “Is Less Than” and “Is Greater Than”, respectively.
Step-by-step Instructions
1. Press the Define Entry/Exit Systems button on the toolbar. This will display the Define Entry/Exit Systems Dialog.
Ä Note: If you cannot locate this icon, you can also select Define Entry/Exit System… from the Tools menu.
2. On the Define Entry/Exit Systems Dialog, press the New… button beneath the list of Entry/Exit System 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 entry/exit system, press the New… button at the bottom of the list of system groups. This will display the Modify Entry/Exit System Group Dialog.
3. On the Modify Entry/Exit System Group Dialog, accept the default name “My Entry/Exit Systems” and press OK.
This dialog will have the name “My Entry/Exit Systems” 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 Entry/Exit Systems”, a different default name will appear.
“My Entry/Exit Systems” will now appear selected in the list of system groups. The list of systems in this group will be empty.
4. On the Define Entry/Exit Systems Dialog, press the New… button beneath the list of Entry/Exit Systems in the Selected Group.
Now, we want to add our first entry/exit system to our new group. To create a new system, press the New… button at the bottom of the list of functions in the selected group. This will display the Create an Entry/Exit System Wizard.
5. On the Create an Entry/Exit System Wizard: Basic Information page, enter a Display Name of “Trading Range Reversal System”.
The first page of the Create an Entry/Exit System 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.
Since this system is based on the price reversing when it is outside the previous day’s trading range, let’s call our entry/exit system “Trading Range Reversal System”. Unlike function definitions, entry/exit systems do not have short names since they are not available to be used directly in formulas.
6. (optional) Enter the following description:…
You may also want to enter a description. An appropriate description might be as follows:
Enter the market for a price reversal when the closing price is outside of the previous day’s trading range.
Press the Next button to continue to the Enter Rules page.
7. Press Next.
8. On the Enter Rules page, Enter Long sub-page, press the Add… button to add a rule for entering long.
i Understand the Enter Rules page.
The Enter Rules page allows you to specify the rules for when to enter and exit the market in long and short directions. Rules are entered as formulas similar to those used for function definitions. When a rule formula evaluates to a non-zero value, the signal associated with that rule is generated.
For example, we will enter the following rule for Enter Long:
LT ( Close , Lag ( Low , 1 ) )
On each day that the close is less than the low of the previous day, an Enter Long signal will be generated.
i What happens when multiple conditions are true?
You may be wondering what happens when the conditions for multiple types of rules are true on a single day. The rules for the signals are evaluated in the following order:
· Enter Long
· Enter Short
· Exit Long
· Exit Short
This means that if the Enter Long rule is true, an Enter Long signal will be generated without checking the other rules. If it is not true, the Enter Short rule will be checked, followed by the Exit Long rule and the Exit Short rule.
Now, let’s enter our entry rules.
To add a rule for generating a particular signal, press the Add… button on the sub-page for that signal. This will display the Modify Rule Dialog.
9. On the Modify Enter Long Rule Dialog, insert the “Is Less Than” function from the “Logical Comparisons” group.
Select the “Is Less Than” function from the “Logical Comparisons” group and press Insert into Formula.
The rule formula should now appear as follows:
LT ( Value 1, Value 2 )
Ä Note: The blue text appears as reverse text in the formula display.
10. Insert a new input named “Close” by pressing the Create New Input… button, entering the name “Close”, selecting “Data Series” as the type, and pressing OK.
The value that we want to watch with this rule is the value in the Close field. This means that we want to create an input to this system that defaults to using the Close field.
To do this, press the Create New Input… button to display the Modify Input Dialog. Select “Close” from under “Stock Fields” in the List of Common Default Names.
Ä Note: This is equivalent to typing “Close” in the Default Name box.
You may also want to restrict the Type of Values selection to only Data Series. This is not necessary, but it helps to restrict the user to using the entry/exit system in the way it was intended.
After you have entered this information, press the OK button to return to the Modify Rule Dialog.
The rule formula should now appear as follows:
LT ( Close , Value 2 )
11. On the Function Definition sub-page, insert the “Lag” function from the “General Series Functions” group.
We want to compare the value in the Close field to the previous value in the Low field. To do this, we will use the Lag function to delay the value of the Low field by one day.
To select a function, select the Function Definition tab. Then, select the “Lag” function from the “General Series Functions” group and press Insert into Formula.
The rule formula should now appear as follows:
LT ( Close , Lag ( Data , Period ) )
12. Insert a new input named “Low”.
Since we want the previous value of the Low field, we now want to create an input to this system that defaults to using the Low field.
To do this, press the Create New Input… button and enter “Low” in the Default Name box. This time, the Type of Values selection is already set to Data Series. This is because the Lag function will only take a data series as an input, rather than accepting constant values.
Press the OK button to return to the Modify Rule Dialog.
The rule formula should now appear as follows:
LT ( Close , Lag ( Low , Period ) )
13. We want to compare the current close to the value from one bar ago. On the Numeric Constant sub-page, press the Insert into Formula button to use the default period of 1.
The last value that we need to set in our Enter Long rule is the Period for the Lag function. The Period indicates how many samples to delay the value. In other words, how many samples into the past we would like to see the value. Since we want the value from one sample ago, we will set this value to 1.
To do this, select the Numeric Constant tab, accept the default value of 1, and press Insert into Formula button.
The rule formula should now be the one we were trying to create:
LT ( Close , Lag ( Low , 1 ) )
Ä Note: The entire formula will be selected. This allows you to change the main function to another function.
14. Press Close.
We are finished entering this Enter Long rule. Press the Close button to return to Enter Rules page.
The list of rules for when to Enter Long now includes the rule we created.
15. On the Enter Rules page, Enter Short sub-page, repeat this process with the “Is Greater Than” function comparing the “Close” input with a new input named “High”.
The steps are the same for creating an Enter Short rule. Press the Enter Short tab and then press the Add… button. Now, use basically the same steps used to create the following rule:
GT ( Close , Lag ( High , 1 ) )
The important differences will be:
· Use the “Is Greater Than” function instead of the “Is Less Than” function.
· Select to use the current Close input instead of creating a new one.
· Create and use a new input for the High field instead of the Low field.
When you have created the Enter Short rule, press the Close button to return to Enter Rules page.
Ä Note: Be sure to use the existing “Close” instead of creating a new one.
16. We are done defining the rules for this entry/exit system. On the Enter Rules page, press Next.
We are now finished entering the rules for this entry/exit system. Press the Next button to advance in the Create an Entry/Exit System Wizard to the next page.
17. On the Verify Inputs page, slide the “High” input so that it is second.
The Verify Inputs page displays the inputs to the entire entry/exit system in the order that they will be entered as parameters. By default, these inputs are in the order that they were created while entering the rules. This means that the parameters to this system would currently be (Close, Low, High).
When High and Low are specified as parameters, they are typically specified in the order High, then Low. Therefore, it may be favorable to change this order to (Close, High, Low).
To do this, we can slide the High parameter to the second position. First, select High from the list of inputs with the mouse. Then, press the Slide Up button. The order should now be (Close, High, Low).
Ä Note: You can also reorder the inputs by dragging individual inputs to new positions in the list.
18. Press Finish.
We are now finished entering this entry/exit system. Press the Finish button to save it and return the Define Entry/Exit Systems Dialog.
19. When you are finished with the Define Entry/Exit Systems Dialog, press Close.
20. (optional) You can now use this entry/exit system like any other one. As an optional task, apply the “Trading Range Reversal System” to a data series.
You have now created your own entry/exit system that you can use the same as you would use any other system in TradingSolutions. Add a new field to a data series in your portfolio and apply the “Trading Range Reversal System”. After it has been calculated, analyze the signal to determine whether it is an effective trading strategy for that particular stock.
Ä Note: You may find that the “Trading Range Reversal System” does a better job with stocks remaining in a constant trading range, such as “MBNA Cp”, rather than stocks trending strongly in a particular direction.