market_structure

This Pine Script implements a multi-timeframe market structure indicator to detect pivot-based trend directions and potential change of character (CHoCH) in price action. It's useful for identifying trend shifts and drawing key structural levels like swing highs, swing lows, and break points.
🔍 Core Features:
Pivot-Based Highs and Lows: Uses ta.pivothigh and ta.pivotlow with configurable lookback periods (left and right) to detect local highs and lows.
Direction Detection Modes: Supports multiple methods for trend direction:
'hl': based on price breaking recent high/low
'close': based on close price
'fbc': full-body candle break (min(open, close) vs. max(open, close))
'minmax': based on dynamically tracked min highs and max lows (via get_dir)
Trend Direction Tracking:
pp_dir: Primary pivot direction
pp_high_dir, pp_low_dir: Direction of highs/lows
pp_sl_dir: Secondary structure direction (based on previous pivot comparisons)
Multi-Timeframe Support: The indicator fetches pivot and direction signals from a selectable higher timeframe using request.security.
🔧 Inputs:
Timeframe (tf): Timeframe to analyze (e.g. '60', 'D', or leave empty for current)
Direction Mode (dir_mode): One of 'hl', 'close', 'fbc', 'minmax'
Source High/Low: Customizable source series for pivot calculations (default: high and low)
Pivot Lookback (left and right): Controls how far left/right to check for pivots
Centering Option (center): Offsets plots for pivot centering
Show Dir (show_dir): Toggles whether to color lines based on trend direction
🧠 Function Logic:
get_dir(pp_high, pp_low)
Tracks dynamic support/resistance levels and detects Change of Character (CHoCH):
If price breaks above tracked highs → Bullish shift
If price breaks below tracked lows → Bearish shift
market_structure(...)
Calculates pivots, smoothed highs/lows (pp_high, pp_low)
Determines trend direction using the selected mode
Computes midpoints and potential SL levels (pp_sl_high, pp_sl_low)
📊 Plots:
BUY (pp_high): Bullish pivot line
SELL (pp_low): Bearish pivot line
Middle (pp_mid): Midpoint of structure (optional, hidden by default)
SL BUY / SL SELL: Potential stop-loss zones, based on previous pivot extensions
Line colors are adjusted depending on direction:
Green: Bullish trend
Red: Bearish trend
This indicator is powerful for traders applying Smart Money Concepts (SMC) or Price Action-based analysis, as it automates structural trend detection and visualizes key breakout or reversal zones.
Would you like a visual diagram to explain the pivot logic or direction modes?
Libreria Pine
In pieno spirito TradingView, l'autore ha pubblicato questo codice Pine come libreria open-source in modo che altri programmatori Pine della nostra comunità possano riutilizzarlo. Complimenti all'autore! È possibile utilizzare questa libreria privatamente o in altre pubblicazioni open-source, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento.
Declinazione di responsabilità
Libreria Pine
In pieno spirito TradingView, l'autore ha pubblicato questo codice Pine come libreria open-source in modo che altri programmatori Pine della nostra comunità possano riutilizzarlo. Complimenti all'autore! È possibile utilizzare questa libreria privatamente o in altre pubblicazioni open-source, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento.