Figure 61: Example of stock chart with moving average6
Figure 61 provides an example of a 2‐years stock chart and the moving average (red) with a period of 50. For generating trading signals based on such a MA curve, the following rules are applied:
When the price curve crosses the MA line from top to bottom, an up‐trend turns into a down‐trend.
This gives a sell‐signal.
When the price curve crosses the MA line from bottom to top, a down‐trend turns into an up‐trend.
This gives a buy‐signal.
These simple rules are based on the idea that the smoothed MA‐line always moves behind the price curve. As long as the price does not change the major direction, they do not cross. When they cross, it is an indication of a trend change.
Exactly these crossing points can also be utilized to extract the turning points in the trend of the overall chart with respect to a given MA period. The algorithm first computes the MA and determines the crossing points. In the next step, the slopes between these points are computed and finally they are compared to each other.
These steps are repeated in multiple passes with varying MA periods. In this way a search pattern can be detected at different scaling levels. At the end, the result contains a list of possible matches for the series, each match having a sum of slope deviations and a starting point in the series. Algorithm 12 summarizes the steps in pseudo code.
Algorithm 12: Base algorithm for comparing two time series Input: TimeSeries pattern, TimeSeries targetSeries
Output: Sorted list of similarity matches as tuples of time‐series subsequence and similarity score
Variables: Arrays of time‐series data points patternPoints, targetPoints; deviation (sum of slope deviations for a certain match); Arrays of slope values patternSlopes, targetSlopes; Indizes MAPeriod, startpoint, index
1:
2:
//iterate in multiple passes through the data with varying MA period for MAPeriod = minPeriod to maxPeriod step precision
6 Chart created with YAHOO Finance, finance.yahoo.com. Copyright: YAHOO Inc.
72
for startingPoint = firstPointInTarget to lastPointInTarget step 1
for index = 0 to patternLength step 1
deviation += Difference (targetSlopes[index+startPoint],
patternSlopes[index]);
next
StoreDeviationForThisMatch();
deviation = 0;
next
else
//iterate over the pattern sequence instead of the target sequence next
In the following the method, several parameters and introduced variations are described in greater detail. For instance, in Algorithm 12 the search pattern and the source series are both smoothed with the same MA period which is not generally desirable.
5.3.2.3 Determination of turning points
In the above overview, it was said that the crossing points between the MA curve and the original curve are used as the turning points between which the slopes are computed and subsequently compared. In fact, this has the downside that the turning points are set after the actual turn of the trend. Especially for longer MA periods, this can be very decisive. The rule is that the greater the MA period, the later can the actual reversal be detected. The advantage is that we of course have the possibility to track back the series to the actual point where the trend reversed: in case of an up‐trend turning into a down‐trend this is the highest data point since the last trend reversal or the beginning of the series, for a down‐up trend reversal, it is the lowest point.
Out of these considerations, several modes are introduced for determining the turning points. Ultimately, the user can choose which mode to apply. The modes available are:
Extremum – Take the extremum between the current and the last crossing point.
CrossingPoint – Take the value of the crossing point directly.
AvgExtremumAndCrossingPoint – Build an average between the extremum and the crossing point.
Averages the value as well as the timestamp.
ExtremeValuesAverage – Compute an average of the highest/lowest percent of values. Set the timestamp to the point of the absolute extremum. This technique is used to straight out extreme outliers.
Figure 62: Impact of different turning point modes
Figure 62 illustrates the impact of these turning point modes. In Figure 62a, the MA crossing points are marked for an MA period of 50. It can be seen that such a relatively large MA period smoothes out big movements, such as the price high in November and the subsequent temporary decline.
In Figure 62b, the series of turning points resulting from the turning point mode “CrossingPoint” is shown. The resulting curve is very smooth; many smaller movements nearly disappear completely. Reversal points are mostly far behind the actual turning point of the curve.
Figure 62c shows the use of the turning point mode “Extremum”. The mode emphasizes the original movements of the curve, but in case of many short subsequent changes such as in August/September, the result is a sequence of rough spikes, which may be undesirable.
In Figure 62d, the “AvgExtremumCrossingPoint” turning point mode is applied. In this case the resulting turning points must not necessarily be points on the original curve. Turning points are set later than the actual curve turns. Smaller up/down alterations are smoothed better.
Finally, Figure 62e shows the result of applying the “ExtremeValuesAverage” technique. The result is similar to the “Extremum” technique. Yet, extreme outliers are not used directly. This may be of advantage in case of extreme short term variations which should not be considered for the overall trend.
Algorithm 13 demonstrates the combined computation of the moving average and the extraction of turning points depending on the turning point mode.
74