xmd79

Ultimate MA

Nov 21st, 2021
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.86 KB | None | 0 0
  1. // © ClassicScott
  2.  
  3.  
  4. //@version=5
  5.  
  6. //What is the "Ultimate MA" exactly, you ask? Simple. It actually takes as its influence the Rex Dog Moving Average (which I have included as an MA in some of
  7. //my other indicators), an invention by xkavalis that is simply an average of different length moving averages.
  8. //It's available for free on his account, so take a look at it.
  9. //I've recently become drawn to using fibonacci sequence numbers as lookbacks for moving averages, and they work really well (I'm honestly beginning to think the
  10. //number doesn't matter).
  11.  
  12. //You can see where this is going. The Ultimate MA is an average of several (eight) moving averages of varying lengths (5 - 144) all of fibonacci numbers.
  13. //Sounds pretty basic, right? That's not actually the case, however.
  14. //If you were to take all these numbers, add them up, then average them by eight you'd get ~46. Now, stick a 46 period moving average on the chart and compare it
  15. //to this one and see what you get. They track price very differently. Still, this all sort of sounds like I'm copying the RDMA, which isn't a sin in itself
  16. //but is hardly grounds for releasing a new MA into the wild.
  17.  
  18. //The actual initial problem I wanted to tackle was how to take in to account for the entire range of price action in a candle in a moving average.
  19. //ohlc4 sort of does this, but it's still just one line that is an average of all these prices, and I thought there might be a better way
  20. //(not claiming that what I came upon is, but I like it).
  21. //My solution was to plot two moving averages: one an average of price highs, and the other an average of lows, thus creating a high/low price channel. Perhaps
  22. //this is not a new thing at all. I don't know. This is just an idea I had that I figured I could implement easily enough.
  23. //Originally I had just applied this to a 21 period EMA, but then the idea sort of expanded into what you see here. I kept thinking "is 21 the best?"
  24. //What about faster or slower? Then I thought about the RDMA and decided on this implimentation.
  25.  
  26. //Further, I take the high and low moving averages and divide them by two in order to get a basis. You can turn all this stuff on or off, though I do like the default settings.
  27. //After that I wanted to add bands to it to measure volatility. There is an RDMA version that utilizes ATR bands, but I could never find myself happy with these.
  28. //I just wanted something... else. I also, actually made my own version of xkavalis' RDMA bands with some of the extra stuff I included here, but obviously didn't
  29. //feel comfortable releasing it as an indicator as I hadn't changed it enough significantly in my mind to fairly do so.
  30. //I eventually settled on Bollinger Bands as an appropriate solution to apply to the situation. I really like them. It took some fiddling because I had to create
  31. //a standard deviation for both the high and low MAs instead of just one, and then figure out the best combination of moving averages and standard deviations
  32. //to add and subtract to get the bands right.
  33.  
  34. //Then I decided I wanted to add a few different moving averages to choose from instead of just an EMA even though I think it's the "best."
  35. //I didn't want to make things too complicated, so I just went with the standards--EMA, SMA, WMA, HMA-- + 1, the ALMA (which gives some adjustability
  36. //with its offset and sigma).
  37. //Also, you can run more than one moving average at a time (try running an HMA with a slower one).
  38.  
  39. //Oh yeah, the bands? You can set them, in a dropdown box, to be based on which ever moving average you want.
  40.  
  41. //Furthermore, this is a multi-timeframe indicator, so if you want to run it on a higher time frame than the one you are trading on, it's great for that.
  42.  
  43. //ALSO, I actually have the basis color setup as multi-timeframe. What this means is that if you are looking at an hourly chart, you can set the color to a 4h
  44. //(or higher) chart if you want, and if the current candle is above or below the previous close of the basis on that higher timeframe you will know simply by
  45. //looking at the color of it ((while still being on the hourly chart). It's just a different way of utilizing higher timeframe information, but without
  46. //the indicator itself plotted as higher timeframe.
  47.  
  48. //I'm nearly finished. Almost last thing is a 233 period moving average. It's plotted as an average of the SMA, EMA, and Kijun-sen.
  49.  
  50. //Lastly, there are alerts for price crossing the inner border of the bands, or the 233 MA.
  51.  
  52.  
  53. indicator('+ Ultimate MA', shorttitle='+ UMA', overlay=true, timeframe='')
  54.  
  55.  
  56. ////ALMA
  57. _alma = input.bool(defval=false, title='ALMA', group='ALMA')
  58. i_alma_tf = input.timeframe(title='Resolution for Basis Color', defval='', group='ALMA', tooltip='Pick your time frame and the basis will be colored based on if price is above or below that previous timeframe\'s close.')
  59. alma_bull_color = input.color(color.new(#00bcd4, 50), title='Bullish', inline='basis', group='ALMA')
  60. alma_bear_color = input.color(color.new(#e91e63, 50), title='Bearish', inline='basis', group='ALMA')
  61. greater_alma_color = input.color(color.new(#b2b5be, 85), title='Greater ALMA Color', inline='gma', group='ALMA')
  62.  
  63. alma_offset = input.float(title='Offset', defval=0.85, step=0.025, inline='inputs', group='ALMA')
  64. alma_sigma = input.float(title='Sigma', defval=6, step=0.5, inline='inputs', group='ALMA')
  65.  
  66.  
  67. h_alma_5 = ta.alma(high, 5, alma_offset, alma_sigma)
  68. h_alma_8 = ta.alma(high, 8, alma_offset, alma_sigma)
  69. h_alma_13 = ta.alma(high, 13, alma_offset, alma_sigma)
  70. h_alma_21 = ta.alma(high, 21, alma_offset, alma_sigma)
  71. h_alma_34 = ta.alma(high, 34, alma_offset, alma_sigma)
  72. h_alma_55 = ta.alma(high, 55, alma_offset, alma_sigma)
  73. h_alma_89 = ta.alma(high, 89, alma_offset, alma_sigma)
  74. h_alma_144 = ta.alma(high, 144, alma_offset, alma_sigma)
  75.  
  76. h_alma_avg = (h_alma_5 + h_alma_8 + h_alma_13 + h_alma_21 + h_alma_34 + h_alma_55 + h_alma_89 + h_alma_144) / 8
  77.  
  78. l_alma_5 = ta.alma(low, 5, alma_offset, alma_sigma)
  79. l_alma_8 = ta.alma(low, 8, alma_offset, alma_sigma)
  80. l_alma_13 = ta.alma(low, 13, alma_offset, alma_sigma)
  81. l_alma_21 = ta.alma(low, 21, alma_offset, alma_sigma)
  82. l_alma_34 = ta.alma(low, 34, alma_offset, alma_sigma)
  83. l_alma_55 = ta.alma(low, 55, alma_offset, alma_sigma)
  84. l_alma_89 = ta.alma(low, 89, alma_offset, alma_sigma)
  85. l_alma_144 = ta.alma(low, 144, alma_offset, alma_sigma)
  86.  
  87. l_alma_avg = (l_alma_5 + l_alma_8 + l_alma_13 + l_alma_21 + l_alma_34 + l_alma_55 + l_alma_89 + l_alma_144) / 8
  88.  
  89.  
  90. alma_basis = (h_alma_avg + l_alma_avg) / 2
  91.  
  92. p_alma_basis = plot(_alma ? alma_basis : na, color=request.security(syminfo.tickerid, i_alma_tf, close > alma_basis[1] ? alma_bull_color : close < alma_basis[1] ? alma_bear_color : na), linewidth=2, title='ALMA High+Low Average')
  93. p_alma_h = plot(_alma ? h_alma_avg : na, title='ALMA-High', display=display.none)
  94. p_alma_l = plot(_alma ? l_alma_avg : na, title='ALMA-Low', display=display.none)
  95.  
  96. fill(p_alma_h, p_alma_l, greater_alma_color, title='Greater ALMA', transp=90)
  97.  
  98.  
  99. ////////////////////////////////////////////////////////////////////////////////
  100.  
  101.  
  102. ////EMA
  103. _ema = input.bool(defval=true, title='EMA', group='EMA')
  104. i_ema_tf = input.timeframe(title='Resolution for Basis Color', defval='', group='EMA', tooltip='Pick your time frame and the basis will be colored based on if price is above or below that previous timeframe\'s close.')
  105. ema_bull_color = input.color(color.new(#00bcd4, 50), title='Bullish', inline='basis', group='EMA')
  106. ema_bear_color = input.color(color.new(#e91e63, 50), title='Bearish', inline='basis', group='EMA')
  107. greater_ema_color = input.color(color.new(#b2b5be, 85), title='Greater EMA Color', inline='gma', group='EMA')
  108.  
  109.  
  110. h_ema_5 = ta.ema(high, 5)
  111. h_ema_8 = ta.ema(high, 8)
  112. h_ema_13 = ta.ema(high, 13)
  113. h_ema_21 = ta.ema(high, 21)
  114. h_ema_34 = ta.ema(high, 34)
  115. h_ema_55 = ta.ema(high, 55)
  116. h_ema_89 = ta.ema(high, 89)
  117. h_ema_144 = ta.ema(high, 144)
  118.  
  119. h_ema_avg = (h_ema_5 + h_ema_8 + h_ema_13 + h_ema_21 + h_ema_34 + h_ema_55 + h_ema_89 + h_ema_144) / 8
  120.  
  121. l_ema_5 = ta.ema(low, 5)
  122. l_ema_8 = ta.ema(low, 8)
  123. l_ema_13 = ta.ema(low, 13)
  124. l_ema_21 = ta.ema(low, 21)
  125. l_ema_34 = ta.ema(low, 34)
  126. l_ema_55 = ta.ema(low, 55)
  127. l_ema_89 = ta.ema(low, 89)
  128. l_ema_144 = ta.ema(low, 144)
  129.  
  130. l_ema_avg = (l_ema_5 + l_ema_8 + l_ema_13 + l_ema_21 + l_ema_34 + l_ema_55 + l_ema_89 + l_ema_144) / 8
  131.  
  132.  
  133. ema_basis = (h_ema_avg + l_ema_avg) / 2
  134.  
  135.  
  136. p_ema_basis = plot(_ema ? ema_basis : na, color=request.security(syminfo.tickerid, i_ema_tf, close > ema_basis[1] ? ema_bull_color : close < ema_basis[1] ? ema_bear_color : na), linewidth=2, title='EMA High+Low Average')
  137. p_ema_h = plot(_ema ? h_ema_avg : na, title='EMA-High', display=display.none)
  138. p_ema_l = plot(_ema ? l_ema_avg : na, title='EMA-Low', display=display.none)
  139.  
  140. fill(p_ema_h, p_ema_l, greater_ema_color, title='Greater EMA', transp=90)
  141.  
  142.  
  143. ////////////////////////////////////////////////////////////////////////////////
  144.  
  145.  
  146. ////HMA
  147. _hma = input.bool(defval=false, title='HMA', group='HMA')
  148. i_hma_tf = input.timeframe(title='Resolution for Basis Color', defval='', group='HMA', tooltip='Pick your time frame and the basis will be colored based on if price is above or below that previous timeframe\'s close.')
  149. hma_bull_color = input.color(color.new(#00bcd4, 50), title='Bullish', inline='basis', group='HMA')
  150. hma_bear_color = input.color(color.new(#e91e63, 50), title='Bearish', inline='basis', group='HMA')
  151. greater_hma_color = input.color(color.new(#b2b5be, 85), title='Greater HMA Color', inline='gma', group='HMA')
  152.  
  153.  
  154. h_hma_5 = ta.hma(high, 5)
  155. h_hma_8 = ta.hma(high, 8)
  156. h_hma_13 = ta.hma(high, 13)
  157. h_hma_21 = ta.hma(high, 21)
  158. h_hma_34 = ta.hma(high, 34)
  159. h_hma_55 = ta.hma(high, 55)
  160. h_hma_89 = ta.hma(high, 89)
  161. h_hma_144 = ta.hma(high, 144)
  162.  
  163. h_hma_avg = (h_hma_5 + h_hma_8 + h_hma_13 + h_hma_21 + h_hma_34 + h_hma_55 + h_hma_89 + h_hma_144) / 8
  164.  
  165. l_hma_5 = ta.hma(low, 5)
  166. l_hma_8 = ta.hma(low, 8)
  167. l_hma_13 = ta.hma(low, 13)
  168. l_hma_21 = ta.hma(low, 21)
  169. l_hma_34 = ta.hma(low, 34)
  170. l_hma_55 = ta.hma(low, 55)
  171. l_hma_89 = ta.hma(low, 89)
  172. l_hma_144 = ta.hma(low, 144)
  173.  
  174. l_hma_avg = (l_hma_5 + l_hma_8 + l_hma_13 + l_hma_21 + l_hma_34 + l_hma_55 + l_hma_89 + l_hma_144) / 8
  175.  
  176.  
  177. hma_basis = (h_hma_avg + l_hma_avg) / 2
  178.  
  179.  
  180. p_hma_basis = plot(_hma ? hma_basis : na, color=request.security(syminfo.tickerid, i_hma_tf, close > hma_basis[1] ? hma_bull_color : close < hma_basis[1] ? hma_bear_color : na), linewidth=2, title='HMA High+Low Average')
  181. p_hma_h = plot(_hma ? h_hma_avg : na, title='HMA-High', display=display.none)
  182. p_hma_l = plot(_hma ? l_hma_avg : na, title='HMA-Low', display=display.none)
  183.  
  184. fill(p_hma_h, p_hma_l, greater_hma_color, title='Greater HMA', transp=90)
  185.  
  186.  
  187. ////////////////////////////////////////////////////////////////////////////////
  188.  
  189.  
  190. ////SMA
  191. _sma = input.bool(defval=false, title='SMA', group='SMA')
  192. i_sma_tf = input.timeframe(title='Resolution for Basis Color', defval='', group='SMA', tooltip='Pick your time frame and the basis will be colored based on if price is above or below that previous timeframe\'s close.')
  193. sma_bull_color = input.color(color.new(#00bcd4, 50), title='Bullish', inline='basis', group='SMA')
  194. sma_bear_color = input.color(color.new(#e91e63, 50), title='Bearish', inline='basis', group='SMA')
  195. greater_sma_color = input.color(color.new(#b2b5be, 85), title='Greater SMA Color', inline='gma', group='SMA')
  196.  
  197.  
  198. h_sma_5 = ta.sma(high, 5)
  199. h_sma_8 = ta.sma(high, 8)
  200. h_sma_13 = ta.sma(high, 13)
  201. h_sma_21 = ta.sma(high, 21)
  202. h_sma_34 = ta.sma(high, 34)
  203. h_sma_55 = ta.sma(high, 55)
  204. h_sma_89 = ta.sma(high, 89)
  205. h_sma_144 = ta.sma(high, 144)
  206.  
  207. h_sma_avg = (h_sma_5 + h_sma_8 + h_sma_13 + h_sma_21 + h_sma_34 + h_sma_55 + h_sma_89 + h_sma_144) / 8
  208.  
  209. l_sma_5 = ta.sma(low, 5)
  210. l_sma_8 = ta.sma(low, 8)
  211. l_sma_13 = ta.sma(low, 13)
  212. l_sma_21 = ta.sma(low, 21)
  213. l_sma_34 = ta.sma(low, 34)
  214. l_sma_55 = ta.sma(low, 55)
  215. l_sma_89 = ta.sma(low, 89)
  216. l_sma_144 = ta.sma(low, 144)
  217.  
  218. l_sma_avg = (l_sma_5 + l_sma_8 + l_sma_13 + l_sma_21 + l_sma_34 + l_sma_55 + l_sma_89 + l_sma_144) / 8
  219.  
  220.  
  221. sma_basis = (h_sma_avg + l_sma_avg) / 2
  222.  
  223.  
  224. p_sma_basis = plot(_sma ? sma_basis : na, color=request.security(syminfo.tickerid, i_sma_tf, close > sma_basis[1] ? sma_bull_color : close < sma_basis[1] ? sma_bear_color : na), linewidth=2, title='SMA High+Low Average')
  225. p_sma_h = plot(_sma ? h_sma_avg : na, title='SMA-High', display=display.none)
  226. p_sma_l = plot(_sma ? l_sma_avg : na, title='SMA-Low', display=display.none)
  227.  
  228. fill(p_sma_h, p_sma_l, greater_sma_color, title='Greater SMA', transp=90)
  229.  
  230.  
  231.  
  232. ////////////////////////////////////////////////////////////////////////////////
  233.  
  234.  
  235. ////WMA
  236. _wma = input.bool(defval=false, title='WMA', group='WMA')
  237. i_wma_tf = input.timeframe(title='Resolution for Basis Color', defval='', group='WMA', tooltip='Pick your time frame and the basis will be colored based on if price is above or below that previous timeframe\'s close.')
  238. wma_bull_color = input.color(color.new(#00bcd4, 50), title='Bullish', inline='basis', group='WMA')
  239. wma_bear_color = input.color(color.new(#e91e63, 50), title='Bearish', inline='basis', group='WMA')
  240. greater_wma_color = input.color(color.new(#b2b5be, 85), title='Greater WMA Color', inline='gma', group='WMA')
  241.  
  242.  
  243. h_wma_5 = ta.wma(high, 5)
  244. h_wma_8 = ta.wma(high, 8)
  245. h_wma_13 = ta.wma(high, 13)
  246. h_wma_21 = ta.wma(high, 21)
  247. h_wma_34 = ta.wma(high, 34)
  248. h_wma_55 = ta.wma(high, 55)
  249. h_wma_89 = ta.wma(high, 89)
  250. h_wma_144 = ta.wma(high, 144)
  251.  
  252. h_wma_avg = (h_wma_5 + h_wma_8 + h_wma_13 + h_wma_21 + h_wma_34 + h_wma_55 + h_wma_89 + h_wma_144) / 8
  253.  
  254. l_wma_5 = ta.wma(low, 5)
  255. l_wma_8 = ta.wma(low, 8)
  256. l_wma_13 = ta.wma(low, 13)
  257. l_wma_21 = ta.wma(low, 21)
  258. l_wma_34 = ta.wma(low, 34)
  259. l_wma_55 = ta.wma(low, 55)
  260. l_wma_89 = ta.wma(low, 89)
  261. l_wma_144 = ta.wma(low, 144)
  262.  
  263. l_wma_avg = (l_wma_5 + l_wma_8 + l_wma_13 + l_wma_21 + l_wma_34 + l_wma_55 + l_wma_89 + l_wma_144) / 8
  264.  
  265.  
  266. wma_basis = (h_wma_avg + l_wma_avg) / 2
  267.  
  268.  
  269. p_wma_basis = plot(_wma ? wma_basis : na, color=request.security(syminfo.tickerid, i_wma_tf, close > wma_basis[1] ? wma_bull_color : close < wma_basis[1] ? wma_bear_color : na), linewidth=2, title='WMA High+Low Average')
  270. p_wma_h = plot(_wma ? h_wma_avg : na, title='WMA-High', display=display.none)
  271. p_wma_l = plot(_wma ? l_wma_avg : na, title='WMA-Low', display=display.none)
  272.  
  273. fill(p_wma_h, p_wma_l, greater_wma_color, title='Greater WMA', transp=90)
  274.  
  275.  
  276. ////////////////////////////////////////////////////////////////////////////////
  277.  
  278.  
  279. ////DEVIATION BANDS
  280. mult = input.float(defval=2.0, step=0.1, title='StdDev', group='Deviation Bands')
  281.  
  282.  
  283. ////INPUTS AND SELECTION FOR CALCULATION OF DEVIATION BANDS
  284. i_h_ma_avg = input.string(defval='EMA', options=['ALMA', 'EMA', 'HMA', 'SMA', 'WMA'], title='MA Input for Calculation of Upper Deviation Bands', group='Deviation Bands')
  285. h_ma_avg = i_h_ma_avg == 'ALMA' ? h_alma_avg : i_h_ma_avg == 'EMA' ? h_ema_avg : i_h_ma_avg == 'HMA' ? h_hma_avg : i_h_ma_avg == 'SMA' ? h_sma_avg : i_h_ma_avg == 'WMA' ? h_wma_avg : na
  286.  
  287. i_l_ma_avg = input.string(defval='EMA', options=['ALMA', 'EMA', 'HMA', 'SMA', 'WMA'], title='MA Input for Calculation of Lower Deviation Bands', group='Deviation Bands')
  288. l_ma_avg = i_l_ma_avg == 'ALMA' ? l_alma_avg : i_l_ma_avg == 'EMA' ? l_ema_avg : i_l_ma_avg == 'HMA' ? l_hma_avg : i_l_ma_avg == 'SMA' ? l_sma_avg : i_l_ma_avg == 'WMA' ? l_wma_avg : na
  289.  
  290.  
  291. ////COLOR INPUTS FOR BANDS
  292. upper_band_color = input.color(color.new(color.red, 35), title='Upper Band Border', inline='upper', group='Deviation Bands')
  293. inner_upper_band_color = input.color(color.new(color.gray, 35), title='Inner Band Border', inline='upper', group='Deviation Bands')
  294.  
  295. lower_band_color = input.color(color.new(color.blue, 35), title='Lower Band Border', inline='lower', group='Deviation Bands')
  296. inner_lower_band_color = input.color(color.new(color.gray, 35), title='Inner Band Border', inline='lower', group='Deviation Bands')
  297.  
  298. upper_band_fill = input.color(color.new(#ff5252, 85), title='Upper Band', inline='dev fill', group='Deviation Bands')
  299. lower_band_fill = input.color(color.new(#2196f3, 85), title='Lower Band', inline='dev fill', group='Deviation Bands')
  300.  
  301.  
  302. ////LENGTH AND BANDS CALCULATIONS
  303. len = (5 + 8 + 13 + 21 + 34 + 55 + 89 + 144) / 8
  304.  
  305. high_dev = mult * ta.stdev(high, len)
  306. low_dev = mult * ta.stdev(low, len)
  307.  
  308. ////
  309. upper_band = h_ma_avg + low_dev
  310. inner_upper_band = l_ma_avg + high_dev
  311.  
  312. inner_lower_band = h_ma_avg - high_dev
  313. lower_band = l_ma_avg - low_dev
  314.  
  315.  
  316. ////PLOTS & FILLS
  317. p_upper = plot(upper_band, color=upper_band_color, title='Upper Band', display=display.none)
  318. p_inner_upper = plot(inner_upper_band, color=inner_upper_band_color, title='Inner Upper Band', display=display.none)
  319.  
  320. p_lower = plot(lower_band, color=lower_band_color, title='Lower Band', display=display.none)
  321. p_inner_lower = plot(inner_lower_band, color=inner_lower_band_color, title='Inner Lower Band', display=display.none)
  322.  
  323. fill(p_upper, p_inner_upper, upper_band_fill, title='Upper Band', transp=90)
  324. fill(p_lower, p_inner_lower, lower_band_fill, title='Lower Band', transp=90)
  325.  
  326.  
  327. ////////////////////////////////////////////////////////////////////////////////
  328.  
  329.  
  330. ////233 MOVING AVERAGE AVERAGES
  331. ma = input.bool(defval=true, title='233 AVG?', group='233 MA AVGS', tooltip='An average of the 233 period EMA, SMA, and Kijun-sen.')
  332. ma_pos = input.color(color.new(color.blue, 50), title='Bullish', inline='ma', group='233 MA AVGS')
  333. ma_neg = input.color(color.new(color.red, 50), title='Bearish', inline='ma', group='233 MA AVGS')
  334.  
  335.  
  336. kijun = math.avg(ta.lowest(233), ta.highest(233))
  337.  
  338. ma_avgs = (ta.ema(close, 233) + ta.sma(close, 233) + kijun) / 3
  339. ma_color = close > ma_avgs ? ma_pos : ma_neg
  340.  
  341. plot(ma ? ma_avgs : na, color=ma_color, linewidth=2, title='233 MA AVG')
  342.  
  343.  
  344. ////ALERT CONDITIONS
  345. alertcondition(ta.cross(close, inner_upper_band), title='Price at Upper Band', message='Price is at the Upper Band.')
  346. alertcondition(ta.cross(close, inner_lower_band), title='Price at Inner Band', message='Price is at the Inner Band.')
  347. alertcondition(ta.cross(close, ma_avgs), title='Price crossed 233 MA', message='Price has crossed the 233 MA.')
  348.  
  349.  
  350.  
Advertisement
Add Comment
Please, Sign In to add comment