In our new 3 XAvg Crossover System, we’ll call the fastest moving average Avg1, the second fastest moving average Avg2, and the third fastest moving average Avg3. The rules are as follows:
1. Buy when Avg1 crosses over Avg2 AND when Avg2 is greater than Avg3.
2. Sell when Avg1 crosses under Avg2 and Avg2 is less than Avg3.
We’ll exit our long positions on the open of the next bar when Avg1 is less than Avg2. Likewise, we’ll exit our short positions on the open of the next bar when Avg1 is greater than Avg2.
We’ll also place a trailing stop for the first bar, exiting our long position at the low price minus the 4-bar average of the range. On subsequent bars, the stop price will be the initial stop price plus the value that results from subtracting the stop price from the low and dividing by 4. For the short side, on the first bar, we’ll exit our position at the high price plus the 4-bar average of the range. On subsequent bars, the stop price will be the initial stop price minus the value that results from subtracting the high from the initial stop price and dividing by 4.
Defining your Trading Rules
In this system, we defined both long and short entries as well as exit orders. The long and short entries reverse your position, whereas the exits close out your existing position and exit you from the market. We also performed some setup work, which involved identifying the Inside Day and subsequent breakouts. The setup, entry and exits are described next.
Setup
a) Calculate the three exponential moving averages.
Long Entries
a) When Avg1 crosses over Avg2 and when Avg2 is greater than Avg3, buy on the next bar at the open.
Short Entries
a) When Avg1 crosses under Avg2 and Avg2 is less than Avg3, sell on the next bar at the open.
Exits
a) Exit all long positions on the next bar at the open when Avg1 is less than Avg2.
b) Exit all short positions on the next bar at the open when Avg1 is greater than Avg2.
c) On the first bar of a long position, place a trailing stop that exits at the low minus the 4-bar average of the range. On the first bar of a short position, place a trailing stop that exits at the high plust the 4-bar average of the range.
d) On subsequent bars, place a trailing stop to exit long positions at the original stop price plus the value that results from subtracting the stop price from the low and dividing by 4. We’ll place a trailing stop to exit short positions at the initial stop price minus the value that results from subtracting the high from the initial stop price and dividing by 4.
Designing & Formatting
This section presents the EasyLanguage instructions and formatting for the system, with the EasyLanguage instructions broken down and explained line by line.//Three Exponential Moving Average Crossover System
Inputs:
Price(Close),
AvgLen1(6),
AvgLen2(12),
AvgLen3(28);
Vars:
Avg1(0),
Avg2(0),
Avg3(0),
MP(0),
StopPrice(0);
Avg1 = XAverage(Price, AvgLen1);
Avg2 = XAverage(Price, AvgLen2);
Avg3 = XAverage(Price, AvgLen3);
MP = MarketPosition;
// ENTRY
If Avg1 Crosses Over Avg2 AND Avg2 > Avg3 then
Buy next bar at Open;
If Avg1 Crosses Under Avg2 AND Avg2 < Avg3 then
Sellshort next bar at Open;
// EXITS
If Avg1 < Avg2 then SELL next bar at Open;
If Avg1 > Avg2 then Buytocover next bar at Open;
If MP = 1 and MP[1] <> 1 then StopPrice = Low - Average(Range,4);
If MP = 1 then Begin
sell ("ExitTradeLong") next bar at StopPrice Stop;
StopPrice = StopPrice + (Low - StopPrice)/4;
End;
If MP = -1 and MP[1] <> -1 then StopPrice = High + Average(Range, 4);
If MP = -1 then Begin
Buytocover ("ExitTradeShort") next bar at StopPrice Stop;
StopPrice = StopPrice - (StopPrice - High)/3;
End;
SetUp
The exponential moving average values are calculated and assigned to variables. This allows for easy reference later, without recalculation of the XAverage function.
Avg1 = XAverage(Price, AvgLen1);
Avg2 = XAverage(Price, AvgLen2);
Avg3 = XAverage(Price, AvgLen3);
Long Entries
A buy order will be triggered on the open of the next bar if Avg1 (fast average) crosses above Avg2 (medium average) and Avg2 is greater than Avg3 (slow average).
Short Entries
A sell order will be triggered on the open of the next bar if Avg1 crosses below Avg2 and Avg2 is less than Avg3.
Exit Orders
If Avg1 is greater than Avg2, nearly the opposite of the first part of the Buy Entry criteria, a long exit will be triggered on the open of the next bar.
If Avg1 is less than Avg2, nearly the opposite of the first part of the sell entry criteria, a short exit will be triggered on the open of the next bar.
The value of the MarketPosition function is assigned to the variable MP so that we can reference previous market position values.
If the current market position is long (1) and the market position on the previous bar was short or flat (-1 or 0), a stop price is calculated by subtracting the average range of four bars from the low. Thus, the stop price is calculated each time the market position becomes long.
If the current market position is long (1) an long exit order is generated on the stop price (StopPrice) calculated above. In addition, this trailing stop is moved up by ¼ of the StopPrice value.
If the current market position is short (-1) and the market position on the previous bar was long or flat (1 or 0), a stop price is calculated by adding the average range of four bars to the high. Thus, the stop price is calculated each time the market position becomes short.
If the current market position is short (-1) an long exit order is generated on the stop price (StopPrice) calculated above. In addition, this trailing stop is moved up by ¼ of the StopPrice value.
Testing & Improving












Suggestions for Improvement
The most negative feature of this system’s performance was its dependence on the largest winning trade for the good results. This, of course, is not unusual in a trend-following system, as the whole purpose of this type of system is to capture large profits in a big trend and to minimize losses in periods of choppy market activity. Although having the largest winning trade represent a major portion of the system’s profits is a potential problem (what if the market doesn’t provide an exceptional trend again before our money or patience runs out?), the solution is clearly not to reduce the size of the largest winning trade. The most practical solution is found in portfolio management. Since no one really knows for sure when and where the next explosive trend will occur, most successful trend-followers diversify their portfolios to increase their chances of participation in an exceptional trend. Trading a carefully selected “basket” of stocks and/or commodities rather than limiting your efforts to only one market has the potential to smooth your equity curve and increase your profits.