Advertisement
Guest User

EMA55v3

a guest
Jun 26th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. //@version=3
  2. strategy(title="55 EMA Trading Strategy", shorttitle="EMA55", overlay=true, default_qty_type=strategy.percent_of_equity, initial_capital=10000000000, default_qty_value=100, commission_value=0.15)
  3.  
  4. trailingStopLevelLong = na
  5. trailingStopLevelShort = na
  6.  
  7. sourceType = input("OHLC4 HA", title="Source type", options=["Close", "OHLC4", "OHLC4 HA"])
  8.  
  9. len1 = input(defval = 3, title = "Ema 1 length", minval = 1)
  10. len2 = input(defval = 10, title = "Ema 2 length", minval = 2)
  11. len3 = input(defval = 20, title = "Ema 3 length", minval = 3)
  12. len4 = input(defval = 55, title = "Ema 4 length", minval = 4)
  13. len5 = input(defval = 100, title = "Ema 5 length", minval = 2)
  14.  
  15. FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
  16. FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
  17. FromYear = input(defval = 2017, title = "From Year", minval = 2015)
  18. ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
  19. ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
  20. ToYear = input(defval = 9999, title = "To Year", minval = 2017)
  21.  
  22. start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
  23. finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
  24. window() => time >= start and time <= finish ? true : false
  25.  
  26. handleHA = heikinashi(tickerid)
  27. openHA = security(handleHA, period, open)
  28. highHA = security(handleHA, period, high)
  29. lowHA = security(handleHA, period, low)
  30. closeHA = security(handleHA, period, close)
  31.  
  32. src = sourceType == "Close" ? close : sourceType == "OHLC4" ? ohlc4 : (closeHA + highHA + lowHA + openHA) / 4
  33.  
  34. ma8 = ema(src, len1)
  35. ma13 = ema(src, len2)
  36. ma21 = ema(src, len3)
  37. ma55 = ema(src, len4)
  38. ma200 = ema(src, len5)
  39.  
  40. ma55crossover = crossover(ma21, ma55)
  41. ma55crossunder = crossunder(ma21, ma55)
  42. longEntry = ma8 > ma13 and ma21 > ma55 and (ma55crossover or ma55crossover[1] or ma55crossover[2] or ma55crossover[3] or ma55crossover[4]) and ma8 > ma200
  43. shortEntry = ma8 < ma13 and ma21 < ma55 and (ma55crossunder or ma55crossunder[1] or ma55crossunder[2] or ma55crossunder[3] or ma55crossunder[4]) and ma8 < ma200
  44. shortExit = ma8 > ma13 and ma21 > ma55 and (ma55crossover or ma55crossover[1] or ma55crossover[2] or ma55crossover[3] or ma55crossover[4])
  45. longExit = ma8 < ma13 and ma21 < ma55 and (ma55crossunder or ma55crossunder[1] or ma55crossunder[2] or ma55crossunder[3] or ma55crossunder[4])
  46.  
  47. transp1 = 60
  48. transp2 = 48
  49. transp3 = 36
  50. transp4 = 24
  51. transp5 = 12
  52. transp6 = 0
  53.  
  54. plot(ma8, color=aqua, transp=transp2, style=line, title="8", linewidth=1)
  55. plot(ma13, color=aqua, transp=transp3, style=line, title="13", linewidth=1)
  56. plot(ma21, color=aqua, transp=transp4, style=line, title="21", linewidth=1)
  57. plot(ma55, color=aqua, transp=transp5, style=line, title="55", linewidth=1)
  58. plot(ma200, color=aqua, transp=transp6, style=line, title="200", linewidth=1)
  59.  
  60. longCondition = longEntry and not longEntry[1] and not longEntry[2] and not longEntry[3] and not longEntry[4]
  61. shortCondition = shortEntry and not shortEntry[1] and not shortEntry[2] and not shortEntry[3] and not shortEntry[4]
  62. longClose = longExit and not longExit[1] and not longExit[2] and not longExit[3] and not longExit[4]
  63. shortClose = shortExit and not shortExit[1] and not shortExit[2] and not shortExit[3] and not shortExit[4]
  64.  
  65. strategy.entry("Long", strategy.long, when = window() and longCondition)
  66. strategy.close("Long", when=window() and longClose)
  67. strategy.entry("Short", strategy.short, when = window() and shortCondition)
  68. strategy.close("Short", when = window() and shortClose)
  69.  
  70. slPercent = input(6.67, title='Stop Loss %', type=float, step=0.1) / 100
  71. tsPercent = input(0.0, title='Trailing Stop Loss %', type=float, step=0.1) / 100
  72.  
  73. stopLevelLong = strategy.position_size > 0 ? strategy.position_avg_price * (1 - slPercent) : na
  74. stopLevelShort = strategy.position_size < 0 ? strategy.position_avg_price * (1 + slPercent) : na
  75.  
  76. trailingStopLevelLong := strategy.position_size > 0 ? (na(trailingStopLevelLong[1]) ? high * (1 - tsPercent) : max(high * (1 - tsPercent), trailingStopLevelLong[1])) : na
  77. trailingStopLevelShort := strategy.position_size < 0 ? (na(trailingStopLevelShort[1]) ? low * (1 + tsPercent) : min(low * (1 + tsPercent), trailingStopLevelShort[1])) : na
  78.  
  79. plotshape(slPercent > 0 ? stopLevelLong : na, location=location.absolute, style=shape.xcross, color=orange)
  80. plotshape(slPercent > 0 ? stopLevelShort : na, location=location.absolute, style=shape.xcross, color=orange)
  81. plotshape(tsPercent > 0 ? trailingStopLevelLong : na, location=location.absolute, style=shape.xcross, color=blue)
  82. plotshape(tsPercent > 0 ? trailingStopLevelShort : na, location=location.absolute, style=shape.xcross, color=blue)
  83. plotshape(strategy.position_avg_price, location=location.absolute, style=shape.circle, color=white)
  84.  
  85. if slPercent > 0 and not (longCondition or shortCondition or longClose or shortClose)
  86. strategy.exit("Stop Loss (Long)", "Long", stop=stopLevelLong)
  87. strategy.exit("Stop Loss (Short)", "Short", stop=stopLevelShort)
  88. else
  89. strategy.cancel("Stop Loss (Long)")
  90. strategy.cancel("Stop Loss (Short)")
  91.  
  92. if tsPercent > 0 and not (longCondition or shortCondition or longClose or shortClose)
  93. strategy.exit("Trailing Stop Loss (Long)", "Long", stop=trailingStopLevelLong)
  94. strategy.exit("Trailing Stop Loss (Short)", "Short", stop=trailingStopLevelShort)
  95. else
  96. strategy.cancel("Trailing Stop Loss (Long)")
  97. strategy.cancel("Trailing Stop Loss (Short)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement