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. :)

RickAns

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

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

Well-known 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.
 

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

ChristianS

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

Top