Class: TechnicalAnalysis::Rsi
- Defined in:
- lib/technical_analysis/indicators/rsi.rb
Overview
Relative Strength Index
Class Method Summary collapse
-
.calculate(data, period: 14, price_key: :value, date_time_key: :date_time) ⇒ Array<RsiValue>
Calculates the relative strength index for the data over the given period en.wikipedia.org/wiki/Relative_strength_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: 14, **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: 14, price_key: :value, date_time_key: :date_time) ⇒ Array<RsiValue>
Calculates the relative strength index for the data over the given period en.wikipedia.org/wiki/Relative_strength_index
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/technical_analysis/indicators/rsi.rb', line 54 def self.calculate(data, period: 14, price_key: :value, date_time_key: :date_time) period = period.to_i price_key = price_key.to_sym Validation.validate_numeric_data(data, price_key) Validation.validate_length(data, min_data_size(period: period)) Validation.validate_date_time_key(data, date_time_key) data = data.sort_by { |row| row[date_time_key] } output = [] prev_price = data.shift[price_key] prev_avg = nil price_changes = [] smoothing_period = period - 1 data.each do |v| price_change = (v[price_key] - prev_price) price_changes << price_change if price_changes.size == period if prev_avg.nil? avg_gain = ArrayHelper.average(price_changes.map { |pc| pc.positive? ? pc : 0 }) avg_loss = ArrayHelper.average(price_changes.map { |pc| pc.negative? ? pc.abs : 0 }) else if price_change > 0 current_loss = 0 current_gain = price_change elsif price_change < 0 current_loss = price_change.abs current_gain = 0 else current_loss = 0 current_gain = 0 end avg_gain = (((prev_avg[:gain] * smoothing_period) + current_gain) / period.to_f) avg_loss = (((prev_avg[:loss] * smoothing_period) + current_loss) / period.to_f) end if avg_loss == 0 rsi = 100 else rs = avg_gain / avg_loss rsi = (100.00 - (100.00 / (1.00 + rs))) end output << RsiValue.new(date_time: v[date_time_key], rsi: rsi) prev_avg = { gain: avg_gain, loss: avg_loss } price_changes.shift end prev_price = v[price_key] end output.sort_by(&:date_time).reverse end |
.indicator_name ⇒ String
Returns the name of the technical indicator
15 16 17 |
# File 'lib/technical_analysis/indicators/rsi.rb', line 15 def self.indicator_name "Relative Strength Index" end |
.indicator_symbol ⇒ String
Returns the symbol of the technical indicator
8 9 10 |
# File 'lib/technical_analysis/indicators/rsi.rb', line 8 def self.indicator_symbol "rsi" end |
.min_data_size(period: 14, **params) ⇒ Integer
Calculates the minimum number of observations needed to calculate the technical indicator
41 42 43 |
# File 'lib/technical_analysis/indicators/rsi.rb', line 41 def self.min_data_size(period: 14, **params) period.to_i + 1 end |
.valid_options ⇒ Array
Returns an array of valid keys for options for this technical indicator
22 23 24 |
# File 'lib/technical_analysis/indicators/rsi.rb', line 22 def self. %i(period price_key date_time_key) end |
.validate_options(options) ⇒ Boolean
Validates the provided options for this technical indicator
31 32 33 |
# File 'lib/technical_analysis/indicators/rsi.rb', line 31 def self.() Validation.(, ) end |