Scala fetch stock bar data from Yahoo finance service

Scala Example to retrieve yahoo stock bar data from Yahoo finance service.  Saves  resulting BAR data in local CSV file. Fetches each year from server and saves in file name where yy is replaced
with the year. The .day indicates this is a day level bar. The Yahoo service does not offer anything smaller than day.

See Also: Useful Scala Resource Links   and Python Utility to Download Yahoo Finance Bars


  • Reading command line parameters
  • Parse some command line parameters as integer year
  • Display error and correct sample when insufficient command line parameters.
  • Construct complex URI with query parameters.
  • Use for construction like imperitive for loop
  • Retrieving text from a remote URI using Source.FromURL
  • Specifying encoding for remote server when fetching from URL
  • Saving the retrieved string to a file
  • Detecting Error on download and display error message.

Use From Shell

      scalac fetchYahooBarData.scala
      scala fetchYahooBarData CAT 2008 2011

fetchYahooBarData Scala Source

/* fetchYahooBarData.scala
 Scala Example, retrieve yahoo stock bar data
 from Yahoo service and save resulting
 BAR data in specified local CSV file.
 fetches each year from server and saves in
 file name where yy is replaced
 with the year.  The .day indicates this is a 
 day level bar.  The Yahoo service does not offer
 anything smaller than day.
 Months are 0 .. 11
 See Also:  
object fetchYahooBarData {
  def main(args: Array[String]){
     if (args.length < 3) {
        println("fetchYahooBarData.scala usage=   scala fetchYahooBarData Symbol begYear endYear");
        println("""example   scala fetchYahooBarData SLV 1990 1997 """);
     } else {
       val symbol = args(0).toUpperCase().trim()
       val begYear = args(1).trim().toInt
       val endYear = args(2).trim().toInt
       val begMonth = 0
       val endMonth = 11
       val begDay   = 0       
       val endDay   = 31
       for (year  println("fail write file " + outFiName + " e=" + e);
           } catch {
              case e: Exception => println("fail fetch URI: e=" + e);

Sample Bar File Output

Date,Open,High,Low,Close,Volume,Adj Close

We offer expert consulting services

We offer consulting services with deep expertise in Search, Machine Learning, Distributed Architecture and High performance sites.   We specialize in solving the hardest problems you have available. Please contact us if you need engineering help.  joeeatbayesanalyticphone number