DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Snippet To Grab Historical Data For Stocks

Snippets Manager user avatar by
Snippets Manager
·
Aug. 01, 06 · · Code Snippet
Like (0)
Save
Tweet
500 Views

Join the DZone community and get the full member experience.

Join For Free
This is a quick little snippet I'm whipping up to import some historical data in for a graphing app we're building for stock data.  I figured I'd post this snippet before I maul it into something application-specific...

It downloads a csv file through yahoo's finance site and then parses it and prints out the date and adjusted close for each business day that has data.


require 'open-uri'
require 'csv'

def get_adjusted_close stock_symbol
  puts "-- #{stock_symbol} Adjusted Close - Historical --"
  url = "http://ichart.finance.yahoo.com/table.csv?s=#{stock_symbol}&d=7&e=1&f=2006&g=d&a=2&b=26&c=1990&ignore=.csv"
  puts "Connecting to #{url}\n"

  csv = CSV.parse(open(url).read)

  csv.each{|row|
    puts "#{row[0]} - #{row.last}"
  }
  puts "---------------------------------"
end

example_stocks = "CSCO GOOG"
print "Enter a series of stock symbols separated by spaces (example: #{example_stocks}) to retrieve the historical adjusted close.\n"
stock_symbols = gets
stock_symbols ||= example_stocks

stock_symbols.split.each{|symbol|
  get_adjusted_close(symbol)
}
Snippet (programming) Data (computing) Grab (software)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is ERP Testing? - A Brief Guide
  • How To Integrate Third-Party Login Systems in Your Web App Using OAuth 2.0
  • Cloud-Based Integrations vs. On-Premise Models
  • APIs Outside, Events Inside

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo