Class: TechnicalAnalysis::Cci

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

Class Method Summary collapse

Methods inherited from Indicator

find, roster

Class Method Details

.calculate(data, period: 20, constant: 0.015) ⇒ Array<CciValue>

Calculates the commodity channel index (CCI) for the data over the given period en.wikipedia.org/wiki/Commodity_channel_index

Parameters:

  • data (Array)

    Array of hashes with keys (:date_time, :high, :low, :close)

  • period (Integer) (defaults to: 20)

    The given period to calculate the CCI

  • constant (Float) (defaults to: 0.015)

    The given constant to ensure that approximately 70 to 80 percent of CCI values would fall between −100 and +100

Returns:

  • (Array<CciValue>)

    An array of CciValue instances



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/technical_analysis/indicators/cci.rb', line 53

def self.calculate(data, period: 20, constant: 0.015)
  period = period.to_i
  constant = constant.to_f
  Validation.validate_numeric_data(data, :high, :low, :close)
  Validation.validate_length(data, min_data_size(period: period))
  Validation.validate_date_time_key(data)

  data = data.sort_by { |row| row[:date_time] }

  output = []
  typical_prices = []

  data.each do |v|
    typical_price = StockCalculation.typical_price(v)
    typical_prices << typical_price

    if typical_prices.size == period
      period_sma = ArrayHelper.average(typical_prices)
      mean_deviation = ArrayHelper.mean(typical_prices.map { |tp| (tp - period_sma).abs })
      cci = (typical_price - period_sma) / (constant * mean_deviation)

      output << CciValue.new(date_time: v[:date_time], cci: cci)

      typical_prices.shift
    end
  end

  output.sort_by(&:date_time).reverse
end

.indicator_nameString

Returns the name of the technical indicator

Returns:

  • (String)

    A string of the name of the technical indicator



14
15
16
# File 'lib/technical_analysis/indicators/cci.rb', line 14

def self.indicator_name
  "Commodity Channel Index"
end

.indicator_symbolString

Returns the symbol of the technical indicator

Returns:

  • (String)

    A string of the symbol of the technical indicator



7
8
9
# File 'lib/technical_analysis/indicators/cci.rb', line 7

def self.indicator_symbol
  "cci"
end

.min_data_size(period: 20, **params) ⇒ Integer

Calculates the minimum number of observations needed to calculate the technical indicator

Parameters:

  • options (Hash)

    The options for the technical indicator

Returns:

  • (Integer)

    Returns the minimum number of observations needed to calculate the technical indicator based on the options provided



40
41
42
# File 'lib/technical_analysis/indicators/cci.rb', line 40

def self.min_data_size(period: 20, **params)
  period.to_i
end

.valid_optionsArray

Returns an array of valid keys for options for this technical indicator

Returns:

  • (Array)

    An array of keys as symbols for valid options for this technical indicator



21
22
23
# File 'lib/technical_analysis/indicators/cci.rb', line 21

def self.valid_options
  %i(period constant)
end

.validate_options(options) ⇒ Boolean

Validates the provided options for this technical indicator

Parameters:

  • options (Hash)

    The options for the technical indicator to be validated

Returns:

  • (Boolean)

    Returns true if options are valid or raises a ValidationError if they’re not



30
31
32
# File 'lib/technical_analysis/indicators/cci.rb', line 30

def self.validate_options(options)
  Validation.validate_options(options, valid_options)
end