Advertisement
xmd79

ROC (Rate of Change) Refurbished

Jan 22nd, 2023
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.34 KB | None | 0 0
  1. //@version=5
  2. // @author=andre_007
  3.  
  4. // @version=5
  5. // @Author andre_007
  6. // @description ROC (Rate of Change) Refurbished
  7. // @License This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  8. // Thanks and Credits:
  9. // - TradingView: for ROC and Moving Averages
  10. // - allanster: for Dynamic Zones
  11.  
  12. indicator(title='ROC (Rate of Change) Refurbished', shorttitle='ROC Refurbished', timeframe="", timeframe_gaps=true, precision=2, format=format.price, overlay=false)
  13.  
  14. import andre_007/MovingAveragesProxy/2 as MaProxy
  15.  
  16. // ———————————————————————————————————————— Constants {
  17. // Moving Averages Types
  18. var string ALMA = 'Arnaud Legoux Moving Average'
  19. var string HMA = 'Hull Moving Average'
  20. var string JURIK = 'Jurik Moving Average'
  21. var string LSMA = 'Least Squares Moving Average'
  22. var string MEDIAN = 'Median'
  23. var string REGMA = 'Regularized Exponential Moving Average'
  24. var string RSIMA = 'RSI Moving average'
  25. var string SMA = 'Simple Moving Average'
  26. var string SMMA = 'Smoothed Moving Average'
  27. var string WMA = 'Weighted Moving Average'
  28. var string WWMA = 'Welles Wilder Moving Average'
  29.  
  30. var color COLOR_BULL = color.new(#3af13c, 0)
  31. var color COLOR_BEAR = color.new(#ff0000, 0)
  32. var color COLOR_BULL_MA = color.new(#3af13c, 75)
  33. var color COLOR_BEAR_MA = color.new(#ff0000, 75)
  34. // ———————————————————————————————————————— }
  35.  
  36. // ———————————————————————————————————————— Global Variables {
  37. float roc = na
  38. float movingAverage1 = na
  39. float movingAverage2 = na
  40. float movingAverage3 = na
  41. float movingAverage4 = na
  42. float movingAverage5 = na
  43. float movingAverage6 = na
  44. float movingAverage7 = na
  45. float movingAverage8 = na
  46. float dZoneAbove = na
  47. float dZoneBelow = na
  48. float dZoneCenter = na
  49. // ———————————————————————————————————————— }
  50.  
  51. // ———————————————————————————————————————— inputs {
  52. // ————— ROC
  53. var int lengthROC = input.int(defval=21, title='Length', minval=1, group='ROC', inline='1')
  54. sourceROC = input(defval=hl2, title="Source", group='ROC', inline='1')
  55.  
  56. var color inputColorBullRoc = input.color(COLOR_BULL, '', inline='1', group='ROC')
  57. var color inputColorBearRoc = input.color(COLOR_BEAR, '', inline='1', group='ROC')
  58.  
  59. // ————— Dynamic Zone
  60. var int dataSmple = input.int(defval = 55, title = "Sample Length", minval = 1, group = 'Dynamic Zones', inline = '1')
  61. var float pcntAbove = input.float(defval = 90, title = "High is Above X% of Sample", minval = 0, maxval = 100, step = 1.0, group = 'Dynamic Zones', inline = '2')
  62. var float pcntBelow = input.float(defval = 90, title = "Low is Below X% of Sample", minval = 0, maxval = 100, step = 1.0, group = 'Dynamic Zones', inline = '2')
  63.  
  64. var lineDzoneTop = input.color(#143bfc, 'Upper Line of Dynamic Zone', inline='3', group='Dynamic Zones')
  65. var lineDzoneBottom = input.color(#143bfc, 'Lower Line of Dynamic Zone', inline='3', group='Dynamic Zones')
  66. var fillDzoneAbove = input.color(color.new(COLOR_BULL, 60), 'Fill Dynamic Zone when above', inline='4', group='Dynamic Zones')
  67. var fillDzoneInside = input.color(color.new(#143bfc, 85), 'Fill Dynamic Zone inside', inline='5', group='Dynamic Zones')
  68. var fillDzoneBelow = input.color(color.new(COLOR_BEAR, 60), 'Fill Dynamic Zone when below', inline='6', group='Dynamic Zones')
  69.  
  70. var string centerLineOptA = 'Center of Dynamic Zone (x = 50%)'
  71. var string centerLineOptB = 'Zero'
  72. var string centerLine = input.string(defval = centerLineOptA, title = 'Center Line', options = [centerLineOptA, centerLineOptB], group = 'Dynamic Zones', inline = '7')
  73. var color lineDzoneCenter = input.color(#fd6c1e, 'Center Line', inline='7', group='Dynamic Zones')
  74.  
  75. // ————— Moving Averages
  76. var string inputTypeMa = input.string(title='Type', defval=REGMA, options=[ALMA,HMA,JURIK,LSMA,MEDIAN,REGMA,RSIMA,SMA,SMMA,WMA,WWMA], inline='1', group='Moving Averages')
  77. var string GRP_VWMA_TOOLTIP = 'The most common "Volume Weighted Moving Average" is a Simple Moving Average of Price x Volume, divided by Simple Moving Average of Volume. \n' +
  78. 'Enabling this checkbox, it\'s possible to have other types and less common moving averages weighted by volume, for example, Exponencial Volume Weighted Moving Average, Alma Volume Weighted Moving Average, etc...'
  79. var bool applyVolumeWeighted = input.bool(title="Volume Weighted Moving Average?", defval=false, group='Moving Averages', tooltip=GRP_VWMA_TOOLTIP, inline='1')
  80.  
  81. // ————— Average 1
  82. var bool inputShowAvg1 = input.bool(title='Moving Average 1', defval=true, inline='12', group='Moving Averages')
  83. var color inputColorBull1 = input.color(COLOR_BULL_MA, '', inline='12', group='Moving Averages')
  84. var color inputColorBear1 = input.color(COLOR_BEAR_MA, '', inline='12', group='Moving Averages')
  85. var int inputLengthMa1 = input.int(5, minval=1, title='Length', inline='13', group='Moving Averages')
  86.  
  87. // ————— Average 2
  88. var bool inputShowAvg2 = input.bool(title='Moving Average 2', defval=true, inline='15', group='Moving Averages')
  89. var color inputColorBull2 = input.color(COLOR_BULL_MA, '', inline='15', group='Moving Averages')
  90. var color inputColorBear2 = input.color(COLOR_BEAR_MA, '', inline='15', group='Moving Averages')
  91. var int inputLengthMa2 = input.int(10, minval=1, title='Length', inline='16', group='Moving Averages')
  92.  
  93. // ————— Average 3
  94. var bool inputShowAvg3 = input.bool(title='Moving Average 3', defval=true, inline='17', group='Moving Averages')
  95. var color inputColorBull3 = input.color(COLOR_BULL_MA, '', inline='17', group='Moving Averages')
  96. var color inputColorBear3 = input.color(COLOR_BEAR_MA, '', inline='17', group='Moving Averages')
  97. var int inputLengthMa3 = input.int(15, minval=1, title='Length', inline='18', group='Moving Averages')
  98.  
  99. // ————— Average 4
  100. var bool inputShowAvg4 = input.bool(title='Moving Average 4', defval=true, inline='19', group='Moving Averages')
  101. var color inputColorBull4 = input.color(COLOR_BULL_MA, '', inline='19', group='Moving Averages')
  102. var color inputColorBear4 = input.color(COLOR_BEAR_MA, '', inline='19', group='Moving Averages')
  103. var int inputLengthMa4 = input.int(20, minval=1, title='Length', inline='20', group='Moving Averages')
  104.  
  105. // ————— Average 5
  106. var bool inputShowAvg5 = input.bool(title='Moving Average 5', defval=true, inline='21', group='Moving Averages')
  107. var color inputColorBull5 = input.color(COLOR_BULL_MA, '', inline='21', group='Moving Averages')
  108. var color inputColorBear5 = input.color(COLOR_BEAR_MA, '', inline='21', group='Moving Averages')
  109. var int inputLengthMa5 = input.int(25, minval=1, title='Length', inline='22', group='Moving Averages')
  110.  
  111. // ————— Average 6
  112. var bool inputShowAvg6 = input.bool(title='Moving Average 6', defval=true, inline='23', group='Moving Averages')
  113. var color inputColorBull6 = input.color(COLOR_BULL_MA, '', inline='23', group='Moving Averages')
  114. var color inputColorBear6 = input.color(COLOR_BEAR_MA, '', inline='23', group='Moving Averages')
  115. var int inputLengthMa6 = input.int(30, minval=1, title='Length', inline='24', group='Moving Averages')
  116.  
  117. // ————— Average 7
  118. var bool inputShowAvg7 = input.bool(title='Moving Average 7', defval=true, inline='25', group='Moving Averages')
  119. var color inputColorBull7 = input.color(COLOR_BULL_MA, '', inline='25', group='Moving Averages')
  120. var color inputColorBear7 = input.color(COLOR_BEAR_MA, '', inline='25', group='Moving Averages')
  121. var int inputLengthMa7 = input.int(35, minval=1, title='Length', inline='26', group='Moving Averages')
  122.  
  123. // ————— Average 8
  124. var bool inputShowAvg8 = input.bool(title='Moving Average 8', defval=true, inline='27', group='Moving Averages')
  125. var color inputColorBull8 = input.color(COLOR_BULL_MA, '', inline='27', group='Moving Averages')
  126. var color inputColorBear8 = input.color(COLOR_BEAR_MA, '', inline='27', group='Moving Averages')
  127. var int inputLengthMa8 = input.int(40, minval=1, title='Length', inline='28', group='Moving Averages')
  128.  
  129. // ————— Line colors
  130. var string color_A = 'is below or above the next moving average'
  131. var string color_B = 'is below or above Zero or Center of Dynamic Zone'
  132.  
  133. var string avgColorLogic = input.string(defval=color_B, title='Change line colors when', inline='2', group='Line Colors', options=[color_A, color_B])
  134. var bool inputFill = input.bool(title='Fill background between averages?', defval=false, inline='3', group='Line Colors')
  135. var int inputColorFillTransparency = input.int(defval=90 , title="Transparency for fill", minval=0, maxval=100, inline = "4", group='Line Colors')
  136.  
  137. // ————— Bar colors
  138. var string bar_color_A = 'All lines are aligned'
  139. var string bar_color_B = 'ROC is above or under all MAs'
  140. var string bar_color_C = 'All lines are above or under Zero or Center of Dynamic Zone'
  141. var string bar_color_E = 'ROC is above or under Zero or Center of Dynamic Zone'
  142. var string bar_color_D = 'Never'
  143. var string barColorsLogic = input.string(defval=bar_color_E, title='Change bar colors when', inline='2', group='Bar Colors', options=[bar_color_A, bar_color_B, bar_color_C, bar_color_E, bar_color_D])
  144. var color inputBarColorBull = input.color(color.new(#12760e, 0), 'Bullish', inline='3', group='Bar Colors')
  145. var color inputBarColorBear = input.color(color.new(#d45959, 0), 'Bearish', inline='3', group='Bar Colors')
  146.  
  147. var bool inputBarColorDzone = input.bool(title='Change bar colors when ROC is above or below Dynamic Zones?', defval=true, inline='4', group='Bar Colors')
  148. var color inputBarColorBullDzone = input.color(COLOR_BULL, 'Bullish', inline='5', group='Bar Colors')
  149. var color inputBarColorBearDzone = input.color(COLOR_BEAR, 'Bearish', inline='5', group='Bar Colors')
  150. // ———————————————————————————————————————— }
  151.  
  152. // ———————————————————————————————————————— Functions {
  153. // @description Returns a bar color
  154. // @returns Color
  155. applyBarColor() =>
  156.  
  157. color barColor = if (barColorsLogic == bar_color_D) // 'Never'
  158. na
  159. else if (barColorsLogic == bar_color_A) // 'All lines are aligned'
  160. if roc >= nz(movingAverage1, roc) and
  161. nz(movingAverage1) >= nz(movingAverage2) and
  162. nz(movingAverage2) >= nz(movingAverage3) and
  163. nz(movingAverage3) >= nz(movingAverage4) and
  164. nz(movingAverage4) >= nz(movingAverage5) and
  165. nz(movingAverage5) >= nz(movingAverage6) and
  166. nz(movingAverage6) >= nz(movingAverage7) and
  167. nz(movingAverage7) >= nz(movingAverage8)
  168. inputBarColorBull
  169. else
  170. inputBarColorBear
  171. else if (barColorsLogic == bar_color_B) // 'ROC is above or under all MAs'
  172. if roc >= nz(movingAverage1, roc) and
  173. roc >= nz(movingAverage2) and
  174. roc >= nz(movingAverage3) and
  175. roc >= nz(movingAverage4) and
  176. roc >= nz(movingAverage5) and
  177. roc >= nz(movingAverage6) and
  178. roc >= nz(movingAverage7) and
  179. roc >= nz(movingAverage8)
  180. inputBarColorBull
  181. else
  182. inputBarColorBear
  183. else if (barColorsLogic == bar_color_C) // 'All lines are above or under Zero or Center of Dynamic Zone'
  184. float zero = (centerLine == centerLineOptA ? dZoneCenter : 0)
  185. if roc >= zero and
  186. nz(movingAverage1) >= zero and
  187. nz(movingAverage2) >= zero and
  188. nz(movingAverage3) >= zero and
  189. nz(movingAverage4) >= zero and
  190. nz(movingAverage5) >= zero and
  191. nz(movingAverage6) >= zero and
  192. nz(movingAverage7) >= zero and
  193. nz(movingAverage8) >= zero
  194. inputBarColorBull
  195. else
  196. inputBarColorBear
  197. else if (barColorsLogic == bar_color_E) // 'ROC is above or under Zero or Center of Dynamic Zone'
  198. float zero = (centerLine == centerLineOptA ? dZoneCenter : 0)
  199. if roc >= zero
  200. inputBarColorBull
  201. else
  202. inputBarColorBear
  203. else
  204. na
  205.  
  206. // 'ROC is above or under Dynamic Zone'
  207. barColor := if inputBarColorDzone
  208. if roc >= dZoneAbove
  209. inputBarColorBullDzone
  210. else if roc <= dZoneBelow
  211. inputBarColorBearDzone
  212. else
  213. barColor
  214.  
  215. // @function calculateMovingAverage
  216. // @description Abstract function that invokes the calculation of average according to type
  217. // @param type Type of Moving Average
  218. // @param src Source of series (close, high, low, etc.)
  219. // @param len Period of loopback to calculate the average
  220. // @returns series of moving average
  221. calculateMovingAverage(simple string type, float src, simple int len) =>
  222. MaProxy.getMovingAverage(
  223. type=type, src=src, len=len,
  224. lsmaOffset=0,
  225. volumeWeighted=applyVolumeWeighted)
  226.  
  227. // @function f_SetColorLine
  228. // @description Set color line for ROC or MA's
  229. // @param val_1 A line
  230. // @param val_2 Next Line
  231. // @param colorBull Color when bullish
  232. // @param colorBear Color when bearish
  233. // @param previousColor Color for use when next line is deactivated
  234. // @returns series color
  235. f_SetColorLine(float val_1, float val_2, simple color colorBull, simple color colorBear, series color previousColor=na) =>
  236. color color_line = if avgColorLogic == color_A // 'All lines are aligned'
  237. if na(val_2)
  238. previousColor
  239. else
  240. if nz(val_1) >= nz(val_2)
  241. colorBull
  242. else
  243. colorBear
  244. else // 'is below or above Zero or Center of Dynamic Zone'
  245. float zero = (centerLine == centerLineOptA ? dZoneCenter : 0)
  246. if val_1 >= zero
  247. colorBull
  248. else
  249. colorBear
  250. // ———————————————————————————————————————— }
  251.  
  252. // ———————————————————————————————————————— Calculations {
  253. roc := 100 * (sourceROC - sourceROC[lengthROC])/sourceROC[lengthROC]
  254.  
  255. // Moving Averages
  256. if inputShowAvg1
  257. movingAverage1 := calculateMovingAverage(inputTypeMa, roc, inputLengthMa1)
  258. if inputShowAvg2
  259. movingAverage2 := calculateMovingAverage(inputTypeMa, roc, inputLengthMa2)
  260. if inputShowAvg3
  261. movingAverage3 := calculateMovingAverage(inputTypeMa, roc, inputLengthMa3)
  262. if inputShowAvg4
  263. movingAverage4 := calculateMovingAverage(inputTypeMa, roc, inputLengthMa4)
  264. if inputShowAvg5
  265. movingAverage5 := calculateMovingAverage(inputTypeMa, roc, inputLengthMa5)
  266. if inputShowAvg6
  267. movingAverage6 := calculateMovingAverage(inputTypeMa, roc, inputLengthMa6)
  268. if inputShowAvg7
  269. movingAverage7 := calculateMovingAverage(inputTypeMa, roc, inputLengthMa7)
  270. if inputShowAvg8
  271. movingAverage8 := calculateMovingAverage(inputTypeMa, roc, inputLengthMa8)
  272.  
  273. // Dynamic Zones
  274. dZoneAbove := ta.percentile_nearest_rank(roc, dataSmple, pcntAbove)
  275. dZoneBelow := ta.percentile_nearest_rank(roc, dataSmple, 100 - pcntBelow)
  276. dZoneCenter := ta.percentile_nearest_rank(roc, dataSmple, 100 - 50)
  277. // ———————————————————————————————————————— }
  278.  
  279. // ———————————————————————————————————————— Bar Colors {
  280. barcolor( applyBarColor() )
  281. // ———————————————————————————————————————— }
  282.  
  283. // ———————————————————————————————————————— Plots {
  284. // Center Line
  285. hline(0, color = lineDzoneCenter, title="Zero", linewidth=1, linestyle=hline.style_dashed, display=(centerLine==centerLineOptB ? display.all : display.none))
  286. plot(dZoneCenter, title="Center Line", color=lineDzoneCenter, linewidth=1, style=plot.style_line, display=(centerLine==centerLineOptA ? display.all : display.none)) // bar_index % 2 ? lineDzoneCenter : na
  287.  
  288. // ROC and Moving Averages
  289. color colorLineROC = f_SetColorLine(roc, movingAverage1, inputColorBullRoc, inputColorBearRoc)
  290. color colorLineMa1 = f_SetColorLine(movingAverage1, movingAverage2, inputColorBull1, inputColorBear1, colorLineROC)
  291. color colorLineMa2 = f_SetColorLine(movingAverage2, movingAverage3, inputColorBull2, inputColorBear2, colorLineMa1)
  292. color colorLineMa3 = f_SetColorLine(movingAverage3, movingAverage4, inputColorBull3, inputColorBear3, colorLineMa2)
  293. color colorLineMa4 = f_SetColorLine(movingAverage4, movingAverage5, inputColorBull4, inputColorBear4, colorLineMa3)
  294. color colorLineMa5 = f_SetColorLine(movingAverage5, movingAverage6, inputColorBull5, inputColorBear5, colorLineMa4)
  295. color colorLineMa6 = f_SetColorLine(movingAverage6, movingAverage7, inputColorBull6, inputColorBear6, colorLineMa5)
  296. color colorLineMa7 = f_SetColorLine(movingAverage7, movingAverage8, inputColorBull7, inputColorBear7, colorLineMa6)
  297. color colorLineMa8 = f_SetColorLine(movingAverage7, movingAverage8, inputColorBull8, inputColorBear8, colorLineMa7)
  298.  
  299. plotROC = plot(roc, title="ROC", color=colorLineROC, linewidth=2, style=plot.style_line)
  300. plotMa1 = plot(movingAverage1, title="Moving Average 1", color=colorLineMa1, linewidth=1, style=plot.style_circles)
  301. plotMa2 = plot(movingAverage2, title="Moving Average 2", color=colorLineMa2, linewidth=1, style=plot.style_circles)
  302. plotMa3 = plot(movingAverage3, title="Moving Average 3", color=colorLineMa3, linewidth=1, style=plot.style_circles)
  303. plotMa4 = plot(movingAverage4, title="Moving Average 4", color=colorLineMa4, linewidth=1, style=plot.style_circles)
  304. plotMa5 = plot(movingAverage5, title="Moving Average 5", color=colorLineMa5, linewidth=1, style=plot.style_circles)
  305. plotMa6 = plot(movingAverage6, title="Moving Average 6", color=colorLineMa6, linewidth=1, style=plot.style_circles)
  306. plotMa7 = plot(movingAverage7, title="Moving Average 7", color=colorLineMa7, linewidth=1, style=plot.style_circles)
  307. plotMa8 = plot(movingAverage8, title="Moving Average 8", color=colorLineMa8, linewidth=1, style=plot.style_circles)
  308.  
  309. fill(plotROC, plotMa1, color=inputFill ? color.new(f_SetColorLine(roc, movingAverage1, inputColorBullRoc, inputColorBearRoc), inputColorFillTransparency) : na )
  310. fill(plotMa1, plotMa2, color=inputFill ? color.new(f_SetColorLine(movingAverage1, movingAverage2, inputColorBull1, inputColorBear1), inputColorFillTransparency) : na )
  311. fill(plotMa2, plotMa3, color=inputFill ? color.new(f_SetColorLine(movingAverage2, movingAverage3, inputColorBull2, inputColorBear2), inputColorFillTransparency) : na )
  312. fill(plotMa3, plotMa4, color=inputFill ? color.new(f_SetColorLine(movingAverage3, movingAverage4, inputColorBull3, inputColorBear3), inputColorFillTransparency) : na )
  313. fill(plotMa4, plotMa5, color=inputFill ? color.new(f_SetColorLine(movingAverage4, movingAverage5, inputColorBull4, inputColorBear4), inputColorFillTransparency) : na )
  314. fill(plotMa5, plotMa6, color=inputFill ? color.new(f_SetColorLine(movingAverage5, movingAverage6, inputColorBull5, inputColorBear5), inputColorFillTransparency) : na )
  315. fill(plotMa6, plotMa7, color=inputFill ? color.new(f_SetColorLine(movingAverage6, movingAverage7, inputColorBull6, inputColorBear6), inputColorFillTransparency) : na )
  316. fill(plotMa7, plotMa8, color=inputFill ? color.new(f_SetColorLine(movingAverage7, movingAverage8, inputColorBull7, inputColorBear7), inputColorFillTransparency) : na )
  317.  
  318. // Dynamic Zones
  319. above = plot(roc > dZoneAbove ? roc : dZoneAbove, title = "", color = color(na))
  320. probOB = plot(dZoneAbove, title = "Dynamic Zone: Top Line", color = lineDzoneTop, linewidth=1)
  321. probOS = plot(dZoneBelow, title = "Dynamic Zone: Bottom Line", color = lineDzoneBottom, linewidth=1)
  322. below = plot(roc < dZoneBelow ? roc : dZoneBelow, title = "", color = color(na))
  323.  
  324. fill(above, probOB, color = fillDzoneAbove)
  325. fill(probOB, probOS, color = fillDzoneInside)
  326. fill(below, probOS, color = fillDzoneBelow)
  327. // ———————————————————————————————————————— }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement