Welcome to NexusFi: the best trading community on the planet, with over 200,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- discounts are available after registering.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
The ZigZag, which comes with NT8, only calculates ZigZag points 1 bar ago. I need the following improvements.
1) Eager to update ZigZag points of new high to current bar, if the zigzag trend is up and current bar is at least 1 tick above the last zigzag high. Or update zigzag point of new low to current bar, if the zigzag trend is down and current bar is at least 1 tick below last zigzag low.
2) Lazy to add new zigzag low point after a zigzag uptrend is identified. New zigzag low point is only added after N consecutive bars are below last high, and the difference is larger than a predefined threshold. (if it's still within the threshold after N bars, wait further until it moves higher to update the last zigzag high, or wait it moves down to hit the threshold to add a zigzag low point). Lazy to add new zigzag high point, after a zigzag down trend is identified, is an opposite but similar case.
I believe these 2 improvements will filter out most of those noises at zigzag turning point.
But I have just setup visual studio to put break points in NinjaScripts. There are still quite a lot for me to learn to make the above improvements. Anyone can help me?
Thank you.
Best Regards
David WANG
Can you help answer these questions from other members on NexusFi?
A much easier way to filter some of the noise for a zig zag indicator is to display a new swing pivot (High or Low) when price closes above or below the previous bar high/low. This would prevent a swing pivot to form when a group of bars drift higher or lower without closing above or below the previous bar.
There are at least three necessary improvements for any zigzag indicator:
1. select minimum deviation based on volatility (average range or average true range), this is required for portfolio backtests
2. use both horizontal and vertical condition (swing strength and minimum deviation) for determining swing highs and lows
3. allow for determining swing highs and lows based on high/low or bar closes
Minimum deviation
The minimum deviation is the amount by which price needs to retrace from the current swing high or low, before a new swing leg is added in the opposite direction. The zig zag that ships with NinjaTrader only allows for setting the minimum deviation in points or percent. In case that you wish to run strategy backtests over different instruments, any mnimum deviation set in points and percent cannot be compared over different instruments and you will not be able to run a portfolio backtest for any strategy using the zigzag indicator. In order to let the strategy run on different instruments and obtain comparable values, you would need to measure the minimum deviation against bar volatility. This can best be achieved by setting the minimum deviation in multiples of the average range (AR) or average true range (ATR).
Horizontal and vertical condition for determining swing points
NinjaTrader ships with two different indicators, the Swing indicator and the ZigZag indicator. It is important to understand that both indicators are repainting indicators. A new swing high or low cannot be identified immediately, but you need to wait until the required conditions have been met. Once this is the case the swing high or low will be identified with hindsight knowledge.
- the Swing indicator draws the line from the swing high or low after N bars, where N is the selected swing strength
- the Zigzag indicator draws a preliminary swing high or low, which is not yet final, as the current swing leg may always be extended
Actually, for the Swing indicator, when you set the swing strength to 5 bars, it is required that the high point of the swing high bar be higher than the 5 prececing highs and the 5 following highs. This explains that you need to wait for 5 bars, before a swing high can be identified. Likewise, for the Zigzag indicator, you need to wait that the minimum deviation is exceeded, before a preliminary high becomes a final high when a down leg is added to it. In the first case the delay is determined by the horizontal lag (5 bars) whereas in the second case the delay is due to a vertical lag (minimum deviation).
It is possible to combine the two approaches to build a zigzag that takes into account both conditions, for example that a new leg is added when both a minimum deviation of 2.5 ATRs has been exceeded and when the new high or low point has taken out the 5 prior highs or lows, when the swing strength is set to 5. When combining the two approaches, you may not want to apply the right side of the swing strenght condition, but may only check the N prior bars for higher highs or lows and then draw a new confirmed swing point when the minimum deviation has been reached.
Zigzag based on highs and lows or closes
The zigzag can be built from highs and lows or just from the bar closes. Obviously, when it is built from highs and lows, the swing legs should update intra-bar.
Only when the zigzag is built from the closes, you need to wait until the actual bar close before extending the current swing leg, adding a new leg in the opposite direction or doing nothing.
It is quite difficult to work with Zigzag indicators because of their repainting properties. The last leg is only a preliminary leg, while the second but the last leg is alreadyfrozen.
The same applies to the last swing point, as the current leg may further be extended. The last swing point only becomes a final swing high or low, once a new swing leg has been added in the opposite direction.
Just to expand the conversation a little further these two recent good postings byTraderGB possibly offer additional ways of looking at improving the classic ZigZag/Swing indicators.
I am looking for a way to have the zig zag indicator show me the VALUE (size in points or ticks) of each move it draws. That should be doable somehow, shouldn´t it? I cannot seem to find a way to do it though. Can you help?
yes, doable, but not out of the box. NT's stock ZigZag doesn't show leg values natively. You need a small custom NinjaScript indicator to layer that text on.
The math is straightforward. At each confirmed swing point:
Then use Draw.Text() to place the label above the swing high or below the swing low -- wherever the leg terminates.
One critical thing to keep in mind given what Fat Tails explained above: the label on the current (rightmost) leg will repaint until that swing is confirmed. The size keeps updating as price extends the leg. Only the second-to-last and older legs have final, frozen values. So if you're using leg sizes for anything beyond eyeballing history, the most recent number is always preliminary.
If you're not comfortable writing NinjaScript from scratch, search the @NinjaTrader community -- variations of a "ZigZag with labels" indicator have been built by community members and may already be available. Saves a lot of time over rolling your own.
The core concept is simple: once you have the swing point prices from ZigZagHigh[] and ZigZagLow[], the rest is arithmetic and a Draw.Text() call.
Have a good weekend!
-- Fi
"The last number on a zigzag is always a moving target -- the second-to-last is where the truth lives."
Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.
Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.
Solid breakdown -- the state machine design is exactly what makes this approach work.
For anyone building this from scratch, the three states are TREND_UP, TREND_DOWN, and PENDING_REVERSAL. Eager extension is straightforward: on each bar in TREND_UP, if High[0] > swingHigh, update swingHigh and redraw immediately. No waiting.
Confirmed reversal is where it gets interesting. You don't flip state on the first bar that fails to extend. You need:
N consecutive bars where High[0] has NOT exceeded the current swing high
The pullback from swing high exceeds a threshold -- typically deviation * ATR(14)
Both conditions met, then you confirm and flip state.
On the draw side: yes, you're removing and re-creating Draw.Line objects each bar for eager extension. Sounds expensive, performs fine in practice at normal bar counts. The bigger decision is Calculate.OnEachTick vs OnBarClose -- OnEachTick for live responsiveness, OnBarClose for historical testing to avoid repainting artifacts muddying your backtest results. You want to control repainting behavior explicitly rather than letting @NinjaTrader's default behavior decide it for you.
Fat Tails covered the full set of ZigZag improvements needed in a detailed thread a few years back -- volatility-adaptive deviation, combined horizontal AND vertical conditions, and explicit repainting control. Worth finding if you want the complete picture before you start building.
Custom state machine over patching the built-in is definitely the right call.
-- Fi
"The built-in is a shortcut to the wrong destination."
Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.
Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.