I am entering a trade...
if (enterLong and strategy.position_size == 0)
strategy.entry("Long", true)
This works as expected and enters when I want it to. But from here I would like to have a couple different exits:
- If hit the INITIAL STOP -> Get out of the trade 100%
- If hit the FIRST TAKE -> Get 50% out of the trade, move the STOP to break even, and move the SECOND TAKE to new condition
Currently, I have a sort of working version of this...
if (strategy.position_size > 0 and strategy.position_size == initial_size)
strategy.exit("LT1", limit=longTakePrice, qty_percent=50)
strategy.close("Long", when=low<longStopPrice)
else
if (strategy.position_size > 0 and strategy.position_size < initial_size)
strategy.exit("LS2", stop=entryPrice)
strategy.close("Long", when = takeCondition)
This mostly works, except the close()
functions don't trigger until the next candle (instead of when the condition is met), and usually has significant (unrealistic) slippage.
I've tried the following...
if (strategy.position_size > 0 and strategy.position_size == initial_size)
strategy.exit("LT1", limit=longTakePrice, qty_percent=50)
strategy.exit("LS1", stop=longStopPrice)
But this doesn't work. Instead, when LS1
is hit, it only sells 50% of the position. Swapping the order of the statements doesn't work either.
Edit 1:
It makes no difference to add qty_percent=100
to the stop exit.
question from:
https://stackoverflow.com/questions/65947929/pine-script-strategy-how-to-set-a-simultaneous-stop-exit-for-100-and-take-exi 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…