Have an indicator buying before close of candle

OTM

New member
VIP
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.

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:
Solution
I figured it out, [1] will make it wait till the candle closes.
Yes, you can use [1] to wait until the candle closes.

change:
def buy = src > xATRTrailingStop and above and markethours ;
def sell = src < xATRTrailingStop and below and markethours;
to:
def buyCALC = src > xATRTrailingStop and above ;
def sellCALC = src < xATRTrailingStop and below ;
def buy = buyCALC[1] and markethours ;
def sell = sellCALC[1] and markethours;
I figured it out, [1] will make it wait till the candle closes.
Yes, you can use [1] to wait until the candle closes.

change:
def buy = src > xATRTrailingStop and above and markethours ;
def sell = src < xATRTrailingStop and below and markethours;
to:
def buyCALC = src > xATRTrailingStop and above ;
def sellCALC = src < xATRTrailingStop and below ;
def buy = buyCALC[1] and markethours ;
def sell = sellCALC[1] and markethours;
 
Last edited:
Solution

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
350 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