andypartridge47

Swings v3

Jan 1st, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. //@version=5
  2. strategy("Swings", overlay=true)
  3. import andyepartridge/utils/3 as utils
  4.  
  5. pivotLength = input.int(5)
  6.  
  7. pivotHigh = ta.pivothigh(close, pivotLength, pivotLength)
  8. pivotLow = ta.pivotlow(close, pivotLength, pivotLength)
  9.  
  10. var float lastPivotHigh = na
  11. var float lastPivotLow = na
  12.  
  13. var line stopLossLine = na
  14. var line takeProfitLine = na
  15.  
  16. // Add stop loss and take profit to keep track of
  17. var float stopLoss = na
  18. var float takeProfit = na
  19.  
  20. // If no position, reset the stop loss and take profit
  21. if strategy.position_size == 0
  22. stopLoss := na
  23. takeProfit := na
  24.  
  25. // Update last pivot high and low
  26. if not na(pivotHigh)
  27. lastPivotHigh := pivotHigh
  28.  
  29. if not na(pivotLow)
  30. lastPivotLow := pivotLow
  31.  
  32. // Enter and set the exit for short positions
  33. if strategy.position_size == 0 and not na(lastPivotHigh) and not na(lastPivotLow)
  34. if close < lastPivotLow
  35. strategy.entry("Short", strategy.short)
  36. stopLoss := lastPivotHigh
  37. takeProfit := lastPivotLow
  38. strategy.exit("TP / SL", "Short", stop=stopLoss, limit=takeProfit)
  39.  
  40. // Move the trailing stop for short positions
  41. if strategy.position_size < 0 and not na(lastPivotHigh) and pivotHigh < lastPivotHigh
  42. stopLoss := pivotHigh
  43. strategy.exit("TP / SL", "Short", stop=stopLoss, limit=takeProfit)
  44.  
  45. // Enter and set the exit for long positions
  46. if strategy.position_size == 0 and not na(lastPivotHigh) and not na(lastPivotLow)
  47. if close > lastPivotHigh
  48. strategy.entry("Long", strategy.long)
  49. stopLoss := lastPivotLow
  50. takeProfit := lastPivotHigh
  51. strategy.exit("TP / SL", "Long", stop=stopLoss, limit=takeProfit)
  52.  
  53. // Move the trailing stop for long positions
  54. if strategy.position_size > 0 and not na(lastPivotLow) and pivotLow > lastPivotLow
  55. stopLoss := pivotLow
  56. strategy.exit("TP / SL", "Long", stop=stopLoss, limit=takeProfit)
  57.  
  58. // Plot the stop loss and take profit
  59. plot(strategy.position_size != 0 ? stopLoss : na, title="SL", style=plot.style_linebr, color=color.red)
  60. plot(strategy.position_size != 0 ? takeProfit : na, title="TP", style=plot.style_linebr, color=color.green)
  61.  
  62. utils.DrawDailyTable()
Advertisement
Add Comment
Please, Sign In to add comment