Created
March 11, 2016 14:14
-
-
Save Deepakkoli93/44a70a86a528978a0649 to your computer and use it in GitHub Desktop.
Basic stock reader from yahoo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'daru' | |
require 'open-uri' | |
require 'csv' | |
@yahoo_base_url = "http://ichart.finance.yahoo.com/table.csv?" | |
def from_yahoo(sym, options) | |
params = {} | |
params[:s] = sym | |
if options[:from] | |
start_date = DateTime.parse(options[:from]) | |
params[:a] = start_date.month-1 | |
params[:b] = start_date.day | |
params[:c] = start_date.year | |
end | |
if options[:to] | |
end_date = DateTime.parse(options[:to]) | |
params[:d] = end_date.month-1 | |
params[:e] = end_date.day | |
params[:f] = end_date.year | |
end | |
url = @yahoo_base_url+params.map{|key, val| "#{key}=#{val}"}.join('&') | |
res = CSV.parse(open(url).read).transpose | |
h = {} | |
res.each{|x| h[x[0]] = x[1..-1]} | |
puts h | |
df = Daru::DataFrame.new(h) | |
end | |
df = from_yahoo('IBM', {:from => '2016-03-7', :to => '2016-03-10'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment