Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Google finance API instead of Yahoo #111

Merged
merged 1 commit into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ch09-risk/data/download-symbol.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#
# See LICENSE file for further information.

curl -o $2/$1.csv https://ichart.yahoo.com/table.csv?s=$1&a=0&b=1&c=2000&d=0&e=31&f=2013&g=d&ignore=.csv
wget -O ${2}/${1}.csv "http://www.google.com/finance/historical?q=${1}&startdate=Jan+01%2C+2000&enddate=Dec+31%2C+2013&output=csv"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: https is slightly better. curl -o should also work and be consistent with use of curl elsewhere in the book. But neither really matter.

Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class RunRisk(private val spark: SparkSession) {
import spark.implicits._

/**
* Reads a history in the Yahoo format
* Reads a history in the Google format
*/
def readYahooHistory(file: File): Array[(LocalDate, Double)] = {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
def readGoogleHistory(file: File): Array[(LocalDate, Double)] = {
val formatter = DateTimeFormatter.ofPattern("d-MMM-yy")
val lines = scala.io.Source.fromFile(file).getLines().toSeq
lines.tail.map { line =>
val cols = line.split(',')
Expand Down Expand Up @@ -125,17 +125,17 @@ class RunRisk(private val spark: SparkSession) {
val files = stocksDir.listFiles()
val allStocks = files.iterator.flatMap { file =>
try {
Some(readYahooHistory(file))
Some(readGoogleHistory(file))
} catch {
case e: Exception => None
}
}
val rawStocks = allStocks.filter(_.size >= 260 * 5 + 10)

val factorsPrefix = "factors/"
val rawFactors = Array("^GSPC.csv", "^IXIC.csv", "^TYX.csv", "^FVX.csv").
val rawFactors = Array("NYSEARCA%3AGLD.csv", "NASDAQ%3ATLT.csv", "NYSEARCA%3ACRED.csv").
map(x => new File(factorsPrefix + x)).
map(readYahooHistory)
map(readGoogleHistory)

val stocks = rawStocks.map(trimToRegion(_, start, end)).map(fillInHistory(_, start, end))

Expand Down