class StockCruncher::AlphaVantage

This is an data cruncher class for AlphaVantage API.

Constants

API_URL

Public Instance Methods

calculate_missing_data(hash) click to toggle source

Method to calculate missing data (previousClose, change, changePercent)

# 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) click to toggle source

Method to calculate change difference

# File lib/stockcruncher/alphavantage.rb, line 25
def change(value, base)
  (value - base).round(4).to_s
end
change_percent(value, base) click to toggle source

Method to calculate percentage of change

# 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) click to toggle source

Method to create a new hash from two arrays of keys and values

# File lib/stockcruncher/alphavantage.rb, line 35
def create_hash(descriptions, values)
  descriptions.split(',').zip(values.split(',')).to_h
end
crunch_daily(symbol, fullsize) click to toggle source

Main method to crunch data.

# 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) click to toggle source

Main method to crunch data.

# 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) click to toggle source

Method to generate missing data

# 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) click to toggle source

Set parameters of api call

# 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) click to toggle source

Method to transform raw data to constructed hash

# 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) click to toggle source

Method to transform daily result to nested hash

# 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) click to toggle source

Method to transform quote result to hash

# 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