Class: Quant::Indicators::DominantCycles::Acr

Inherits:
DominantCycle show all
Defined in:
lib/quant/indicators/dominant_cycles/acr.rb

Overview

Auto-Correlation Reversals is a method of computing the dominant cycle by correlating the data stream with itself delayed by a lag. Construction of the autocorrelation periodogram starts with the autocorrelation function using the minimum three bars of averaging. The cyclic information is extracted using a discrete Fourier transform (DFT) of the autocorrelation results.

Constant Summary collapse

BANDWIDTH_DEGREES =
370
BANDWIDTH_RADIANS =
BANDWIDTH_DEGREES * Math::PI / 180.0

Constants inherited from Indicator

Indicator::PRIORITIES

Constants included from Mixins::UniversalFilters

Mixins::UniversalFilters::K

Instance Attribute Summary

Attributes inherited from DominantCycle

#points

Attributes inherited from Indicator

#p0, #p1, #p2, #p3, #series, #source, #t0, #t1, #t2, #t3

Instance Method Summary collapse

Methods inherited from DominantCycle

#compute_input_data_points, #compute_mean_period, #compute_phase, #compute_quadrature_components, #compute_smooth_period, #constrain_period_bars, #constrain_period_magnitude_change, #dominant_cycle_indicator_class, #dominant_cycle_period, #points_class, #priority

Methods inherited from Indicator

#<<, #[], #adaptive_half_period, #adaptive_period, dependent_indicator_classes, depends_on, #dominant_cycle, #dominant_cycle_indicator_class, #dominant_cycle_kind, #each, #half_period, #indicator_name, #initialize, #input, #inspect, #max_period, #micro_period, #min_period, #p, #period_points, #pivot_kind, #points_class, #priority, register, #size, #t, #ticks, #values, #warmed_up?

Methods included from Mixins::FisherTransform

#fisher_transform, #inverse_fisher_transform, #relative_fisher_transform

Methods included from Mixins::Stochastic

#stochastic

Methods included from Mixins::SuperSmoother

#three_pole_super_smooth, #two_pole_super_smooth

Methods included from Mixins::HilbertTransform

#hilbert_transform

Methods included from Mixins::ExponentialMovingAverage

#exponential_moving_average

Methods included from Mixins::SimpleMovingAverage

#simple_moving_average

Methods included from Mixins::WeightedMovingAverage

#extended_weighted_moving_average, #weighted_moving_average

Methods included from Mixins::UniversalFilters

#universal_band_pass, #universal_ema, #universal_filter, #universal_one_pole_high_pass, #universal_one_pole_low_pass, #universal_two_pole_high_pass, #universal_two_pole_low_pass

Methods included from Mixins::ButterworthFilters

#three_pole_butterworth, #two_pole_butterworth

Methods included from Mixins::HighPassFilters

#high_pass_filter, #hpf2, #two_pole_high_pass_filter

Methods included from Mixins::Functions

#angle, #bars_to_alpha, #deg2rad, #period_to_alpha, #rad2deg

Constructor Details

This class inherits a constructor from Quant::Indicators::Indicator

Instance Method Details

#computeObject



90
91
92
93
94
95
96
97
98
# File 'lib/quant/indicators/dominant_cycles/acr.rb', line 90

def compute
  p0.hp = two_pole_high_pass_filter(:input, period: max_period)
  p0.filter = two_pole_butterworth(:hp, previous: :filter, period: min_period)

  compute_auto_correlations
  compute_powers
  compute_period
  compute_reversal
end

#compute_auto_correlationsObject



36
37
38
39
40
41
42
43
44
# File 'lib/quant/indicators/dominant_cycles/acr.rb', line 36

def compute_auto_correlations
  (min_period..max_period).each do |period|
    corr = Statistics::Correlation.new
    micro_period.times do |lookback_period|
      corr.add(p(lookback_period).filter, p(period + lookback_period).filter)
    end
    p0.corr[period] = corr.coefficient
  end
end

#compute_periodObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/quant/indicators/dominant_cycles/acr.rb', line 67

def compute_period
  (min_period..max_period).each do |period|
    if p0.pwr[period] >= 0.4
      p0.spx += (period * p0.pwr[period])
      p0.sp += p0.pwr[period]
    end
  end

  p0.interim_period = p0.sp.zero? ? p1.period : p0.spx / p0.sp
  p0.inst_period = two_pole_butterworth(:interim_period, previous: :period, period: min_period)
  p0.period = p0.inst_period.round(0)
end

#compute_powersObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/quant/indicators/dominant_cycles/acr.rb', line 46

def compute_powers
  p0.maxpwr = 0.995 * p1.maxpwr

  (min_period..max_period).each do |period|
    (micro_period..max_period).each do |n|
      radians = BANDWIDTH_RADIANS * n / period
      p0.cospart[period] += p0.corr[n] * Math.cos(radians)
      p0.sinpart[period] += p0.corr[n] * Math.sin(radians)
    end
    p0.sqsum[period] = p0.cospart[period]**2 + p0.sinpart[period]**2
    p0.r1[period] = (0.2 * p0.sqsum[period]**2) + (0.8 * p1.r1[period])
    p0.pwr[period] = p0.r1[period]
    p0.maxpwr = [p0.maxpwr, p0.r1[period]].max
  end
  return if p0.maxpwr.zero?

  (min_period..max_period).each do |period|
    p0.pwr[period] = p0.r1[period] / p0.maxpwr
  end
end

#compute_reversalObject



80
81
82
83
84
85
86
87
88
# File 'lib/quant/indicators/dominant_cycles/acr.rb', line 80

def compute_reversal
  sum_deltas = 0
  (min_period..max_period).each do |period|
    sc1 = (p0.corr[period] + 1) * 0.5
    sc2 = (p1.corr[period] + 1) * 0.5
    sum_deltas += 1 if (sc1 > 0.5 && sc2 < 0.5) || (sc1 < 0.5 && sc2 > 0.5)
  end
  p0.reversal = sum_deltas > 24
end