Advertisement
JustUncleL

MA Ribbon R3

May 1st, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. //@version=3
  2.  
  3. study(title="MA Ribbon R3 by JustUncleL", shorttitle="MA_RIBBON R3", overlay = true)
  4.  
  5. //
  6. // Revision: R3
  7. // Revision Author: JustUncleL
  8. //
  9. // Description:
  10. // ============
  11. // This indicator plots and colour codes a ribbon between two moving averages.
  12. // The colour of the ribbon changes with trend direction: when price in uptrend
  13. // ribbon colour is green; when price in downtrend ribbon colour is red.
  14. //
  15. // Options Available:
  16. // ------------------
  17. // 1) You can select between 11 different types of moving averages, each MA line
  18. // can be a different type:
  19. // -
  20. // 2) Option to display coloured Candles around the Ribbon, the coulouring uses
  21. // the Grab candles scheme:
  22. // - Lime = Bull candle closed above Ribbon.
  23. // - Green = Bear candle closed above Ribbon.
  24. // - Red = Bull candle closed below Ribbon.
  25. // - DarkRed = Bear candle closed below Ribbon.
  26. // - Aqua = Bull candle closed inside Ribbon.
  27. // - Blue = Bear candle closed inside Ribbon.
  28. //
  29. // 3) Option to base the candles on a higher time frame (HFT), this performed by increasing
  30. // the MA length to create equivalent lengths from the HFT. So no re-painting.
  31. // NOTE: The script will time out if the MA lengths get too long after resizing.
  32. //
  33. // Modifications:
  34. // R3 - Work around for transparency
  35. // R2 - Recoded variant function with more efficient version.
  36. // - Change bar colouring to Grab candle style.
  37. // - Added named colour constants, to include fully non-transparent, work around
  38. // for bar colouring transparency bug.
  39. //
  40. // R1 - Original
  41. //
  42.  
  43. // Use Alternate Anchor TF for MAs
  44. anchor = input(0,minval=0,maxval=1440,title="Use Alternate Anchor TimeFrame (0=none, max=1440mins)")
  45.  
  46. // Fast MA - type, length
  47. type1 = input(defval="EMA", title="Fast MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
  48. len1 = input(defval=8, title="Fast - Length", minval=1)
  49. // Medium MA - type, length
  50. type2 = input(defval="EMA", title="Medium MA Type: SMA, EMA, WMA, VWMA, SMMA, DEMA, TEMA, HullMA, ZEMA, TMA, SSMA ( case sensitive )", type=string)
  51. len2 = input(defval=21, title="Medium - Length", minval=2)
  52. //
  53. ma_src = input(close,title="Source")
  54. sBars = input(false,title="Show Coloured Bars")
  55. // - INPUTS END
  56.  
  57. // Constants colours that include fully non-transparent option.
  58. green100 = #008000FF
  59. lime100 = #00FF00FF
  60. red100 = #FF0000FF
  61. blue100 = #0000FFFF
  62. aqua100 = #00FFFFFF
  63. darkred100 = #8B0000FF
  64.  
  65. // - FUNCTIONS
  66.  
  67. // - variant(type, src, len)
  68. // Returns MA input selection variant, default to SMA if blank or typo.
  69.  
  70. // SuperSmoother filter
  71. // © 2013 John F. Ehlers
  72. variant_supersmoother(src,len) =>
  73. a1 = exp(-1.414*3.14159 / len)
  74. b1 = 2*a1*cos(1.414*3.14159 / len)
  75. c2 = b1
  76. c3 = (-a1)*a1
  77. c1 = 1 - c2 - c3
  78. v9 = 0.0
  79. v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  80. v9
  81.  
  82. variant_smoothed(src,len) =>
  83. v5 = 0.0
  84. v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len
  85. v5
  86.  
  87. variant_zerolagema(src,len) =>
  88. ema1 = ema(src, len)
  89. ema2 = ema(ema1, len)
  90. v10 = ema1+(ema1-ema2)
  91. v10
  92.  
  93. variant_doubleema(src,len) =>
  94. v2 = ema(src, len)
  95. v6 = 2 * v2 - ema(v2, len)
  96. v6
  97.  
  98. variant_tripleema(src,len) =>
  99. v2 = ema(src, len)
  100. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  101. v7
  102.  
  103. // return variant, defaults to SMA
  104. variant(type, src, len) =>
  105. type=="EMA" ? ema(src,len) :
  106. type=="WMA" ? wma(src,len):
  107. type=="VWMA" ? vwma(src,len) :
  108. type=="SMMA" ? variant_smoothed(src,len) :
  109. type=="DEMA" ? variant_doubleema(src,len):
  110. type=="TEMA" ? variant_tripleema(src,len):
  111. type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :
  112. type=="SSMA" ? variant_supersmoother(src,len) :
  113. type=="ZEMA" ? variant_zerolagema(src,len) :
  114. type=="TMA" ? sma(sma(src,len),len) :
  115. sma(src,len)
  116.  
  117. // - /variant
  118.  
  119. // - FUNCTIONS END
  120.  
  121. // Make sure we have minimum channel spread.
  122. Length2_ = (len2-len1)<1?len1+1:len2
  123.  
  124. // If this is 5min or less Time Frame select EMAs
  125. mult = not isintraday or anchor==0 or interval<=0 or interval>=anchor? 1 : round(anchor/interval)>1? round(anchor/interval) : 1
  126. Length1 = mult==1 ? len1 : (len1*mult)
  127. Length2 = mult==1 ? Length2_ : (Length2_*mult)
  128. //plotshape(interval,location=location.bottom)
  129.  
  130. // Get the two MAs
  131. ma1 = variant(type1,ma_src, Length1)
  132. ma2 = variant(type2,ma_src, Length2)
  133.  
  134. //Plot the Ribbon
  135. ma1_=plot( ma1,color=rising(ma1,2)?green:red,linewidth=2,transp=20,title="ma1")
  136. ma2_=plot( ma2,color=rising(ma2,2)?green:red,linewidth=2,transp=20,title="ma2")
  137. fcolor = ma1>ma2?green:red
  138. fill(ma1_,ma2_,color=fcolor,transp=80)
  139.  
  140. // Colour bars according to the close position relative to the PAC selected.
  141. bcol = close>=open? close>ma1 and close>ma2? lime100 : close<ma1 and close<ma2? red100 : aqua100 : close>ma1 and close>ma2? green100 : close<ma1 and close<ma2? darkred100 : blue100
  142. barcolor(sBars?bcol:na, title = "Bar Colours",transp=0)
  143.  
  144.  
  145. //eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement