# Custom SwingHighBar: Finding Perfect Peaks with Rising Left Side and Falling Right Side

- Channel: 量化交易科學園區 - 江加宏
- Published: 2022-11-28
- Series: [Quant Trading Science Park](https://xknow.app/en/s/quant-trading-science-park/?ref=agent) ([series index](https://xknow.app/en/s/quant-trading-science-park/llms.txt))
- Topics: [Technical Analysis](https://xknow.app/en/topics/technicals/llms.txt), [AI & Software Engineering](https://xknow.app/en/topics/ai-engineering/llms.txt)
- Page: https://xknow.app/en/v/quant-trading-science-park-ep8/?ref=agent
- YouTube: https://www.youtube.com/watch?v=wzFtwU9gzrY

> 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.

## Section summaries

### 0:05 Limits 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.

[Page](https://xknow.app/en/v/quant-trading-science-park-ep8/?ref=agent#s-0) | [YouTube 0:05](https://www.youtube.com/watch?v=wzFtwU9gzrY&t=5s)

### 0:45 Function 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.

[Page](https://xknow.app/en/v/quant-trading-science-park-ep8/?ref=agent#s-1) | [YouTube 0:45](https://www.youtube.com/watch?v=wzFtwU9gzrY&t=45s)

### 6:55 Bar-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.

[Page](https://xknow.app/en/v/quant-trading-science-park-ep8/?ref=agent#s-2) | [YouTube 6:55](https://www.youtube.com/watch?v=wzFtwU9gzrY&t=415s)

### 13:05 Indicator 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.

[Page](https://xknow.app/en/v/quant-trading-science-park-ep8/?ref=agent#s-3) | [YouTube 13:05](https://www.youtube.com/watch?v=wzFtwU9gzrY&t=785s)

---

Full notes (with slides) and other episodes in this series: https://xknow.app/en/v/quant-trading-science-park-ep8/?ref=agent

Watch the original video: https://www.youtube.com/watch?v=wzFtwU9gzrY
