Algorithm to accurately finding swing & internal points - page 2

 
Talal N Z Aljarusha #:

I would separate the problem into two layers:

  1. Confirmed swing points.
  2. Internal structure points.

If you mix both in one rule, the indicator usually becomes noisy or starts repainting. A confirmed swing needs right-side bars, so it will always have some delay. Internal points can be detected earlier, but they should be treated as lower-confidence structure until price confirms them.

A simple confirmed swing high/low function can start like this:

bool IsSwingHigh(const int i,const double &high[],int depth) { for(int k=1;k<=depth;k++) { if(high[i] <= high[i-k]) return false; if(high[i] <= high[i+k]) return false; } return true; } bool IsSwingLow(const int i,const double &low[],int depth) { for(int k=1;k<=depth;k++) { if(low[i] >= low[i-k]) return false; if(low[i] >= low[i+k]) return false; } return true; }

After that, I would not classify HH/HL/LL/LH from every candle. Store only accepted pivots first, then compare the last accepted pivot with the previous pivot of the same type.

For internal structure, use a smaller depth and add validation rules, for example:

  • minimum distance from the previous pivot;
  • minimum candle range or ATR fraction;
  • break of a previous internal high/low;
  • do not accept two consecutive highs or lows unless the new one is more extreme.

You can keep the data in a small structure:

The important part is to decide what you want from the indicator. If you want non-repainting structure, accept the delay. If you want earlier internal points, mark them as provisional and update them until confirmation. Trying to make them early and final at the same time is usually where the logic becomes unstable.

I've done both...I've made a zigzag which records every candidate swing (accepting repaint), and made a zigzag that only draws markers on confirmed swings. If the trading system works on historical swings for support and resistance...then use the confirmed swings of course. If the trading system looks for entry signals, then you use the extremum candidate with confirmation on moving averages or momentum.

 

Thank you to @Ryan L Johnson & @Conor Mcnamara for sharing indicators & source; I appreciate it and both are very interesting.

WMBR, ESB.

 

One thing I would add to the good answers above: treat swing detection like a strategy and measure it, instead of judging it visually.


When I test a structure engine I log two numbers over a few thousand bars per instrument: the flip rate (how often an HH/HL/LL/LH classification changes after being assigned) and survival (what percentage of detected swings still matter N bars later). A detector can look clean on a screenshot and still have a high flip rate, which silently destroys any EA built on top of it.


Fixed right-side confirmation (fractal style) scores well on flip rate but labels late; percentage or ATR zigzags label earlier but flip more. There is no free lunch - only a trade-off to choose based on what consumes the output. A human eye tolerates late labels; an EA does not.


One practical detail that bit me: store accepted pivots in their own array and classify HH/HL/LL/LH only between accepted pivots, never against raw candles. And test on gold and indices, not just EURUSD - parameters that give stable structure on majors often fall apart on high-ATR symbols.