Trend Reversal Indicator with Signals for ThinkorSwim

BenTen

BenTen

Administrative
Staff
VIP
@aharvard Let me confirm, you want both reversal bubbles and reversal arrows? Eventually a duplicate of the same data but in different format? If so, you can use the default indicator and load up the script under the Scanner link (that version will display the reversal arrows).
 
A

aharvard

New member
The code I posted displays reversal lines and arrows (No Bubbles). To answer your question, yes, I only want the reversal lines and arrows. In the code i posted, it allows the option to display or not display the reversal lines or arrows. There is a orange triangle at the top of the Thinkscript editor from Thinkorswim stating the code is complex and my run slow. I tried to see what part of the code I can take out, but its way to complicated.

Essentially, I just want to remove the unnecessary code
 
BenTen

BenTen

Administrative
Staff
VIP
@aharvard Ahh I see. I believe @theelderwand has already minify the indicator through his scanner version. You can just take a look at that indicator's source code, compare it to yours, and remove what all the extra stuff.
 
horserider

horserider

Well-known member
VIP
Code:
#Trend Reverse Indicator

def price = close;
def superfast_length = 9;
def fast_length = 14;
def slow_length = 21;
def displace = 0;

def mov_avg9 = ExpAverage(price[-displace], superfast_length);
def mov_avg14 = ExpAverage(price[-displace], fast_length);
def mov_avg21 = ExpAverage(price[-displace], slow_length);

#moving averages
def Superfast = mov_avg9;
def Fast = mov_avg14;
def Slow = mov_avg21;

def buy = mov_avg9 > mov_avg14 and mov_avg14 > mov_avg21 and low > mov_avg9;
def stopbuy = mov_avg9 <= mov_avg14;
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_avg9 < mov_avg14 and mov_avg14 < mov_avg21 and high < mov_avg9;
def stopsell = mov_avg9 >= mov_avg14;
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);

plot 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);
#___________________________________________________________________________

input method = {default average, high_low};
def bubbleoffset = .0005;
def percentamount = .01;
def revAmount = .05;
def atrreversal = 2.0;
def atrlength = 5;
def pricehigh = high;
def pricelow = low;
def averagelength = 5;
def averagetype = AverageType.EXPONENTIAL;
def mah = MovingAverage(averagetype, pricehigh, averagelength);
def mal = MovingAverage(averagetype, pricelow, averagelength);
def priceh = if method == method.high_low then pricehigh else mah;
def pricel = if method == method.high_low then pricelow else mal;
def EI = ZigZagHighLow("price h" = priceh, "price l" = pricel, "percentage reversal" = percentamount, "absolute reversal" = revAmount, "atr length" = atrlength, "atr reversal" = atrreversal);
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;

#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);
def barnumber = BarNumber()[10];

def bullish2 = signal > 0 and signal[1] <= 0;
plot upArrow = bullish2;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetDefaultColor(CreateColor(145, 210, 144));

def bearish2 = signal < 0 and signal[1] >= 0;
plot downArrow = bearish2;
downArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downArrow.SetDefaultColor(CreateColor(255, 15, 10));

def revLineTop;
def revLineBot;

if barnumber and D1 {
revLineBot = Double.NaN;
revLineTop = high[1];
} else if barnumber and U1 {
revLineTop = Double.NaN;
revLineBot = low[1];
} else if !IsNaN(revLineBot[1]) and (Colorbars[2] == 2 or Colorbars[1] == 2) {
revLineBot = revLineBot[1];
revLineTop = Double.NaN;
} else if !IsNaN(revLineTop[1]) and (Colorbars[2] == 1 or Colorbars[1] == 1) {
revLineTop = revLineTop[1];
revLineBot = Double.NaN;
} else {
revLineTop = Double.NaN;
revLineBot = Double.NaN;
}

plot botLine = revLineBot[-1];
botLine.SetDefaultColor(Color.LIGHT_GREEN);
plot topLine = revLineTop[-1];
topLine.SetDefaultColor(Color.LIGHT_RED);
 
J

jeffxcore2

New member
Hello. I am new to thinkorswim, but is it possible for someone to teach me how to use this indicator while scanning for stocks? I can't seem to find the study while making my scanner. Thank you!
 
NikeNewl

NikeNewl

New member
Hello Ben,

I am a NewB here. I appreciated very much for sharing. I am not able to import the file http://tos.mx/6m77gW to the scanner. Can you do a video tutorial?

TIA
 
NikeNewl

NikeNewl

New member
@hurricane5 Did you try to load it on a chart? If so, use the actual indicator above instead. This one should be loaded via the Scan tab instead.
@BenTen @hurricane5 I have the same problem as @hurricane5 I am loaded the file into the chart and copy the script to scanner tap and it gave me the error "Exactly one plot expected"
@hurricane5 How did you clear the error?

Thanks all!
 
NikeNewl

NikeNewl

New member
@hurricane5 When you use it scanner use like the following

plot Data = TrendReversalScanner().upArrow or downArrow. There are two plots defined, choose wisely :)
@theelderwand What do you mean by "plot Data = TrendReversalScanner().upArrow or downArrow "? I have the same problem as @hurricane5 " Exactly one plot expected"
 
BenTen

BenTen

Administrative
Staff
VIP
@NikeNewl The link under the scanner section is actually an indicator (same version but with arrows) so you can use the scanner with it.
 
A

Alex

New member
VIP
Would it be possible to display the reversal signals as labels not just as bubbles / arrows? Any help would be much appreciated.
 
A

askie13

New member
VIP
Hello Ben, thanks for sharing this code I've been using it for a while on 3min time frame and it's working well for me so far day trading. My question is will it be possible to create conditional orders using the study?
 
BenTen

BenTen

Administrative
Staff
VIP
@askie13 Like auto trading? Don't think that is possible in ToS.
 
A

askie13

New member
VIP
@benti know tos allow you to place conditional orders where you can put in the study. Hopefully someone can look into this. It's not fully automatic but close
 
S

stockminion

New member
VIP
@horserider Unfortunately ZigZagHighLow function is the main evil for TOS Mobile :( ..any alternatives pls ?
 
BenTen

BenTen

Administrative
Staff
VIP
@askie13 It's possible and it's already been posted in this thread before. But since this indicator repaint, the result will likely be 99.9% win rate.
 
horserider

horserider

Well-known member
VIP
@stockminion Do not use mobile sorry cannot help.
 
T

Thomas

Active member
@HighNDryTrader
Add these to the bottom of the script to create the alerts that I think you are wanting

alert(bullish2[1], "Bull", Alert.BAR, Sound.Ring);
alert(bearish2[1], "Bear", Alert.BAR, Sound.Ding);
zzz/BLT,.........can you help me learn and write this scan simpler, error message, "Too Complex." http://tos.mx/GvfXBE3
BTW, after many years I am using several of your creations as standards on every chart I use..........
 

Top