Advertisement
BigHodler

EMA Trend Meter and SMI Strategy

Oct 27th, 2024
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | Cryptocurrency | 0 0
  1. //@version=5
  2. strategy("EMA Trend Meter and SMI Strategy", overlay=true, pyramiding=5, commission_type=strategy.commission.percent, commission_value=0.02, initial_capital=1000, default_qty_type = strategy.cash, default_qty_value = 1000)
  3.  
  4. // Inputs
  5. risk_ratio = input(2.0, title="Risk Management Ratio")
  6. prevent_short_below_ema = input(true, title="Prevent Short Trades Below EMA")
  7. prevent_long_above_ema = input(true, title="Prevent Long Trades Above EMA")
  8. ema_period_1 = input(13, title="EMA Period 1")
  9. ema_period_2 = input(21, title="EMA Period 2")
  10. ema_period_3 = input(34, title="EMA Period 3")
  11. ema_period_4 = input(55, title="EMA Period 4")
  12. ema_period_5 = input(89, title="EMA Period 5")
  13. smi_a = input(10, title="SMI Percent K Length")
  14. smi_b = input(3, title="SMI Percent D Length")
  15. smi_c = input(13, title="SMI EMA Signal Length")
  16. smi_smooth_period = input(5, title="SMI Smoothing Period")
  17. smi_ob = input(40, title="SMI Overbought")
  18. smi_os = input(-40, title="SMI Oversold")
  19.  
  20. // EMA Trend Meter
  21. // Uses Exponential Moving Averages (EMAs) to identify trends.
  22. // Modified default settings to make it less sensitive and only trigger signals when a strong trend is likely.
  23. ema1 = ta.ema(close, ema_period_1)
  24. ema2 = ta.ema(close, ema_period_2)
  25. ema3 = ta.ema(close, ema_period_3)
  26. ema4 = ta.ema(close, ema_period_4)
  27. ema5 = ta.ema(close, ema_period_5)
  28.  
  29. plot(ema1, color=color.green, title="EMA1")
  30. plot(ema2, color=color.green, title="EMA2")
  31. plot(ema3, color=color.green, title="EMA3")
  32. plot(ema4, color=color.green, title="EMA4")
  33. plot(ema5, color=color.green, title="EMA5")
  34.  
  35. // Stochastic Momentum Index (SMI)
  36. // Measures the momentum of price movements.
  37. // Uses a custom version by "surjithctl" with adjusted colors for better visualization.
  38. // EMA Signal length = 13
  39. a = smi_a // Percent K Length
  40. b = smi_b // Percent D Length
  41. c = smi_c // EMA Signal Length
  42. smooth_period = smi_smooth_period // Smoothing Period
  43. ob = smi_ob // Overbought
  44. os = smi_os // Oversold
  45.  
  46. // Range Calculation
  47. ll = ta.lowest(low, a)
  48. hh = ta.highest(high, a)
  49. diff = hh - ll
  50. rdiff = close - (hh+ll)/2
  51.  
  52. avgrel = ta.ema(rdiff, b)
  53. avgdiff = ta.ema(diff, b)
  54.  
  55. // SMI calculations
  56. SMI = avgdiff != 0 ? (avgrel/(avgdiff/2)*100) : 0.0
  57.  
  58. // Apply smoothing to SMI
  59. SMI_smoothed = ta.sma(SMI, smooth_period)
  60.  
  61. SMIsignal = ta.ema(SMI_smoothed, b)
  62. emasignal = ta.ema(SMI_smoothed, c)
  63.  
  64. // Plotting
  65. plot(SMI_smoothed, "Stochastic", color=color.black)
  66. plot(emasignal, "EMA", color=color.red)
  67.  
  68. h0 = hline(ob, "Overbought", color=color.gray)
  69. h1 = hline(os, "Oversold", color=color.gray)
  70.  
  71. level_ob = ob
  72. level_obsmi = SMI_smoothed > level_ob ? SMI_smoothed : level_ob
  73.  
  74. level_os = os
  75. level_ossmi = SMI_smoothed < level_os ? SMI_smoothed : level_os
  76.  
  77. p1 = plot(level_ob, display=display.none)
  78. p2 = plot(level_obsmi, display=display.none)
  79.  
  80. p3 = plot(level_os, display=display.none)
  81. p4 = plot(level_ossmi, display=display.none)
  82.  
  83. fill(p1, p2, color=color.new(color.red, 60), title='OverBought')
  84. fill(p3, p4, color=color.new(color.green, 60), title='OverSold')
  85.  
  86. // Long Entry Signal
  87. // All lines of the EMA Trend Meter turn green.
  88. // SMI white line dips below the blue line (potential pullback).
  89. // SMI white line crosses above the red line (bullish momentum).
  90. longEntry = ta.crossover(SMI_smoothed, emasignal) and ema1 > ema2 and ema2 > ema3 and ema3 > ema4 and ema4 > ema5
  91. if (prevent_long_above_ema)
  92. longEntry := longEntry and close < ema5
  93.  
  94. // Short Entry Signal
  95. // All lines of the EMA Trend Meter turn red.
  96. // SMI white line goes above the blue line.
  97. // SMI white line crosses below the red line (bearish momentum).
  98. shortEntry = ta.crossunder(SMI_smoothed, emasignal) and ema1 < ema2 and ema2 < ema3 and ema3 < ema4 and ema4 < ema5
  99. if (prevent_short_below_ema)
  100. shortEntry := shortEntry and close > ema5
  101.  
  102. // Risk Management
  103. // Use a 2:1 risk-to-reward ratio for take profit and stop loss levels.
  104. longStop = longEntry ? low - syminfo.mintick * 2 : na
  105. longTakeProfit = longEntry ? low + risk_ratio * (low - low[1]) : na
  106. shortStop = shortEntry ? high + syminfo.mintick * 2 : na
  107. shortTakeProfit = shortEntry ? high - risk_ratio * (high[1] - high) : na
  108.  
  109. // Strategy Entry and Exit
  110. if (longEntry)
  111. strategy.entry("Long", strategy.long)
  112.  
  113. if (shortEntry)
  114. strategy.entry("Short", strategy.short)
  115.  
  116. if (strategy.position_size > 0)
  117. strategy.exit("Exit Long", "Long", stop=longStop, limit=longTakeProfit)
  118.  
  119. if (strategy.position_size < 0)
  120. strategy.exit("Exit Short", "Short", stop=shortStop, limit=shortTakeProfit)
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement