OPEN-SOURCE SCRIPT

Market structure + TF Bucket

172
Market Structure + TF Bucket
This Pine Script™ indicator, published under the Mozilla Public License 2.0, extends the "Market Structure" script by mickes (tradingview.com/script/ud6AQSm2-Market-structure/), with full credit to mickes. It integrates the enhanced MarketStructure library by Fenomentn (tradingview.com/script/hfkBS2oS-MarketStructureLib/), also based on mickes’ library under MPL 2.0, to provide advanced market structure analysis with multi-timeframe pivot length customization.
Functionality

Market Structure Analysis: Detects internal (orderflow) and swing market structures, visualizing Break of Structure (BOS), Change of Character (CHoCH), Equal High/Low (EQH/EQL), and liquidity zones using the MarketStructure library.
Timeframe Bucket (TF Bucket): Dynamically adjusts pivot lengths for six user-defined timeframes (e.g., 3m, 5m, 10m, 15m, 4h, 12h), optimizing structure detection across different chart timeframes.
Trend Strength Visualization: Displays a trend strength metric (from the library) for internal and swing structures, indicating trend reliability based on pivot frequency and volatility.
Statistics Table: Shows yearly counts of BOS and CHoCH events for internal and swing structures, configurable by a user-defined period.
Screener Support: Outputs BOS and CHoCH signals for TradingView’s screener, with a configurable signal persistence period.
Customizable Alerts: Enables alerts for BOS and CHoCH events, separately configurable for internal and swing structures.

Methodology

Pivot Detection: Uses the library’s Pivot function, which applies a volatility filter (ATR-based) to confirm significant pivots, reducing false signals in low-volatility markets.
TF Bucket: Maps user-selected timeframes to Pine Script’s timeframe.period using f_getTimeframePeriod, applying custom pivot lengths when the chart’s timeframe matches a selected one (or base lengths in Static mode).
Trend Strength: Calculates a score as pivotCount / LeftLength * (currentATR / ATR), displayed via labels to help traders assess trend reliability.
BOS/CHoCH Detection: Identifies BOS when price breaks a pivot in the trend direction and CHoCH when price reverses against the trend, labeling events as “MSF” or “MSF+” based on pivot patterns.
EQH/EQL and Liquidity: Draws boxes for equal high/low zones within ATR-based thresholds and visualizes liquidity levels with confirmation bars.
Statistics and Screener: Tracks BOS/CHoCH events in a yearly table and outputs signals for screener use, with persistence controlled by a user-defined period.

Usage

Integration: Apply the indicator to any chart and import the library via import Fenomentn/MarketStructure/1.
Configuration: Set up to six timeframes with custom pivot lengths, enable/disable internal and swing structures, configure alerts, and adjust statistics years in the settings panel.
Alerts: Enable BOS and CHoCH alerts for real-time notifications, triggered on bar close to avoid repainting.
Screener: Use the plotted signals to monitor BOS/CHoCH events across multiple tickers in TradingView’s screener.
Best Practices: Optimal for forex and crypto charts on 1m to 12h timeframes. Adjust pivot lengths and the library’s volatility threshold for specific market conditions.

Originality
This indicator enhances mickes’ original script with:

Timeframe Bucket: Dynamic pivot length selection for multi-timeframe analysis, not present in the original.
Trend Strength Display: Visualizes the library’s TrendStrength metric for enhanced trend analysis.
Enhanced Library Integration: Leverages Fenomentn/MarketStructure/1, which adds a volatility-based pivot filter, dynamic label sizing, and customizable BOS/CHoCH visualization styles.No additional open-source code was reused beyond mickes’ script and library, fully credited under MPL 2.0.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.