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

- Channel: 量化交易科學園區 - 江加宏
- Published: 2022-12-31
- 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-ep9/?ref=agent
- YouTube: https://www.youtube.com/watch?v=PjfOAzyUvo8

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

## Section summaries

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

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

### 2:55 Writing 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.

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

---

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

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