Advertisement
JustUncleL

MA Ribbon R2

Apr 27th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. //@version=3
  2.  
  3. study(title="MA Ribbon R2 by JustUncleL", shorttitle="MA_RIBBON R2", overlay = true)
  4.  
  5. //
  6. // Revision: R2
  7. // Revision Author: JustUncleL
  8. //
  9. // Description:
  10. //
  11. // References:
  12. //
  13. // Modifications:
  14. // R2 - Replaced variant function with more efficient version.
  15. // - Change bar colouring to Grab candle style.
  16. // R1 - Original
  17. //
  18.  
  19. // Use Alternate Anchor TF for MAs
  20. anchor = input(0,minval=0,maxval=1440,title="Use Alternate Anchor TimeFrame (0=none, max=1440mins)")
  21.  
  22. // Fast MA - type, length
  23. type1 = input(defval="EMA", title="Fast MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
  24. len1 = input(defval=8, title="Fast - Length", minval=1)
  25. // Medium MA - type, length
  26. type2 = input(defval="EMA", title="Medium MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
  27. len2 = input(defval=21, title="Medium - Length", minval=2)
  28. //
  29. ma_src = input(close,title="Source")
  30. sBars = input(false,title="Show Coloured Bars")
  31. // - INPUTS END
  32.  
  33. // - FUNCTIONS
  34.  
  35. // - variant(type, src, len)
  36. // Returns MA input selection variant, default to SMA if blank or typo.
  37.  
  38. // SuperSmoother filter
  39. // © 2013 John F. Ehlers
  40. variant_supersmoother(src,len) =>
  41. a1 = exp(-1.414*3.14159 / len)
  42. b1 = 2*a1*cos(1.414*3.14159 / len)
  43. c2 = b1
  44. c3 = (-a1)*a1
  45. c1 = 1 - c2 - c3
  46. v9 = 0.0
  47. v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  48. v9
  49.  
  50. variant_smoothed(src,len) =>
  51. v5 = 0.0
  52. v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len
  53. v5
  54.  
  55. variant_zerolagema(src,len) =>
  56. ema1 = ema(src, len)
  57. ema2 = ema(ema1, len)
  58. v10 = ema1+(ema1-ema2)
  59. v10
  60.  
  61. variant_doubleema(src,len) =>
  62. v2 = ema(src, len)
  63. v6 = 2 * v2 - ema(v2, len)
  64. v6
  65.  
  66. variant_tripleema(src,len) =>
  67. v2 = ema(src, len)
  68. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  69. v7
  70.  
  71. // return variant, defaults to SMA
  72. variant(type, src, len) =>
  73. type=="EMA" ? ema(src,len) :
  74. type=="WMA" ? wma(src,len):
  75. type=="VWMA" ? vwma(src,len) :
  76. type=="SMMA" ? variant_smoothed(src,len) :
  77. type=="DEMA" ? variant_doubleema(src,len):
  78. type=="TEMA" ? variant_tripleema(src,len):
  79. type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :
  80. type=="SSMA" ? variant_supersmoother(src,len) :
  81. type=="ZEMA" ? variant_zerolagema(src,len) :
  82. type=="TMA" ? sma(sma(src,len),len) : sma(src,len)
  83.  
  84. // - /variant
  85.  
  86. // - FUNCTIONS END
  87.  
  88. // Make sure we have minimum channel spread.
  89. Length2_ = (len2-len1)<1?len1+1:len2
  90.  
  91. // If this is 5min or less Time Frame select EMAs
  92. mult = not isintraday or anchor==0 or interval<=0 or interval>=anchor? 1 : round(anchor/interval)>1? round(anchor/interval) : 1
  93. Length1 = mult==1 ? len1 : (len1*mult)
  94. Length2 = mult==1 ? Length2_ : (Length2_*mult)
  95.  
  96.  
  97. // Get the two MAs
  98. ma1 = variant(type1,ma_src, Length1)
  99. ma2 = variant(type2,ma_src, Length2)
  100.  
  101. //Plot the Ribbon
  102. ma1_=plot( ma1,color=rising(ma1,2)?green:red,linewidth=2,transp=20,title="ma1")
  103. ma2_=plot( ma2,color=rising(ma2,2)?green:red,linewidth=2,transp=20,title="ma2")
  104. fcolor = ma1>ma2?green:red
  105. fill(ma1_,ma2_,color=fcolor,transp=80)
  106.  
  107. //bc = not sBars? na : close>ma1 and close>ma2? green: close<ma1 and close<ma2? red : yellow
  108. //barcolor(bc,title="Bar Colours")
  109. // Colour bars according to the close position relative to the PAC selected.
  110. bcol = close>=open? close>ma1 and close>ma2? lime : close<ma1 and close<ma2? red : aqua : close>ma1 and close>ma2? green : close<ma1 and close<ma2? maroon : blue
  111. barcolor(sBars?bcol:na, title = "Bar Colours",transp=0)
  112.  
  113.  
  114. //eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement