Custom SwingHighBar: Finding Perfect Peaks with Rising Left Side and Falling Right Side
TL;DR: XQ's built-in SwingHighBar only requires the bars on both sides of a high to be lower, which easily picks up unattractive turning points, so a custom function was written that requires the left side to rise bar by bar, the right side to fall bar by bar, and every step to exceed a set percentage.
Key ideas
- The built-in function only checks that the bars on both sides of the highest point are lower, but in practice what is wanted is a reversal shape where the left side keeps rising and the right side keeps falling, which is the motivation for rewriting it.
- The first 5 parameters of the function (price series, lookback length, left shoulder, right shoulder, which peak) are the same as the original; the added step-difference percentage determines the shape: the larger it is, the sharper the peak, and the smaller it is, the more it resembles a gentle small hill.
- The price series is passed in by the indicator that calls the function, and can be a series such as high, low or close, not a manually entered value, which differs from ordinary indicator parameters.
- The core is a loop plus 2 conditions: condition 1 checks that the right shoulder falls bar by bar, condition 2 checks that the left shoulder rises bar by bar, and a peak is counted only when both hold. The loop only runs up to the lookback length minus the right shoulder, ensuring the highest point lies within the specified range, and it returns -1 if none is found.
- The function's return value represents how many bars back the peak is; after the indicator calls it, a dot can be drawn below low to mark it. It can describe reversal patterns and can also be used to decide on short entries.
Ask AI about this episode
Copy the prompt, paste it into an AI service, and add the question. The AI reads the notes on this site and links to the page and the video timestamps.
Or open directly:ChatGPTClaudeGrokPerplexity
Section summaries
0:05Limits of the Built-in High Detection and Motivation for Rewriting
XQ's built-in SwingHighBar only requires the bars on both sides of the highest point to be lower, but in practice what is more needed is a turning point where the left side rises step by step and the right side falls step by step, so it was decided to write a custom function to improve on it.
▶ Watch from here0:45Function Parameter Design and Loop Initialization
A numeric function is created with parameters including the price series, lookback length, left and right shoulders, which peak to find, and the step-difference percentage, and it is explained that the larger the step difference, the sharper the shape. Next, the CNT and bar variables are declared and the loop range is set.
▶ Watch from here6:55Bar-by-Bar Rise and Fall Conditions and Peak Counting
Inside the loop, condition 1 checks that the right shoulder falls bar by bar and condition 2 checks that the left shoulder rises bar by bar, with every gap required to exceed the set percentage. When both hold, CNT increases by 1; once the specified peak is reached, its position is recorded and the loop exits, and -1 is returned if none is found.
▶ Watch from here13:05Indicator Call, Plot Verification and Applications
The function is called from an indicator and plotted to verify that the bars found meet the conditions on both sides, with every gap exceeding 1%, and they are marked below low. This function can describe reversal patterns and suits short entries, though a better way to write it can still be considered.
▶ Watch from here


