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.
量化交易科學園區 - 江加宏Length 15:19Quant Trading Science Park
Episodes
  1. Mon, Nov 28Custom SwingHighBar: Finding Perfect Peaks with Rising Left Side and Falling Right Side
  2. Sat, Dec 31Automatically Detecting an M-Top with XScript: Swing Highs, Neckline and Multiple Filter Conditions
  3. Thu, Mar 16Detecting Volume-Price Divergence with Code: Comparing Price and Volume at Two Swing Highs
  4. Wed, Mar 29Recreating the Day-Trading Three Lines and a Short Alert for Breakouts That Fall Back, Using XS
  5. Tue, Apr 11RSI Bearish Divergence Algorithm and Tolerant Sampling: Syncing Price Highs with Indicator Highs
  6. Thu, Apr 20Measuring Bullish Strength from a Single Candlestick
  7. Fri, Apr 28Manual Entry, Automatic Exit: Using XQ Semi-Automated Trading to Free Up Office Workers
  8. Sun, May 14Automated Trading Without Coding Skills: Split a Strategy into Four Building Blocks of Stock Selection, Entry, Exit and Position Control
  9. Mon, Jun 5Is a High-Volume Long Upper Shadow a Reversal or a Bullish Shakeout? A 3-Year Backtest
  10. Sat, Jun 17Picking Stocks at the Second-Wave Breakout: Big Prior Rally, Low-Volume Consolidation, Then a High-Volume Long Red Candle
  11. Wed, Sep 23Disposition stocks no longer pull back under the new rules: buy those that close strong on day 1 and sell on day 5
  12. Wed, Oct 7A Live Trading Strategy Under the New Disposition Stock Rules: Buy on Day 1, Sell on the Last Day, and Use Stop-Losses
  13. Wed, Oct 21Foreign Capital Rotates Into Defensive Stocks, and a Review of Live Disposition Stock Trades
  14. Wed, Oct 28SDT Shared Variables: Fixed Memory That Saves Resources and Lets Instruments in the Same Strategy Pass Data to Each Other
  15. Wed, Nov 4Disposition Stock Strategy Flips From Bull to Bear: Why It Is Paused When the Market Weakens
  16. Wed, Nov 18Auditing Online Course Strategies with AI: Out-of-Sample Backtesting and Building Your Own XQ Minute-Bar Database
  17. Wed, Nov 25Options Long Straddle Backtest: Trading It Every Day Loses Over the Long Run, With Chances to Profit Only in High-Volatility Periods
▶ Watch the original on YouTube
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

  1. 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 here
    Slide: Limits of the Built-in High Detection and Motivation for Rewriting
  2. 0: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 here
    Slide: Function Parameter Design and Loop Initialization
  3. 6: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 here
  4. 13: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
    Slide: Indicator Call, Plot Verification and Applications

This page summarizes the video; it is not an official transcript. Original video: Custom SwingHighBar: Finding Perfect Peaks with Rising Left Side and Falling Right Side (量化交易科學園區 - 江加宏), copyright belongs to the original creator.