Class: TechnicalAnalysis::Cci
- Defined in:
- lib/technical_analysis/indicators/cci.rb
Class Method Summary collapse
-
.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.
-
.indicator_name ⇒ String
Returns the name of the technical indicator.
-
.indicator_symbol ⇒ String
Returns the symbol of the technical indicator.
-
.min_data_size(period: 20, **params) ⇒ Integer
Calculates the minimum number of observations needed to calculate the technical indicator.
-
.valid_options ⇒ Array
Returns an array of valid keys for options for this technical indicator.
-
.validate_options(options) ⇒ Boolean
Validates the provided options for this technical indicator.
Methods inherited from Indicator
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
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_name ⇒ String
Returns 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_symbol ⇒ String
Returns 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
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_options ⇒ Array
Returns an array of valid keys for options for this technical indicator
21 22 23 |
# File 'lib/technical_analysis/indicators/cci.rb', line 21 def self. %i(period constant) end |
.validate_options(options) ⇒ Boolean
Validates the provided options for this technical indicator
30 31 32 |
# File 'lib/technical_analysis/indicators/cci.rb', line 30 def self.() Validation.(, ) end |