Zigzag Indicator

The previously posted zigzag study above was quite useful and my compliments to the authors. I needed something slightly different, however, more suitable for real-time identification and so here's a slightly tighter version that's about 80% smaller (from 100 lines of code down to about 18).

This zigzag indicator is more of an indicator than a study and therefore it identifies peaks one bar "late" which is the only way to behave for real time peak identification. Most importantly, it should (in theory) not set peaks/valleys retroactively. It does not offer the various lines of the other zigzag -- I had no need to know the bars since the last peak or valley but these can be added after the fact in a single line by counting a list comprehension or by using a ternary iteration.

As well you can specify a minimum threshold for the current bar minus the last bar to qualify as being recognized as a minimum, as well as a threshold for the minimum distance to the last peak/valley. Naturally, as in the case below, if you set these thresholds to non-zero values you can have peaks without valleys and valleys without peaks in some instances.

If you find a bug please let me know and any feedback is appreciated.

And here's a screenshot of this on a randomly (and poorly) selected SMA and the thresholds were just set arbitrarily just to show an example:

@backtrader My pleasure Daniel - I'm probably being too cagey with the above so here's something that might (or might not) help you and what I am working towards. The below is my use of a zigzag indicator with very minimal filtering to identify wavelet inflection points at various increasing frequencies (decomposition level edge detection):

Hi Amir, sorry for the delay in responding. There is no period as this pulls out highs and lows (peaks and valleys) where the high is highest compared to the previous low, and the subsequent low is lowest compared to the previous high so it starts from the line's beginning. A period would not work especially for situations such as [5,5,5,5,5,5,5] -- with five being ambiguous as to whether it's a high or a low or somewhere in between.

looking into zigzag / swing pivot indicators as well atm. has someone already converted something like https://github.com/tomas-rampas/ZigZag/blob/master/zigzag/init.py to backtrader. i'm pretty new with this tool and it seems as data feed must be processed the other way around as backtrader is looking backwards. any help appreciated :)