Divergence Lines based on RSI?

venus360

New member
I am looking for code snippets that plot divergence lines between price and the RSI. I mean if the price has higher highs while RSI has a lower high (or vice versa). Instead of visually verifying the divergence, I want to be able to plot it.

Here is the code that I want to use to add these lines. @tomsk helped me and I made few tweaks.
Code:
# Divergence Updown Points
# tomsk  (modfied further by [USER=2184]@venus360[/USER])
# 1.17.2020

# Plots the ratio of Up/Down points relative to the prior candle

input period = 14;
declare lower;

def upPoints   = fold i = 0 to period
                 with p
                 do p + GetValue(if close > close[1] then close - close[1] else 0, i);
def downPoints = fold j = 0 to period
                 with r
                 do r + GetValue(if close < close[1] then close - close[1] else 0, j);

def au = upPoints / period;
def ad = AbsValue(downPoints) / period;

def ratio = au / ad;

def step0 = 1 + ratio;
def step1 = 100 / step0;
plot final = 100 - step1;

# End UpDown Points Divergence

0uufIZ1.png


I manually added the lines, I want to explore if the divergence lines can be plotted using code.
 
Last edited by a moderator:

venus360

New member
Unfortunately, I could not get any help on my request. I went ahead and purchased a script at funwiththinkscript that has a RSI divergence which seems to work very well. If there is any coder who can help with my above request, I would add sugar on top of @StockJockey's pretty please :)
 

venus360

New member
Regarding funwiththinkscript's RSI divergence - Hindsight 20/20. That indicator showed negative divergence for all the indexes and many of the Dow stocks. I bought it after that unfortunately. The script I have above seems pretty close in terms of RSI values even though it uses completely different logic and math. I am able to use this in day trading as well as with daily charts. For day trading, I use 1000 ticks or 1 hr charts to get the best results. @Playstation - I haven't looked at that script. I will over the weekend and try it out next week. Thank you.
 
Here is an upper divergence study that you might like. It will plot points on the bars or there is a paintbar option.
The first image shows how many signals will pop up will removal of futurebar filter.
The second image shows how divergence can work wonders sometimes and other times in a strong directional push it will throw false signals.
The third image just shows the points instead of paintbars.


Code:
#RSI_DIVERGENCE_POINTS
#DeusMecanicus

declare upper;
input Length = 14;
input Price = close;
input AverageType = AverageType.WILDERS;
input Lookback1 = 20;
input Lookback2 = 50;
input Lookback3 = 100;
input PaintBars = No;
input HidePoints = No;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

def futurebar = rsi > rsi[-1];
def futurebar1 = RSI < RSI[-1];
plot RSI_Div_Down1 = if rsi != Highest(rsi, Lookback1) and close == Highest(close, Lookback1) and futurebar then midbodyval() else Double.NaN;
RSI_Div_Down1.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Down1.AssignValueColor(Color.PINK);
RSI_Div_Down1.SetLineWeight(3);
plot RSI_Div_Down2 = if rsi != Highest(rsi, Lookback2) and close == Highest(close, Lookback2) and futurebar then midbodyval() else Double.NaN;
RSI_Div_Down2.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Down2.AssignValueColor(Color.RED);
RSI_Div_Down2.SetLineWeight(3);
plot RSI_Div_Down3 = if rsi != Highest(rsi, Lookback3) and close == Highest(close, Lookback3) and futurebar then midbodyval() else Double.NaN;
RSI_Div_Down3.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Down3.AssignValueColor(Color.DARK_RED);
RSI_Div_Down3.SetLineWeight(3);
plot RSI_Div_Up1 = if rsi != Lowest(rsi, Lookback1) and close == Lowest(close, Lookback1) and futurebar1 then midbodyval() else Double.NaN;
RSI_Div_Up1.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Up1.AssignValueColor(Color.GREEN);
RSI_Div_Up1.SetLineWeight(3);
plot RSI_Div_Up2 = if rsi != Lowest(rsi, Lookback2) and close == Lowest(close, Lookback2) and futurebar1 then midbodyval() else Double.NaN;
RSI_Div_Up2.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Up2.AssignValueColor(Color.DARK_GREEN);
RSI_Div_Up2.SetLineWeight(3);
plot RSI_Div_Up3 = if rsi != Lowest(rsi, Lookback3) and close == Lowest(close, Lookback3) and futurebar1 then midbodyval() else Double.NaN;
RSI_Div_Up3.SetPaintingStrategy(PaintingStrategy.POINTS);
RSI_Div_Up3.AssignValueColor(Color.CYAN);
RSI_Div_Up3.SetLineWeight(3);

RSI_DIV_Up1.sethiding(HidePoints);
RSI_DIV_Up2.sethiding(HidePoints);
RSI_DIV_Up3.sethiding(HidePoints);
RSI_DIV_Down1.sethiding(HidePoints);
RSI_DIV_Down2.sethiding(HidePoints);
RSI_DIV_Down3.sethiding(HidePoints);

AssignPriceColor(if !paintBars then Color.CURRENT else
if rsi != Highest(rsi, Lookback1) and close == Highest(close, Lookback1) and futurebar then Color.PINK else
if rsi != Highest(rsi, Lookback2) and close == Highest(close, Lookback2) and futurebar then Color.RED else
if rsi != Highest(rsi, Lookback3) and close == Highest(close, Lookback3) and futurebar then Color.DARK_RED else
if rsi != Lowest(rsi, Lookback1) and close == Lowest(close, Lookback1) and futurebar1 then Color.GREEN else
if rsi != Lowest(rsi, Lookback2) and close == Lowest(close, Lookback2) and futurebar1 then Color.DARK_GREEN else
if rsi != Lowest(rsi, Lookback3) and close == Lowest(close, Lookback3) and futurebar1 then Color.CYAN else Color.BLACK );
 
Last edited:

Similar threads

Top