OPEN-SOURCE SCRIPT
Updated RSI Divergence Strategy - AliferCrypto

Strategy Overview
The RSI Divergence Strategy is designed to identify potential reversals by detecting regular bullish and bearish divergences between price action and the Relative Strength Index (RSI). It automatically enters positions when a divergence is confirmed and manages risk with configurable stop-loss and take-profit levels.
Key Features
- Swing-based: Stops placed a configurable percentage beyond the most recent swing high/low.
- ATR-based: Stops placed at a multiple of Average True Range, with a separate risk/reward multiplier.
Detailed Logic
- Identifies RSI pivot lows/highs by scanning a lookback window to the left and right.
- Uses ta.barssince to ensure pivots are separated by a minimum/maximum number of bars.
- Bullish: Price makes a lower low while RSI makes a higher low.
- Bearish: Price makes a higher high while RSI makes a lower high.
- Opens a Long position when bullish divergence is true.
- Opens a Short position when bearish divergence is true.
- Swing Method: Computes the recent swing high/low then adjusts by a percentage margin.
- ATR Method: Uses the current ATR × multiplier applied to the entry price.
- Take-Profit: Calculated as entry price ± (risk × R/R ratio).
*Exit Orders: Uses strategy.exit to place bracket orders (stop + limit) for both long and short positions.
Inputs and Configuration
Usage Notes
Disclaimer
This script is for educational purposes only and does not constitute financial advice. Trading carries significant risk and you may lose more than your initial investment. Always conduct your own research and consider consulting a professional before making any trading decisions.
The RSI Divergence Strategy is designed to identify potential reversals by detecting regular bullish and bearish divergences between price action and the Relative Strength Index (RSI). It automatically enters positions when a divergence is confirmed and manages risk with configurable stop-loss and take-profit levels.
Key Features
- Automatic Divergence Detection: Scans for RSI pivot lows/highs vs. price pivots using user-defined lookback windows and bar ranges.
- Dual SL/TP Methods:
- Swing-based: Stops placed a configurable percentage beyond the most recent swing high/low.
- ATR-based: Stops placed at a multiple of Average True Range, with a separate risk/reward multiplier.
- Long and Short Entries: Buys on bullish divergences; sells short on bearish divergences.
- Fully Customizable: Input groups for RSI, divergence, swing, ATR, and general SL/TP settings.
- Visual Plotting: Marks divergences on chart and plots stop-loss (red) and take-profit (green) lines for active trades.
- Alerts: Built-in alert conditions for both bullish and bearish RSI divergences.
Detailed Logic
- RSI Calculation: Computes RSI of chosen source over a specified period.
- Pivot Detection:
- Identifies RSI pivot lows/highs by scanning a lookback window to the left and right.
- Uses ta.barssince to ensure pivots are separated by a minimum/maximum number of bars.
- Divergence Confirmation:
- Bullish: Price makes a lower low while RSI makes a higher low.
- Bearish: Price makes a higher high while RSI makes a lower high.
- Entry:
- Opens a Long position when bullish divergence is true.
- Opens a Short position when bearish divergence is true.
- Stop-Loss & Take-Profit:
- Swing Method: Computes the recent swing high/low then adjusts by a percentage margin.
- ATR Method: Uses the current ATR × multiplier applied to the entry price.
- Take-Profit: Calculated as entry price ± (risk × R/R ratio).
*Exit Orders: Uses strategy.exit to place bracket orders (stop + limit) for both long and short positions.
Inputs and Configuration
- RSI Settings: Length & price source for the RSI.
- Divergence Settings: Pivot lookback parameters and valid bar ranges.
- SL/TP Settings: Choice between Swing or ATR method.
- Swing Settings: Swing lookback length, margin (%), and risk/reward ratio.
- ATR Settings: ATR length, stop multiplier, and risk/reward ratio.
Usage Notes
- Adjust the Pivot Lookback and Range values to suit the volatility and timeframe of your market.
- Use higher ATR multipliers for wider stops in choppy conditions, or tighten swing margins in trending markets.
- Backtest different R/R ratios to find the balance between win rate and reward.
Disclaimer
This script is for educational purposes only and does not constitute financial advice. Trading carries significant risk and you may lose more than your initial investment. Always conduct your own research and consider consulting a professional before making any trading decisions.
Release Notes
Update - Change LogRSI-Zone Filter
- Added Enable RSI Zone Filter toggle
- New inputs: RSI Oversold Level (arms bullish only after RSI < level), RSI Overbought Level (arms bearish only after RSI > level)
- Internal flags (rsiOversoldFlag / rsiOverboughtFlag) track first breach and reset on entry
Trend Filter
- Added Enable Trend Filter toggle
- Full MA configuration under “Filter Settings”:
- MA Timeframe (blank = chart)
- MA Type (SMA, EMA, SMMA, WMA, VWMA)
- MA Length, MA Source, MA Color
- Only allows longs when price > MA, shorts when price < MA
MA Plotting
- Plots the configured MA line when the trend filter is enabled
Entry Logic Updates
- Incorporated both RSI-zone and trend checks into the final bullCond / bearCond conditions
- Reset of oversold/overbought flags immediately upon taking a trade
No other parameters were changed—everything else (divergence detection, SL/TP methods) remains as originally implemented.
Release Notes
Update – Change LogExit Levels Mode
- Added Exit Levels Mode toggle under SL/TP Settings with two options:
- - Dynamic: Recalculates stop-loss and take-profit each bar based on current swing/ATR levels
- - Static: Locks SL/TP once at the entry bar and holds those fixed levels for the life of the trade
- Introduced entrySL and entryTP variables to capture and store the static exit prices at the moment of entry
- Exit logic now chooses between live (slCalc/tpCalc) or locked (entrySL/entryTP) values based on the selected mode
- Static storage is automatically reset when the position closes
- No other behaviors (divergence detection, filters, SL/TP calculations) have been changed.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
For quick access on a chart, add this script to your favorites — learn more here.
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.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
For quick access on a chart, add this script to your favorites — learn more here.
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.