Peak & Trough Based ZigZag
Author: Bandit292
Creation Date: 6/1/2013 4:21 PM
profile picture


Before I go about trying to develop a custom indicator, do you know if one is already available for my requirements (listed below)? If not already available, what components would you recommend to build such a graphic indicator (outside lookback looping using the existing MA, Peak and Trough indicators) ?

I am attempting to graphically define a projected Peak/Trough based on the average of "x" number of lookback DOWN (Peak-to-Trough) or UP (Trough-to-Peak) ZigZag trend lines defined by fast/slow moving average input parameters.

The current trend would be defined by the latest fast/slow moving average crossover that has not reversed within a defined number of bars (UP if last MA cross was fast over slow, DOWN if last MA cross was fast under slow). Once the current trend is defined, the latest "x" number of UP or DOWN trend ZigZag trend lines would be identified (see below) and averaged to produce a projected Peak/Trough from the most current Trough/Peak. The lookback and projected trend lines could then be plotted.

Chart Peaks & Troughs for the lookback trend line parameters would be identified for any dual moving average crossover (defined by fast/slow input parameters) that did not reverse within a specified number of bars. Peak would be identified by stepping back from any fast under slow moving average crossover to identify the maximum bar value desired (OHLC or Mid) in the range of bars from the last Trough to the defined MA crossover bar. Trough would be identified by stepping back from any fast over slow moving average crossover to identify the minimum bar value desired (OHLC or Mid) in the range of bars from the last Peak to the defined MA crossover bar. The trend line parameters to average would be duration (number of bars) and range (TtoP - positive / PtoT - negative).

Thanks in advance for any guidance in this project.
profile picture


Pretty interesting! Where did the idea for creating moving-average crossover-based zigzag come from? I'd like to consider using it for a Trading System Lab article in ActiveTrader magazine.

Here's some code that creates that zigzag and stores the peak/trough bars in a DataSeries that you can use later for the averaging. It's not clear to me how you'll use this to project trend lines. Can you attach an image with details?

This code requires that you install the Community.Indicators Extension.

Please log in to see this code.
profile picture



Thanks for the quick reply.

This idea has been bouncing around my brain for some time, but I'm just now getting around to codifying it. As an options trader, this tool gives me a quick way to determine appropriate strike price and expiration dates based on historical moves. You are more than welcome to use the idea for any AT article. I would like to work with you on the idea development, if you don't mind. I don't need any mention in the article, but would like to use your opinions to develop my tool. I'm a C# dummy, but have a working knowledge of VB.

I've modified your code some below, but it fails in the final steps because I can't determine how to draw into the padded area of the chart.

Some ideas:

1.) Create a user selected parameter that would define how many lookback Zigs/Zags would be averaged to determine the new time/price target.

2.) Loop backward, rather than forward through the bar objects so all calculations and ZigZag line drawing can be halted when the appropriate number of lookback legs have been evaluated.

3.) If a large number of lookback legs are used, allow the user to throw out a selected number of min & max move legs.

4.) Use a weighted average of the lookback legs, so more recent performance has more weighting in the target determination.

5.) Allow the option to determine a target box, rather than target point, by using the min lookback leg bar range to define the left edge of the target box and the max lookback bar range to define the right edge. The lower edge of the target box would be defined by the min lookback leg price move and the upper edge by the max lookback leg price move.

Please log in to see this code.
profile picture


how to draw into the padded area of the chart.
Unfortunately, you cannot do it with WealthScript. :( Probably the best you can do is draw to the last bar and show the projection value with AnnotateBar or DrawLabel, for example.

1.) See WealthScript Prog. Guide > Programming Trading Strategies > Strategy Parameters

2.) Going backwards from the most recent bar is fine if you don't want to backtest the idea, i.e., that's just a "scan". However, you should see how the idea works as if you were actually trading it, and that means you go forward, but looking back on each successive bar, or in this case on each crossing trigger.

3.) You can use another StrategyParameter to control that.

4) and 5) aside, I think the first thing to look at would be to see how accurate the predictions are over time by plotting targets. You can't do the plotting in the padded bars area, but there's plenty of other data to work with.

Here's a shot at it. It includes average the last "Legs back" zig zags, using both peaks and troughs in the current estimate. It seems that the full cycle would be a better estimate than just averaging peak or trough legs to estimate the next peak or trough; but I could be wrong. The results are interesting at best, but I'm not sure how I'd use them :|

Note that the Fuchsia projections are drawn from the previous peak or trough at the time the moving average crossing occurs. In some cases you'll see that the line doesn't even make it to the bar of the crossing, which doesn't help to "project" much of anything!

Please log in to see this code.

profile picture



Thanks again for the quick reply.

Let me look at your inputs and put them into play tomorrow. I'll provide more feedback and Q&A shortly.

profile picture


Good day! Very interesting methods for predicting peaks / troughs. I am also puzzled by this question. How do you like the idea to predict the peaks / troughs use fractals?
profile picture


If it's Bill Williams' definition of fractals, then I don't like the idea much My simulation of his "Profitunity" fractal breakout strategy in the October 2013 issue of ActiveTrader magazine yielded quite poor results over a 16-year backtest.

I just gave a cursory look at some charts using the code above, and actually those projections often turn out to be quite good as long as you only look at the ones in the direction of the trend, and they're even better for non-trending markets.
profile picture



I am using the ZigZag class from Community.Components library trying to replicate an indicator from another platform as I move to WLP.

My problem is I have been unable to obtain the bar at which the reversal occurs, shown by the circles in the attachment. While the peakDetected and troughDetected properties show the highest and lowest points, what I need is the point at which the Draw method changes colors.

I have been unable to find any way to get this information and was hoping you could point me in the right direction.

profile picture



How about the lastPeakBar and lastTroughBar properties (combined with the peakDetected and troughDetected) as per the documentation? Note that the reversal occurs not on the circled bars but when color is changed.