I’ve read the documentation over and over again. To my understanding we should be able to place partial take profits / multiple exits using the strategy.exit function for a single strategy.entry. This is a very simplified version of what is not working for me. The simple logic is;
(1) Enter Trade
(2) Set 1 x stop order and 2 x limit orders (a stop loss and 2 take profit levels)
(3) If take profit 1 is hit, close 50% of the open position.
(4) if take profit 2 or the stop loss is hit, close 100% of the open position.
I structured the orders as below. I’ve include the basic variables that were used to calculated stop and targets for clarity (sorry if its too much info). Only the SL ‘stop’ strategy.exit order is being executed in the code below. If I combine TP2 (limit) and SL (stop) order under the one strategy.exit, both these orders will execute, but then TP1(limit) will not. Any help is so much appreciated.
//@version=5
strategy("My strategy", overlay=true, initial_capital = 10000, currency = currency.AUD, use_bar_magnifier = true, close_entries_rule = "ANY")
LONG_STOP = close - (close * 0.02)
SHORT_STOP = close + (close * 0.02)
LONG_TP1 = close + (close * 0.02) * 1
LONG_TP2 = close + (close * 0.02) * 2
SHORT_TP1 = close - (close * 0.02) * 1
SHORT_TP2 = close - (close * 0.02) * 2
var SL = 0.0
var TP1 = 0.0
var TP2 = 0.0
longCondition = ta.crossover(ta.sma(close, 21), ta.sma(close, 50))
shortCondition = ta.crossunder(ta.sma(close, 21), ta.sma(close, 50))
if longCondition and strategy.position_size == 0
SL := LONG_STOP
TP1 := LONG_TP1
TP2 := LONG_TP2
strategy.entry("Long", strategy.long)
strategy.exit(id = "Long SL", from_entry = "Long", stop = SL, qty_percent = 100)
strategy.exit(id = "Long TP1", from_entry = "Long", limit = TP1, qty_percent = 50)
strategy.exit(id = "Long TP2", from_entry = "Long", limit = TP2, qty_percent = 100)
if shortCondition and strategy.position_size == 0
SL := SHORT_STOP
TP1 := SHORT_TP1
TP2 := SHORT_TP2
strategy.entry("Short", strategy.short)
strategy.exit(id = "Short SL", from_entry = "Short", stop = SL, qty_percent = 100)
strategy.exit(id = "Short TP1", from_entry = "Short", limit = TP1, qty_percent = 50)
strategy.exit(id = "Short TP2", from_entry = "Short", limit = TP2, qty_percent = 100)
plot(strategy.position_size != 0 ? SL : na, title = "SL", color = color.red, style = plot.style_linebr)
plot(strategy.position_size != 0 ? TP1 : na, title = "TP", color = color.green, style = plot.style_linebr)
plot(strategy.position_size != 0 ? TP2 : na, title = "TP", color = color.purple, style = plot.style_linebr)`
Mitchell Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.