Advertisement
Guest User

position logic pine script 77991257

a guest
Mar 4th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. // Logic while in positions
  2. if strategy.position_size > 0
  3. trade_entry_price := strategy.opentrades.entry_price(0)
  4. longStopPrice := trade_entry_price * (1 - longSL)
  5. longTakePrice := trade_entry_price * (1 + longTP)
  6. strategy.exit("Exit", from_entry = "LONG", stop = longStopPrice, comment = "Long Stop Loss " + str.tostring(longStopPrice) + " / " + str.tostring(trade_entry_price))
  7. if trail_longTP == 0.0
  8. if high > longTakePrice
  9. trail_longTP := high * (1 - longTO)
  10. strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit, on first calc")
  11. else
  12. if (high * (1 - longTO)) > trail_longTP
  13. b_long_to_tp_set := true
  14. trail_longTP := high * (1 - longTO)
  15. strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit, on calc")
  16. else if high[1] > high or close < trail_longTP
  17. strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit, close < TO")
  18.  
  19. else if strategy.position_size < 0
  20. trade_entry_price := strategy.opentrades.entry_price(0)
  21. shortStopPrice := trade_entry_price * (1 + shortSL)
  22. shortTakePrice := trade_entry_price * (1 - shortTP)
  23. strategy.exit("Exit", from_entry = "SHORT", stop = shortStopPrice, comment = "Short Stop Loss " + str.tostring(shortStopPrice) + " / " + str.tostring(trade_entry_price))
  24. if trail_shortTP == 0.0
  25. if low < shortTakePrice
  26. trail_shortTP := low * (1 + shortTO)
  27. strategy.exit("Exit", from_entry = "SHORT", stop = trail_shortTP, comment = "Short Trail TP Hit, on first calc")
  28. else
  29. if low < low[1] and (low * (1 + shortTO)) < trail_shortTP
  30. trail_shortTP := low * (1 + shortTO)
  31. b_short_to_tp_set := false
  32. strategy.exit("Exit", from_entry = "SHORT", stop = trail_shortTP, comment = "Short Trail TP Hit, on calc")
  33. else if low[1] < low or close > trail_shortTP
  34. strategy.exit("Exit", from_entry = "SHORT", stop = trail_shortTP, comment = "Short Trail TP Hit, close > TP")
  35.  
  36. else if strategy.position_size == 0
  37. trail_shortTP := 0.0
  38. trail_longTP := 0.0
  39. shortStopPrice := 0.0
  40. longStopPrice := 0.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement