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:

RickAns

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

RickAns

Active member
VIP
KRHills, I used the variable names from what was originaly posted. If yours are different then you would need to change what I posted to match yours. Plus I do not know what their 'condition' might be since it is condition = condition in the first line.

What I posted would basically plot a horizontal line at their 'target' price. The other is if their condition is true then use this as target.

Give me some more info to work with and I will help as I can.
 

rad14733

Well-known member
VIP
Hi, Is that possible to have addOrder created something like this:
Qty 100, TRG +5 Limit, Mark -2 TrailStop, GTC
AddOrder() is very limited in features as it is merely a tool for backtesting potential trading strategies... It is not a panacea for actual real world trading... However, you can use multiple Buy and Sell AddOrder() scenarios to achieve your desired overall trade logic goals... All you can do is play with your logic and see what happens... But remember, what you see afterhours will not be the same as how realtime trading plays out... And TIF orders are not supported for backtesting...
 

kmg526

Member
VIP
@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!
 

Slippage

Active member
@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.
 

kmg526

Member
VIP
@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.
Oh, good news thank you for the info! You da man!
 

Sparrow

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

Sparrow

New member
VIP
@rad14733 When I try to add EntryPrice() to a label in a study and to a conditional order, it just returns N/A. So I am guessing that is not going to work either. I will contact support during the week and see what I can find. Thanks :)

Code:
AddLabel(1, "Entry Price: " + entryPrice(), Color.WHITE);
 

Similar threads

Top