Plot stoploss or target in thinkscript

Here is my code currently when long or short entry im having to wait for exit or stop to plot once either is hit. Is there a way to plot points as soon as entryt is taken to see where the proit target is and the stop target is. Thank in advance.

Code:
# Long

AddOrder(tickcolor = Color.GREEN, arrowcolor = Color.RED, name = "long", tradeSize = contracts, condition = condition, type = OrderType.BUY_to_open);

# Profit

def target = EntryPrice() + ATR() * profit_mult;

AddOrder(type = OrderType.SELL_to_close, high[-1] >= target, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Target", price = target);

# Trailing stop

def stop = EntryPrice() - ATR() * stop_mult;

AddOrder(OrderType.SELL_to_close, low[-1] <= stop, tickcolor = Color.GRAY, arrowcolor = Color.GRAY, name = "Stop", tradeSize = contracts, price = stop);

Plot user input target lines.

Code:
# TARGETS
# DREWGRIFFITH15 (C) 2015

INPUT AGGREGATIONPERIOD = AGGREGATIONPERIOD.DAY;
INPUT TARGET_ONE = 250;
INPUT TARGET_TWO = 20;
INPUT TARGET_THR = 252;
INPUT DISPLACE = -1;
INPUT SHOWONLYLASTPERIOD = YES;

# PLOT HIGH TARGETS
PLOT HIGH_ONE;
IF SHOWONLYLASTPERIOD AND !ISNAN(HIGH(PERIOD = AGGREGATIONPERIOD)[-1]) { HIGH_ONE = DOUBLE.NAN;
} ELSE { HIGH_ONE = HIGHEST(HIGH(PERIOD = AGGREGATIONPERIOD)[-DISPLACE], TARGET_ONE);
}

PLOT HIGH_TWO;
IF SHOWONLYLASTPERIOD AND !ISNAN(HIGH(PERIOD = AGGREGATIONPERIOD)[-1]) { HIGH_TWO = DOUBLE.NAN;
} ELSE { HIGH_TWO = HIGHEST(HIGH(PERIOD = AGGREGATIONPERIOD)[-DISPLACE], TARGET_TWO);
}

PLOT HIGH_THR;
IF SHOWONLYLASTPERIOD AND !ISNAN(HIGH(PERIOD = AGGREGATIONPERIOD)[-1]) { HIGH_THR = DOUBLE.NAN;
} ELSE { HIGH_THR = HIGHEST(HIGH(PERIOD = AGGREGATIONPERIOD)[-DISPLACE], TARGET_THR);
}

# PLOT LOW TARGETS
PLOT LOW_ONE;
IF SHOWONLYLASTPERIOD AND !ISNAN(LOW(PERIOD = AGGREGATIONPERIOD)[-1]) { LOW_ONE = DOUBLE.NAN;
} ELSE { LOW_ONE = LOWEST(LOW(PERIOD = AGGREGATIONPERIOD)[-DISPLACE], TARGET_ONE);
}

PLOT LOW_TWO;
IF SHOWONLYLASTPERIOD AND !ISNAN(LOW(PERIOD = AGGREGATIONPERIOD)[-1]) { LOW_TWO = DOUBLE.NAN;
} ELSE { LOW_TWO = LOWEST(LOW(PERIOD = AGGREGATIONPERIOD)[-DISPLACE], TARGET_TWO);
}

PLOT LOW_THR;
IF SHOWONLYLASTPERIOD AND !ISNAN(LOW(PERIOD = AGGREGATIONPERIOD)[-1]) { LOW_THR = DOUBLE.NAN;
} ELSE { LOW_THR = LOWEST(LOW(PERIOD = AGGREGATIONPERIOD)[-DISPLACE], TARGET_THR);
}

HIGH_ONE.SETDEFAULTCOLOR(CREATECOLOR(255, 0, 0));
HIGH_ONE.SETPAINTINGSTRATEGY(PAINTINGSTRATEGY.POINTS);
HIGH_TWO.SETDEFAULTCOLOR(CREATECOLOR(255, 255, 0));
HIGH_TWO.SETPAINTINGSTRATEGY(PAINTINGSTRATEGY.POINTS);
HIGH_THR.SETDEFAULTCOLOR(CREATECOLOR(255, 255, 255));
HIGH_THR.SETPAINTINGSTRATEGY(PAINTINGSTRATEGY.POINTS);

LOW_ONE.SETDEFAULTCOLOR(CREATECOLOR(0, 255, 0));
LOW_ONE.SETPAINTINGSTRATEGY(PAINTINGSTRATEGY.POINTS);
LOW_TWO.SETDEFAULTCOLOR(CREATECOLOR(255, 255, 0));
LOW_TWO.SETPAINTINGSTRATEGY(PAINTINGSTRATEGY.POINTS);
LOW_THR.SETDEFAULTCOLOR(CREATECOLOR(255, 255, 255));
LOW_THR.SETPAINTINGSTRATEGY(PAINTINGSTRATEGY.POINTS);

##############################################
 
Last edited by a moderator:
Solution
@Dupre It turns out that conditional study orders cannot access your portfolio as a normal chart study can. That breaks all functions like GetAveragePrice() if you try to use it in a buy/sell order. What I ended up doing is getting the highest price in the last 3 days and subtracting from that. Hope this helps. :)
Have you tried something like this?

Code:
plot ProfitTarget = target;
ProfitTarget.SetPaintingStrategy(PaintingStrategy.Horizontal);
ProfitTarget.SetDefaultColor(Color.Green);

Could do a

Code:
def target = if condition then EntryPrice() + ATR() * profit_mult else DoubleNaN;

in front of the plot as well.
 

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

@BenTen and ALL....Hope you had a good weekend...I am trying to make a strategy that will close when it hits stoploss at 5% of buy price or the close "criteria" I have set for it. Is there a code on here I can look through or do you know how to code that? I can't get the close signal on my strategy to show when I set both criteria...
I don't know what the correct term is or what the buy order price is to be able to input into the Sell order.
ex.
AddOrder(OrderType.SELL_AUTO, close <= (close-.05) or MA_Max , open, Shares, name = "Sell", tickcolor = Color.RED, arrowcolor = Color.RED);

Thanks for any help!
 
@kmg526 If you don't mind running multiple exit strategies, which is a perfectly legitimate way to handle this, there's built-in StopLossLX and StopLossSX strategies the are based on percent by default. If you're running multiple exit strategies, whichever one qualifies first will exit the position and the other(s) will never fire.

Aside from that, close <= (close-.05) will never be true. close can't be less than close minus something.
 
Hello everyone. I am trying to code a simple 7% stop loss in think script. I have this code working just fine on a chart. But it depends on "GetAveragePrice()" to get the start price of the position. Then calculates the stop from that. Apparently, that is an "unknown function" when you try to use the custom study in a conditional order. So, how do you get the starting price if you cannot use GetAveragePrice()? This code is just an example that displays the calculations in a label, but you can get the idea.

After a good bit of looking, I am not sure it is possible. But it sure would be nice.

Code:
############################################
#  STOP LOSS
############################################
def averagePrice = RoundUp(GetAveragePrice());
input stopLoss = 0.07; # 0.07 is 7%.  .10 is 10%.
def stopLossPrice = averagePrice - (averagePrice * stopLoss);
def sellTrigger = close <= stopLossPrice;
AddLabel(1, "Purchased: $" + averagePrice + "  Stop Loss: " + (stopLoss * 100) + "%  Exit at: " + stopLossPrice + " Sell Trigger: " + sellTrigger,
if averagePrice > stopLossPrice then Color.GREEN else if averagePrice < stopLossPrice then Color.ORANGE else Color.WHITE);
############################################
 
Last edited:
@Dupre It turns out that conditional study orders cannot access your portfolio as a normal chart study can. That breaks all functions like GetAveragePrice() if you try to use it in a buy/sell order. What I ended up doing is getting the highest price in the last 3 days and subtracting from that. Hope this helps. :)
 
Solution
Hello everyone. I am trying to code a simple 7% stop loss in think script. I have this code working just fine on a chart. But it depends on "GetAveragePrice()" to get the start price of the position. Then calculates the stop from that. Apparently, that is an "unknown function" when you try to use the custom study in a conditional order. So, how do you get the starting price if you cannot use GetAveragePrice()? This code is just an example that displays the calculations in a label, but you can get the idea.

After a good bit of looking, I am not sure it is possible. But it sure would be nice.

Code:
############################################
#  STOP LOSS
############################################
def averagePrice = RoundUp(GetAveragePrice());
input stopLoss = 0.07; # 0.07 is 7%.  .10 is 10%.
def stopLossPrice = averagePrice - (averagePrice * stopLoss);
def sellTrigger = close <= stopLossPrice;
AddLabel(1, "Purchased: $" + averagePrice + "  Stop Loss: " + (stopLoss * 100) + "%  Exit at: " + stopLossPrice + " Sell Trigger: " + sellTrigger,
if averagePrice > stopLossPrice then Color.GREEN else if averagePrice < stopLossPrice then Color.ORANGE else Color.WHITE);
############################################
you could help me add an alert to this condition, thank you very much
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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