RSI DIVERGENCE – INDICATOR
Concept:
It checks for the lowest low while the RSI is oversold and stores it when the market leaves the oversold condition.If there’s a second low that is lower than the first low, with a higher RSI value, and within a maximum distance, it checks for a crossover of the highest price between the two lows and the EMA.If the conditions for bullish divergence are met, it sets the bullishDivergence flag to true and draws a line indicating the divergence.
Our Requirements as Images
What I Try to Create
// @version=5
indicator("Live RSI Divergence with SMA Condition", overlay=true)
// Get user input
int i_MaxDistance = input.int(title="Max Bar Distance", defval=100)
int i_RSI_Length = input.int(title="RSI Length", defval=13, minval=1)
float i_RSI_Overbought = input.float(title="RSI Overbought", defval=70.0)
float i_RSI_Oversold = input.float(title="RSI Oversold", defval=30.0)
int emaLength = input.int(title="EMA Length", defval=50, minval=1)
// Get RSI value
rsiValue = ta.rsi(close, i_RSI_Length)
emavalue = ta.ema(close, emaLength)
// --------- BULLISH DIVERGENCE --------- //
var float checkFirstLow = na
var float initialLowPrice = na
var float initialLowRSI = na
var int initialLowIndex = na
bool bullishDivergence = false
float swingLowRSI = ta.lowest(rsiValue, 5)
// Save lowest low while RSI is oversold, and store it when market leaves OS condition
if (rsiValue < i_RSI_Oversold or rsiValue[1] < i_RSI_Oversold)
if not na(initialLowPrice)
initialLowPrice := na
if na(checkFirstLow) or low < checkFirstLow
checkFirstLow := low
initialLowIndex := bar_index
initialLowRSI := swingLowRSI
else
// RSI is no longer OS, save lowest price during OS condition and reset temporary low
if not na(checkFirstLow)
initialLowPrice := checkFirstLow
checkFirstLow := na
// Check for a second low that is lower than the first, and with a higher RSI value, and within our max distance
if low < initialLowPrice and swingLowRSI > initialLowRSI and bar_index - initialLowIndex <= i_MaxDistance
float highestPriceBetween = ta.highest(high, bar_index - initialLowIndex)
if ta.crossover(highestPriceBetween,emavalue)
bullishDivergence := true
line.new(initialLowIndex, initialLowPrice, bar_index, low, color=color.yellow, width=2)
initialLowPrice := na
plot(emavalue,linewidth = 2)```
**Compiler Warning**
Warning at 41:34 The function 'ta.highest' should be called on each calculation for consistency. It is recommended to extract the call from this scope
Warning at 42:8 The function 'ta.crossover' should be called on each calculation for consistency. It is recommended to extract the call from this sco
pe.
**Result on Chart**
[Result](https://i.sstatic.net/ykzHJIJ0.png)
Candel High Crossover EMA Within a Range But it's Not Respect the condition & unexpected results.
God Mentor – Sk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.