Search code examples
pine-scripttrading

PinesCript v5 problem with exit orders only reaching first target and rest are ignored by strategy


I am having a trouble with this "Seems to be great structured" way to place the entry and stop loss orders in pinescript v5 but I can't get the point in which just tps are filled normally. Let me share my approach and you can use it if you want on your strategy too (when fixed xD)

The unexpected behaviour is just that. prices far from tp1 avobe tp3, tp4 are reached and the strategy does not paint them as TP1 with its comment (another important point)

stopLossValue = 0.0
trailValue = 0.0

if strategy.position_size > 0
    stopLossValue := strategy.position_avg_price * (1 - max_risk_percentage)
    // trailValue := localHigh * (1 - max_risk_percentage)
else if strategy.position_size < 0
    stopLossValue := strategy.position_avg_price * (1 + max_risk_percentage)
    // trailValue := localLow * (1 + max_risk_percentage)

// strategy.position_size > 0 ? math.max(stopLossValue, trailValue) : strategy.position_size < 0 ? math.min(stopLossValue, trailValue) : na

takeProfitTarget1 = strategy.position_size > 0 ? strategy.position_avg_price * (1 + takeProfit1) : strategy.position_size < 0 ? strategy.position_avg_price * (1 - takeProfit1) : na
takeProfitTarget2 = strategy.position_size > 0 ? strategy.position_avg_price * (1 + takeProfit2) : strategy.position_size < 0 ? strategy.position_avg_price * (1 - takeProfit2) : na
takeProfitTarget3 = strategy.position_size > 0 ? strategy.position_avg_price * (1 + takeProfit3) : strategy.position_size < 0 ? strategy.position_avg_price * (1 - takeProfit3) : na
takeProfitTarget4 = strategy.position_size > 0 ? strategy.position_avg_price * (1 + takeProfit4) : strategy.position_size < 0 ? strategy.position_avg_price * (1 - takeProfit4) : na
takeProfitTarget5 = strategy.position_size > 0 ? strategy.position_avg_price * (1 + takeProfit5) : strategy.position_size < 0 ? strategy.position_avg_price * (1 - takeProfit5) : na

trail_price = 0.0
if startTrailingAtTP == 1
    trail_price := takeProfitTarget1
else if startTrailingAtTP == 2
    trail_price := takeProfitTarget2
else if startTrailingAtTP == 3
    trail_price := takeProfitTarget3
else if startTrailingAtTP == 4
    trail_price := takeProfitTarget4
else if startTrailingAtTP == 5
    trail_price := takeProfitTarget5

direction = ""
fromEntry = ""

if strategy.position_size > 0
    direction := "Long"
    fromEntry := "Long"
else if strategy.position_size < 0
    direction := "Short"
    fromEntry := "Short"
    
if strategy.position_size != 0
    strategy.exit(direction, from_entry = fromEntry, qty_percent = takeProfitPercent5, limit = takeProfitTarget5, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP5", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")
    strategy.exit(direction, from_entry = fromEntry, qty_percent = takeProfitPercent4, limit = takeProfitTarget4, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP4", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")
    strategy.exit(direction, from_entry = fromEntry, qty_percent = takeProfitPercent3, limit = takeProfitTarget3, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP3", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")
    strategy.exit(direction, from_entry = fromEntry, qty_percent = takeProfitPercent2, limit = takeProfitTarget2, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP2", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")
    strategy.exit(direction, from_entry = fromEntry, qty_percent = takeProfitPercent1, limit = takeProfitTarget1, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP1", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")
   

Solution

  • You should give unique ids to your exit orders otherwise you will modify the existing order.

    You are using direction as id which will be the same for all those 5 exits.

    Something like below should do it (changed the id to Exit-n):

    if strategy.position_size != 0
        strategy.exit("Exit-5", from_entry = fromEntry, qty_percent = takeProfitPercent5, limit = takeProfitTarget5, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP5", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")
        strategy.exit("Exit-4", from_entry = fromEntry, qty_percent = takeProfitPercent4, limit = takeProfitTarget4, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP4", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")
        strategy.exit("Exit-3", from_entry = fromEntry, qty_percent = takeProfitPercent3, limit = takeProfitTarget3, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP3", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")
        strategy.exit("Exit-2", from_entry = fromEntry, qty_percent = takeProfitPercent2, limit = takeProfitTarget2, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP2", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")
        strategy.exit("Exit-1", from_entry = fromEntry, qty_percent = takeProfitPercent1, limit = takeProfitTarget1, stop = stopLossValue, trail_price = trail_price, comment_profit = "TP1", trail_points = trailingPips, trail_offset = trailingOffset, comment_trailing = "Trailing Stop", comment_loss = "Stop Loss")