Advertisement
andypartridge47

EMA 3, 6, 200 with trailing stop loss

Jan 6th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. //@version=6
  2. strategy('BTC 101 Michaels Bands Back to Basics', overlay=true)
  3.  
  4. // Averages
  5. ema3 = ta.ema(close, 3)
  6. ema6 = ta.ema(close, 6)
  7. ema200 = ta.ema(close, 200)
  8.  
  9. // Declare bandColor variable
  10. var color bandColor = na
  11.  
  12. // Entry conditions
  13. longCondition = ema3 > ema6 and close > ema200
  14. shortCondition = ema3 < ema6 and close < ema200
  15.  
  16. // Confirmed break away from EMA 200 by 1%
  17. breakAwayThreshold = 0.01
  18. confirmedBreakAway = math.abs(close - ema200) / ema200 > breakAwayThreshold
  19.  
  20. // Variables to track stop loss levels
  21. var float highestPrice = na
  22. var float lowestPrice = na
  23. var float stopLossLevel = na
  24. var line stopLossLine = na
  25.  
  26. // Entry logic with stop loss tracking
  27. if longCondition and confirmedBreakAway
  28. bandColor := color.green
  29. strategy.entry('Long', strategy.long)
  30. highestPrice := close
  31. stopLossLevel := highestPrice * 0.98
  32. strategy.exit("Long Stop", "Long", stop=stopLossLevel)
  33. if not na(stopLossLine)
  34. line.delete(stopLossLine)
  35. stopLossLine := line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=2)
  36.  
  37. if shortCondition and confirmedBreakAway
  38. bandColor := color.red
  39. strategy.entry('Short', strategy.short)
  40. lowestPrice := close
  41. stopLossLevel := lowestPrice * 1.02
  42. strategy.exit("Short Stop", "Short", stop=stopLossLevel)
  43. if not na(stopLossLine)
  44. line.delete(stopLossLine)
  45. stopLossLine := line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=2)
  46.  
  47. // Update stop loss for existing positions
  48. if strategy.position_size > 0
  49. highestPrice := na(highestPrice) ? close : math.max(highestPrice, close)
  50. if close > highestPrice
  51. highestPrice := close
  52. stopLossLevel := highestPrice * 0.98
  53. strategy.exit("Long Stop", "Long", stop=stopLossLevel)
  54. if not na(stopLossLine)
  55. line.delete(stopLossLine)
  56. stopLossLine := line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=2)
  57.  
  58. if strategy.position_size < 0
  59. lowestPrice := na(lowestPrice) ? close : math.min(lowestPrice, close)
  60. if close < lowestPrice
  61. lowestPrice := close
  62. stopLossLevel := lowestPrice * 1.02
  63. strategy.exit("Short Stop", "Short", stop=stopLossLevel)
  64. if not na(stopLossLine)
  65. line.delete(stopLossLine)
  66. stopLossLine := line.new(bar_index, stopLossLevel, bar_index + 1, stopLossLevel, color=color.red, width=2)
  67.  
  68. // Plots
  69. plot(ema3, color=bandColor, linewidth=1)
  70. plot(ema6, color=bandColor, linewidth=3)
  71. plot(ema200, color=color.blue, linewidth=2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement