Advertisement
andypartridge47

BTC 101 Michaels Bands Back to Basics

Jan 7th, 2025
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 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 ema6 > ema200
  14. shortCondition = ema3 < ema6 and ema6 < 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.  
  25. // Trailing stop percentage input
  26. trailingStopPercent = input.float(2, title="Trailing Stop (in %)", minval=0.1) / 100
  27.  
  28. // Entry logic with stop loss tracking
  29. if longCondition and confirmedBreakAway
  30. bandColor := color.green
  31. strategy.entry('Long', strategy.long)
  32. highestPrice := high
  33. highestPrice == stopLossLevel
  34.  
  35. if shortCondition and confirmedBreakAway
  36. bandColor := color.red
  37. strategy.entry('Short', strategy.short)
  38. lowestPrice := low
  39. lowestPrice == stopLossLevel
  40.  
  41. // Update trailing stop for long positions
  42. if (strategy.position_size > 0)
  43. highestPrice := na(highestPrice) ? high : math.max(highestPrice, high)
  44. stopLossLevel := highestPrice * (1 - trailingStopPercent)
  45. strategy.exit('Trailing Stop', 'Long', stop=stopLossLevel)
  46.  
  47. // Update trailing stop for short positions
  48. if (strategy.position_size < 0)
  49. lowestPrice := na(lowestPrice) ? low : math.min(lowestPrice, low)
  50. stopLossLevel := lowestPrice * (1 + trailingStopPercent)
  51. strategy.exit('Trailing Stop', 'Short', stop=stopLossLevel)
  52.  
  53. // Plot stop loss level
  54. plot(stopLossLevel, title="Stop Loss Level", color=color.red, linewidth=2, style=plot.style_line)
  55.  
  56. // Plots
  57. plot(ema3, color=bandColor, linewidth=1)
  58. plot(ema6, color=bandColor, linewidth=3)
  59. plot(ema200, color=color.blue, linewidth=2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement