UT Bot For ThinkOrSwim

samer800

Conversion Expert
VIP
Lifetime
The UT Bot Indicator uses advanced algorithms to identify key support and resistance levels.
This code gives signals based upon the ATR Trailing Stop calculated with HeikinAshi candlesticks.
As a trend analysis tool, it provides the general direction of where an equity is heading.

mod note:
Here is the most updated version of the UT Bot For ThinkOrSwim
This particular UT Bot was written for use on: 4 hour, Daily, 3 Day and Weekly time frames
However, many members are using it on shorter aggregations. Read through this thread to find out what modifications other members have found useful when applying to lower timeframes.
Appreciation to @ttsdmagic for all the support provided in this thread.
(his original version is in the next post below)
Q4bniph.png

CSS:
# ATR Trailing Stop
# https://usethinkscript.com/threads/ut-bot-for-thinkorswim.12640/#post-116615

input Key_Vaule = 1.0;
def a = key_vaule;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input SignalStyle = {Default "Arrow", "Label", "None"};

def na = Double.NaN;
defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);


def arrow = SignalStyle==SignalStyle."Arrow";
def Label = SignalStyle==SignalStyle."Label";
def SigStyle = if arrow then 1 else if Label then -1 else 0;
def xATR = atr(c);
def nLoss = a * xATR;


def src = if h then (open + high + low + close) / 4 else close;

script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}

#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);

#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);

def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);

def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;

def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

plot ArrowUp = if Arrow and buy then low - ATR(5)/10 else na;
ArrowUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowUp.SetDefaultColor(globalColor("Green"));
ArrowUp.SetLineWeight(2);
plot ArrowDn = if Arrow and sell then high + ATR(5)/10 else na;
ArrowDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowDn.SetDefaultColor(globalColor("Red"));
ArrowDn.SetLineWeight(2);



addchartbubble(Label and buy, low, "B", globalColor("Green"), no);
addchartbubble(Label and sell, high, "S", globalColor("Red"), yes);

input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);

input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
[/COLOR]
 
Last edited by a moderator:
I actually paid someone to convert UT Bot To TOS. Here's the code. Maybe you can use some of what he wrote. And yes I know he mis-spelled Value early in the code. Doesn't matter. The conversion seems accurate, and the signals are great. The original Tradingview code spells value that way. I had him shorten Buy and Sell to B and S to conserve screen space. Good luck.

Ruby:
input Key_Vaule = 1.0;
def a = key_vaule;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input show_labels = yes;

def xATR = atr(c);
def nLoss = a * xATR;


def src = if h then (open + high + low + close) / 4 else close;

script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}

#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);

#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);

def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);

def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;

def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);
addchartbubble(show_labels and buy, low, "B", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "S", globalColor("Red"), yes);

input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);

input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
 
Last edited:
@esvafromdc @ttsdmagic Thank you. The input parameter for the first post code is "keyvalue" of 3, and for the second post code, it is "key value" of 1. What is the ideal input configuration (key value and atrperiod) for the 1 or 2 min time frame, based on your experience?. Thank you
 
@esvafromdc @ttsdmagic Thank you. The input parameter for the first post code is "keyvalue" of 3, and for the second post code, it is "key value" of 1. What is the ideal input configuration (key value and atrperiod) for the 1 or 2 min time frame, based on your experience?. Thank you
I swing trade so I use 4hr D. 3D and W. The value is a parameter in the study so you'll need to experiment with it to find the best value for your time frames. B and. S are pretty accurate on 4:hr and Daily with it set to 1.
 
I actually paid someone on Fiverr to convert UT Bot To TOS. Here's the code. Maybe you can use some of what he wrote. And yes I know he mis-spelled Value early in the code. Doesn't matter. The conversion seems accurate, and the signals are great. I had him shorten Buy and Sell to B and S to conserve screen space. Good luck.

input Key_Vaule = 1.0;
def a = key_vaule;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input show_labels = yes;

def xATR = atr(c);
def nLoss = a * xATR;


def src = if h then (open + high + low + close) / 4 else close;

script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}

#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);

#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);

def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);

def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;

def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);
addchartbubble(show_labels and buy, low, "B", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "S", globalColor("Red"), yes);

input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);

input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
Thanks very much. I definitely would have never arrived at that formula conversion, so, I'm learning!
 
I actually paid someone on Fiverr to convert UT Bot To TOS. Here's the code. Maybe you can use some of what he wrote. And yes I know he mis-spelled Value early in the code. Doesn't matter. The conversion seems accurate, and the signals are great. I had him shorten Buy and Sell to B and S to conserve screen space. Good luck.

Ruby:
input Key_Vaule = 1.0;
def a = key_vaule;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input show_labels = yes;

def xATR = atr(c);
def nLoss = a * xATR;


def src = if h then (open + high + low + close) / 4 else close;

script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}

#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);

#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);

def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);

def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;

def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);
addchartbubble(show_labels and buy, low, "B", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "S", globalColor("Red"), yes);

input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);

input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
thank you for share it! really like it, can add the scan and a MTF? that will be great. thanks in advance!!
 
I actually paid someone on Fiverr to convert UT Bot To TOS. Here's the code. Maybe you can use some of what he wrote. And yes I know he mis-spelled Value early in the code. Doesn't matter. The conversion seems accurate, and the signals are great. I had him shorten Buy and Sell to B and S to conserve screen space. Good luck.

Ruby:
input Key_Vaule = 1.0;
def a = key_vaule;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input show_labels = yes;

def xATR = atr(c);
def nLoss = a * xATR;


def src = if h then (open + high + low + close) / 4 else close;

script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}

#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);

#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);

def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);

def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;

def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);
addchartbubble(show_labels and buy, low, "B", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "S", globalColor("Red"), yes);

input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);

input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
to all great coding here! it is possible to create a multi time frame labels for this indicator. i will like to see a label for a time frame : M-W-D_4H, thank you in advance
 
I have the study on all my timeframes (4 Hr D 3D and W.) That works for me. Your mileage may vary.

link to ubot_buy_sell_scan: http://tos.mx/08GKzEF

I use this scan to reduce my watchlist of over 300 stocks down to about 20 or 30. It scans for EITHER buy or sell signals. It is based on a 4 hour timeframe.

NOTE: You will need to remove my INTERSECT With (my symbols) and put YOUR SYMBOLS list in there.
 
@esvafromdc @ttsdmagic Thank you. The input parameter for the first post code is "keyvalue" of 3, and for the second post code, it is "key value" of 1. What is the ideal input configuration (key value and atrperiod) for the 1 or 2 min time frame, based on your experience?. Thank you
For 5 min charts or less, I find the best setting to be key value of 2.0 and atr period of 4. I suggest trying it on the 5 min SPX and 5 min /ES charts. Then a 5 min SPX daily options chart. Usually the options chart buy signals will follow the signals on the SPX and /ES charts.

I actually paid someone to convert UT Bot To TOS. Here's the code. Maybe you can use some of what he wrote. And yes I know he mis-spelled Value early in the code. Doesn't matter. The conversion seems accurate, and the signals are great. I had him shorten Buy and Sell to B and S to conserve screen space. Good luck.

Ruby:
input Key_Vaule = 1.0;
def a = key_vaule;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input show_labels = yes;

def xATR = atr(c);
def nLoss = a * xATR;


def src = if h then (open + high + low + close) / 4 else close;

script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}

#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);

#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);

def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);

def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;

def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);
addchartbubble(show_labels and buy, low, "B", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "S", globalColor("Red"), yes);

input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);

input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
@ttsdmagic Thank you for posting this UT Bot Alerts study. Of the 200+ signal type studies that I posses, this one is by far the best! That was very kind of you to share a study that you paid to have converted.
 
For 5 min charts or less, I find the best setting to be key value of 2.0 and atr period of 4. I suggest trying it on the 5 min SPX and 5 min /ES charts. Then a 5 min SPX daily options chart. Usually the options chart buy signals will follow the signals on the SPX and /ES charts.
On the 1 minute for /ES, I find that Key Value of 3.0 and ATR period of 2 works good for me.
 
Mobile users: adding this may gain some functionality on mobile app

CSS:
input mobile_arrows_on = yes;
plot mobile_arrows_buy = if buy and mobile_arrows_on then 1 else 0;
mobile_arrows_buy.SetLineWeight(5);
mobile_arrows_buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
mobile_arrows_buy.SetDefaultColor(Color.UPTICK);

plot mobile_arrows_sell = if sell and mobile_arrows_on then 1 else 0;
mobile_arrows_sell.SetLineWeight(5);
mobile_arrows_sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
mobile_arrows_sell.SetDefaultColor(Color.DOWNTICK);
 
Comparing this UT Bot and one in TradingView I see minor differences. The one in TV is a bit more accurate. So far I can't spot what makes this difference? Can someone compare two codes?
 
Comparing this UT Bot and one in TradingView I see minor differences. The one in TV is a bit more accurate. So far I can't spot what makes this difference? Can someone compare two codes?
You may want to tweak the parameters for the time frames you use. If anyone can come up with a more accurate conversion, that would be great too.
 
Ruby:
# ATR Trailing Stop
# This script implements an ATR-based trailing stop with some additional features

# Input variables
input Key_Value = 1.0; # This changes the sensitivity of the stop loss
input atr_period = 5; # The period used for the ATR calculation
input Signals_from_Heikin_Ashi_Candles = yes; # Use Heikin-Ashi candles instead of regular candles
input show_labels = yes; # Show labels for buy and sell signals on the chart

# ATR calculation
def xATR = atr(atr_period);

# Loss calculation
# The nLoss variable represents the stop loss value
def nLoss = Key_Value * xATR;

# Heikin-Ashi calculation
def src = if Signals_from_Heikin_Ashi_Candles then (open + high + low + close) / 4 else close;

# nz function
# This function is used to ensure that the xATRTrailingStop variable
# does not contain any NaN values
script nz{
    input x = close;
    input y = 0;
    plot result = if isnan(x) then y else x;
}

# xATRTrailingStop calculation
# This variable represents the current value of the trailing stop
def xATRTrailingStop =compoundValue(1,
    # If the current src is greater than the previous xATRTrailingStop and the previous src is also greater than the previous xATRTrailingStop
    if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0),
        # Set the current xATRTrailingStop to the max of the previous xATRTrailingStop and the current src minus nLoss
        max(nz(xATRTrailingStop[1]), src - nLoss),
    # If the current src is less than the previous xATRTrailingStop and the previous src is also less than the previous xATRTrailingStop
    if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0),
        # Set the current xATRTrailingStop to the min of the previous xATRTrailingStop and the current src plus nLoss
        min(nz(xATRTrailingStop[1]), src + nLoss),
    # If the current src is greater than the previous xATRTrailingStop
    if(src > nz(xATRTrailingStop[1], 0),
        # Set the current xATRTrailingStop to the current src minus nLoss
        src - nLoss,
    # If the current src is less than the previous xATRTrailingStop
    src + nLoss))),0);

# Position calculation
# This variable represents the current position (long = 1, short = -1, flat = 0)
def pos = compoundValue(1,
    # If the previous src is less than the previous xATRTrailingStop and the current src is greater than the current xATRTrailingStop
    if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
    # If the previous src is greater than the previous xATRTrailingStop and the current src is less than the current xATRTrailingStop
    if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1,
    # Otherwise, use the previous position
    nz(pos[1], 0))) , 0);

# EMA calculation
# This variable represents the Exponential Moving Average of the src variable
def ema = expaverage(src,1);

# Buy and sell signals
# These variables represent the buy and sell signals generated by the script
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);

def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;

def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;

# Chart label colors
defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);

# Chart labels for buy and sell signals
addchartbubble(show_labels and buy, low, "B", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "S", globalColor("Red"), yes);

# Bar coloring
input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);

# Alerts
input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);

I don't read code very well, so I had some help adding comments as well as fixed the eyesore that was 'Vaule'. Still, many thanks to OP.
 
Interesting. It would be great if one of the developers here could compare the original TradingView to the conversion to validate the quality of the conversion. Any takers?
 
mod note:
The indicator that is the subject of this thread is NOT A REPAINTING INDICATOR.
This is an ATR Trailing Stop script written for use with Heiken Ashi candles


@ttsdmagic, @Nomak, @rip78, This script uses close to define the price. In the ToS, "close" means "last", the most current tick price.
So, as @Tradervic pointed out, on the most current candle the "close" will fluctuate with the "last" price until the bar completes. No idea, if Tradingview uses the same concept.

To read more about ATR Trailing Stops and how to use them:
https://www.incrediblecharts.com/indicators/atr_average_true_range_trailing_stops.php
 
Last edited:
Thank you @ttsdmagic for posting this indicator. It works great. Since I use 2 min and 5 min timeframe to trade, but the watchlist doesn't get updated within that timeframe when using the scan search posted in #7 . I hope it is alright that I took the liberty to modify the indicator so that would work in the watchlist column. I'm still new to code in thinkscript. All I did was really simple. I added the three lines of code below right after the statement (def sell = src < xATRTrailingStop and below) and then delete the rest of the code. The 1 and -1 is just random numbers for me to sort the column and also can be displayed on my mobile app. I hope this help!
Code:
plot UT_Bot = if buy then 1 else if sell then -1 else 0;
AssignBackgroundColor(if buy then color.dark_green else if sell then color.red else color.gray);
UT_Bot.assignValueColor(color.black);

Here is what the column looks like.
cnM1icn.png
 
Hello calvin8tor . I'm glad you're able to use the study. Looks like a great enhancement to the code for shorter time frame use on your watchlist. If anyone else is using this study and can suggest improvements, bring em on. When I first saw this one on TradingView it looked like it had potential to offer good Buy and Sell signals. I'll keep checking back here to see what others are experiencing.
 
Hello calvin8tor . I'm glad you're able to use the study. Looks like a great enhancement to the code for shorter time frame use on your watchlist. If anyone else is using this study and can suggest improvements, bring em on. When I first saw this one on TradingView it looked like it had potential to offer good Buy and Sell signals. I'll keep checking back here to see what others are experiencing.
hey man I am enjoying this script. If there's anything I could suggest would it be possible to add custom arrows instead of buy sell labels? I would love to be able to have an arrow pointing under the candle signaling buy and an another arrow pointing down on top of the candle signaling sell. Anyway there can be an updated script for that? other than that this script is awesome!

@samer800 can you please help? I just need one line in the script to be able to add color to the arrows. :(
 
Last edited by a moderator:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
464 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