Smart Trend Lines [The_lurker]

Smart Trend Lines
A multi-level trend classifier that detects bullish and bearish conditions using a methodology based on drawing trend lines—main, intermediate, and short-term—by identifying peaks and troughs. The tool highlights trend strength by applying filters such as the Average Directional Index (ADX) (A), Relative Strength Index (RSI) (R), and Volume (V), making it easier to interpret trend strength. The filter markers (V, A, R) in the Smart Trend Lines indicator are powerful tools for assessing the reliability of breakouts. Breakouts containing [VAR] are the most reliable, as they indicate strong volume support, trend strength, and favorable momentum. Breakouts with partial filters (such as [VA] or [VR]) require additional confirmation, while breakouts without filters ([]) should be avoided unless supported by other strong signals. By understanding the meaning of each filter and the market context.
Core Functionality
1. Trend Line Types
The indicator generates three distinct trend line categories, each serving a specific analytical purpose:
Main Trend Lines: These are long-term trend lines designed to capture significant market trends. They are calculated based on pivot points over a user-defined period (default: 50 bars). Main trend lines are ideal for identifying macro-level support and resistance zones.
Mid Trend Lines: These are medium-term trend lines (default: 21 bars) that focus on intermediate price movements. They provide a balance between short-term fluctuations and long-term trends, suitable for swing trading strategies.
Short Trend Lines: These are short-term trend lines (default: 9 bars) that track rapid price changes. They are particularly useful for scalping or day trading, highlighting immediate support and resistance levels.
Each trend line type can be independently enabled or disabled, allowing traders to tailor the indicator to their preferred timeframes.
2. Breakout Detection
The indicator employs a robust breakout detection system that identifies when the price crosses a trend line, signaling a potential trend reversal or continuation. Breakouts are validated using the following filters:
ADX Filter: The Average Directional Index (ADX) measures trend strength. A user-defined threshold (default: 20) ensures that breakouts occur during strong trends, reducing false signals in range-bound markets.
RSI Filter: The Relative Strength Index (RSI) identifies overbought or oversold conditions. Breakouts are filtered based on RSI thresholds (default: 65 for overbought, 35 for oversold) to avoid signals in extreme market conditions.
Volume Filter: Breakouts are confirmed only when trading volume exceeds a moving average (default: 20 bars) and aligns with the breakout direction (e.g., higher volume on bullish breakouts when the candle closes higher).
Breakout events are marked with labels on the chart, indicating the type of trend line broken (Main, Mid, or Short) and the filters satisfied (Volume, ADX, RSI). Alerts are triggered for each breakout, providing real-time notifications.
3. Customization Options
The indicator offers extensive customization through input settings, organized into logical groups for ease of use:
Main Trend Line Settings
Length: Defines the number of bars used to calculate pivot points (default: 50).
Bullish Color: Color for upward-sloping (bullish) main trend lines (default: green).
Bearish Color: Color for downward-sloping (bearish) main trend lines (default: red).
Style: Line style options include solid, dashed, or dotted (default: solid).
Mid Trend Line Settings
Length: Number of bars for mid-term pivot points (default: 21).
Show/Hide: Toggle visibility of mid trend lines (default: enabled).
Bullish Color: Color for bullish mid trend lines (default: lime).
Bearish Color: Color for bearish mid trend lines (default: maroon).
Style: Line style (default: dashed).
Short Trend Line Settings
Length: Number of bars for short-term pivot points (default: 9).
Show/Hide: Toggle visibility of short trend lines (default: enabled).
Bullish Color: Color for bullish short trend lines (default: teal).
Bearish Color: Color for bearish short trend lines (default: purple).
Style: Line style (default: dotted).
General Display Settings
Break Check Price: Selects the price type for breakout detection (Close, High, or Low; default: Close).
Show Previous Trendlines: Option to display historical main trend lines (default: disabled).
Label Size: Size of breakout labels (Tiny, Small, Normal, Large, Huge; default: Small).
Filter Settings
ADX Threshold: Minimum ADX value for trend strength confirmation (default: 25).
Volume MA Period: Period for the volume moving average (default: 20).
RSI Filter: Enable/disable RSI filtering (default: enabled).
RSI Upper Threshold: Upper RSI limit for overbought conditions (default: 65).
RSI Lower Threshold: Lower RSI limit for oversold conditions (default: 35).
4. Technical Calculations
The indicator relies on several technical calculations to ensure accuracy:
Pivot Points: Pivot highs and lows are detected using the ta.pivothigh and ta.pivotlow functions, with separate lengths for Main, Mid, and Short trend lines.
Slope Calculation: The slope of each trend line is calculated as the change in price divided by the change in bar index between two pivot points.
ADX Calculation: ADX is computed using a 14-period Directional Movement Index (DMI), with smoothing over 14 bars.
RSI Calculation: RSI is calculated over a 14-period lookback using the ta.rsi function.
Volume Moving Average: A simple moving average (SMA) of volume is used to determine if current volume exceeds the average.
5. Strict Mode Validation
To ensure the reliability of trend lines, the indicator employs a strict mode check:
For bearish trend lines, all prices between pivot points must remain below the projected trend line.
For bullish trend lines, all prices must remain above the projected trend line.
Post-pivot break checks ensure that no breakouts occur between pivot points, enhancing the validity of the trend line.
6. Trend Line Extension
Trend lines are dynamically extended forward until a breakout occurs. The extension logic:
Projects the trend line using the calculated slope.
Continuously validates the extension using strict mode checks.
Stops extension upon a breakout, fixing the trend line at the breakout point.
7. Alerts and Labels
Labels: Breakout labels are placed above (for bearish breakouts) or below (for bullish breakouts) the price bar. Labels include:
A prefix indicating the trend line type (B for Main, M for Mid, S for Short).
A suffix showing satisfied filters (e.g., [VAR] for Volume, ADX, and RSI).
Alerts: Each breakout triggers a one-time alert per bar close, with a descriptive message indicating the trend line type and filters met.
Detailed Code Breakdown
1. Initialization and Inputs
The script begins by defining the indicator with indicator('Smart Trend Lines [The_lurker]', overlay = true), ensuring it overlays on the price chart. Input settings are grouped into categories (Main, Mid, Short, General Display, Filters) for user convenience. Each input includes a tooltip in both English and Arabic, enhancing accessibility.
2. Technical Indicator Calculations
Volume MA: Calculated using ta.sma(volume, volPeriod) to compare current volume against the average.
ADX: Computed using custom dirmov and adx functions, which calculate the Directional Movement Index and smooth it over 14 periods.
RSI: Calculated with ta.rsi(close, rsiPeriod) over 14 periods.
Price Selection: The priceToCheck function selects the price type (Close, High, or Low) for breakout detection.
3. Pivot Detection
Pivot points are detected using ta.pivothigh and ta.pivotlow for each trend line type. The lookback period is set to the respective trend line length (e.g., 50 for Main, 21 for Mid, 9 for Short).
4. Trend Line Logic
For each trend line type (Main, Mid, Short):
Bearish Trend Lines: Identified when two consecutive pivot highs form a downward slope. The script validates the trend line using strict mode and post-pivot break checks.
Bullish Trend Lines: Identified when two consecutive pivot lows form an upward slope, with similar validation.
Trend lines are drawn using line.new, with separate lines for the initial segment (between pivots) and the extended segment (from the second pivot forward).
5. Breakout Detection and Labeling
Breakouts are detected when the selected price crosses the trend line level. The script checks:
Volume conditions (above average and aligned with candle direction).
ADX condition (above threshold).
RSI condition (within thresholds if enabled). Labels are created with label.new, and alerts are triggered with alert.
6. Trend Line Extension
The extendTrendline function dynamically updates the trend line’s endpoint unless a breakout occurs. It uses strict mode checks to ensure the trend line remains valid.
7. Previous Trend Lines
If enabled, previous main trend lines are stored in arrays (previousBearishStartLines, previousBullishTrendLines, etc.) and displayed on the chart, providing historical context.
Disclaimer:
The information and publications are not intended to be, nor do they constitute, financial, investment, trading, or other types of advice or recommendations provided or endorsed by TradingView.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Untuk akses pantas pada carta, tambah skrip ini kepada kegemaran anda — ketahui lebih lanjut di sini.
Penafian
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Untuk akses pantas pada carta, tambah skrip ini kepada kegemaran anda — ketahui lebih lanjut di sini.