xmd79

ma_function

Nov 17th, 2021
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © boitoki
  3.  
  4. //@version=5
  5.  
  6. // @description TODO: This library is a package of various MAs.
  7. library("ma_function")
  8.  
  9. kama(xPrice, Length)=>
  10. xvnoise = math.abs(xPrice - xPrice[1])
  11. nfastend = 0.666
  12. nslowend = 0.0645
  13. nsignal = math.abs(xPrice - xPrice[Length])
  14. nnoise = math.sum(xvnoise, Length)
  15. nefratio = (nnoise != 0 ? nsignal / nnoise : 0)
  16. nsmooth = math.pow(nefratio * (nfastend - nslowend) + nslowend, 2)
  17. float nAMA = na
  18. nAMA := nz(nAMA[1]) + nsmooth * (xPrice - nz(nAMA[1]))
  19.  
  20. zlema (src, length) =>
  21. zxLag = length/2==math.round(length/2) ? length/2 : (length - 1) / 2
  22. zxEMAData = (src + (src - src[zxLag]))
  23. ZLEMA = ta.ema(zxEMAData, length)
  24.  
  25. mav (source, length) =>
  26. ((ta.sma(source, length)[1]*(length-1)) + source) / length
  27.  
  28. dema (src, len) =>
  29. e = ta.ema(src, len)
  30. 2 * e - ta.ema(e, len)
  31.  
  32. tema (src, len) =>
  33. e = ta.ema(src, len)
  34. 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
  35.  
  36. hma (src, len) =>
  37. ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
  38.  
  39. tma (source, length) =>
  40. ta.sma(ta.sma(source, math.ceil(length / 2)), math.floor(length / 2) + 1)
  41.  
  42. // @function This function provides the MA specified in the argument.
  43. // @param name MA's name
  44. // @param source Source value
  45. // @param length Look back periods
  46. // @returns Ma value
  47. export ma_function (simple string name, float source, simple int length) =>
  48. switch name
  49. 'SMA' => ta.sma(source, length)
  50. 'EMA' => ta.ema(source, length)
  51. 'RMA' => ta.rma(source, length)
  52. 'WMA' => ta.wma(source, length)
  53. 'LSMA' => ta.linreg(source, length, 0)
  54. 'ALMA' => ta.alma(source, length, 0.85, 6)
  55. 'TMA' => tma(source, length)
  56. 'DEMA' => dema(source, length)
  57. 'TEMA' => tema(source, length)
  58. 'MAV' => mav(source, length)
  59. 'KAMA' => kama(source, length)
  60. 'ZZLEMA' => zlema(source, length)
  61. 'HMA' => hma(source, length)
  62.  
  63. plot(ma_function('SMA', close, 10), color=color.red)
  64. plot(ma_function('MAV', close, 10), color=color.orange)
  65. plot(ma_function('KAMA', close, 10), color=color.blue)
  66. plot(ma_function('ZZLEMA', close, 10), color=color.green)
  67.  
  68.  
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment