Last active
May 25, 2017 06:15
-
-
Save rosdyana/151c385413c7a11de2c9662004b138c8 to your computer and use it in GitHub Desktop.
Yahoo market data download - R
This file contains hidden or 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(data.table) | |
yahoo_downloader_env <- new.env() | |
.get_yahoo_curl = function(symbol) { | |
curl = RCurl::getCurlHandle( cookiejar = '' ) | |
html = RCurl::getURL( paste0('https://finance.yahoo.com/quote/',symbol,sep=""), curl = curl ) | |
crumb = gsub( '^.*crumb":"\\s*|".*', '', html ) | |
assign( 'crumb', crumb, envir = yahoo_downloader_env ) | |
assign( 'curl' , curl , envir = yahoo_downloader_env ) | |
} | |
.get_yahoo_data = function( symbol, from, to = Sys.Date(), events ) { | |
if( is.null( yahoo_downloader_env$crumb ) ) .get_yahoo_curl(symbol) | |
url = paste0( 'https://query1.finance.yahoo.com/v7/finance/download/', symbol, | |
'?period1=', as.numeric( as.POSIXct( from ) ), | |
'&period2=', as.numeric( as.POSIXct( to ) ), | |
'&interval=1d&events=', events, '&crumb=', yahoo_downloader_env$crumb ) | |
x = RCurl::getURL( url, curl = yahoo_downloader_env$curl ) | |
if( x == '' | grepl( 'Not Found|Bad Request', x ) ) return( NULL ) | |
if( grepl( 'Invalid cookie', x, fixed = T ) ) { | |
.get_yahoo_curl() | |
.get_yahoo_data( symbol, from, to, events ) | |
} | |
x = gsub( 'null', '', x, fixed = T ) | |
x = fread( x ) | |
setnames( x, tolower( gsub( ' ', '_', names( x ) ) ) ) | |
. = stock_splits = date = NULL | |
if( events == 'split' ) x[, stock_splits := sapply( parse( text = stock_splits ), eval ) ] | |
x[, date := as.Date( date ) ] | |
x[] | |
} | |
get_yahoo_data = function( symbol, from, to = Sys.Date(), split.adjusted = TRUE ) { | |
splits = .get_yahoo_data( symbol, from, to, events = 'split' ) | |
dat = .get_yahoo_data( symbol, from, to, events = 'history' ) | |
if( is.null( dat ) || nrow( dat ) == 0 ) return( dat ) | |
# return downloaded data | |
return( dat[] ) | |
} | |
# get_yahoo_data("SHY", from = "2016-06-21") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment