I figured it out, [1] will make it wait till the candle closes. I continued to look through forum posts and found it. There is so much knowledge in these posts it is amazing.
I am having a problem where there will be a small reversal (say I am short and there is the start of a green candle but no signal by the code that says buy) and it will take a long position from there and then the candle will turn back red and I am now long. Is there something in my code that I am missing that will stop that.
I am having a problem where there will be a small reversal (say I am short and there is the start of a green candle but no signal by the code that says buy) and it will take a long position from there and then the candle will turn back red and I am now long. Is there something in my code that I am missing that will stop that.
Code:
# ATR Trailing Stop
# [URL]https://usethinkscript.com/threads/ut-bot-for-thinkorswim.12640/#post-116615[/URL]
input TargetProfit = 00;#hint TargetProfit: Target Profit in Dollars
input TargetPercent = 00;#hint TargetPercent; Target Profit in Percent
input StopLoss = 00;#hint StopLoss: Stop Loss in Dollar
def TProfit = AbsValue(TargetProfit);
def TPercent = AbsValue(TargetPercent / 100);
def SLoss = AbsValue (StopLoss) * -1;
def qty = GetQuantity();
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"};
input daytrade = No;
def markethours = if daytrade == yes then (secondsFromTime(925) > 0 and secondsTillTime(1550) >0) else yes;
def eodexit = if daytrade == yes then (secondsTillTime(1555) ==0) else no;
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 and markethours ;
def sell = src < xATRTrailingStop and below and markethours;
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);
Last edited: