Advertisement
JustUncleL

MA Ribbon R4

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