Derivative RSI Oscillator for ThinkorSwim

horserider

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:
markos

markos

Well-known member
VIP
Very interesting @horserider thanks for that.
 
H

HighBredCloud

Well-known member
VIP
@horserider is there a BULL/BEAR scanner for this?
 
S

SparkyFlary

Member
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

horserider

Well-known member
VIP
@SparkyFlary The idea was a smoother RSI. Using exponential will introduce more choppiness.
 
wtf_dude

wtf_dude

Active member
why the 5,3,9 lengths for smoothing? Just curious. Guessing it's not arbitrary haha
 
horserider

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.
 
S

SparkyFlary

Member
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

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

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

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

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

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
 

Similar threads

Top