Class: Quant::Indicators::Cci

Inherits:
Indicator show all
Defined in:
lib/quant/indicators/cci.rb

Overview

Correlation Cycle Index The very definition of a trend mode and a cycle mode makes it simple to create a state variable that identifies the market state. If the state is zero, the market is in a cycle mode. If the state is +1 the market is in a trend up. If the state is -1 the market is in a trend down.

SOURCE: www.mesasoftware.com/papers/CORRELATION%20AS%20A%20CYCLE%20INDICATOR.pdf

Constant Summary

Constants inherited from Indicator

Indicator::PRIORITIES

Constants included from Mixins::UniversalFilters

Mixins::UniversalFilters::K

Instance Attribute Summary

Attributes inherited from Indicator

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

Instance Method Summary collapse

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, #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



56
57
58
59
60
61
62
# File 'lib/quant/indicators/cci.rb', line 56

def compute
  p0.hp = two_pole_butterworth :input, previous: :hp, period: min_period

  compute_correlations
  compute_angle
  compute_state
end

#compute_angleObject



41
42
43
44
45
46
47
48
# File 'lib/quant/indicators/cci.rb', line 41

def compute_angle
  # Compute the angle as an arctangent and resolve the quadrant
  p0.angle = 90 + rad2deg(Math.atan(p0.real / p0.imag))
  p0.angle -= 180 if p0.imag > 0

  # Do not allow the rate change of angle to go negative
  p0.angle = p1.angle if (p0.angle < p1.angle) && (p1.angle - p0.angle) < 270
end

#compute_correlationsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/quant/indicators/cci.rb', line 27

def compute_correlations
  corr_real = Statistics::Correlation.new
  corr_imag = Statistics::Correlation.new
  arc = 2.0 * Math::PI / max_period.to_f
  (0...max_period).each do |period|
    radians = arc * period
    prev_hp = p(period).hp
    corr_real.add(prev_hp, Math.cos(radians))
    corr_imag.add(prev_hp, -Math.sin(radians))
  end
  p0.real = corr_real.coefficient
  p0.imag = corr_imag.coefficient
end

#compute_stateObject



50
51
52
53
54
# File 'lib/quant/indicators/cci.rb', line 50

def compute_state
  return unless (p0.angle - p1.angle).abs < 9

  p0.state = p0.angle < 0 ? -1 : 1
end

#max_periodObject



23
24
25
# File 'lib/quant/indicators/cci.rb', line 23

def max_period
  [min_period, dc_period].max
end