Reputation: 660
Here is some of my pinescript code for my exit strategies:
// Strategy exit
// Long Exits
strategy.exit("Long TP1", "Long", profit=close*0.02/syminfo.mintick, qty_percent=20)
strategy.exit("Long TP2", "Long", profit=close*0.04/syminfo.mintick, qty_percent=25)
strategy.exit("Long TP3", "Long", profit=close*0.06/syminfo.mintick, qty_percent=33)
strategy.exit("Long TP4", "Long", profit=close*0.08/syminfo.mintick, qty_percent=50)
strategy.exit("Long exit", "Long", stop=long_stop_price, qty_percent=100)
// Short Exits
strategy.exit("Short TP1", "Short", profit=close*0.02/syminfo.mintick, qty_percent=20)
strategy.exit("Short TP2", "Short", profit=close*0.04/syminfo.mintick, qty_percent=25)
strategy.exit("Short TP3", "Short", profit=close*0.06/syminfo.mintick, qty_percent=33)
strategy.exit("Short TP4", "Short", profit=close*0.08/syminfo.mintick, qty_percent=50)
strategy.exit("Short exit", "Short", stop=short_stop_price, qty_percent=100)
The goal is to exit at multiple take profit levels (2%, 4%, 6%, 8%, then let the trade carry on until it hits the trailing stop loss). I want to have a trailing stop loss behind each of these take profit levels. My issue is that it seems like the strategy.exit
functions are executing in the order that they are written, meaning that the trailing stop loss function (with the ID "Long exit" and "Short exit") are never able to execute unless all take profit levels are reached. This is obviously not how I want the script to run. How do I get round this execution order problem?
(My trailing stop loss does work. I have tested it using a single strategy.exit
with the profit
and loss
parameters)
Upvotes: 1
Views: 2514
Reputation: 1714
The concept of strategy.exit
is the "guardian" for some part of entry/position (fom 1% to 100%). You have made five "guardians" with 20, 25, 33, 50, 100 % from the "Long" entry. If "Long" entry have 100 contracts, then the "guardians" will protect: 20, 25, 33, 22, 0 contracts repective. I.e. for last one guardian there is no any qty.
You can try pass stop=long_stop_price
to each "guardian":
strategy.exit("Long TP1", "Long", profit=close*0.02/syminfo.mintick, stop=long_stop_price, qty_percent=20)
strategy.exit("Long TP2", "Long", profit=close*0.04/syminfo.mintick, stop=long_stop_price, qty_percent=25)
strategy.exit("Long TP3", "Long", profit=close*0.06/syminfo.mintick, stop=long_stop_price, qty_percent=33)
strategy.exit("Long TP4", "Long", profit=close*0.08/syminfo.mintick, stop=long_stop_price, qty_percent=50)
Upvotes: 2