Bekebu

Lesson 4.4

Oct 21st, 2024 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | Cryptocurrency | 0 0
  1. //@version=5
  2. strategy("Michaels Bands SL", overlay=true)
  3.  
  4. stopLossPercentage = input.float(3, "Stop Loss Percentage") * 0.01
  5. takeProfitPercentage = input.float(3, "Take Profit Percentage") * 0.01
  6.  
  7. ema12 = ta.ema(close, 12)
  8. ema21 = ta.ema(close, 21)
  9.  
  10. var line stopLossLine = na
  11. var line takeProfitLine = na
  12.  
  13. if strategy.position_size == 0 // If we don't have a position we reset the lines to na
  14. stopLossLine := na
  15. takeProfitLine := na
  16. else
  17. stopLossLine.set_x2(bar_index) // If we do have a position we extend the lines to the next bar
  18. takeProfitLine.set_x2(bar_index)
  19.  
  20. if ta.crossover(ema12, ema21) and strategy.position_size <= 0
  21. stopLoss = close * (1 - stopLossPercentage)
  22.  
  23. takeProfit = close * (1 + takeProfitPercentage)
  24.  
  25. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  26. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  27.  
  28. strategy.entry("Long", strategy.long)
  29. strategy.exit("SL / TP", "Long", stop=stopLoss, limit=takeProfit)
  30.  
  31. if ta.crossunder(ema12, ema21) and strategy.position_size >= 0
  32. stopLoss = close * (1 + stopLossPercentage)
  33.  
  34. takeProfit = close * (1 - takeProfitPercentage)
  35.  
  36. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  37. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  38.  
  39. strategy.entry("Short", strategy.short)
  40. strategy.exit("SL / TP", "Short", stop=stopLoss, limit=takeProfit)
  41.  
  42.  
  43. bandColor = ema12 > ema21 ? color.green : color.red
  44. plot(ema12, color=bandColor, linewidth=1)
  45. plot(ema21, color=bandColor, linewidth=3)
Advertisement
Add Comment
Please, Sign In to add comment