Automatically Detecting an M-Top with XScript: Swing Highs, Neckline and Multiple Filter Conditions

TL;DR: An M-top can be detected automatically with code: define the lookback range, two highs, the neckline and the starting low, then find the highs with a swing high function and stack several filter conditions so the result stays close to the standard pattern.

Key ideas

  • Before writing code, break the pattern into measurable elements: the length of the observation range, the two highs and their positions, the low between the two highs (the neckline), the starting low, and the head height and base amplitude. Once these are clearly defined, the code is easier to write.
  • The highs are obtained with the custom c swing height and c swing height bar, which give the value and the position. They differ from the built-in swing height by only one parameter; without the custom functions you can use swing height instead, though the result is less good.
  • The neckline is the lowest point between the two highs, and the starting low is searched backward from the second high. The higher of the two highs is set as top; head height is top minus the neckline, and base amplitude is the neckline minus the bottom.
  • Detection works by stacking conditions one by one: the second high must exist, both highs must be 10% above the neckline, head height must be greater than base amplitude, there must be no higher point between the two highs, and one of the highs must be the highest point in the range. Every threshold can be adjusted to personal needs.
  • After backtesting, several M-tops can be marked on the chart. Most are quite standard, but in one case the two highs are not as clear.
量化交易科學園區 - 江加宏Length 10:04Quant 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:05M-Top Definition and the Functions Needed

    Defines the M-top's range, two highs, neckline and starting low, and introduces the functions that will be used, such as swing height, max list and the ifs conditional.

    ▶ Watch from here
    Slide: M-Top Definition and the Functions Needed
  2. 2:55Writing the M-Top Code Step by Step and Backtesting

    Use the custom c swing height and c swing height bar to get the two highs and their positions, then use an ifs conditional to find the lowest point between the two highs as the neckline, and search backward from the second high for the starting low. Next, add conditions one by one: the second high must exist, both highs must be 10% above the neckline, head height must be greater than base amplitude, there must be no higher point between the two highs, and one of the highs must be the highest point in the range. In the backtest, several M-tops are marked on the chart, and one of them is not as clear.

    ▶ Watch from here
    Slide: Writing the M-Top Code Step by Step and Backtesting

This page summarizes the video; it is not an official transcript. Original video: Automatically Detecting an M-Top with XScript: Swing Highs, Neckline and Multiple Filter Conditions (量化交易科學園區 - 江加宏), copyright belongs to the original creator.