PROTECTED SOURCE SCRIPT

TMA - RSI Divergence Indicator - upgraded to ver6 Pine Editor

Key Features
Divergence Detection:

The indicator detects four types of divergences:
Regular Bullish Divergence: RSI forms a higher low while price forms a lower low.
Hidden Bullish Divergence: RSI forms a lower low while price forms a higher low.
Regular Bearish Divergence: RSI forms a lower high while price forms a higher high.
Hidden Bearish Divergence: RSI forms a higher high while price forms a lower high.
Customizable Inputs:

RSI Parameters:
len: RSI period.
src: RSI source (e.g., close price).
Pivot Lookback:
lbR and lbL: Lookback periods for identifying pivot points (used to determine divergences).
Lookback Range:
rangeUpper and rangeLower: Defines the maximum and minimum range for divergence detection.
Visualization:

Plots the RSI as a yellow line.
Optionally overlays a Smoothed Moving Average (SMMA) on the RSI for trend confirmation.
Marks divergence points with labels (Bull, H Bull, Bear, H Bear) and plots corresponding lines at detected pivots.
Alerts:

Alerts are generated for each type of divergence when they are detected.
Analysis of the Core Logic
1. RSI Calculation
RSI is computed using ta.rsi(src, len), where the source is user-defined (default: close price).
2. SMMA (Smoothed Moving Average)
A custom implementation of SMMA (exponential smoothing) is included for trend confirmation:
Properly initialized using ta.sma().
Subsequent values calculated iteratively.
3. Divergence Detection
Pivot Detection:

ta.pivotlow() and ta.pivothigh() identify pivot lows and highs in the RSI based on lbL (left lookback) and lbR (right lookback).
Conditions for Divergence:

The script checks whether RSI and price meet divergence criteria:
Regular Bullish: RSI higher low (oscHL), price lower low (priceLL).
Hidden Bullish: RSI lower low (oscLL), price higher low (priceHL).
Regular Bearish: RSI lower high (oscLH), price higher high (priceHH).
Hidden Bearish: RSI higher high (oscHH), price lower high (priceLH).
Lookback Range Enforcement:

_inRange() ensures divergence pivots are within the specified lookback range (rangeLower to rangeUpper).
Strengths
Comprehensive Divergence Detection:
Detects both regular and hidden divergences for bullish and bearish scenarios.
Customization:
Allows users to configure RSI parameters, lookback ranges, and divergence types to suit their trading strategies.
Visualization:
Clear labels and lines highlight divergence points.
Optional SMMA adds an additional trend-following component.
Alerts:
Automated alerts enable traders to react quickly to divergence signals.
Proper Use of Pine Script Features:
Efficient pivot and divergence detection using built-in functions (ta.pivotlow, ta.pivothigh, ta.barssince, etc.).
Potential Improvements
Dynamic Line Colors for RSI:
Change the RSI line color dynamically based on whether it's above or below the SMMA (currently always yellow).
Advanced Alerts:
Include additional information in the alert (e.g., time, price level, and divergence type).
Range Visualization:
Plot shaded regions or vertical lines to indicate the lookback range (rangeLower to rangeUpper) for better visual context.
Price Lines:
Draw price-based divergence lines on the chart overlay for easier comparison between price and RSI.
Filter Weak Signals:
Introduce a strength filter (e.g., minimum divergence magnitude or minimum RSI change) to reduce false positives.
Performance Optimization:
Refactor code to optimize for large datasets, especially the SMMA initialization and pivot-based lookbacks.
Use Cases
Trend Reversals:
Identify potential reversals by spotting divergences before they occur.
Trend Continuation:
Use hidden divergences to confirm the continuation of the existing trend.
Combination with Other Indicators:
Use alongside moving averages, Bollinger Bands, or MACD for additional confirmation.
OscillatorsTrend AnalysisVolatility

نص برمجي محمي

تم نشر هذا النص البرمجي بمصدر غير مفتوح ويمكنك استخدامه بحرية. يمكنك جعله مفضلاً لاستخدامه على الرسم البياني. لا يمكنك مشاهدة أو تعديل كود المصدر الخاص به.

هل تريد استخدام هذا النص البرمجي على الرسم البياني؟

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