alphavantage 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d51452a0f6881ff6e8367211af1f58c681251d0672c55126e6d1f6089d9d5c31
4
- data.tar.gz: 9cf0b3b24fb9eb32b153601875ab817f5b783ac9d9897c216d0b14b53324d788
3
+ metadata.gz: acaace4e62bf29e6aba9bd5998b4cefba6b05f51fcb7e249926d4ec595c7fae7
4
+ data.tar.gz: 6af34132223a9bff241714c23906351bcd5cc5aad91435ce8a04e04467b20ece
5
5
  SHA512:
6
- metadata.gz: 4efbdbda2a60d5daaf770924412c8546ed5e68154e0db241a655efcda6336f735736c4cf4a578d4e208e171d86256893feaee1048e2af06f30185e9ff60bca2d
7
- data.tar.gz: 4d4430af5318d46ac4acf197598c63bd4a57ec7a2ebb3e52ea696bfb193f67c4be1c83b2c4027b60af26b62174eadf73338ac63e0b26d3bbcdb90fe32824162f
6
+ metadata.gz: 989affa5607d4e8ded7df6dcd67d6087557950a3bcc17239b8a2484ddb82b495dadf9bdf3ffb1a0c9e7412178d58108c1e1108810f068fe7c7571f65b83443b6
7
+ data.tar.gz: cdf9b3bbb6247501d7c319569927fda4aa7691ea6adfcf38d0d7e62c09081fb6195fad871549aac665e72b3d6f18192ba19f555809596ca3ce58130f42149fbd
data/.gitignore CHANGED
@@ -11,4 +11,6 @@
11
11
  .rspec_status
12
12
 
13
13
  .byebug_history
14
- .DS_Store
14
+ .DS_Store
15
+ .ruby-version
16
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- alphavantage (1.0.2)
4
+ alphavantage (1.1.0)
5
5
  faraday (~> 1.4)
6
6
  hashie (~> 4.1)
7
7
 
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Gem Version](https://badge.fury.io/rb/alphavantage.svg)](https://badge.fury.io/rb/alphavantage)
1
2
  # Alpha Vantage Ruby Library
2
3
 
3
4
  The Alpha Vantage Ruby library provides convenient access to the [Alpha Vantage API](https://www.alphavantage.co/documentation/) from applications written in the Ruby language.
data/lib/alphavantage.rb CHANGED
@@ -9,4 +9,5 @@ require "alphavantage/validations"
9
9
  require "alphavantage/time_series"
10
10
  require "alphavantage/fundamental"
11
11
  require "alphavantage/forex"
12
- require "alphavantage/crypto"
12
+ require "alphavantage/crypto"
13
+ require "alphavantage/indicator"
@@ -0,0 +1,241 @@
1
+ module Alphavantage
2
+ class Indicator
3
+ include Validations
4
+
5
+ def initialize(symbol:,interval:)
6
+ @symbol = symbol
7
+ @interval = interval
8
+ end
9
+
10
+ def sma(time_period:,series_type:)
11
+ Client.get(params: {
12
+ function: __callee__.upcase,
13
+ symbol: @symbol,
14
+ interval: validate_indicator_interval(@interval),
15
+ time_period: validate_integer(label: 'time period', value: time_period),
16
+ series_type: validate_series_type(series_type)
17
+ })
18
+ end
19
+ alias :ema :sma
20
+ alias :wma :sma
21
+ alias :dema :sma
22
+ alias :tema :sma
23
+ alias :trima :sma
24
+ alias :kama :sma
25
+ alias :mama :sma
26
+ alias :t3 :sma
27
+ alias :rsi :sma
28
+ alias :mom :sma
29
+ alias :cmo :sma
30
+ alias :roc :sma
31
+ alias :rocr :sma
32
+ alias :trix :sma
33
+ alias :midpoint :sma
34
+
35
+ def macd(series_type:, fastperiod: 12, slowperiod: 26, signalperiod: 9)
36
+ Client.get(params: {
37
+ function: __callee__.upcase,
38
+ symbol: @symbol,
39
+ interval: validate_indicator_interval(@interval),
40
+ series_type: validate_series_type(series_type),
41
+ fastperiod: validate_integer(label: 'fastperiod', value: fastperiod),
42
+ slowperiod: validate_integer(label: 'slowperiod', value: slowperiod),
43
+ signalperiod: validate_integer(label: 'signalperiod', value: signalperiod)
44
+ })
45
+ end
46
+
47
+ MOVING_AVERAGE_TYPES = {
48
+ sma: 0,
49
+ ema: 1,
50
+ wma: 2,
51
+ dema: 3,
52
+ tema: 4,
53
+ trima: 5,
54
+ t3: 6,
55
+ kama: 7,
56
+ mama: 8
57
+ }
58
+
59
+ def macdext(
60
+ series_type:,
61
+ fastperiod: 12,
62
+ slowperiod: 26,
63
+ signalperiod: 9,
64
+ fastmatype: 'sma',
65
+ slowmatype: 'sma',
66
+ signalmatype: 'sma'
67
+ )
68
+ Client.get(params: {
69
+ function: __callee__.upcase,
70
+ symbol: @symbol,
71
+ interval: validate_indicator_interval(@interval),
72
+ series_type: validate_series_type(series_type),
73
+ fastperiod: validate_integer(label: 'fastperiod', value: fastperiod),
74
+ slowperiod: validate_integer(label: 'slowperiod', value: slowperiod),
75
+ signalperiod: validate_integer(label: 'signalperiod', value: signalperiod),
76
+ fastmatype: validate_mat(MOVING_AVERAGE_TYPES[fastmatype.to_sym]),
77
+ slowmatype: validate_mat(MOVING_AVERAGE_TYPES[slowmatype.to_sym]),
78
+ signalmatype: validate_mat(MOVING_AVERAGE_TYPES[signalmatype.to_sym])
79
+ })
80
+ end
81
+
82
+ def stoch(
83
+ fastkperiod: 5,
84
+ slowkperiod: 3,
85
+ slowdperiod: 3,
86
+ slowkmatype: 'sma',
87
+ slowdmatype: 'sma'
88
+ )
89
+ Client.get(params: {
90
+ function: __callee__.upcase,
91
+ symbol: @symbol,
92
+ interval: validate_indicator_interval(@interval),
93
+ fastkperiod: validate_integer(label: 'fastkperiod', value: fastkperiod),
94
+ slowkperiod: validate_integer(label: 'slowkperiod', value: slowkperiod),
95
+ slowdperiod: validate_integer(label: 'slowdperiod', value: slowdperiod),
96
+ slowkmatype: validate_mat(MOVING_AVERAGE_TYPES[slowkmatype.to_sym]),
97
+ slowdmatype: validate_mat(MOVING_AVERAGE_TYPES[slowdmatype.to_sym])
98
+ })
99
+ end
100
+
101
+ def stochf(
102
+ fastkperiod: 5,
103
+ fastdperiod: 3,
104
+ fastdmatype: 'sma'
105
+ )
106
+ Client.get(params: {
107
+ function: __callee__.upcase,
108
+ symbol: @symbol,
109
+ interval: validate_indicator_interval(@interval),
110
+ fastkperiod: validate_integer(label: 'fastkperiod', value: fastkperiod),
111
+ fastdperiod: validate_integer(label: 'fastdperiod', value: fastdperiod),
112
+ fastdmatype: validate_mat(MOVING_AVERAGE_TYPES[fastdmatype.to_sym])
113
+ })
114
+ end
115
+
116
+ def stochrsi(
117
+ time_period:,
118
+ series_type:,
119
+ fastkperiod: 5,
120
+ fastdperiod: 3,
121
+ fastdmatype: 'sma'
122
+ )
123
+ Client.get(params: {
124
+ function: __callee__.upcase,
125
+ symbol: @symbol,
126
+ interval: validate_indicator_interval(@interval),
127
+ time_period: validate_integer(label: 'time period', value: time_period),
128
+ series_type: validate_series_type(series_type),
129
+ fastkperiod: validate_integer(label: 'fastkperiod', value: fastkperiod),
130
+ fastdperiod: validate_integer(label: 'fastdperiod', value: fastdperiod),
131
+ fastdmatype: validate_mat(MOVING_AVERAGE_TYPES[fastdmatype.to_sym])
132
+ })
133
+ end
134
+
135
+ def willr(time_period:)
136
+ Client.get(params: {
137
+ function: __callee__.upcase,
138
+ symbol: @symbol,
139
+ interval: validate_indicator_interval(@interval),
140
+ time_period: validate_integer(label: 'time period', value: time_period)
141
+ })
142
+ end
143
+ alias :adx :willr
144
+ alias :adxr :willr
145
+ alias :aroon :willr
146
+ alias :aroonosc :willr
147
+ alias :mfi :willr
148
+ alias :dx :willr
149
+ alias :minus_di :willr
150
+ alias :plus_di :willr
151
+ alias :minus_dm :willr
152
+ alias :plus_dm :willr
153
+ alias :midprice :willr
154
+ alias :atr :willr
155
+ alias :natr :willr
156
+
157
+ def vwap
158
+ Client.get(params: {
159
+ function: __callee__.upcase,
160
+ symbol: @symbol,
161
+ interval: [:bop,:trange,:ad,:obv].include?(__callee__) ? validate_indicator_interval(@interval) : validate_interval(@interval)
162
+ })
163
+ end
164
+ alias :bop :vwap
165
+ alias :trange :vwap
166
+ alias :ad :vwap
167
+ alias :obv :vwap
168
+
169
+ def adosc(fastperiod: 3, slowperiod: 10)
170
+ Client.get(params: {
171
+ function: __callee__.upcase,
172
+ symbol: @symbol,
173
+ interval: validate_indicator_interval(@interval),
174
+ fastperiod: validate_integer(label: 'fastperiod', value: fastperiod),
175
+ slowperiod: validate_integer(label: 'slowperiod', value: slowperiod)
176
+ })
177
+ end
178
+
179
+ def ht_trendline(series_type:)
180
+ Client.get(params: {
181
+ function: __callee__.upcase,
182
+ symbol: @symbol,
183
+ interval: validate_indicator_interval(@interval),
184
+ series_type: validate_series_type(series_type)
185
+ })
186
+ end
187
+ alias :ht_sine :ht_trendline
188
+ alias :ht_trendmode :ht_trendline
189
+ alias :ht_dcperiod :ht_trendline
190
+ alias :ht_dcphase :ht_trendline
191
+ alias :ht_phasor :ht_trendline
192
+
193
+ def apo(series_type:, fastperiod: 12, slowperiod: 26, matype: 'sma')
194
+ Client.get(params: {
195
+ function: __callee__.upcase,
196
+ symbol: @symbol,
197
+ interval: validate_indicator_interval(@interval),
198
+ series_type: validate_series_type(series_type),
199
+ fastperiod: validate_integer(label: 'fastperiod', value: fastperiod),
200
+ slowperiod: validate_integer(label: 'slowperiod', value: slowperiod),
201
+ matype: validate_mat(MOVING_AVERAGE_TYPES[matype.to_sym])
202
+ })
203
+ end
204
+ alias :ppo :apo
205
+
206
+ def bbands(time_period:, series_type:, nbdevup: 2, nbdevdn: 2, matype: 'sma')
207
+ Client.get(params: {
208
+ function: __callee__.upcase,
209
+ symbol: @symbol,
210
+ interval: validate_indicator_interval(@interval),
211
+ time_period: validate_integer(label: 'time period', value: time_period),
212
+ series_type: validate_series_type(series_type),
213
+ nbdevup: validate_integer(label: 'nbdevup', value: nbdevup),
214
+ nbdevdn: validate_integer(label: 'nbdevdn', value: nbdevdn),
215
+ matype: validate_mat(MOVING_AVERAGE_TYPES[matype.to_sym])
216
+ })
217
+ end
218
+
219
+ def sar(acceleration: 0.01, maximum: 0.20)
220
+ Client.get(params: {
221
+ function: __callee__.upcase,
222
+ symbol: @symbol,
223
+ interval: validate_indicator_interval(@interval),
224
+ acceleration: validate_integer(label: 'acceleration', value: acceleration),
225
+ maximum: validate_integer(label: 'maximum', value: maximum)
226
+ })
227
+ end
228
+
229
+ def ultosc(timeperiod1: 7, timeperiod2: 14, timeperiod3: 28)
230
+ Client.get(params: {
231
+ function: __callee__.upcase,
232
+ symbol: @symbol,
233
+ interval: validate_indicator_interval(@interval),
234
+ timeperiod1: validate_integer(label: 'timeperiod1', value: timeperiod1),
235
+ timeperiod2: validate_integer(label: 'timeperiod2', value: timeperiod2),
236
+ timeperiod3: validate_integer(label: 'timeperiod3', value: timeperiod3)
237
+ })
238
+ end
239
+
240
+ end
241
+ end
@@ -20,7 +20,7 @@ module Alphavantage
20
20
  end
21
21
 
22
22
  def sanitize_key(key)
23
- key.tr('.()','').gsub(/^\d+.?\s/, "").tr(' ','_')
23
+ key.tr('.():/','').gsub(/^\d+.?\s/, "").tr(' ','_')
24
24
  end
25
25
 
26
26
  def is_date?(key)
@@ -7,7 +7,9 @@ module Alphavantage
7
7
  end.flatten
8
8
 
9
9
  VALID_INTERVALS = %w{ 1min 5min 15min 30min 60min }
10
- VALID_OUTPUTSIZES = %{ compact full }
10
+ VALID_INDICATOR_INTERVALS = VALID_INTERVALS + %w{ daily weekly monthly }
11
+ VALID_OUTPUTSIZES = %w{ compact full }
12
+ VALID_SERIES_TYPE = %w{ close open high low }
11
13
 
12
14
  private
13
15
 
@@ -25,5 +27,29 @@ module Alphavantage
25
27
  raise Alphavantage::Error, "Invalid outputsize given." unless VALID_OUTPUTSIZES.include?(outputsize)
26
28
  outputsize
27
29
  end
30
+
31
+ def validate_indicator_interval(interval)
32
+ raise Alphavantage::Error, "Invalid interval given." unless VALID_INDICATOR_INTERVALS.include?(interval)
33
+ interval
34
+ end
35
+
36
+ def validate_series_type(series_type)
37
+ raise Alphavantage::Error, "Invalid series type given." unless VALID_SERIES_TYPE.include?(series_type)
38
+ series_type
39
+ end
40
+
41
+ def validate_integer(label:,value:)
42
+ raise Alphavantage::Error, "Invalid #{label} given. Must be integer." unless is_integer?(value)
43
+ value
44
+ end
45
+
46
+ def validate_mat(moving_average_type)
47
+ raise Alphavantage::Error, "Invalid moving average type given." if !(0..8).include?(moving_average_type)
48
+ moving_average_type
49
+ end
50
+
51
+ def is_integer?(str)
52
+ Integer(str) rescue false
53
+ end
28
54
  end
29
55
  end
@@ -1,3 +1,3 @@
1
1
  module Alphavantage
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alphavantage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Teh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-25 00:00:00.000000000 Z
11
+ date: 2021-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -135,6 +135,7 @@ files:
135
135
  - lib/alphavantage/error.rb
136
136
  - lib/alphavantage/forex.rb
137
137
  - lib/alphavantage/fundamental.rb
138
+ - lib/alphavantage/indicator.rb
138
139
  - lib/alphavantage/normalize_key.rb
139
140
  - lib/alphavantage/time_series.rb
140
141
  - lib/alphavantage/validations.rb