OPEN-SOURCE SCRIPT

SMT Divergence [Kodexius]

135
SMT Divergence is a correlation-based divergence detector built around the Smart Money Technique concept: when two normally correlated instruments should be making similar swing progress, but one prints a new extreme while the other fails to confirm it. This “disagreement” can be a valuable contextual signal around liquidity runs, distribution phases, and potential reversal or continuation points.

The script compares the chart symbol (primary) with a user-selected comparison symbol (for example BTC vs ETH, ES vs NQ, EUR/USD vs GBP/USD) and automatically scans both instruments for confirmed swing highs and swing lows using pivot logic. Once swings are established, it checks for classic SMT conditions:
Primary makes a new swing extreme while the comparison symbol forms a non-confirming swing.

To support a wider range of markets, the indicator includes an Inverse Correlation option for pairs that typically move opposite to each other (for example DXY vs EUR/USD). With this enabled, the divergence rules are logically flipped so that the script still detects “non-confirmation” in a way that is consistent with the pair’s relationship.

The indicator is designed to be readable and actionable. It can draw divergence labels directly on the main chart, connect the relevant swing points with lines, show a compact information table with the last signal and settings, and optionally render the comparison symbol as a mini candle chart in the indicator pane for quick visual validation.

لقطة

🔹 Features

🔸 Two-Symbol SMT Analysis (Primary vs Compare)

Select any comparison symbol to evaluate correlation structure and divergence. The script fetches the comparison OHLC data using the current chart timeframe to keep both series aligned for analysis.

🔸 Inverse Correlation Mode

For inversely correlated pairs, enable “Inverse Correlation” so the script interprets confirmation appropriately (for example, a higher low on the comparison instrument might be expected to correspond to a lower low on the primary, depending on the relationship). This helps avoid false conclusions when the pair naturally moves opposite.

🔸 Pivot-Based Swing with Adjustable Sensitivity

Swings are detected using confirmed pivots (left bars and right bars). This provides cleaner structural swing points compared with raw candle-to-candle comparisons, and it lets you control sensitivity for different market conditions and timeframes. The script also limits stored swing history to keep performance stable.

🔸 Flexible Detection Mode: Time Matched or Independent Swings

You can choose how swings are paired across instruments:

Time Matched searches for a comparison swing that occurred at the same pivot time as the primary swing.

لقطة

Independent Swings compares each symbol’s own last two swings without requiring an exact time match.

لقطة

🔸 Range Control and Noise Filtering

To reduce weak or irrelevant signals:

“Max Bars Between Swings” ensures the two swings being compared are close enough in structure to be meaningful.

“Min Price Diff (%)” can require a minimum percentage change between the primary’s last two swing prices to confirm the move is significant.

🔸 Clear Visual Output with Tooltips

When a divergence is detected, the script can print a label (“SMT”) with bullish or bearish styling and a tooltip that includes the symbol pair and the primary swing price for quick context.

🔸 Divergence Lines for Context

Optional lines connect the relevant swing points, making it easier to see the exact structure that triggered the signal. One line can be drawn on the main chart and another in the indicator pane for the comparison series.

🔸 Info Table (At a Glance)

A compact table can display the active symbols, correlation mode, total divergences stored, and the most recent signal type.

🔸 Alerts Included

Built-in alert conditions are provided for bullish SMT, bearish SMT, and any SMT event so you can automate notifications without editing the code.

🔸 Optional Comparison Candle Panel

If enabled, the indicator can plot the comparison symbol as candles in the indicator pane. This is useful for confirming whether the divergence is happening around major levels, consolidations, or impulsive legs on the secondary instrument.

🔹 Calculations

This section summarizes the core logic used by the script.

1. Data Synchronization (Comparison Symbol)

The comparison instrument is requested on the chart’s current timeframe so swing calculations are performed consistently:

Pine Script®
[compareOpen, compareHigh, compareLow, compareClose] = request.security(compareSymbolInput, timeframe.period, [open, high, low, close])


This ensures pivots and swing times are derived from the same bar cadence as the primary chart.

2. Swing Detection via Confirmed Pivots

Swings are detected using pivot logic with user-defined left and right bars:

Pine Script®
primaryPivotHigh = ta.pivothigh(high, pivotLeftBars, pivotRightBars) primaryPivotLow = ta.pivotlow(low, pivotLeftBars, pivotRightBars)


Because pivots are confirmed only after the “right bars” have closed, the script stores each swing using an offset so the swing’s bar index and time reflect where the pivot actually occurred, not where it was confirmed.

3. Swing Storage and Retrieval

Both symbols maintain arrays of SwingPoint objects. Each new swing is pushed into the array, and older swings are dropped once the array exceeds the configured maximum. This makes the divergence engine predictable and prevents uncontrolled memory growth.

The script then retrieves the last and previous swing highs and lows (per symbol) to evaluate structure.

4. Matching Logic (Time Matched vs Independent)

When “Time Matched” is selected, the script searches the comparison swing array for a pivot that occurred at the exact same timestamp as the primary swing. When “Independent Swings” is selected, it simply uses the comparison symbol’s last two swings of the same type.

5. Bullish SMT Condition (LL vs HL)

A bullish SMT event is defined as:

Primary forms a lower low (last low < previous low)

Comparison forms a higher low (last low > previous low)

If inverse correlation is enabled, the comparison condition flips to maintain logical confirmation rules

The two primary swings must be within the configured bar distance window

Optional minimum percentage difference must be satisfied

A simple anti duplication rule prevents repeated triggers on the same structure

These checks are implemented directly in the bullish detection block.

6. Bearish SMT Condition (HH vs LH)

A bearish SMT event is defined as:

Primary forms a higher high (last high > previous high)

Comparison forms a lower high (last high < previous high)

Inverse correlation flips the comparison rule

Range checks, minimum difference filtering, and duplicate protection apply similarly

These checks are implemented in the bearish detection block.

7. Percentage Difference Filter

The optional “Min Price Diff (%)” filter measures the relative distance between the last two primary swing prices. This prevents very small structural changes from being treated as valid SMT signals.

Pine Script®
priceDiffPerc = math.abs(lastSwing.price - prevSwing.price) / prevSwing.price * 100.0


The divergence condition is only allowed to trigger if this value exceeds the user defined threshold.

Pine Script®
priceOk = priceDiffPerc >= minPriceDiff


This filter is especially useful on higher timeframes or during low volatility conditions, where micro structure noise can otherwise produce misleading signals.

8. Visualization and Output

When a divergence is confirmed, the script:

Stores the event in a divergence array (limited by “Max Divergences to Display”)

Draws a directional SMT label with a tooltip (optional)

Draws connecting lines using time based coordinates for clean alignment (optional)

It also updates an information table on the last bar only, and exposes alertconditions for automation workflows.

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.