Derivative RSI Oscillator for ThinkorSwim

horserider

Well-known member
VIP
This is a triple smoothed RSI designed to show smoother and clearer oscillator swings compared to other RSI or momentum studies.

Code:
#  Derivative RSI Oscillator
#  Can be used alone as a smoother RSI or as part of the Charting Wealth strategy when combined    with the MACD and MAs Charting Wealth study.
#  By Horserider 12/25/2019
#

declare lower;

input length = 14;
input length1 = 5;
input length2 = 3;
input length3 = 9;
input price = close;


def NetChgAvg = WildersAverage(price - price[1], length);
def TotChgAvg = WildersAverage(AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = (50 * (ChgRatio + 1) - 50);

#################################
plot  ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.YELLOW);
ZeroLine.SetLineWeight(1);

input averageType = AverageType.EXPONENTIAL;
input averageType2 = averageType.SIMPLE;
def x = MovingAverage(averageType,RSI, length1);
def x2 = MovingAverage(averageType, x, length2);
def xs = MovingAverage(averageType2, x2 , length3);
def rsi__linediff = x2 - xs;

plot RSI_Line = rsi__linediff;
RSI_Line.SetDefaultColor(GetColor(1));

RSI_Line.SetDefaultColor(GetColor(5));
RSI_Line.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
RSI_Line.SetLineWeight(3);
RSI_Line.DefineColor("Positive and Up", Color.GREEN);
RSI_Line.DefineColor("Positive and Down", Color.DARK_GREEN);
RSI_Line.DefineColor("Negative and Down", Color.RED);
RSI_Line.DefineColor("Negative and Up", Color.DARK_RED);
RSI_Line.AssignValueColor(if RSI_Line >= 0 then if RSI_Line > RSI_Line[1] then RSI_Line.color("Positive and Up") else RSI_Line.color("Positive and Down") else if RSI_Line < RSI_Line[1] then RSI_Line.color("Negative and Down") else RSI_Line.color("Negative and Up"));

 
Last edited:
I've played around with it by forcing in code to see if the concept of divergence signals can work like RSI does. I think it works well, I like it. I don't understand why use simple and wilders moving averages though instead of exponential but those seem to actually work better for the purpose of finding divergence, atleast when I was backtesting.
 

horserider

Well-known member
VIP
@wtf_dude If I remember those may have been from the Charting Wealth strategy I was trying to mirror. I put this up as a separate study also for anyone wanting a smoother RSI. Play with lengths all you want and report back if you find a sweet spot.
 
Somehow the smoothing from simple and wilders from the code are pretty good. I think they're actually needed in order to test for divergence properly but not sure. Are those numbers and moving average types just sweet spots that were tested or did they come recommended like that by the creator or some professional or something? I haven't tried playing around with the numbers and ma's yet but they don't seem bad the way they are, just wanted to know.
 

zeek

Active member
2019 Donor
Wondering if it`s possible to create an alert when the RSI line is close to cross the zero line? Would like the alert to trigger when line crosses 0.50 value from the upside and -0.50 from the downside. Can this be done? @horserider
 

horserider

Well-known member
VIP
@zeek See if this works.

Alert(RSI_Line crosses below .5, "", Alert.BAR, Sound.Bell);
Alert(RSI_Line crosses above -.5, "", Alert.BAR, Sound.Bell);
 

zeek

Active member
2019 Donor
Works great, thanks (y) @horserider

I have another request for this oscillator and was wondering if it would be possible to make it work with multiple timeframes? Basically, i would like to see the RSI swings from two different timeframes on one chart/timeframe. Is there any way to add a timeframe input selector to the settings which would open the possibility to have two different oscillators on the same chart showing the swings from both a 1min and 3min as an example?
 

horserider

Well-known member
VIP
@zeek Have fun.

Code:
#  Derivative RSI Oscillator
#  Can be used alone as a smoother RSI or as part of the Carting Wealth strategy when combined    with the MACD and MAs Charting Wealth study.
#  By Horserider 12/25/2019
#  Added One higher time frame option per Zeek request 7/3/2020

declare lower;
input n = 10 ;
input length = 14;
input length1 = 3;
input length2 = 5;
input length3 = 9;
input price = close;
input overbought = 4;
input oversold = -4;

def NetChgAvg = WildersAverage(price - price[1], length);
def TotChgAvg = WildersAverage(AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = (50 * (ChgRatio + 1) - 50);

#################################
plot  ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.YELLOW);
ZeroLine.SetLineWeight(1);

plot ob = overbought;
ob.SetDefaultColor(Color.GRAY);
ob.SetLineWeight(1);

plot os = oversold;
os.SetDefaultColor(Color.GRAY);
os.SetLineWeight(1);

input averageType = AverageType.EXPONENTIAL;
input averageType2 = AverageType.SIMPLE;
def x = MovingAverage(averageType, RSI, length1);
def x2 = MovingAverage(averageType, x, length2);
def xs = MovingAverage(averageType2, x2 , length3);
def rsi__linediff = x2 - xs;

plot RSI_Line = rsi__linediff;
RSI_Line.SetDefaultColor(GetColor(1));

#RSI_Line.SetDefaultColor(GetColor(5));
RSI_Line.SetPaintingStrategy(PaintingStrategy.LINE);
RSI_Line.SetLineWeight(3);
RSI_Line.DefineColor("Positive and Up", Color.GREEN);
RSI_Line.DefineColor("Positive and Down", Color.DARK_GREEN);
RSI_Line.DefineColor("Negative and Down", Color.RED);
RSI_Line.DefineColor("Negative and Up", Color.DARK_RED);
RSI_Line.AssignValueColor(if RSI_Line >= 0 then if RSI_Line > RSI_Line[1] then RSI_Line.Color("Positive and Up") else RSI_Line.Color("Positive and Down") else if RSI_Line < RSI_Line[1] then RSI_Line.Color("Negative and Down") else RSI_Line.Color("Negative and Up"));

#########################################
#AssignPriceColor(if  RSI_Line >= 0 then if RSI_Line > RSI_Line[1] then RSI_Line.color("Positive and Up") else RSI_Line.color("Positive and Down") else if RSI_Line < RSI_Line[1] then RSI_Line.color("Negative and Down") else RSI_Line.color("Negative and Up"));

#########################################
input agg1 = AggregationPeriod.DAY;
def priceagg = close(period = agg1);
def NetChgAvgagg = WildersAverage(priceagg - priceagg[1], length);
def TotChgAvgagg = WildersAverage(AbsValue(priceagg - priceagg[1]), length);
def ChgRatioagg = if TotChgAvgagg != 0 then NetChgAvgagg / TotChgAvgagg else 0;

def RSIagg = (50 * (ChgRatioagg + 1) - 50);

#################################

def xagg = MovingAverage(averageType, RSIagg, length1);
def x2agg = MovingAverage(averageType, xagg, length2);
def xsagg = MovingAverage(averageType2, x2agg , length3);
def rsi__linediffagg = x2agg - xsagg;

plot RSI_Lineagg = rsi__linediffagg;
RSI_Lineagg.SetDefaultColor(GetColor(1));

#RSI_Line.SetDefaultColor(GetColor(5));
RSI_Lineagg.SetPaintingStrategy(PaintingStrategy.LINE);
RSI_Lineagg.SetLineWeight(3);
RSI_Lineagg.DefineColor("Positive and Up", Color.GREEN);
RSI_Lineagg.DefineColor("Positive and Down", Color.DARK_GREEN);
RSI_Lineagg.DefineColor("Negative and Down", Color.RED);
RSI_Lineagg.DefineColor("Negative and Up", Color.DARK_RED);
RSI_Lineagg.AssignValueColor(if RSI_Lineagg >= 0 then if RSI_Lineagg > RSI_Lineagg[1] then RSI_Lineagg.Color("Positive and Up") else RSI_Lineagg.Color("Positive and Down") else if RSI_Lineagg < RSI_Lineagg[1] then RSI_Lineagg.Color("Negative and Down") else RSI_Lineagg.Color("Negative and Up"));
 

zeek

Active member
2019 Donor
Have another request for this great indicator and was wondering if there is any way to make it look for divergence between price and the swings? Preferably, i would like it to look for a divergence of at least 3 bars and when this condition is met, would like to have a viusal & audio alert.

Any possibility this can be added? @horserider
 

gstreff

New member
Hi @horserider ..... is there a way to freeze the zero line for this indicator. At the moment it floats around based on the value of positive or negative values across the screen. I am overlaying it with a momentum oscillator that is somewhat different values than this RSI indicator so the right axis comes up as a percentage scale ( which is fine) But even on its own the RSI zero line floats rather than fixed at the center (Price axis center) of the window it is in. Thank you

BTW I would not be concerned if the RSI bars in the histogram would overrun,,,,,, go beyond the border of the window..... with this type mod to freeze the zero line.. Thanks
 
Last edited by a moderator:

tome10

New member
I would like to add arrows on the change from positive to negative, and negative to positive, which would be the top and bottom of the oscillator. Also, maybe it would be prudent to also add the arrows to the price chart as well, or a toggle on off for the price chart. thanks
 

Similar threads

Top