addorder trailing stop or exit order when order pl hits trigger

Z

zmike

New member
Hello All,
Thank you for your time. I've got code that will enter an order using AddOrder, I want the order to exit once I've hit a stop or take profit once I've hit a limit. But all I've found is getting out on some other indicator like the candles position against a MA or something. Also I cannot create my open order with a 'trailing stop' type which would be similar.

This is what I tried as an exit, but it only exits on the MA (last line). Also I cant add an AddOrder inside an if statement so that does not help.

Basically how can I get pl of an order I just entered ?

Code:
# sell if you can make 100
AddOrder(OrderType.BUY_TO_CLOSE,GetOpenPL()> 100, open[-1], 100, Color.ORANGE, Color.BLUE);
# sell if you will loose 50
AddOrder(OrderType.BUY_TO_CLOSE,GetOpenPL() < -50, open[-1], 100, Color.ORANGE, Color.BLUE);
# get out of short position when first candle above moving average

AddOrder(OrderType.BUY_TO_CLOSE, low > AVG1 , open[-1], 100, Color.RED, Color.RED);
Can anyone tell me what the -1 means in open[-1] ? Does it mean the open of the next candle ?

Thanks !
 
BenTen

BenTen

Administrative
Staff
VIP
I have seen that being used by a few of our members on here as well. Not sure the difference. But I use close instead of open[-1].
 
R

RickAns

Member
VIP
Does this help?

https://tlc.thinkpipes.com/center/charting/thinkscript/reference/Functions/Others/EntryPrice.html
EntryPrice();

Description
Returns the price of the entry order. For several entry orders in the same direction as the currently held position the function returns the average price for all of them.

Example
AddOrder(OrderType.SELL_TO_CLOSE, close > EntryPrice() + 3 or close < EntryPrice() - 9);

Adds a Sell order for closing a long position when the Close price is either greater than the entry price by 3 (for taking profits) or less by 9 (for safety).
 
Z

zmike

New member
Wow that looks good, thanks I'll try it later when I get home
 
R

RickAns

Member
VIP
Glad to help, hope it works. :)

I thought the EntryPrice() + 'Whatever your limit number' would be handy for the stop exit.

Believe I read somewhere (but not verified) that numbers or percentages could be used for something like that. Such as up 100 points or up 50% from said price. Of course it was from another page with different examples which I cannot find at the moment. Also may be confusing two separate things completely. Been reading through a lot of stuff trying to learn.
 
Last edited:
Z

zmike

New member
Hi RickAns,
Thanks yes it worked exactly as expected. So you can compare the current price with the price at which you entered. So lets say you bought when the symbol was at 500 then you could sell when the symbol gets to $5 more than your entry or $5 less and so on. In backtesting it proved to be a very poor strategy for me to implement.
Code:
# if goes up $5 or goes down $3,
AddOrder(OrderType.SELL_TO_CLOSE, close < (EntryPrice()-3) or close > (EntryPrice()+5) , open[0], 100, Color.RED, Color.RED);
as far as what is open[0], open[1] and open[-1], I think 0 is the current candle, 1 is the previous candle and -1 is the next candle, but I'm not totally sure on that.
 

Similar threads

Top