here<\/a>.<\/p>\n\r\n# Goal: download adjusted price data for selected security, convert to returns, and write to output file\r\n\r\nlibrary(tseries)\r\n\r\n# Load security data from Yahoo! Finance\r\nprices1 <- get.hist.quote("SPY", quote="Adj", start="2005-12-25", retclass="zoo")\r\n\r\nprices1 <- na.locf(prices1) # Copy last traded price when NA\r\n\r\n# To make week end prices:\r\nnextfri.Date <- function(x) 7 * ceiling(as.numeric(x - 1)\/7) + as.Date(1)\r\nweekly.prices <- aggregate(prices, nextfri.Date,tail,1)\r\n\r\n# To convert month end prices:\r\nmonthly.prices <- aggregate(prices1, as.yearmon, tail, 1)\r\n\r\n# Convert weekly prices into weekly returns\r\nr.weekly <- diff(log(weekly.prices)) # convert prices to log returns\r\nr1.weekly <- exp(r.weekly)-1 # back to simple returns\r\n\r\n# Convert monthly prices into monthly returns\r\nr.monthly <- diff(log(monthly.prices)) # convert prices to log returns\r\nr1.monthly <- exp(r.monthly)-1 # back to simple returns\r\n\r\n# Write output data to csv file\r\nwrite.zoo(r1.weekly, file="weekly.csv",sep=",",col.names=c("Dates","Percent Return"))\r\nwrite.zoo(r1.monthly, file="monthly.csv",sep=",",col.names=c("Dates","Percent Return"))\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"In this screencast, I demonstrate how to use R to download\u00a0historical price\u00a0data from Yahoo! Finance.\u00a0 I also demonstrate how to convert the price data series into a\u00a0return data series, and I\u00a0show how to write the output to a file which can be read into a spreadsheet program such as Excel or Google Docs Spreadsheet. I […]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/posts\/2899"}],"collection":[{"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/comments?post=2899"}],"version-history":[{"count":45,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/posts\/2899\/revisions"}],"predecessor-version":[{"id":4713,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/posts\/2899\/revisions\/4713"}],"wp:attachment":[{"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/media?parent=2899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/categories?post=2899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/tags?post=2899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}