I'm building a Rails app where I want to download historical financial data. I've found this URL that I can use:
Yahoo Finance API - historical
but I haven't found any way to download multiple financial data simultaneously. The only thing that I have found is to download multiple quotes, like so:
Is there a way to download multiple historical data simultaneously?
(The reason why I ask is because I want to upload the data to a SQLite database and use that in my app. Of course I can download the data individually, stock by stock, but it would be quite tedious.
Now, I've found this Ruby script on the internet:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'sqlite3'
START_DATE=['01','01','2014']
END_DATE=['01','05','2014']
YURL="http://ichart.finance.yahoo.com/table.csv?a=#{START_DATE[0]}&b=#{START_DATE[1]}&c=#{START_DATE[2]}&d=#{END_DATE[0]}&e=#{END_DATE[1]}&f=#{END_DATE[2]}&g=d&ignore=.csv&s="
DBNAME = "data-hold/sp500-data.sqlite"
DB = SQLite3::Database.new( DBNAME )
SUBDIR = 'data-hold/yahoo-data'
Dir.mkdir(SUBDIR) unless File.exists?SUBDIR
DB.execute("SELECT DISTINCT ticker_symbol from companies").each do |sym|
fname = "#{SUBDIR}/#{sym}.csv"
unless File.exists?fname
puts fname
d = open("#{YURL}#{sym}")
File.open(fname, 'w') do |ofile|
ofile.write(d.read)
sleep(1.5 + rand)
end
end
end
but when I run it Rails throws me an error:
bad URI (is not URI?):
So my question is basically: What is the best way to solve the problem?)
Most financial data providers limit historical downloads to one ticker per API call. You can imagine that pulling multiple time series in the same JSON output is confusing and puts heavy loads on servers.
There is a Ruby wrapper of Intrinio's API on github, you can see it here, that will make it easier to get historical time series data.
This will pull Apple's price history:
curl "https://api.intrinio.com/prices?ticker=AAPL" -u "APIusername:APIpassword"
This will pull the current price dimensionally, for up to 150 stocks:
curl "https://api.intrinio.com/data_point?ticker=AAPL,MSFT,T,XOM&item=last_price" -u "APIusername:APIpassword"
You will, of course, need to exchange your own API keys in the curl, but using the github wrapper will make it easy. API username and password are free.