xmd79

Epanechnikov algo fct

May 6th, 2023
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 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. // © peacefulLizard50262
  3.  
  4. //@version=5
  5. indicator("Epanechnikov algo fct")
  6. import lastguru/DominantCycle/2 as d
  7.  
  8. sine_wave(_wave_height, _wave_duration, _phase_shift, _phase_shift_2) =>
  9. _pi = math.pi
  10. _w = 2 * _pi / _wave_duration
  11. _sine_wave = _wave_height * math.sin(_w * bar_index + _phase_shift + _phase_shift_2)
  12. _sine_wave
  13.  
  14. // Epanechnikov kernel function
  15. epanechnikov_kernel(_src, _size, _h, _r) =>
  16. _currentWeight = 0.0
  17. _cumulativeWeight = 0.0
  18. for i = 0 to _size
  19. y = _src[i]
  20. u = math.pow(i, 2) / (math.pow(_h, 2) * _r)
  21. w = (u >= 1) ? 0 : (3. / 4) * (1 - math.pow(u, 2))
  22. _currentWeight := _currentWeight + y * w
  23. _cumulativeWeight := _cumulativeWeight + w
  24. _currentWeight / _cumulativeWeight
  25.  
  26. phase = input.float(-45, "Phase") * math.pi/180
  27.  
  28. // Estimations
  29. epk = epanechnikov_kernel(close, 50, 2, 1)
  30.  
  31. source = epk - ta.sma(epk, 9)
  32.  
  33. cycle = math.round(d.mamaPeriod(source, 1, 2048))
  34. var cycles = array.new<float>(1)
  35. var count_cycles = array.new<int>(1)
  36. if not array.includes(cycles, cycle)
  37. array.push(cycles, cycle)
  38. array.push(count_cycles, 1)
  39.  
  40. else
  41. index = array.indexof(cycles, cycle)
  42. array.set(count_cycles, index, array.get(count_cycles, index) + 1)
  43.  
  44. max_index = array.indexof(count_cycles, array.max(count_cycles))
  45. max_cycle = array.get(cycles, max_index)
  46.  
  47. sine = sine_wave(ta.max(math.abs(source)), max_cycle, phase, 0)
  48. plot(source)
  49. plot(sine, color = color.orange)
Advertisement
Add Comment
Please, Sign In to add comment