SuperTrend Of MACD and AVG RSI For ThinkOrSwim

MichaelSF

New member
I was playing around with Mobius's original SuperTrend script recently -
https://usethinkscript.com/threads/supertrend-indicator-by-mobius-for-thinkorswim.7/

- and combining it with other popular indicators and found a couple of interesting results after a lot of tweaking and testing. Below is the first Indicator I tested this with; the MACD. It plots the MACD Diff Line (The Histogram Part) and uses that to calculate a SuperTrend "of" that line in order to smooth its movement. I'm sure that I'm not the first person to have done this, but I couldn't find a thread on here relating to it so here's my shot at it.

Code:
#SuperTrendOfMACD
#MichaelSF 2/2020
#Original SuperTrend Code By Mobius

declare lower;

## MACD ##
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);

plot MACDdiff = Value - Avg;
MACDdiff.SetPaintingStrategy(PaintingStrategy.LINE);

plot ZeroLine = 0;
ZeroLine.setDefaultColor(Color.dark_GRAY);

## SUPERTREND ##
input AtrMult = 1.0;
input nATR = 18;

def ATR = MovingAverage(AverageType.HULL, TrueRange(MACDdiff, MACDdiff, MACDdiff), nATR);
def UP = MACDdiff + (AtrMult * ATR);
def DN = MACDdiff + (-AtrMult * ATR);
def ST = if MACDdiff < ST[1] then UP else DN;

plot SuperTrend = ST;
SuperTrend.setDefaultColor(Color.dark_GRAY);

## COLORS & PLOTS ##

MACDDiff.AssignValueColor(if ST < MACDdiff then color.green else if ST > MACDdiff then color.red else color.Gray);

plot UpSignal = if ST crosses below MACDdiff then MACDdiff else Double.NaN;
plot DownSignal = if ST crosses above MACDdiff then MACDdiff else Double.NaN;
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

bdgrYws.png


Above is an example of this indicator being used on SPY in combination with the normal RSI. I find it works best on extreme overbought/oversold conditions, which is why the RSI is a good pair with it.

Unsurprisingly, the next indicator I tried to convert to SuperTrend was the RSI. The thing with the RSI is that it tends to be very choppy, and SuperTrending it would just be a zig-zag mess of nothingness, which is pictured below. Still could be useful in some cases maybe, but too choppy to be reliable in my opinion.

p9ceE9G.png


The solution to this was creating a MovingAverage of the RSI, and then adding a SuperTrend calculated off of that line instead. Below is the result of this and the code for the script.

SBVNaZe.png


In the picture above its crosses were almost identical to the Original SuperTrend crosses, so I added another picture from a different day that shows it better below.
NOTE: I've found that when the Classic SuperTrend and AvgRSISuperTrend do line up it's actually a pretty good way of confirming a strong move, so if you choose to use this remember to look out for that.

WRqTVdv.png


Code:
#SuperTrendOfAvgRSI
#MichaelSF 2/2020
#Original SuperTrend Code By Mobius

declare lower;

## RSI ##
input AvgLength = 10;
input AvgType = AverageType.SIMPLE;
input price = close;

def NetChgAvg = MovingAverage(AverageType.WILDERS, price - price[1], 14);
def TotChgAvg = MovingAverage(AverageType.WILDERS, AbsValue(price - price[1]), 14);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = 50 * (ChgRatio + 1);

plot RSIAvg = MovingAverage(AvgType, RSI, Avglength);
RSIAvg.SetPaintingStrategy(PaintingStrategy.Line);

## SUPERTREND ##
input AtrMult = 1.0;
input nATR = 20;

def ATR = MovingAverage(AverageType.HULL, TrueRange(RSIAvg, RSIAvg, RSIAvg), nATR);
def UP = RSIAvg + (AtrMult * ATR);
def DN = RSIAvg + (-AtrMult * ATR);
def ST = if RSIAvg < ST[1] then UP else DN;

plot SuperTrend = ST;
SuperTrend.setDefaultColor(Color.dark_GRAY);

## COLORS ##
RSIAvg.AssignValueColor(if SuperTrend < RSIAvg then color.green else if SuperTrend > RSIAvg then color.red else color.Gray);

plot OverSold = 30;
OverSold.setDefaultColor(Color.dark_GRAY);
plot OverBought = 70;
OverBought.setDefaultColor(Color.dark_GRAY);

plot UpSignal = if ST crosses below RSIAvg then RSIAvg else Double.NaN;
plot DownSignal = if ST crosses above  RSIAvg then RSIAvg else Double.NaN;
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

A few other things:
  • The colors on the chart are from the Mobius SuperTrend, not the indicators
  • The Settings on these indicators are what I found works best, but play around with them yourself to see if something works better for you.

Hopefully someone can find some use out of these. I've created a few strategies based on them and they ended up working pretty well.

Thank you to everyone that's apart of this community! Learned more than I could've imagined since finding this forum.
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

How is this an ATR ?
def ATR = MovingAverage(AverageType.HULL, TrueRange(RSIAvg, RSIAvg, RSIAvg), nATR);
Yeah I'm sure it's not. I literally just spliced the MACD and RSI together with Mobius's code and got these indicators as the result, so it might be closer to a Moving Average than an ATR. It still ends up having unique smoothing similar to the Classic SuperTrend however, which is what I was going for.
 
Last edited:
@MichaelSF Which Mobius SuperTrend did you compare this to? The one below? The AVG RSI SuperTrend looks nice...I will have to test it out and see how it compares to standard Heikin Ashi.

Code:
# SuperTrend
# Mobius
# Chat Room Request
# 11.20.2019 tomsk Enhanced and adjusted bubbles with coloring and description tag

input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
input n = 0;
def n1  = n + 1;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST
                 then Color.RED
                 else if PaintBars and close > ST
                      then Color.GREEN
                      else Color.CURRENT);

#AddChartBubble(close[n] crosses below ST[n], low[n+1] + TickSize() * n, "Sell @ " + low[n1], color.Cyan, yes);
#AddChartBubble(close[n] crosses above ST[n], high[n+1] - TickSize() * n, "Buy @ " + high[n1], color.Yellow, no);

# End Code SuperTrend
 
Yeah that particular SuperTrend...IF you change the ATRMULT to .7 even .5 from 1.0 will hit faster for more precise results...Have you tested your RSIavg SuperTrend with such settings changed to see how it stacks up or what it can be improved...
Yes, I did a lot of adjusting with different combinations of AtrMult and NAtr settings to see how it affected the quickness and effectiveness, but as I remember there wasn't anything that really stood out as a perfect setting for it. Keeping the AtrMult at 1.0 seems to be a good neutral level, but different settings work well for different tickers and timeframes.
 
Last edited:
I like this but also like the normal MACD, so I made the crossover arrows an upper study and it gives good signals. Here you go...

Code:
#SuperTrendOfMACD
#MichaelSF 2/2020
#Original SuperTrend Code By Mobius



## MACD ##
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);

plot MACDdiff = Value - Avg;
MACDdiff.SetPaintingStrategy(PaintingStrategy.LINE);
MACDdiff.Hide();

plot ZeroLine = 0;
ZeroLine.setDefaultColor(Color.dark_GRAY);
Zeroline.Hide();

## SUPERTREND ##
input AtrMult = 1.0;
input nATR = 18;

def ATR = MovingAverage(AverageType.HULL, TrueRange(MACDdiff, MACDdiff, MACDdiff), nATR);
def UP = MACDdiff + (AtrMult * ATR);
def DN = MACDdiff + (-AtrMult * ATR);
def ST = if MACDdiff < ST[1] then UP else DN;

plot SuperTrend = ST;
SuperTrend.setDefaultColor(Color.dark_GRAY);
SuperTrend.Hide();

## COLORS & PLOTS ##

MACDDiff.AssignValueColor(if ST < MACDdiff then color.green else if ST > MACDdiff then color.red else color.Gray);

plot UpSignal = if ST crosses below MACDdiff then MACDdiff else Double.NaN;
plot DownSignal = if ST crosses above MACDdiff then MACDdiff else Double.NaN;
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 
This whole study is flawed. Essentially what it does is multiply the MACDDIFF by 1. A lot of calculation for nothing. If you want the supertrend and MACDIFF crossing just add supertrend to standard MACD study.

Same with RSI study. Only thing making the RSI smoother is applying the simple moving average to the RSI plot. So just do the RSI average and supertrend crosses if that is what you want.

These both do not make a supertrend of the underlying indicators. So if anyone thought it was a study doing that it is not. Make sure you understand the studies you are using. Maybe it should be renamed MACDDIFF and Supertrend cross and RSI average and Supertrend cross.
 
This whole study is flawed. Essentially what it does is multiply the MACDDIFF by 1. A lot of calculation for nothing. If you want the supertrend and MACDIFF crossing just add supertrend to standard MACD study.

Same with RSI study. Only thing making the RSI smoother is applying the simple moving average to the RSI plot. So just do the RSI average and supertrend crosses if that is what you want.

These both do not make a supertrend of the underlying indicators. So if anyone thought it was a study doing that it is not. Make sure you understand the studies you are using. Maybe it should be renamed MACDDIFF and Supertrend cross and RSI average and Supertrend cross.
I definitely understand what you're saying, but I would argue that it does more than "just multiply the MACDDIFF by 1".

In the main part of the code which is in question...

def ATR = MovingAverage(AverageType.HULL, TrueRange(MACDdiff, MACDdiff, MACDdiff), nATR);
def UP = MACDdiff + (AtrMult * ATR);
def DN = MACDdiff + (-AtrMult * ATR);
def ST = if MACDdiff < ST[1] then UP else DN;

Its actually adding to the MACD diff with a Hull moving average of the MACD Diff with a length of 4, and then makes it either positive or negative based on the candle before it. This is not the same as multiplying it by 1, and gives if an effect that I found was similar enough to a the original SuperTrend to call it by that name. Again, i was just splicing the two codes for fun and getting this as a result, if someone can make it better then I 100% encourage it.
 
Last edited:
Ok I admit my math may be off, not my best subject. But still not calculating an ATR. There is no high or low so only one value is being used and somehow giving you a change due to the period of days of the average. No idea what kind of number that is coming up with.

Seems would be closer to a true range if used TrueRange(MACDdiffhigh, close, MACDdifflow), nATR); but still off. Would need to find the high and low of the macddiff over the length used.

Best might be to just do this. Uses correct calculations and might work better.
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

input averagetype2 = averageType.HULL;
input lengthh = 4;
plot h = Movingaverage(averagetype2,diff,lengthh);
 
@Csharp The scan for the up arrow syntax-wise runs successfully but I have never seen it has populated with any results.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
510 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top