SuperTrend with Reversal For ThinkOrSwim

RDJ

New member
VIP
I would like thank all of you for all the work involved in creating and sharing these indicators and strategies. I am not a programmer but I enjoy trying to learn and this has been so helpful.
I was watching a very popular post in regards to the Reversal Indicator. I know this indicator repaints, but I noticed someone was review the signals from the Super Trend indicator along with the signals from the Reversal indicator. I’ve created a Strategy, with lots of help, that will trigger when both of these conditions meet. I’ve looked at a few charts and as long as you take the strategy signal I do not think this SuperTrend/Reversal strategy repaints but please correct this post if I am wrong. I’m posting this to see if anyone would like to use or improve it. I tried to create a Watch list but did not have any luck.
• The Super Tend targets can be turn on and off.
• The Reversal Indicator can also be turn on and off. I normally keep this on so it will give me an advanced notice.
• You can change the look back or forward to when both the Super Trend and Reversal Indicators trigger a buy or sell.
• I have created a changeable moving average indicator that will tell you the long term and short term direction.
• The mADX indicator will show you a possible direction change, this can also be turn on and off.
I do not use one indicator.
I will normally use the RSI Laguerre to give me advanced direction notice along with the B4 indicator. If the B4 indicator is tending down and red, I will not take the trade because it will normally just move sideways.
Before trading, I will back test and review the stock along with the FloatingPL indicator (Floating Profit/Loss histogram) for best results.
I normally use this on a day chart but the results look very good on a 4-hour chart.
I would suggest to set a stop loss aa originally noted with the Trend Reversal Indicator below.
Trend Reversal Indicator provides signals for potential reversals
This indicator has the option to paint candles:
  • Green = Bullish
  • Purple = Neutral
  • Red = Bearish
You can use that as a trigger point to cut losses early if that happens.
#MUST LOAD AS A STRATEGIES
Ruby:
#Name: SUPERTREND_WITH_TREND_REVERSAL

#Inputs
#Look back and forward at each bar to see when both indicators trigger together
input quantity = 100;
#New code
input lookback = 8;
input lookforward = 8;
input AtrMult = 1.00;#hint AtrMult: Modify to FloatingPL PLot Indicator fine tune 0.60 +/-
input nATR = 6;
input AvgType = AverageType.HULL;
input ShowRBubbles = YES;#hint ShowRBubbles: Shows Reversal Bubble on Chart
input ShowSuperTBubbles = NO;#hint ShowSuperTBubbles: Turn off SuperTrend dots from "Plots"
#input PaintBars = no;
#input ShowBubbles = YES; # SUPERTREND BUBBLES
#input AlertDisplace = 0;

#__________________________________________________________________________________________
#Short Term and Long Term Bearish or Bull trend Direction

input M_AVERAGE1_len = 5;#hint m_average1_len: 8 for Day Trade and 5 for Intraday suggested
input M_AVERAGE2_len = 8;#hint m_average2_len: 13 for Day Trade and 8 for Intraday suggested
input M_AVERAGE3_len = 13;#hint m_average3_len: 21 for Day Trade and 13 for Intraday suggested
input M_AVERAGE4_len = 50;
input M_AVERAGE5_len = 200;
input AverageType= AverageType.EXPONENTIAL;#hint AverageType: This is for Short and Long Term Direction Setting Above

def M_AVERAGE1 = MovAvgExponential(length = M_AVERAGE1_len);
def M_AVERAGE2 = MovAvgExponential(length = M_AVERAGE2_len);
def M_AVERAGE3 = MovAvgExponential(length = M_AVERAGE3_len);
def M_AVERAGE4 = MovAvgExponential(length = M_AVERAGE4_len);
def M_AVERAGE5 = MovAvgExponential(length = M_AVERAGE5_len);

def bullishStacked = M_AVERAGE1 > M_AVERAGE2 and M_AVERAGE2 > M_AVERAGE3 and M_AVERAGE3 > M_AVERAGE4;
def bearishStacked = M_AVERAGE1 < M_AVERAGE2 and M_AVERAGE2 < M_AVERAGE3 and M_AVERAGE3 < M_AVERAGE4;

AddLabel(yes, if bullishStacked then "ST_Bullish" else if bearishStacked then "ST_Bearish" else "N/A", if bullishStacked then Color.GREEN else if bearishStacked then Color.RED else Color.YELLOW);

#Long Term Bearish or Bull trend Direction

def LT_bullishStacked = M_AVERAGE4 > M_AVERAGE5;
def LT_bearishStacked = M_AVERAGE4 < M_AVERAGE5;

AddLabel(yes, if LT_bullishStacked then "LT_Bullish" else if LT_bearishStacked then "LT_Bearish" else "N/A", if LT_bullishStacked then Color.GREEN else if LT_bearishStacked then Color.RED else Color.YELLOW);

#__________________________________________________________________________________________

# SuperTrend Yahoo Finance Replica - Modified from Modius SuperTrend
# Modified Modius ver. by RConner7
# Modified by Barbaros to replicate look from TradingView version
# v3.1

def ATR = ATR("length" = nATR, "average type" = AvgType);

def UP_Band_Basic = HL2 + (AtrMult * ATR);

def LW_Band_Basic = HL2 + (-AtrMult * ATR);

def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];

def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];

def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band

else if ((ST[1] == UP_Band[1]) and (close > UP_Band)) then LW_Band

else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band

else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band

else LW_Band;

def Long = if close > ST then ST else Double.NaN;

#Long.AssignValueColor(Color.GREEN);

#Long.SetLineWeight(2);

def Short = if close < ST then ST else Double.NaN;

#Short.AssignValueColor(Color.RED);

#Short.SetLineWeight(3);

def LongTrigger = IsNaN(Long[1]) and !IsNaN(Long);

def ShortTrigger = IsNaN(Short[1]) and !IsNaN(Short);

plot LongDot = if LongTrigger then ST else Double.NaN;

LongDot.SetPaintingStrategy(PaintingStrategy.POINTS);

LongDot.AssignValueColor(Color.GREEN);

LongDot.SetLineWeight(4);



plot ShortDot = if ShortTrigger then ST else Double.NaN;

ShortDot.SetPaintingStrategy(PaintingStrategy.POINTS);

ShortDot.AssignValueColor(Color.RED);

ShortDot.SetLineWeight(4);


#Bubbles FOR_SUPERTEND_INDICATOR_____

AddChartBubble(ShowSuperTBubbles and LongTrigger, ST, "B", Color.LIGHT_GREEN, no);

AddChartBubble(ShowSuperTBubbles and ShortTrigger, ST, "S", Color.pink, yes);

######


#AssignPriceColor(if PaintBars and close < ST

#then Color.RED

#else if PaintBars and close > ST

#then Color.GREEN

#else Color.CURRENT);



#Alert(LongTrigger[AlertDisplace], "Long", Alert.BAR, Sound.Ding);

#Alert(ShortTrigger[AlertDisplace], "Short", Alert.BAR, Sound.Ding);

#new Code

def longBars = if LongTrigger then 0 else longBars[1] + 1;

def shortBars = if ShortTrigger then 0 else shortBars[1] + 1;
#_______________________________________________________________

#TREND REVERSAL MOVING AVERAGE

input movingAverage = AverageType.EXPONENTIAL;

def price = close;

def superfast_length = 8;

def fast_length = 13;

def slow_length = 21;

def displace = 0;

def mov_avg8 = ExpAverage(price[-displace], superfast_length);

def mov_avg13 = ExpAverage(price[-displace], fast_length);

def mov_avg21 = ExpAverage(price[-displace], slow_length);

#moving averages

def Superfast = mov_avg8;

def Fast = mov_avg13;

def Slow = mov_avg21;

def buy = mov_avg8 > mov_avg13 and mov_avg13 > mov_avg21 and low > mov_avg8;

def stopbuy = mov_avg8 <= mov_avg13;

def buynow = !buy[1] and buy;

def buysignal = CompoundValue(1, if buynow and !stopbuy then 1 else if buysignal[1] == 1 and stopbuy then 0 else buysignal[1], 0);

def Buy_Signal = buysignal[1] == 0 and buysignal == 1;

#Alert(condition = buysignal[1] == 0 and buysignal == 1, text = "Buy Signal", sound = Sound.Bell, "alert type" = Alert.BAR);

def Momentum_Down = buysignal[1] == 1 and buysignal == 0;


#Alert(condition = buysignal[1] == 1 and buysignal == 0, text = "Momentum_Down", sound = Sound.Bell, "alert type" = Alert.BAR);

def sell = mov_avg8 < mov_avg13 and mov_avg13 < mov_avg21 and high < mov_avg8;

def stopsell = mov_avg8 >= mov_avg13;

def sellnow = !sell[1] and sell;

def sellsignal = CompoundValue(1, if sellnow and !stopsell then 1 else if sellsignal[1] == 1 and stopsell then 0 else sellsignal[1], 0);



def Sell_Signal = sellsignal[1] == 0 and sellsignal;



#Alert(condition = sellsignal[1] == 0 and sellsignal == 1, text = "Sell Signal", sound = Sound.Bell, "alert type" = Alert.BAR);

def Momentum_Up = sellsignal[1] == 1 and sellsignal == 0;


#Alert(condition = sellsignal[1] == 1 and sellSignal == 0, text = "Momentum_Up", sound = Sound.Bell, "alert type" = Alert.BAR);

def Colorbars = if buysignal == 1 then 1 else if sellsignal == 1 then 2 else if buysignal == 0 or sellsignal == 0 then 3 else 0;

#Colorbars.Hide();

#Colorbars.DefineColor("Buy_Signal_Bars", Color.GREEN);

#Colorbars.DefineColor("Sell_Signal_Bars", Color.RED);

#Colorbars.DefineColor("Neutral", Color.PLUM);

#_______________________________________________________________
#TREND REVERSAL STUDY

# used to switch between "zigzag based on high/low of price" and "zigzag based on then high/low of moving averages"

input method = {default average, high_low};

# data for the built-in zigzag indicator which is referenced in the study. To see the ZigZagHighLow() code, look for it in the built-in TOS studies

def bubbleoffset = .0005;

def percentamount = .01;

def revAmount = .05;

def atrreversal = 2.0;

def atrlength = 5;

# data for the moving averages

def pricehigh = high;

def pricelow = low;

def averagelength = 5;

#def averagetype = AverageType.EXPONENTIAL;

# here are the moving averages used for the signals - changed to plots and given colors

plot mah = MovingAverage(averageType, pricehigh, averagelength);

plot mal = MovingAverage(averageType, pricelow, averagelength);

mah.SetDefaultColor(Color.CYAN);

mal.SetDefaultColor(Color.CYAN);

# continues the switch between the two different zigzag data points

def priceh = if method == method.high_low then pricehigh else mah;

def pricel = if method == method.high_low then pricelow else mal;

# references the built-in zigzag indicator

def EI = ZigZagHighLow("price h" = priceh, "price l" = pricel, "percentage reversal" = percentamount, "absolute reversal" = revAmount, "atr length" = atrlength, "atr reversal" = atrreversal);

# not very clear on how this exactly works - but the end result is to plot the zigzag lines

def reversalAmount = if (close * percentamount / 100) > Max(revAmount < atrreversal * reference ATR(atrlength), revAmount) then (close * percentamount / 100) else if revAmount < atrreversal * reference ATR(atrlength) then atrreversal * reference ATR(atrlength) else revAmount;

rec EISave = if !IsNaN(EI) then EI else GetValue(EISave, 1);

def chg = (if EISave == priceh then priceh else pricel) - GetValue(EISave, 1);

def isUp = chg >= 0;

rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue(EI, 1)) and GetValue(isConf, 1));

def EId = if isUp then 1 else 0;

# zigzag plot and colors

plot EnhancedLines = if EId <= 1 then EI else Double.NaN;

#EnhancedLines.AssignValueColor(if EId == 1 then Color.GREEN else if EId == 0 then Color.RED else Color.DARK_ORANGE);

#EnhancedLines.SetStyle(Curve.FIRM);

#EnhancedLines.EnableApproximation();

#EnhancedLines.HideBubble();

#Price Change between Enhanceds

def xxhigh = if EISave == priceh then priceh else xxhigh[1];

def chghigh = priceh - xxhigh[1];

def xxlow = if EISave == pricel then pricel else xxlow[1];

def chglow = pricel - xxlow[1];

#Bar Count between Enhanceds

rec EIcount = if EISave[1] != EISave then 1 else if EISave[1] == EISave then EIcount[1] + 1 else 0;

def EIcounthilo = if EIcounthilo[1] == 0 and (EISave == priceh or EISave == pricel) then 1 else if EISave == priceh or EISave == pricel then EIcounthilo[1] + 1 else EIcounthilo[1];

def EIhilo = if EISave == priceh or EISave == pricel then EIcounthilo else EIcounthilo + 1;

def EIcounthigh = if EISave == priceh then EIcount[1] else Double.NaN;

def EIcountlow = if EISave == pricel then EIcount[1] else Double.NaN;

#Arrows

# calculates and plots the arrows

def EIL = if !IsNaN(EI) and !isUp then pricel else GetValue(EIL, 1);

def EIH = if !IsNaN(EI) and isUp then priceh else GetValue(EIH, 1);

def dir = CompoundValue(1, if EIL != EIL[1] or pricel == EIL[1] and pricel == EISave then 1 else if EIH != EIH[1] or priceh == EIH[1] and priceh == EISave then -1 else dir[1], 0);

def signal = CompoundValue(1, if dir > 0 and pricel > EIL then if signal[1] <= 0 then 1 else signal[1] else if dir < 0 and priceh < EIH then if signal[1] >= 0 then -1 else signal[1] else signal[1], 0);

def showarrows = yes;

def U1 = showarrows and signal > 0 and signal[1] <= 0;

#U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

#U1.SetDefaultColor(Color.GREEN);

#U1.SetLineWeight(4);

def D1 = showarrows and signal < 0 and signal[1] >= 0;

#D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

#D1.SetDefaultColor(Color.RED);

#D1.SetLineWeight(4);

#New Code

def barnumber = BarNumber()[10];

def longCond = (barnumber and U1) and signal > 0 and signal[1] <= 0 ;

def shortCond = (barnumber and D1) and signal < 0 and signal[1] >= 0;

#BUBBLES_FOR_TRENDREVERSAL_INDICATOR___

AddChartBubble(ShowRBubbles and longCond, if isUp then low else high, if showarrows and signal > 0 and signal[1] <= 0 then "R" else "" , if Colorbars == 3 then Color.PLUM else Color.UPTICK, no);

AddChartBubble(ShowRBubbles and shortCond, if isUp then low else high, if showarrows and signal < 0 and signal[1] >= 0 then "R" else "" , if Colorbars == 3 then Color.PLUM else Color.DOWNTICK, yes);

def longBars1 = if longCond then 0 else longBars1[1] + 1;

def shortBars1 = if shortCond then 0 else shortBars1[1] + 1;

plot buyCondition = (longCond and longBars <= lookback)

or (LongTrigger and longBars1 <= lookforward);

;

plot sellCondition = (shortCond and shortBars <= lookback)

or (ShortTrigger and shortBars1 <= lookforward);

;

buyCondition.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

sellCondition.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

buyCondition.Hide();

sellCondition.Hide();

#____________________________________________________________________________________

#mADX
#
# Free for use. Header credits must be included when any form of the code included in this package is used.
# User assumes all risk. Author not responsible for errors or use of tool.
# Copyright (c) 2021 B4 Signals
#
# Get support at: https://b4signals.com
# Join us at: https://discord.gg/kD3pKE2CQd
#
#
# v1.0 - chuck - initial integration
# v2.0 - barbaros - cleanup
# v2.1 - barbaros - added adx limit

input ADDON_ADX_show = yes;#hint ADDON_ADX_show: Turn mADX lines ON or OFF
input ADDON_ADX_dmilength = 10;
input ADDON_ADX_limit = 25; #hint ADDON_ADX_limit: 25 for daily and 35 for intraday suggested
input ADDON_ADX_dmiaverageType = AverageType.WILDERS;

def ADDON_ADX_ADX = (DMI(ADDON_ADX_dmilength, ADDON_ADX_dmiaverageType).ADX) - 18;
def ADDON_ADX_signal = ADDON_ADX_ADX > ADDON_ADX_limit and ADDON_ADX_ADX >= 1 and ADDON_ADX_ADX < ADDON_ADX_ADX[1] and ADDON_ADX_ADX[1] > ADDON_ADX_ADX[2];

AddVerticalLine (ADDON_ADX_show and ADDON_ADX_signal, "mADX", Color.WHITE);
Alert(ADDON_ADX_signal, "mADX", Alert.BAR, Sound.Ding);


addorder(orderType.BUY_AUTO, buyCondition, open()[-1], quantity, color.white, color.white, "buy");
addorder(orderType.SELL_AUTO, sellCondition, open[-1], quantity, color.white, color.white, "sell");

#END STRATEGY
 
Last edited by a moderator:

RDJ

New member
VIP
The mADX line is actually an indicator a lot of people used with the B4. It’s very good and it will tell you when theirs a possible change in direction. But it does not tell you the direction. I normally use it to begin to scale out of a trade. If I see the mADX trigger before the Buy or Sell, there’s a good chance this is the direction the stock will follow.
 

BigScalp

New member
VIP
The mADX line is actually an indicator a lot of people used with the B4. It’s very good and it will tell you when theirs a possible change in direction. But it does not tell you the direction. I normally use it to begin to scale out of a trade. If I see the mADX trigger before the Buy or Sell, there’s a good chance this is the direction the stock will follow.
How do I take off the floating FPL at the top of my charts after installing the indicator? Also, this isn't just for backtesting, correct?
 

RDJ

New member
VIP
Hopefully I can answer your question, I'm very new at all this posting.

I normally create and save a layout with just the FPL, Floating Profit/Loss so I can quickly review a stock but if I understand the question correctly the FPL chart should be on the lower chart, but you can select the X to remove this when highlighted if you would like. And you are correct, I use this for more than just back testing. I will only go in if all my indicators say its a buy or sell. I might miss a few but it normally works out pretty well.


O3i3e7v.jpg



ypFvMN6.jpg
 

mactheknife

New member
VIP
Can this appear, (say showing a sell) then disappear if the price goes higher only to appear again as a sell signal at a higher level ?
 

RDJ

New member
VIP
That would be repainting and If I actually take the buy or sell signal, I have not been able to find a stock where this happens. So I don't think it does repaint. I've actually tested down to a 5 min chart to see if it does but it did not on the few stocks I selected. But please, if someone finds out that it does repaint, please post it so we all know. My programming is very, very limited...Good luck every!
 

RDJ

New member
VIP
Hello,great job but lower indicators are not on the script and I can’t seem to find them anywhere else
This is for the B4, they did a fantastic job on this indicator.
https://usethinkscript.com/threads/balanced-bb-breakout.5708/

This is for the Laguerre, this is also a very nice indicator. There is a lot of Laguerre indicators and sometime you need to find the one you like.
https://usethinkscript.com/threads/rsi-in-laguerre-time-mtf-for-thinkorswim.1134

The nice thing about all of these indicators is they work in any time frame you choose to use. Good luck and enjoy!
 

RDJ

New member
VIP
This is for the B4, they did a fantastic job on this indicator.
https://usethinkscript.com/threads/balanced-bb-breakout.5708/

This is for the Laguerre, this is also a very nice indicator. There is a lot of Laguerre indicators and sometime you need to find the one you like.
https://usethinkscript.com/threads/rsi-in-laguerre-time-mtf-for-thinkorswim.1134

The nice thing about all of these indicators is they work in any time frame you choose to use. Good luck and enjoy!
The B4 indicator has a very nice YouTube also, just type in B4 indicator.
 

Southeastern99

New member
VIP
This is for the B4, they did a fantastic job on this indicator.
https://usethinkscript.com/threads/balanced-bb-breakout.5708/

This is for the Laguerre, this is also a very nice indicator. There is a lot of Laguerre indicators and sometime you need to find the one you like.
https://usethinkscript.com/threads/rsi-in-laguerre-time-mtf-for-thinkorswim.1134

The nice thing about all of these indicators is they work in any time frame you choose to use. Good luck and enjoy!
Thanks for the hard work and patience with others.
 
  • Like
Reactions: RDJ

tem2005

Member
VIP
thanks for the hard work. first time seen trend reversals been used in a strategy. i think it will repaint as TR is in it
 

RDJ

New member
VIP
thanks for the hard work. first time seen trend reversals been used in a strategy. i think it will repaint as TR is in it
I actually thought it would also knowing the TR is being used as part of the program. Having the Supertrend trigger along with the TR, I think it stays put once the candle closes. Please give it a test and see if I'm correct. Thank you!
 

BigScalp

New member
VIP
Hopefully I can answer your question, I'm very new at all this posting.

I normally create and save a layout with just the FPL, Floating Profit/Loss so I can quickly review a stock but if I understand the question correctly the FPL chart should be on the lower chart, but you can select the X to remove this when highlighted if you would like. And you are correct, I use this for more than just back testing. I will only go in if all my indicators say its a buy or sell. I might miss a few but it normally works out pretty well.


O3i3e7v.jpg



ypFvMN6.jpg
Thanks for your time. All I’m looking for now is a color code alert in my watchlist every time I get a green “r” signal
 

RDJ

New member
VIP
Pardon me - what indicator is the Red/Green zones? Does the script above include them? If yes, how do you enable them?
The script above does not include these zones.

The red green zone in the upper or price that actually follow's price, is the Laguerre Filter indicator. I think you can find this one if you add it to the search tab, if not please let me know and I will find the link.

I probably have way too many indicator shown, but the other red green zone in the upper or price that looks like a cloud is the Ichimoku cloud but with all the lines turn off. This indicator is stock from TOS under studies. It can help with support and resistance.

The red green zone shown on the lower study is the B4 indicator. This can be turn on or off within the B4 settings.

I hope this helps!
 

MerryDay

Administrative
Staff member
Staff
VIP
Thanks for your time. All I’m looking for now is a color code alert in my watchlist every time I get a green “r” signal
The green “r” signal is from the Trend Reversal Indicator. This is a REPAINTING INDICATOR!
Read more about this indicator: https://usethinkscript.com/threads/trend-reversal-questions.7600/#post-72213
Read more about the problem with this indicator's watchlist: https://usethinkscript.com/threads/supertrend-with-reversal-for-thinkorswim.8259/#post-79003
 
Last edited:

mactheknife

New member
VIP
Just a note on this indicator. Was using it this morning, and the indicator came on as a "Buy" showing a reverse to the long side at 8:16. At 8:36, it disappeared as the index I was watching was falling. SO, the lesson here is it repainted or disappeared 20 minutes after it first showed, then showed up again as a buy again at 8:45. Looking on a daily or long term chart you would have never known this. Also, the reason I know this, is that I record my sessions.
 

RDJ

New member
VIP
Just a note on this indicator. Was using it this morning, and the indicator came on as a "Buy" showing a reverse to the long side at 8:16. At 8:36, it disappeared as the index I was watching was falling. SO, the lesson here is it repainted or disappeared 20 minutes after it first showed, then showed up again as a buy again at 8:45. Looking on a daily or long term chart you would have never known this. Also, the reason I know this, is that I record my sessions.
Thank you, this is very good information. I've tried this on a few stock but I could not get it to change like this. I'm curious what time frame you are using.
 

Similar threads

Top