Quasimodo (QML) Pattern [UAlgo]Quasimodo (QML) Pattern is a market structure pattern detector that identifies Quasimodo formations using confirmed swing pivots and then visualizes the full structure directly on the chart. The Quasimodo concept is often described as a stop run and reversal model where price first violates a prior swing point, then breaks structure in the opposite direction. This script formalizes that idea into a strict pivot sequence and draws the key structural components so the pattern can be reviewed consistently.
The indicator maintains a rolling history of pivot highs and pivot lows, then checks the most recent four pivots for a valid Quasimodo sequence. When a bullish or bearish QML is confirmed, the script draws a connected structure, highlights the pattern area with a fill, plots a Market Structure Break reference line, and marks a QML zone derived from the left shoulder and the head. The most recent pattern zone is extended forward to keep it visible for potential retests.
This tool is intended to support structured analysis of reversal setups, with visual anchors that make it easy to locate left shoulder, reaction, head, and MSB points without manual drawing.
🔹 Features
1) Pivot Based Swing Engine
The script uses pivot highs and pivot lows to define swings. Left Bars and Right Bars control how many bars are required to confirm each pivot. This creates stable swing points and reduces noise compared to raw candle comparisons.
Confirmed pivots are stored as PivotPoint objects with price, bar index, and direction (high or low). The pivot history is capped to keep memory efficient while retaining enough context for detection.
2) Strict Four Pivot Pattern Validation
Detection uses the most recent four pivots, enforcing an alternating sequence so the structure follows a zig zag pattern. Only sequences that alternate high and low consistently are eligible for classification.
This avoids false detections where multiple highs or multiple lows occur in a row.
3) Bullish QML Detection Logic
The bullish model requires:
Left shoulder is a low
Reaction is a high
Head is a lower low than the left shoulder
MSB is a higher high than the reaction
This represents a liquidity grab below the prior swing low followed by a break above the reaction high, which is treated as the structure break confirmation.
4) Bearish QML Detection Logic
The bearish model requires:
Left shoulder is a high
Reaction is a low
Head is a higher high than the left shoulder
MSB is a lower low than the reaction
This represents a liquidity grab above the prior swing high followed by a break below the reaction low.
5) Full Pattern Visualization with Structural Labels
When a pattern is detected, the script draws:
Three visible connector lines linking left shoulder to reaction, reaction to head, and head to MSB
A filled highlight over the core structure area to emphasize the liquidity grab geometry
Point labels at left shoulder, head, and MSB for quick reading
A descriptive pattern label showing Bullish QML or Bearish QML
Visual styling, colors, and label sizing are user configurable.
6) MSB Break Reference Line
A dotted horizontal line is drawn from the reaction point to the MSB point at the reaction price level. This acts as a clear market structure break reference and helps validate that the break level was actually exceeded in the required direction.
7) QML Zone Projection
The script draws a QML zone box that spans the price range between the left shoulder and the head. This zone is extended forward in time so it remains visible for potential retests and reactions.
8) Efficient Execution and Update Behavior
Pattern checks run only when a new pivot is confirmed, reducing repeated evaluation. The script also extends the most recent QML zone and repositions the main label forward for better ongoing visibility.
🔹 Calculations
1) Pivot Detection
Pivot highs and pivot lows are confirmed using Left Bars and Right Bars:
float ph = ta.pivothigh(leftLen, rightLen)
float pl = ta.pivotlow(leftLen, rightLen)
When a pivot is confirmed, the bar index is aligned to the pivot location using the rightLen offset:
if not na(ph)
pivotArray.pushPivot(ph, bar_index , true)
if not na(pl)
pivotArray.pushPivot(pl, bar_index , false)
Each pivot is stored as:
price
index
isHigh flag
2) Alternating Sequence Requirement
The script evaluates the last four pivots p0 through p3 and requires strict alternation:
bool correctSequence =
(p0.isHigh != p1.isHigh) and (p1.isHigh != p2.isHigh) and (p2.isHigh != p3.isHigh)
This ensures the sequence forms a valid zig zag structure.
3) Bullish Quasimodo Conditions
Bullish QML is evaluated when p0 is a low:
if not p0.isHigh
if p2.price < p0.price and p3.price > p1.price
Quasimodo qml = Quasimodo.new(p0, p1, p2, p3, true)
Interpretation:
Head is a lower low relative to the left shoulder
MSB is a higher high relative to the reaction
This produces a sweep and reversal structure with confirmation
4) Bearish Quasimodo Conditions
Bearish QML is evaluated when p0 is a high:
if p0.isHigh
if p2.price > p0.price and p3.price < p1.price
Quasimodo qml = Quasimodo.new(p0, p1, p2, p3, false)
Interpretation:
Head is a higher high relative to the left shoulder
MSB is a lower low relative to the reaction
5) Pattern Drawing Components
When a pattern is confirmed, the script draws connecting lines:
line.new(LS.index, LS.price, R.index, R.price)
line.new(R.index, R.price, H.index, H.price)
line.new(H.index, H.price, MSB.index, MSB.price)
It also draws a horizontal MSB reference at the reaction price:
line.new(reaction.index, reaction.price, msb.index, msb.price, style=line.style_dotted)
A QML zone box is created using left shoulder and head prices and extended forward:
qml.qmlZone := box.new(LS.index, LS.price, bar_index + 10, H.price, bgcolor=color.new(col, 85))
On each bar, the most recent zone is extended further to remain visible:
box.set_right(lastQml.qmlZone, bar_index + 5)
label.set_x(lastQml.lbl, bar_index + 5)
6) Pivot History Management
To keep execution efficient, the pivot array is capped:
if pivots.size() > 50
pivots.shift()
Pattern checking is triggered only when a new pivot is found, which reduces redundant processing on bars where no structural update occurred.
Pine Script® インジケーター






















