Module: Quant::Mixins::SuperSmoother
- Defined in:
- lib/quant/mixins/super_smoother.rb
Instance Method Summary collapse
-
#ss3p(source, prev_source, ssperiod) ⇒ Object
super smoother 3 pole.
- #super_smoother(source, prev_source, period) ⇒ Object
- #three_pole_super_smooth(source, prev_source, ssperiod) ⇒ Object
- #two_pole_super_smooth(source, prev_source, ssperiod) ⇒ Object
Instance Method Details
#ss3p(source, prev_source, ssperiod) ⇒ Object
super smoother 3 pole
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/quant/mixins/super_smoother.rb', line 62 def ss3p(source, prev_source, ssperiod) p0 = points[-1] p1 = points[-2] || p0 p2 = points[-3] || p1 p3 = points[-4] || p2 v0 = source.is_a?(Symbol) ? p0.send(source) : source return v0 if p0 == p3 a1 = Math.exp(-Math::PI / ssperiod) b1 = 2 * a1 * Math.cos(Math::PI * Math.sqrt(3) / ssperiod) c1 = a1**2 coef2 = b1 + c1 coef3 = -(c1 + b1 * c1) coef4 = c1**2 coef1 = 1 - coef2 - coef3 - coef4 v1 = p1.send(prev_source) v2 = p2.send(prev_source) v3 = p3.send(prev_source) (coef1 * v0) + (coef2 * v1) + (coef3 * v2) + (coef4 * v3) end |
#super_smoother(source, prev_source, period) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/quant/mixins/super_smoother.rb', line 6 def super_smoother(source, prev_source, period) v0 = (source.is_a?(Symbol) ? p0.send(source) : source).to_d return v0.to_f if points.size < 4 k = Math.exp(-Math.sqrt(2) * Math::PI / period) coef3 = -k**2 coef2 = 2.0 * k * Math.cos(Math.sqrt(2) * (Math::PI / 2) / period) coef1 = 1.0 - coef2 - coef3 v1 = p1.send(prev_source).to_d v2 = p2.send(prev_source).to_d p3.send(prev_source).to_d ((coef1 * (v0 + v1)) / 2.0 + (coef2 * v1) + (coef3 * v2)).to_f end |
#three_pole_super_smooth(source, prev_source, ssperiod) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/quant/mixins/super_smoother.rb', line 37 def three_pole_super_smooth(source, prev_source, ssperiod) a1 = Math.exp(-Math::PI / ssperiod) b1 = 2 * a1 * Math.cos(Math::PI * Math.sqrt(3) / ssperiod) c1 = a1**2 coef2 = b1 + c1 coef3 = -(c1 + b1 * c1) coef4 = c1**2 coef1 = 1 - coef2 - coef3 - coef4 p0 = prev(0) p1 = prev(1) p2 = prev(2) p3 = prev(3) v0 = source.is_a?(Symbol) ? p0.send(source) : source return v0 if [p0, p1, p2].include?(p3) v1 = p1.send(prev_source) v2 = p2.send(prev_source) v3 = p3.send(prev_source) (coef1 * v0) + (coef2 * v1) + (coef3 * v2) + (coef4 * v3) end |
#two_pole_super_smooth(source, prev_source, ssperiod) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/quant/mixins/super_smoother.rb', line 21 def two_pole_super_smooth(source, prev_source, ssperiod) return p1.send(source) if [p1 == p3] radians = Math::PI * Math.sqrt(2) / ssperiod a1 = Math.exp(-radians) coef2 = 2.0 * a1 * Math.cos(radians) coef3 = -a1 * a1 coef1 = 1.0 - coef2 - coef3 v0 = (p1.send(source) + p2.send(source)) / 2.0 v1 = p2.send(prev_source) v2 = p3.send(prev_source) (coef1 * v0) + (coef2 * v1) + (coef3 * v2) end |