Skip to content

Commit 4203453

Browse files
authored
Merge pull request #45 from vertfin/jms/rsi_date_time_key
Add date_time_key support to RSI indicator
2 parents 2ba75f6 + abf8f26 commit 4203453

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

lib/technical_analysis/indicators/rsi.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def self.indicator_name
2020
#
2121
# @return [Array] An array of keys as symbols for valid options for this technical indicator
2222
def self.valid_options
23-
%i(period price_key)
23+
%i(period price_key date_time_key)
2424
end
2525

2626
# Validates the provided options for this technical indicator
@@ -48,16 +48,17 @@ def self.min_data_size(period: 14, **params)
4848
# @param data [Array] Array of hashes with keys (:date_time, :value)
4949
# @param period [Integer] The given period to calculate the RSI
5050
# @param price_key [Symbol] The hash key for the price data. Default :value
51+
# @param date_time_key [Symbol] The hash key for the date time data. Default :date_time
5152
#
5253
# @return [Array<RsiValue>] An array of RsiValue instances
53-
def self.calculate(data, period: 14, price_key: :value)
54+
def self.calculate(data, period: 14, price_key: :value, date_time_key: :date_time)
5455
period = period.to_i
5556
price_key = price_key.to_sym
5657
Validation.validate_numeric_data(data, price_key)
5758
Validation.validate_length(data, min_data_size(period: period))
58-
Validation.validate_date_time_key(data)
59+
Validation.validate_date_time_key(data, date_time_key)
5960

60-
data = data.sort_by { |row| row[:date_time] }
61+
data = data.sort_by { |row| row[date_time_key] }
6162

6263
output = []
6364
prev_price = data.shift[price_key]
@@ -96,7 +97,7 @@ def self.calculate(data, period: 14, price_key: :value)
9697
rsi = (100.00 - (100.00 / (1.00 + rs)))
9798
end
9899

99-
output << RsiValue.new(date_time: v[:date_time], rsi: rsi)
100+
output << RsiValue.new(date_time: v[date_time_key], rsi: rsi)
100101

101102
prev_avg = { gain: avg_gain, loss: avg_loss }
102103
price_changes.shift

spec/technical_analysis/indicators/rsi_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
describe 'Indicators' do
55
describe "RSI" do
6-
input_data = SpecHelper.get_test_data(:close)
6+
input_data = SpecHelper.get_test_data(:close, date_time_key: :timestep)
77
indicator = TechnicalAnalysis::Rsi
88

99
describe 'Relative Strength Index' do
1010
it 'Calculates RSI (14 day)' do
11-
output = indicator.calculate(input_data, period: 14, price_key: :close)
11+
output = indicator.calculate(input_data, period: 14, price_key: :close, date_time_key: :timestep)
1212
normalized_output = output.map(&:to_hash)
1313

1414
expected_output = [
@@ -67,7 +67,7 @@
6767
end
6868

6969
it "Throws exception if not enough data" do
70-
expect {indicator.calculate(input_data, period: input_data.size+2, price_key: :close)}.to raise_exception(TechnicalAnalysis::Validation::ValidationError)
70+
expect {indicator.calculate(input_data, period: input_data.size+2, price_key: :close, date_time_key: :time)}.to raise_exception(TechnicalAnalysis::Validation::ValidationError)
7171
end
7272

7373
it 'Returns the symbol' do
@@ -82,11 +82,11 @@
8282

8383
it 'Returns the valid options' do
8484
valid_options = indicator.valid_options
85-
expect(valid_options).to eq(%i(period price_key))
85+
expect(valid_options).to eq(%i(period price_key date_time_key))
8686
end
8787

8888
it 'Validates options' do
89-
valid_options = { period: 22, price_key: :close }
89+
valid_options = { period: 22, price_key: :close, date_time_key: :timespec }
9090
options_validated = indicator.validate_options(valid_options)
9191
expect(options_validated).to eq(true)
9292
end

0 commit comments

Comments
 (0)