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 symbol-yy.day.csv 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

Demonstrates

  • 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

 
  Compile
      scalac fetchYahooBarData.scala
   Run
      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 symbol-yy.day.csv 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: http://www.jarloo.com/yahoo_finance/  
*/ 
 
import scala.io.Source
 
object fetchYahooBarData {
  def main(args: Array[String]){
     System.out.printf("HTTPGet.scala")     
     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
2011-12-30,90.720001,91.419998,90.400002,90.599998,3279800,78.616004
2011-12-29,89.459999,90.860001,89.459999,90.580002,3570800,78.598652
2011-12-28,91.760002,91.800003,89.169998,89.370003,5248800,77.548704
2011-12-27,91.699997,92.379997,91.510002,91.550003,3216200,79.440348
2011-12-23,92.199997,92.419998,91.650002,92.25,3368600,80.047754
2011-12-22,92.029999,92.50,91.019997,91.809998,4766000,79.665952
2011-12-21,91.779999,92.010002,89.900002,91.629997,7177300,79.509761
2011-12-20,89.260002,91.900002,89.230003,91.730003,7844300,79.596539
2011-12-19,88.089996,89.720001,86.940002,87.25,7919100,75.709122

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