Advertisement
andypartridge47

Combined EMA Strat

Oct 21st, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. //@version=5
  2. strategy("POPCAT EMA Crossover Strategy", overlay=true, calc_on_every_tick=true)
  3.  
  4. // Input for stop loss and take profit percentages
  5. stopLossPercentage = input.float(3, "Stop Loss Percentage") * 0.01
  6. takeProfitPercentage = input.float(3, "Take Profit Percentage") * 0.01
  7.  
  8. // EMA settings
  9. emaFastPeriod = 12
  10. emaSlowPeriod = 21
  11. emaFast = ta.ema(close, emaFastPeriod)
  12. emaSlow = ta.ema(close, emaSlowPeriod)
  13.  
  14. // Entry amount input
  15. entryAmount = input.float(10, title="Entry Amount", minval=10)
  16.  
  17. // Variables for stop loss and take profit lines
  18. var line stopLossLine = na
  19. var line takeProfitLine = na
  20.  
  21. // Reset lines if no position
  22. if strategy.position_size == 0
  23. stopLossLine := na
  24. takeProfitLine := na
  25. else
  26. stopLossLine.set_x2(bar_index)
  27. takeProfitLine.set_x2(bar_index)
  28.  
  29. // Buy condition
  30. if ta.crossover(emaFast, emaSlow) and strategy.position_size <= 0
  31. stopLoss = close * (1 - stopLossPercentage)
  32. takeProfit = close * (1 + takeProfitPercentage)
  33. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  34. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  35. strategy.entry("Long", strategy.long, qty=entryAmount)
  36. strategy.exit("SL / TP", "Long", stop=stopLoss, limit=takeProfit)
  37.  
  38. // Sell condition
  39. if ta.crossunder(emaFast, emaSlow) and strategy.position_size >= 0
  40. stopLoss = close * (1 + stopLossPercentage)
  41. takeProfit = close * (1 - takeProfitPercentage)
  42. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  43. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  44. strategy.entry("Short", strategy.short, qty=entryAmount)
  45. strategy.exit("SL / TP", "Short", stop=stopLoss, limit=takeProfit)
  46.  
  47. // Plot EMAs on chart
  48. bandColor = emaFast > emaSlow ? color.green : color.red
  49. plotFast = plot(emaFast, color=bandColor, linewidth=1, title="EMA 12")
  50. plotSlow = plot(emaSlow, color=bandColor, linewidth=3, title="EMA 21")
  51.  
  52. // Fill the area between the EMAs with 25% opacity
  53. fill(plotFast, plotSlow, color=emaFast > emaSlow ? color.new(color.green, 75) : color.new(color.red, 75))
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement