Class: StockCruncher::AlphaVantage
- Defined in:
- lib/stockcruncher/alphavantage.rb
Overview
This is an data cruncher class for AlphaVantage API.
Constant Summary collapse
- API_URL =
'https://www.alphavantage.co/query?'
Instance Method Summary collapse
-
#calculate_missing_data(hash) ⇒ Object
Method to calculate missing data (previousClose, change, changePercent).
-
#change(value, base) ⇒ Object
Method to calculate change difference.
-
#change_percent(value, base) ⇒ Object
Method to calculate percentage of change.
-
#create_hash(descriptions, values) ⇒ Object
Method to create a new hash from two arrays of keys and values.
-
#crunch_daily(symbol, fullsize) ⇒ Object
Main method to crunch data.
-
#crunch_quote(symbol) ⇒ Object
Main method to crunch data.
-
#generate_missing_data(current, previous) ⇒ Object
Method to generate missing data.
-
#parameters(symbol, serie) ⇒ Object
Set parameters of api call.
-
#prepare_daily_timeserie(data) ⇒ Object
Method to transform raw data to constructed hash.
-
#transform_daily(rawdata) ⇒ Object
Method to transform daily result to nested hash.
-
#transform_quote(rawdata) ⇒ Object
Method to transform quote result to hash.
Methods inherited from Cruncher
Constructor Details
This class inherits a constructor from StockCruncher::Cruncher
Instance Method Details
#calculate_missing_data(hash) ⇒ Object
Method to calculate missing data (previousClose, change, changePercent)
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/stockcruncher/alphavantage.rb', line 12 def calculate_missing_data(hash) keys = hash.keys hash.each_with_index do |(date, v), index| prevday = keys[index + 1] next if prevday.nil? prevclose = hash[prevday]['close'] hash[date] = v.merge(generate_missing_data(v['close'], prevclose)) end hash end |
#change(value, base) ⇒ Object
Method to calculate change difference
25 26 27 |
# File 'lib/stockcruncher/alphavantage.rb', line 25 def change(value, base) (value - base).round(4).to_s end |
#change_percent(value, base) ⇒ Object
Method to calculate percentage of change
30 31 32 |
# File 'lib/stockcruncher/alphavantage.rb', line 30 def change_percent(value, base) ((value / base - 1) * 100).round(4).to_s end |
#create_hash(descriptions, values) ⇒ Object
Method to create a new hash from two arrays of keys and values
35 36 37 |
# File 'lib/stockcruncher/alphavantage.rb', line 35 def create_hash(descriptions, values) descriptions.split(',').zip(values.split(',')).to_h end |
#crunch_daily(symbol, fullsize) ⇒ Object
Main method to crunch data.
40 41 42 43 44 45 |
# File 'lib/stockcruncher/alphavantage.rb', line 40 def crunch_daily(symbol, fullsize) url = API_URL + parameters(symbol, 'TIME_SERIES_DAILY') url += "&datatype=csv&outputsize=#{fullsize ? 'full' : 'compact'}" res = request(url) transform_daily(res.body) end |
#crunch_quote(symbol) ⇒ Object
Main method to crunch data.
48 49 50 51 52 53 |
# File 'lib/stockcruncher/alphavantage.rb', line 48 def crunch_quote(symbol) url = API_URL + parameters(symbol, 'GLOBAL_QUOTE') url += '&datatype=csv' res = request(url) transform_quote(res.body) end |
#generate_missing_data(current, previous) ⇒ Object
Method to generate missing data
56 57 58 59 60 61 62 |
# File 'lib/stockcruncher/alphavantage.rb', line 56 def generate_missing_data(current, previous) { 'previousClose' => previous, 'change' => change(current.to_f, previous.to_f), 'changePercent' => change_percent(current.to_f, previous.to_f) } end |
#parameters(symbol, serie) ⇒ Object
Set parameters of api call
65 66 67 68 69 70 |
# File 'lib/stockcruncher/alphavantage.rb', line 65 def parameters(symbol, serie) p = "function=#{serie}" p += "&symbol=#{symbol}" p += "&apikey=#{@config[self.class.name.split('::').last]['apikey']}" p end |
#prepare_daily_timeserie(data) ⇒ Object
Method to transform raw data to constructed hash
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/stockcruncher/alphavantage.rb', line 73 def prepare_daily_timeserie(data) lines = data.split("\r\n") desc = lines.shift.split(',').drop(1) hash = {} lines.each do |line| values = line.split(',') date = values.shift hash[date] = desc.zip(values).to_h end hash end |
#transform_daily(rawdata) ⇒ Object
Method to transform daily result to nested hash
86 87 88 89 90 91 |
# File 'lib/stockcruncher/alphavantage.rb', line 86 def transform_daily(rawdata) raise StandardError, 'No data' if rawdata.match?(/Error Message/) values = prepare_daily_timeserie(rawdata) calculate_missing_data(values) end |
#transform_quote(rawdata) ⇒ Object
Method to transform quote result to hash
94 95 96 97 98 99 100 101 |
# File 'lib/stockcruncher/alphavantage.rb', line 94 def transform_quote(rawdata) raise StandardError, 'No data' if rawdata.match?(/{}/) values = create_hash(*rawdata.split("\r\n")) values['close'] = values.delete('price') values['changePercent'] = values['changePercent'].delete('%') values end |