Advertisement
PineCoders

HEMA vs HMA

Jan 15th, 2020
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 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. // © alexgrover
  3. //@version=4
  4. study("Hull Estimate","HEMA",true)
  5. runEstimate = input(true, "Run HEMA instead of HMA")
  6. length = input(50)
  7. //----
  8. loopCount = input(400, "Loop iterations per bar")
  9.  
  10. // —————————— ▼▼▼▼▼ Calculate run time of the script (insert this snippet after your "input()" calls).
  11. // Produces 3 values:
  12. // _totalTime = total elapsed time (ms)
  13. // _msPerBar = avg time per bar (ms)
  14. // _barsTimed = bars timed.
  15. // Get time at first bar.
  16. var _timeBegin = timenow
  17. // Get ms elapsed since first bar.
  18. var _timeElapsed = 0.
  19. if not barstate.islast
  20. _timeElapsed := timenow - _timeBegin
  21. // Calculate avg/bar only when time changes.
  22. var _msPerBar = 0.
  23. // Total bars timed before last change in "timenow".
  24. var _barsTimed = 0
  25. // ————— Bars elapsed since last change in "timenow".
  26. var _barsNotTimed = 0
  27. if change(_timeElapsed)
  28. _barsTimed := bar_index + 1
  29. _msPerBar := _timeElapsed / _barsTimed
  30. // ————— In between time changes, which only occur every second, estimate elapsed time using avg time per bar.
  31. if not barstate.islast
  32. // Bars elapsed since last change of time.
  33. _barsNotTimed := bar_index + 1 - _barsTimed
  34. // ————— Add (bars since "timenow" change * avg bar time) to time elapsed since last "timenow" change to get better estimate of total time elapsed.
  35. _totalTime = _timeElapsed + (_barsNotTimed * _msPerBar)
  36. // —————————— Display results in one of 3 modes (comment out those you don't need).
  37. _msPerBarColor = _msPerBar > 50 ? color.red : _msPerBar > 5 ? color.maroon : _msPerBar == 0 ? color.lime : color.green
  38. // ————— Mode 1: Print label at the end of chart.
  39. if barstate.islast
  40. var _label_text = tostring(_msPerBar, "Avg time per bar\n#.#### ms\n\n") + tostring(_totalTime / 1000, "Time elapsed\n#.#### seconds\n\n") + tostring(_barsTimed + _barsNotTimed, "Bars analyzed\n#")
  41. var label _timeLabel = label.new(bar_index, na, _label_text, xloc.bar_index, yloc.belowbar, style = label.style_none, textcolor = _msPerBarColor)
  42. label.set_xy(_timeLabel, bar_index, na)
  43. // ————— Mode 2: Plot elapsed time.
  44. plot(_totalTime, "Execution time (ms)", color.gray)
  45. // ————— Mode 3: Print Data Window values.
  46. plotchar(_msPerBar, "Avg time / bar (ms)", "", location.top, _msPerBarColor)
  47. plotchar(_totalTime, "Execution time (ms)", "", location.top)
  48. plotchar(_barsTimed, "Bars timed", "", location.top)
  49. // —————————— ▲▲▲▲▲
  50.  
  51.  
  52. // ——————————————————————————————————————————————————
  53. // ————— Your script code goes here.
  54. var a = 0.
  55. src = input(close)
  56. if runEstimate
  57. for i = 0 to loopCount
  58. a := a + (3*wma(src,length/2) - 2*ema(src,length/2))
  59. else
  60. for i = 0 to loopCount
  61. a := a + wma(2*wma(src, length/2)-wma(src, length), round(sqrt(length)))
  62.  
  63. plot(a,"",#e91e63,2,transp=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement