Advertisement
JustUncleL

MA Ribbon R5 by JustUncleL

Oct 18th, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.38 KB | None | 0 0
  1. //@version=3
  2.  
  3. study(title="MA Ribbon R5 by JustUncleL", shorttitle="MA_RIBBON", overlay = true)
  4.  
  5. //
  6. // Author: JustUncleL
  7. // Revision: R5
  8. //
  9. //
  10. // Description:
  11. // ============
  12. // This indicator plots and colour codes a ribbon between two moving averages.
  13. // The colour of the ribbon changes with trend direction: when price in uptrend
  14. // ribbon colour is green; when price in downtrend ribbon colour is red.
  15. //
  16. // Options Available:
  17. // ------------------
  18. // 1) You can select between 11 different types of moving averages, each MA line
  19. // can be a different type:
  20. // - SMA = Simple Moving Average.
  21. // - EMA = Exponential Moving Average.
  22. // - WMA = Weighted Moving Average
  23. // - VWMA = Volume Weighted Moving Average
  24. // - SMMA = Smoothed Simple Moving Average.
  25. // - DEMA = Double Exponential Moving Average
  26. // - TEMA = Triple Exponential Moving Average.
  27. // - LAGMA = Laguerre Moving Average.
  28. // - HullMA = Hull Moving Average
  29. // - SSMA = Ehlers Super Smoother Moving average
  30. // - ZEMA = Near Zero Lag Exponential Moving Average.
  31. // - TMA = Triangular (smoothed) Simple Moving Average.
  32. //
  33. //
  34. // 2) Option to display coloured Candles around the Ribbon, the coulouring uses
  35. // the Standard candle colours:
  36. // - Lime = candle closed above Ribbon.
  37. // - Red = candle closed below Ribbon.
  38. // - Gray = Candle Closed inside Ribbon.
  39. // the Grab candles scheme:
  40. // - Lime = Bull candle closed above Ribbon.
  41. // - Green = Bear candle closed above Ribbon.
  42. // - Red = Bull candle closed below Ribbon.
  43. // - DarkRed = Bear candle closed below Ribbon.
  44. // - Aqua = Bull candle closed inside Ribbon.
  45. // - Blue = Bear candle closed inside Ribbon.
  46. //
  47. // 3) Option to base the candles on a higher time frame (HFT), this performed by increasing
  48. // the MA length to create equivalent lengths from the HFT. So no re-painting.
  49. // NOTE: The script will time out if the MA lengths get too long after resizing.
  50. //
  51. // Modifications:
  52. // 06-Apr-2018
  53. // - Change Anchor calculation to be based on Minutes for trading period
  54. // Daily (1440 = 1 Day), Weekly (7200 = 5Days), and Monthly (30240 = 21 Days), to
  55. // make it consistant across the Intraday/extraDaily time frame crossover.
  56. //
  57. // 22-Jan-2018
  58. // - Added optional Filter on Alerts to only allow signals in direction
  59. // of main trend, ie Fast MA Directional Slope.
  60. //
  61. // 20-Jan-2018 R5 Release:
  62. // - Added Alert Buy/Sell Entry and exits option based on candle colour:
  63. // - When candle changes to green or lime, then Buy signal
  64. // - When candle changes to red or darkred, then Sell signal.
  65. // - When candle changes to different color to Buy/Sell then exit signal.
  66. // - Added alarms for TradingView's Alarm Subsystem.
  67. //
  68. // 13-Jan-2018
  69. // - Corrected Anchor calculation for non-intraday charts.
  70. // - Added implied GPL copyright notice.
  71. // - Added option for both open and close outside ribbon colouring, instead of
  72. // just close.
  73. //
  74. // 22-Aug-2017
  75. // - Added Laguerre MA type.
  76. // - Some code tidy up.
  77. //
  78. // 11-Aug-2017 Minor updates:
  79. // - Changed MA Type input to options selection list.
  80. // - Change to Disable coloured bars by default, to allow already installed
  81. // scripts to colour bars first.
  82. //
  83. // 22-Jul-2017 - R4:
  84. // - Allow anchor option to be used for Days and Weeks.
  85. // - Allow option to use standard 3-tone bar colouring, or 6-tone Grab candle Colouring
  86. // - Fixed bug on bar colouring with some displays.
  87. //
  88. // R1 - Original
  89. //
  90. // R2 - Recoded variant function with more efficient version.
  91. // - Change bar colouring to Grab candle style.
  92. // - Added named colour constants, to include fully non-transparent, work around
  93. // for bar colouring transparency bug.
  94. //
  95. // R3 - Work around for transparency with drwaing bar colours.
  96. //
  97. // R4 - Added optional Alert
  98. //
  99. // -----------------------------------------------------------------------------
  100. // Copyright 2017,2018 JustUncleL
  101. //
  102. // This program is free software: you can redistribute it and/or modify
  103. // it under the terms of the GNU General Public License as published by
  104. // the Free Software Foundation, either version 3 of the License, or
  105. // any later version.
  106. //
  107. // This program is distributed in the hope that it will be useful,
  108. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  109. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  110. // GNU General Public License for more details.
  111. //
  112. // The GNU General Public License can be found here
  113. // <http://www.gnu.org/licenses/>.
  114. //
  115. // -----------------------------------------------------------------------------
  116. //
  117.  
  118.  
  119. // Use Alternate Anchor TF for MAs
  120. anchor = input(0,minval=0,title="Set to an Anchor TimeFrame in mins (0=none, 1D=1440, 1W=7200)")
  121.  
  122. // Fast MA - type, length
  123. type1 = input(defval="EMA", title="Fast MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "LAGMA", "HullMA", "ZEMA", "TMA", "SSMA"])
  124. len1 = input(defval=16, title="Fast MA - Length", minval=1)
  125. gamma1 = input(defval=0.33,title="Fast MA - Gamma for LAGMA")
  126. // Medium MA - type, length
  127. type3 = input(defval="EMA", title="Medium MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "LAGMA", "HullMA", "ZEMA", "TMA", "SSMA"])
  128. len3 = input(defval=21, title="Medium MA - Length (1=disabled)", minval=1)
  129. gamma3 = input(defval=0.55,title="Medium MA - Gamma for LAGMA")
  130. // Slow MA - type, length
  131. type2 = input(defval="EMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "LAGMA", "HullMA", "ZEMA", "TMA", "SSMA"])
  132. len2 = input(defval=26, title="Slow MA - Length", minval=2)
  133. gamma2 = input(defval=0.77,title="Slow MA - Gamma for LAGMA")
  134. //
  135. ma_src = input(close,title="MA Source")
  136. sBars = input(false,title="Show Coloured Bars")
  137. uGrabClr= input(false,title="Use Grab Bar 6-tone Colours, instead of Standard 3-tone")
  138. uOpen = input(false,title="Candles must open and close outside ribbon Colouring" )
  139. //
  140. ShowSwing = input(false,title="Show Swing Alerts")
  141. rFilter = input(false,title="Filter Alerts to Ribbon Colour")
  142. dFilter = input(false,title="Filter Alerts to Fast MA Directional Slope")
  143. //
  144. // - INPUTS END
  145.  
  146. // Constants colours that include fully non-transparent option.
  147. green100 = #008000FF
  148. lime100 = #00FF00FF
  149. red100 = #FF0000FF
  150. blue100 = #0000FFFF
  151. aqua100 = #00FFFFFF
  152. darkred100 = #8B0000FF
  153. gray100 = #808080FF
  154.  
  155. // - FUNCTIONS
  156.  
  157. // - variant(type, src, len, gamma)
  158. // Returns MA input selection variant, default to SMA if blank or typo.
  159.  
  160. // SuperSmoother filter
  161. // © 2013 John F. Ehlers
  162. variant_supersmoother(src,len) =>
  163. a1 = exp(-1.414*3.14159 / len)
  164. b1 = 2*a1*cos(1.414*3.14159 / len)
  165. c2 = b1
  166. c3 = (-a1)*a1
  167. c1 = 1 - c2 - c3
  168. v9 = 0.0
  169. v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  170. v9
  171.  
  172. variant_smoothed(src,len) =>
  173. v5 = 0.0
  174. v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len
  175. v5
  176.  
  177. variant_zerolagema(src,len) =>
  178. xLag = (len - 1) / 2
  179. xEMA = (src + (src - src[xLag]))
  180. v10 = ema(xEMA, len)
  181. v10
  182.  
  183. variant_doubleema(src,len) =>
  184. v2 = ema(src, len)
  185. v6 = 2 * v2 - ema(v2, len)
  186. v6
  187.  
  188. variant_tripleema(src,len) =>
  189. v2 = ema(src, len)
  190. v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  191. v7
  192.  
  193. //calc Laguerre
  194. variant_lag(p,g) =>
  195. L0 = 0.0
  196. L1 = 0.0
  197. L2 = 0.0
  198. L3 = 0.0
  199. L0 := (1 - g)*p+g*nz(L0[1])
  200. L1 := -g*L0+nz(L0[1])+g*nz(L1[1])
  201. L2 := -g*L1+nz(L1[1])+g*nz(L2[1])
  202. L3 := -g*L2+nz(L2[1])+g*nz(L3[1])
  203. f = (L0 + 2*L1 + 2*L2 + L3)/6
  204. f
  205.  
  206. // return variant, defaults to SMA
  207. variant(type, src, len, g) =>
  208. type=="EMA" ? ema(src,len) :
  209. type=="WMA" ? wma(src,len):
  210. type=="VWMA" ? vwma(src,len) :
  211. type=="SMMA" ? variant_smoothed(src,len) :
  212. type=="DEMA" ? variant_doubleema(src,len):
  213. type=="TEMA" ? variant_tripleema(src,len):
  214. type=="LAGMA" ? variant_lag(src,g) :
  215. type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :
  216. type=="SSMA" ? variant_supersmoother(src,len) :
  217. type=="ZEMA" ? variant_zerolagema(src,len) :
  218. type=="TMA" ? sma(sma(src,len),len) :
  219. sma(src,len)
  220.  
  221. // - /variant
  222.  
  223. // - FUNCTIONS END
  224.  
  225. // Make sure we have minimum channel spread.
  226. Length2_ = type1==type2?(len2-len1)<1?len1+1:len2 : len2
  227.  
  228. // If have anchor specified, calculate the base multiplier.
  229. mult = isintraday ? anchor==0 or interval<=0 or interval>=anchor? 1 : round(anchor/interval) : 1
  230. mult := isdwm? isdaily ? (anchor==0 or interval<=0 or interval>=anchor or anchor<=1440 ? 1 : round(anchor/1440)) :
  231. isweekly ? (anchor==0 or interval<=0 or interval>=anchor or anchor<=7200 ? 1 : round(anchor/7200)) :
  232. ismonthly ? (anchor==0 or interval<=0 or interval>=anchor or anchor<=30240 ? 1 : round(anchor/30240)) : 1 : mult
  233.  
  234. // Adjust MA lengths with Anchor multiplier
  235. Length1 = mult==1 ? len1 : (len1*mult)
  236. Length2 = mult==1 ? Length2_ : (Length2_*mult)
  237. Length3 = mult==1 ? len3 : (len3*mult)
  238. //plotshape(interval,location=location.bottom)
  239.  
  240. // Get the two MAs
  241. ma1 = variant(type1,ma_src, Length1, gamma1)
  242. ma2 = variant(type2,ma_src, Length2, gamma2)
  243. ma3 = Length3==1 ? na : variant(type3,ma_src, Length3, gamma3)
  244.  
  245.  
  246. fDirection = 0
  247. sDirection = 0
  248. mDirection = 0
  249. fDirection := hlc3 > ma1 ? 1 : hlc3 < ma1 ? -1 : nz(fDirection[1],1)
  250. sDirection := hlc3 > ma2 ? 1 : hlc3 < ma2 ? -1 : nz(sDirection[1],1)
  251. mDirection := Length3==1 ? na : hlc3 > ma3 ? 1 : hlc3 < ma3 ? -1 : nz(mDirection[1],1)
  252.  
  253. //Plot the Ribbon
  254. ma1_=plot( ma1,color=fDirection==1?green:red,style=line,join=true,linewidth=1,transp=20,title="Fast MA")
  255. ma2_=plot( ma2,color=sDirection==1?green:red,style=line,join=true,linewidth=1,transp=20,title="Slow MA")
  256. ma3_=plot( ma3,color=sDirection==1?green:red,style=circles,join=true,linewidth=1,transp=20,title="Medium MA")
  257. fcolor = ma1>ma2?green:red
  258. fill(ma1_,ma2_,color=fcolor,transp=80,title="Ribbon Fill")
  259.  
  260. // Colour bars according to the close position relative to the MA selected
  261. // Or Grab candle colour code bars according to the close position relative to the MA selected
  262. grabcol = uGrabClr? close>=open? hlc3>ma1 and hlc3>ma2 and (not uOpen or (open>ma1 and open>ma2))? lime100 :
  263. hlc3<ma1 and hlc3<ma2 and (not uOpen or (open<ma1 and open<ma2))? red100 : aqua100 :
  264. hlc3>ma1 and hlc3>ma2 and (not uOpen or (open>ma1 and open>ma2))? green100 :
  265. hlc3<ma1 and hlc3<ma2 and (not uOpen or (open<ma1 and open<ma2))? darkred100 : blue100 : na
  266. grabcol := uGrabClr? grabcol : hlc3>ma1 and hlc3>ma2 and (not uOpen or (open>ma1 and open>ma2))? lime100 :
  267. hlc3<ma1 and hlc3<ma2 and (not uOpen or (open<ma1 and open<ma2))? red100 : gray100
  268.  
  269. barcolor(sBars?grabcol:na, title = "Bar Colours")
  270.  
  271. // Generate Alert Arrows
  272. //
  273. buy = 0
  274. sell=0
  275. buyT = 0
  276. sellT =0
  277. // Generate signal by Candle Colour
  278. buy := grabcol==lime100 or grabcol==green100? (nz(buy[1])+1) : 0
  279. sell := grabcol==red100 or grabcol==darkred100? (nz(sell[1])+1) : 0
  280.  
  281. // Trend Filter
  282. buyT := buy==0? 0 : (rFilter and ma1<ma2) or (dFilter and falling(ma1,2))? 0 : nz(buyT[1])+1
  283. sellT := sell==0? 0 : (rFilter and ma1>ma2) or (dFilter and rising(ma1,2))? 0 : nz(sellT[1])+1
  284.  
  285. // Exit conditions
  286. exitbuy = nz(buyT[1])>0 and buyT==0
  287. exitsell = nz(sellT[1])>0 and sellT==0
  288.  
  289. //
  290. plotarrow(ShowSwing and buyT==1 ?1:na, title="BUY Swing Arrow", colorup=lime, maxheight=60, minheight=50, transp=20)
  291. plotarrow(ShowSwing and sellT==1 ?-1:na, title="SELL Swing Arrow", colordown=red, maxheight=60, minheight=50, transp=20)
  292. //
  293. plotshape(ShowSwing and exitbuy, title='BUY Exit', style=shape.xcross, location=location.belowbar, color=gray, text="Exit\nBuy", offset=0,transp=0)
  294. plotshape(ShowSwing and exitsell, title='Sell Exit', style=shape.xcross, location=location.abovebar, color=gray, text="Exit\nSell", offset=0,transp=0)
  295.  
  296. // Generate Alarms
  297. alertcondition(buyT==1,title="BUY Alert",message="BUY")
  298. alertcondition(sellT==1,title="SELL Alert",message="SELL")
  299. alertcondition(exitbuy,title="BUY Exit Alert",message="ExitBuy")
  300. alertcondition(exitsell,title="SELL Exit Alert",message="ExitSell")
  301.  
  302. //eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement