Reputation: 1752
I use Pinescript v6 ( it looks to work on v5 )
I have an entry, I setup my stop loss and my take profit in a strategy.exit function. But after a few candles, I want to modify the limit price, so I created 2 differents exit with 2 differents ID so I can change the take profit price without touching the stop loss price :
if long_condition and barstate.isconfirmed and strategy.opentrades == 0
strategy.entry(id = "BUY", comment = str.tostring(entry_price), direction = strategy.long)
strategy.exit(id = "Stop", from_entry = "BUY", loss = stop_loss_in_ticks, stop= stopLoss, comment_loss = "SL Long")
strategy.exit(id = "Profit", from_entry = "BUY", profit = take_profit_in_ticks, limit = takeProfit, comment_profit = "TP Long")
But it doesn't work, only the first exit is working, in this case the stop loss work, but the take profit never happened. If I switch the exits, it's the opposite.
If I do it in one line, it work but I can't change the take profit afterwards because it will delete my stop loss :
strategy.exit(id = "ExitLong", from_entry = "BUY", profit = take_profit_in_ticks, loss = stop_loss_in_ticks, comment_loss = "SL Long", comment_profit = "TP Long")
How do I fix that ? thanks
Edit :
Here is a sample of what I try to do. Stategy is buy at lower bollinger, and sell at upper bollinger. At each candle I need to update the take profit, but the stop loss need to remain the same :
//@version=6
strategy("Example", overlay=true)
[basis, upper, lower] = ta.bb(close, 20, 2)
// Plot Bollinger Bands
plot(basis, "Bollinger Basis", color=color.blue)
plot(upper, "Bollinger Upper", color=color.red)
plot(lower, "Bollinger Lower", color=color.green)
bool long_condition = close < lower
if long_condition and strategy.opentrades == 0
strategy.entry("BUY", strategy.long)
takeProfit = (upper - close) / syminfo.mintick // Take profit when price reach upper band
stopLoss = (close * 0.01) / syminfo.mintick // Stop loss 1% lower than current price
strategy.exit("Exit", "BUY", profit = takeProfit, loss = stopLoss, comment_loss = "SL", comment_profit = "TP")
// And now, how do I update my takeProfit depending of upper bollinger ?
// If I do something like that, it will remove the stop loss
if strategy.opentrades > 0
strategy.exit("Exit", "BUY", profit = (upper - close) / syminfo.mintick)
And that's why I tried to split the strategy.exit in 2 differents strategy.exit, one for take profit and the other for stop loss.
Upvotes: 2
Views: 107