Advertisement
PineCoders

MACD strat

Dec 30th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. //@version=4
  2. study(title="", shorttitle="",overlay=true)
  3. len = input(14, minval=1, title="DI Length")
  4. lensig = input(14, title="ADX Smoothing", minval=1, maxval=50)
  5.  
  6. up = change(high)
  7. down = -change(low)
  8. plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
  9. minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
  10. trur = rma(tr, len)
  11. plus = fixnan(100 * rma(plusDM, len) / trur)
  12. minus = fixnan(100 * rma(minusDM, len) / trur)
  13. sum = plus + minus
  14. adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
  15.  
  16. //plot(plus, color=color.blue, title="+DI")
  17. //plot(minus, color=color.orange, title="-DI")
  18. //plot(adx, color=color.red, title="ADX")
  19. ////////////////////////////////////////////////////////////////////////////
  20. fast_length = input(title="Fast Length", type=input.integer, defval=12)
  21. slow_length = input(title="Slow Length", type=input.integer, defval=26)
  22. src1 = input(title="Source", type=input.source, defval=close)
  23. signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
  24. sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
  25. sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)
  26.  
  27. // Plot colors
  28. //col_grow_above = #26A69A
  29. //col_grow_below = #FFCDD2
  30. //col_fall_above = #B2DFDB
  31. //col_fall_below = #EF5350
  32. //col_macd = #0094ff
  33. //col_signal = #ff6a00
  34.  
  35. // Calculating
  36. fast_ma = sma_source ? sma(src1, fast_length) : ema(src1, fast_length)
  37. slow_ma = sma_source ? sma(src1, slow_length) : ema(src1, slow_length)
  38. macd = fast_ma - slow_ma
  39. signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
  40. hist = macd - signal
  41.  
  42. //plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )
  43. //plot(macd, title="MACD", color=col_macd, transp=0)
  44. //plot(signal, title="Signal", color=col_signal, transp=0)
  45. ////////////////////////////////////////////////////////////////////////////
  46. // cond1 = (crossover(plus,minus))
  47. // cond2 = (crossover(macd,signal))
  48. // cond3 = (crossunder(plus,minus))
  49. // cond4 = (crossunder(macd,signal))
  50. cond1 = plus > minus
  51. cond2 = macd > signal
  52. cond3 = plus < minus
  53. cond4 = macd < signal
  54. var inLong = false
  55. enterLong = cond1 and cond2 and not inLong
  56. enterShort = cond3 and cond4 and inLong
  57. if enterLong
  58. inLong := true
  59. else
  60. if enterShort
  61. inLong := false
  62.  
  63. //conditionbuy = cond1 and cond2 or cond1 and not cond2
  64. //conditionsell = cond3 and cond4 or cond3 and not cond4
  65. //plotshape(conditionbuy, text="BUY", style=shape.labelup,color=color.green, textcolor=color.white, location=location.belowbar, transp=0)
  66. //plotshape(conditionsell, text="SELL", style=shape.labeldown,color=color.red, textcolor=color.white, location=location.abovebar, transp=0)
  67.  
  68. plotshape(cond1, "cond1", shape.circle, location.top, color.silver, text = "1", size = size.small)
  69. plotshape(cond2, "cond2", shape.diamond, location.top, color.orange, text = "2", size = size.tiny)
  70. plotshape(cond3, "cond3", shape.circle, location.bottom, color.fuchsia, text = "3", size = size.small)
  71. plotshape(cond4, "cond4", shape.diamond, location.bottom, color.aqua, text = "4", size = size.tiny)
  72. plotshape(enterLong, "enterLong", shape.triangleup, location.belowbar, color.green, 0, text = "enterLong", size = size.tiny)
  73. plotshape(enterShort, "enterShort", shape.triangledown, location.abovebar, color.maroon, 0, text = "enterShort", size = size.tiny)
  74. // Place these markers one bar late so they don't overprint the "plotshape()" triangles.
  75. //plotchar(cond5[1], "cond5", "⮝", location.belowbar, color.lime, 0, size = size.tiny)
  76. //plotchar(cond6[1], "cond6", "⮟", location.abovebar, color.red, 0, size = size.tiny)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement