OPEN-SOURCE SCRIPT

Contrarian 100 MA

142
Pairs nicely with Enhanced-Stock-Ticker-with-50MA-vs-200MA located here:
Enhanced Stock Ticker with 50MA vs 200MA


Description
The Contrarian 100 MA is a sophisticated Pine Script v6 indicator designed for traders seeking to identify key market structure shifts and trend reversals using a combination of a 100-period Simple Moving Average (SMA) envelope and Inner Circle Trader (ICT) Break of Structure (BoS) and Market Structure Shift (MSS) logic. By overlaying a semi-transparent SMA-based shadow on the price chart and plotting bullish and bearish structure signals, this indicator helps traders visualize critical price levels and potential trend changes. It leverages higher timeframe (HTF) pivot points and dynamic logic to adapt to various chart timeframes, making it ideal for swing and contrarian trading strategies. Customizable colors, timeframes, and alert conditions enhance its versatility for manual and automated trading setups.
Key Features
SMA Envelope: Plots a 100-period SMA for high and low prices, creating a semi-transparent (50% opacity) purple shadow to highlight the price range and provide context for price movements.

ICT BoS/MSS Logic: Identifies Break of Structure (BoS) and Market Structure Shift (MSS) signals for both bullish and bearish conditions, based on HTF pivot points.

Dynamic Timeframe Support: Adjusts pivot detection based on user-selected HTF (default: 1D) and chart timeframe (1M, 5M, 15M, 30M, 1H, 4H, 1D), ensuring adaptability across markets.

Visual Signals: Draws dotted lines for BoS (bullish/bearish) and MSS (bullish/bearish) signals at pivot levels, with customizable colors for easy identification.

Contrarian Approach: Signals potential reversals by combining SMA context with ICT structure breaks, ideal for traders looking to capitalize on trend shifts.

Alert Conditions: Supports alerts for bullish/bearish BoS and MSS signals, enabling integration with TradingView’s alert system for automated trading.

Performance Optimization: Uses efficient pivot detection and line management to minimize resource usage while maintaining accuracy.

Technical Details
SMA Calculation:
Computes 100-period SMAs for high (smaHigh) and low (smaLow) prices.

Plots invisible SMAs (fully transparent) and fills the area between them with 50% transparent purple for visual context.

Pivot Detection:
Uses ta.pivothigh and ta.pivotlow to identify HTF swing points, with dynamic lookback periods (rlBars: 5 for daily, 2 for intraday).

Tracks pivot highs (pH, nPh) and lows (pL, nPl) using a custom piv type for price and time.

BoS/MSS Logic:
Bullish BoS: Triggered when price breaks above a pivot high in a bullish trend, drawing a line at the pivot level.

Bearish BoS: Triggered when price breaks below a pivot low in a bearish trend.

Bullish MSS: Occurs when price breaks a pivot high in a bearish trend, signaling a potential trend reversal.

Bearish MSS: Occurs when price breaks a pivot low in a bullish trend.

Lines are drawn using line.new with xloc.bar_time for precise alignment, styled as dotted with customizable colors.

HTF Integration: Fetches HTF close prices and pivot data using request.security with lookahead_on for accurate signal timing.

Line Management: Maintains an array of lines (lin), removing outdated lines when new MSS signals occur to keep the chart clean.

Pivot Reset: Clears broken pivots (e.g., when price exceeds a pivot high or falls below a pivot low) to ensure fresh signal generation.

How to Use
Add to Chart:
Copy the script into TradingView’s Pine Editor and apply it to your chart.

Configure Settings:
SMA Length: Adjust the SMA period (default: 100 bars) to suit your trading style.

Structure Timeframe: Set the HTF for pivot detection (default: 1D).

Chart Timeframe: Select the chart timeframe (1M, 5M, 15M, 30M, 1H, 4H, 1D) to adjust pivot sensitivity.

Colors: Customize bullish/bearish BoS and MSS line colors via input settings.

Interpret Signals:
Bullish BoS: White dotted line (default) at a broken pivot high in a bullish trend, indicating trend continuation.

Bearish BoS: White dotted line at a broken pivot low in a bearish trend.

Bullish MSS: White dotted line at a broken pivot high in a bearish trend, suggesting a reversal to bullish.

Bearish MSS: White dotted line at a broken pivot low in a bullish trend, suggesting a reversal to bearish.

Use the SMA shadow to gauge price position within the recent range.

Set Alerts:
Create alerts for bullish/bearish BoS and MSS signals using TradingView’s alert system.

Customize Visuals:
Adjust line colors or SMA fill transparency via TradingView’s settings for better visibility.

Example Use Cases
Swing Trading: Use MSS signals to enter trades at potential trend reversals, with the SMA envelope confirming price extremes.

Contrarian Trading: Capitalize on BoS and MSS signals to trade against prevailing trends, using the SMA shadow for context.

Automated Trading: Integrate BoS/MSS alerts with trading bots for systematic entries and exits.

Multi-Timeframe Analysis: Combine HTF signals (e.g., 1D) with lower timeframe charts (e.g., 1H) for precise entries.

Notes
Testing: Backtest the indicator on your chosen market and timeframe to validate performance.

Compatibility: Built for Pine Script v6 and tested on TradingView as of June 19, 2025.

Limitations: Signals rely on HTF pivot accuracy, which may lag in fast-moving markets. Adjust rlBars or timeframe for sensitivity.

Optional Enhancements: Consider uncommenting or adding a histogram for SMA divergence (e.g., smaHigh - smaLow) for additional insights.

Acknowledgments
This indicator combines ICT’s market structure concepts with a dynamic SMA envelope to provide a unique contrarian trading tool. Share your feedback or suggestions in the TradingView comments, and happy trading!

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.