Downloading market data from Stooq to R


/ Published in: R
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # http://www.rhinocerus.net/forum/lang-php/662887-how-read-download-attachment-uri.html - inspiration :)
  2.  
  3.  
  4. library(RCurl)
  5.  
  6.  
  7. getStooqData <- function(asset_code,static_cookie=TRUE) {
  8.  
  9. data_tmp <- tempfile() # "data.csv"
  10. cookie_tmp <- "cookie.txt"
  11.  
  12. u1 <- paste("http://stooq.com/q/d/?s=",asset_code,sep="")
  13. u2 <- paste("http://stooq.com/q/d/l/?s=",asset_code,"&i=d",sep="")
  14.  
  15. if (!static_cookie) {
  16.  
  17.  
  18.  
  19. h <- c(paste("GET ",u1," HTTP/1.0",sep=""),
  20. Accept="image/gif",Accept="image/x-xbitmap",Accept="image/jpeg",Accept="mage/pjpeg",Accept="application/x-shockwave-flash",Accept="application/vnd.ms-excel",Accept="application/msword",Accept="*/*",
  21. 'Accept-Language'="pl, en-us;q=0.7",'User-Agent'="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)",'Proxy-Connection'="Keep-Alive")
  22.  
  23.  
  24. u1Opts <- curlOptions(header=TRUE,httpheader=h,cookiejar=cookie_tmp)
  25. curlPerform(url=u1,.opts=u1Opts,verbose=TRUE)
  26.  
  27. h <- c(paste("GET",u2,"HTTP/1.0"),
  28. Accept="image/gif",Accept="image/x-xbitmap",Accept="image/jpeg",Accept="mage/pjpeg",Accept="application/x-shockwave-flash",Accept="application/vnd.ms-excel",Accept="application/msword",Accept="*/*",
  29. 'Accept-Language'="pl, en-us;q=0.7",'User-Agent'="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)",'Proxy-Connection'="Keep-Alive")
  30.  
  31. u2Opts <- curlOptions(header=TRUE,httpheader=h,cookiefile=cookie_tmp)
  32.  
  33. }
  34.  
  35. else {
  36.  
  37.  
  38. h <- c(paste("GET",u2,"HTTP/1.0"),
  39. Accept="image/gif",Accept="image/x-xbitmap",Accept="image/jpeg",Accept="mage/pjpeg",Accept="application/x-shockwave-flash",Accept="application/vnd.ms-excel",Accept="application/msword",Accept="*/*",
  40. 'Accept-Language'="pl, en-us;q=0.7",'User-Agent'="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)",'Proxy-Connection'="Keep-Alive",Cookie=paste("cookie_uu=p;cookie_user=%3F0001dllg000011500d1300%7C",asset_code,sep=""))
  41.  
  42. u2Opts <- curlOptions(header=TRUE,httpheader=h)
  43.  
  44. }
  45.  
  46.  
  47. reader <- basicTextGatherer()
  48.  
  49. w <- getURLContent(url=u2,.opts=u2Opts)
  50.  
  51.  
  52. write(w,file=data_tmp)
  53.  
  54. stooq_data <- read.csv(data_tmp)
  55.  
  56. stooq_data
  57. }
  58.  
  59.  
  60. stooq_data <- getStooqData("es.f",static_cookie=TRUE)

URL: http://mjaniec.bloger.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.