ZigZag Up Down Status
Author: grtrader
Creation Date: 2/6/2018 3:29 PM
profile picture

grtrader

#1
ZigZag has in it "Draw(Color upColor, Color downColor, LineStyle style, int width)". Therefore it has to know whether the drawn line is going up or down. I would like to set a variable whether the ZigZag line is increasing or decreasing. When applied to an indicator, I could use it as a filter for both back testing and for active trades (the dotted line). How can this be done?
profile picture

Eugene

#2
Well, as says the online guide there are two properties which discover the state of the ZigZag:

QUOTE:
bool peakDetected Returns true when a peak of the ZigZag reversal percentage has been detected. The value remains true until a new trough is detected.
bool troughDetected Returns true when a trough of the ZigZag reversal percentage has been detected. The value remains true until a new peak is detected.


Naturally, these properties belong to an instance of the ZZ object which on the current bar is:
CODE:
Please log in to see this code.
profile picture

grtrader

#3
I tried using zzLast per the attachment and the peak and trough detections don't match up with the drawn lines. Am I missing something? Appreciate your help.

CODE:
Please log in to see this code.
profile picture

Eugene

#4
Everything matches very well. What's confused you is the fact that the underlying functions Peak and Trough introduce some natural lag necessary to avoid 20/20 hindsight.
profile picture

grtrader

#5
I accept that; however, the lines are not drawn per the peak and trough detections. The coding for zig zag obviously uses something else. Do you know what it is?
profile picture

Eugene

#6
It's documented in the online guide (per the link above). The coding is different and can be found in the open source code.
profile picture

grtrader

#7
How does one get to the open source code?
profile picture

Eugene

#8
The peaks and troughs in the example clearly match the StochK indicator they're applied to - as documented in the Wiki link I referred you to. Do you have a specific concern (if so what is it exactly) or did you expect that the example would apply to prices?
profile picture

grtrader

#9
I need a variable to correspond with each of the lines drawn. For instance, from a peak to a trough I want the variable to be 0. For a trough to a peak, I want the variable to be 1. The variable would need to correspond with each of the lines. An example as how to use this is that if the line is rising (trough to peak), I can use the variable to take it as permission to buy. Using (zzLast.troughDetected) or (zzLast.peakhDetected) apparently does not work as shown from the script. The colors do not match up with the lines.
profile picture

Eugene

#10
The colors do match up with the lines perfectly. But how you want them to match the peak/trough bar is possible in an ideal world only. There will be a delay that you're seeing and which leads you to thinking that there's a mismatch (there is not). See the picture:



The color changes when the trough gets detected. Hope this is more clear now.

So I've got nothing more to add to post #2. The code already contains everything you need to reach your goal. Good luck!
profile picture

grtrader

#11
Let's use the example you provided. It appears that the colors match up with the peaks and troughs of the price series. I would like the colors to match up with the peaks and troughs of the indicator. As far as trading goes, when the stochastic begins to go down (red line), it would signal that it might be a good time to get out of a long trade before it actually goes down. Again, I am looking for a variable to correspond to the peaks and troughs of the indicator, not the price.
profile picture

Cone

#12
QUOTE:
the colors match up with the peaks and troughs of the price series
This is by chance observation only (plus the fact that nearly all indicators lag price).

CODE:
Please log in to see this code.
You have it. The input to the ZZ indicator is the StochK, not price.

The indicator is detecting the peaks and troughs in the StochK on the bar where the color changes. You cannot detect a peak or trough on the bar that it actually occurs. There must be movement in the opposite direction for a peak or trough to form.

What are you trying to achieve?

profile picture

grtrader

#13
Perhaps the stoch is not a good example since it corresponds to price too much. What I want to do is use a longer term indicator to determine whether the trend is up or down, for instance, the TSI. If the trend is up, it would provide a permission for other indicators to determine whether to take a trade or not. If the trend is down, it would prevent taking trades. Since there are ups and downs in the TSI indicator, like any other, I believe this method would show the overall trend since it moves slowly.

The zigzag shows a dotted line after the last peak or trough. Even though not perfect, it shows the trend direction of the indicator. With a variable assigned to the zigzag lines on the indicator, it would also allow back testing.
profile picture

Eugene

#14
CODE:
Please log in to see this code.

Of course the $64,000 question here is which Reverse % to take for each market and how/when to adjust it for changing market volatility.
profile picture

grtrader

#15
Assume low volatility. Long term indicators do not have large swinging peaks and troughs. The way I see it, one can use moving averages, which can be misleading due to false indications. The other way is to compare a signal[bar] to another signal[bar-1] or [bar-2], etc., which can also give false signals. At least the zigzag points in the right direction.

This could also be used to provide alerts for stocks in up trends.
profile picture

Swissi

#16
I would like to have a zigzag indicator with a variable Reverse %. I would like to use the changing volatility as % Up and % Down indicator. If the volatility is 10%, the Reverse % would be f.ex. 2.5%, with 20% it would 5% (just one quarter of the vola).
profile picture

grtrader

#17
Will I be able to get access to a variable which follows zigzag status of an indicator per my original question?

profile picture

Eugene

#18
@Swissi:

You're not alone but I got you covered. Check out this topic:

Is there an indicator that would do PeakTroughMode.ATR ?

@grtrader:

Sure, you already have it! Please see my my reply #2 to which I've got nothing else to add. We're going in circles.