|
We recently received a question from a customer asking how he could create an Entry/Exit system rule that exited when a short-term moving average crosses below a long-term moving average three times since entering the position.
|
|
Let’s look at each of the concepts involved in this:
Counting the number of times one field crosses below another
You can determine when one moving average crosses below another using the Is Crossing Below function. For two exponential moving averages, this would look like this:
CrossBelow(EMA(Data,20),EMA(Data,50))
You can count the number of times this occurs over a period of time using the Count function. For example, to count the number of times this has occurred in the last 100 bars, you could do this:
Count(CrossBelow(EMA(Data,20),EMA(Data,50)),100)
Adjusting the period over which things are being counted
You can dynamically adjust the period of many functions using a Variable Length version of that function. The variable length version of the function takes a calculated or input value for the period and supports values up to a specified maximum period.
In this case, we want to limit the count to when the trade was in the market. In other words, we want our period to be the number of bars that we have been in the current position. This value is available from the System: Bars in Current Position function. Here’s an example of this using this function with 1000 as the maximum number of bars:
CountVL(CrossBelow(EMA(Data,20),EMA(Data,50)),System_Bars( ),1000)
Making this an effective entry/exit system rule
Now that we can count the number of times an event has occurred since we entered, we can make it a rule by comparing it to a value. In this case, let’s compare it to 3 using the Is Greater Than or Equal To function.
GE(CountVL(CrossBelow(EMA(Data,20),EMA(Data,50)),System_Bars( ),1000),3)
One final consideration is rule precedence. Entry rules are always evaluated before exit rules. Therefore, it is possible that your entry rule could override your exit rule. If this is not the desired behavior, you can cause redundant entry rules to not be considered by having them check the current position. The most common way to do this is making sure the System: Current Position is Long function is not true. This would be added as an additional Enter Long rule and requiring all rules to be true:
Not(System_IsLong( ))
Additional information
Step-by-step instructions and a sample entry/exit system using these techniques are available for download from: http://www.tradingsolutions.com/resources/TS_April_Tip_Box.zip