technical-analysis 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d1fc18d8c1ee447d239eee8a10f502b1903a4e3
4
- data.tar.gz: 6cd340eee546df96e2e90af61847f9c128be4285
3
+ metadata.gz: 579692a772601892cca3f48fa17737db6751b51a
4
+ data.tar.gz: f943f70ec1fe5cb26b54dc4e4528ce453505e2f0
5
5
  SHA512:
6
- metadata.gz: 61b922d73c8946eed92c655f62a1edc3b87b3a1ea2eec23c44336b3663737571c04c10de07005b9006147612e3cd1ef00f7bebf2b00342845b099318adfe4bec
7
- data.tar.gz: 6ebbb51e8fcc1ee5f7e80d1c0dda34ba0d2e499d92b17d47786e8d66b2e5404441f75dbc784ecfc7448cb688320618a8feba94c7ff9b16b09959d7fb4f6ba5e8
6
+ metadata.gz: 891b9907c6e16583f8757c7018bb022bc9a9cdb2990e1c74343aac962b85498b5d30725189a0c8266e6521f14613c97424e099a42a447800dd77f3b5efcc2b6c
7
+ data.tar.gz: fa1b723816d62c08b9e39275d26231caf6c708f964e38d7d490db88aa541f4b85692e87dd39c7d839f4b283f6885d2fa857439e25c8d0b620441199c17c300ba
@@ -20,10 +20,10 @@ module TechnicalAnalysis
20
20
  return true if (options.keys - valid_options).empty?
21
21
  raise ValidationError.new "Invalid options provided. Valid options are #{valid_options.join(", ")}"
22
22
  end
23
-
24
- def self.validate_date_time_key(data)
25
- unless data.all? { |row| row.keys.include? :date_time }
26
- raise ValidationError.new "Dataset must include date_time field with timestamps"
23
+
24
+ def self.validate_date_time_key(data, date_time_key=:date_time)
25
+ unless data.all? { |row| row.keys.include? date_time_key }
26
+ raise ValidationError.new "Dataset must include '#{date_time_key}' field with timestamps"
27
27
  end
28
28
  end
29
29
 
@@ -20,7 +20,7 @@ module TechnicalAnalysis
20
20
  #
21
21
  # @return [Array] An array of keys as symbols for valid options for this technical indicator
22
22
  def self.valid_options
23
- %i(period price_key)
23
+ %i(period price_key date_time_key)
24
24
  end
25
25
 
26
26
  # Validates the provided options for this technical indicator
@@ -48,16 +48,18 @@ module TechnicalAnalysis
48
48
  # @param data [Array] Array of hashes with keys (:date_time, :value)
49
49
  # @param period [Integer] The given period to calculate the EMA
50
50
  # @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
51
52
  #
52
53
  # @return [Array<EmaValue>] An array of EmaValue instances
53
- def self.calculate(data, period: 30, price_key: :value)
54
+ def self.calculate(data, period: 30, price_key: :value, date_time_key: :date_time)
54
55
  period = period.to_i
55
56
  price_key = price_key.to_sym
57
+ date_time_key = date_time_key.to_sym
56
58
  Validation.validate_numeric_data(data, price_key)
57
59
  Validation.validate_length(data, min_data_size(period: period))
58
- Validation.validate_date_time_key(data)
60
+ Validation.validate_date_time_key(data, date_time_key)
59
61
 
60
- data = data.sort_by { |row| row[:date_time] }
62
+ data = data.sort_by { |row| row[date_time_key] }
61
63
 
62
64
  output = []
63
65
  period_values = []
@@ -69,7 +71,7 @@ module TechnicalAnalysis
69
71
  ema = StockCalculation.ema(v[price_key], period_values, period, previous_ema)
70
72
  previous_ema = ema
71
73
 
72
- output << EmaValue.new(date_time: v[:date_time], ema: ema)
74
+ output << EmaValue.new(date_time: v[date_time_key], ema: ema)
73
75
  period_values.shift
74
76
  end
75
77
  end
@@ -20,7 +20,7 @@ module TechnicalAnalysis
20
20
  #
21
21
  # @return [Array] An array of keys as symbols for valid options for this technical indicator
22
22
  def self.valid_options
23
- %i(period price_key)
23
+ %i(period price_key date_time_key)
24
24
  end
25
25
 
26
26
  # Validates the provided options for this technical indicator
@@ -48,16 +48,18 @@ module TechnicalAnalysis
48
48
  # @param data [Array] Array of hashes with keys (:date_time, :value)
49
49
  # @param period [Integer] The given period to calculate the SMA
50
50
  # @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
51
52
  #
52
53
  # @return [Array<SmaValue>] An array of SmaValue instances
53
- def self.calculate(data, period: 30, price_key: :value)
54
+ def self.calculate(data, period: 30, price_key: :value, date_time_key: :date_time)
54
55
  period = period.to_i
55
56
  price_key = price_key.to_sym
57
+ date_time_key = date_time_key.to_sym
56
58
  Validation.validate_numeric_data(data, price_key)
57
59
  Validation.validate_length(data, min_data_size(period: period))
58
- Validation.validate_date_time_key(data)
60
+ Validation.validate_date_time_key(data, date_time_key)
59
61
 
60
- data = data.sort_by { |row| row[:date_time] }
62
+ data = data.sort_by { |row| row[date_time_key] }
61
63
 
62
64
  output = []
63
65
  period_values = []
@@ -65,7 +67,7 @@ module TechnicalAnalysis
65
67
  data.each do |v|
66
68
  period_values << v[price_key]
67
69
  if period_values.size == period
68
- output << SmaValue.new(date_time: v[:date_time], sma: ArrayHelper.average(period_values))
70
+ output << SmaValue.new(date_time: v[date_time_key], sma: ArrayHelper.average(period_values))
69
71
  period_values.shift
70
72
  end
71
73
  end
data/spec/spec_helper.rb CHANGED
@@ -6,12 +6,12 @@ class SpecHelper
6
6
  FLOAT_KEYS = [:open, :high, :low, :close].freeze
7
7
  INTEGER_KEYS = [:volume].freeze
8
8
 
9
- def self.get_test_data(*columns)
9
+ def self.get_test_data(*columns, date_time_key: :date_time)
10
10
  @data = CSV.read(TEST_DATA_PATH, headers: true)
11
11
  columns = columns.map(&:to_sym)
12
12
  output = []
13
13
  @data.each do |v|
14
- col_hash = { date_time: v["date_time"] }
14
+ col_hash = { date_time_key => v["date_time"] }
15
15
  columns.each do |col|
16
16
  value = v[col.to_s]
17
17
  value = value.to_f if FLOAT_KEYS.include?(col)
@@ -3,12 +3,12 @@ require 'spec_helper'
3
3
 
4
4
  describe 'Indicators' do
5
5
  describe "EMA" do
6
- input_data = SpecHelper.get_test_data(:close)
6
+ input_data = SpecHelper.get_test_data(:close, date_time_key: :timestep)
7
7
  indicator = TechnicalAnalysis::Ema
8
8
 
9
9
  describe 'Exponential Moving Average' do
10
10
  it 'Calculates EMA (5 day)' do
11
- output = indicator.calculate(input_data, period: 5, price_key: :close)
11
+ output = indicator.calculate(input_data, period: 5, price_key: :close, date_time_key: :timestep)
12
12
  normalized_output = output.map(&:to_hash)
13
13
 
14
14
  expected_output = [
@@ -92,11 +92,11 @@ describe 'Indicators' do
92
92
 
93
93
  it 'Returns the valid options' do
94
94
  valid_options = indicator.valid_options
95
- expect(valid_options).to eq(%i(period price_key))
95
+ expect(valid_options).to eq(%i(period price_key date_time_key))
96
96
  end
97
97
 
98
98
  it 'Validates options' do
99
- valid_options = { period: 22, price_key: :close }
99
+ valid_options = { period: 22, price_key: :close, date_time_key: :timestep }
100
100
  options_validated = indicator.validate_options(valid_options)
101
101
  expect(options_validated).to eq(true)
102
102
  end
@@ -38,7 +38,7 @@ describe 'Indicators' do
38
38
 
39
39
  it 'Calculates valid_options' do
40
40
  valid_options = indicator.calculate('sma', [], :valid_options, { period: 20, price_key: :close })
41
- expect(valid_options).to eq(%i(period price_key))
41
+ expect(valid_options).to eq(%i(period price_key date_time_key))
42
42
  end
43
43
 
44
44
  it 'Calculates validate_options' do
@@ -3,12 +3,12 @@ require 'spec_helper'
3
3
 
4
4
  describe 'Indicators' do
5
5
  describe "SMA" do
6
- input_data = SpecHelper.get_test_data(:close)
6
+ input_data = SpecHelper.get_test_data(:close, date_time_key: :timestep)
7
7
  indicator = TechnicalAnalysis::Sma
8
8
 
9
9
  describe 'Simple Moving Average' do
10
10
  it 'Calculates SMA (5 day)' do
11
- output = indicator.calculate(input_data, period: 5, price_key: :close)
11
+ output = indicator.calculate(input_data, period: 5, price_key: :close, date_time_key: :timestep)
12
12
  normalized_output = output.map(&:to_hash)
13
13
 
14
14
  expected_output = [
@@ -92,11 +92,11 @@ describe 'Indicators' do
92
92
 
93
93
  it 'Returns the valid options' do
94
94
  valid_options = indicator.valid_options
95
- expect(valid_options).to eq(%i(period price_key))
95
+ expect(valid_options).to eq(%i(period price_key date_time_key))
96
96
  end
97
97
 
98
98
  it 'Validates options' do
99
- valid_options = { period: 22, price_key: :close }
99
+ valid_options = { period: 22, price_key: :close, date_time_key: :timestep }
100
100
  options_validated = indicator.validate_options(valid_options)
101
101
  expect(options_validated).to eq(true)
102
102
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: technical-analysis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Intrinio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-27 00:00:00.000000000 Z
11
+ date: 2020-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '12.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '12.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement