How to Get FREE Yahoo Finance Stock Data with R




Have you ever tried to get the stock prices using Yahoo Finance? If you familiar with R programming language you may know that there is a great package called “quantmod” which enables you to download the market data for free. But if you want to use that data for trading you need the most up-to-date data after the market closed. Unfortunately you cannot use the “quantmod” package because it provides you the data with one day lag. At the best practice you need to wait until midnight to be able to download the data and start to analyze them before the next trading day.
In this post I want to show you how you can use the “quantmod” package and its useful function called getSymbols() to get the most up-to-date data from Yahoo Finance by doing a little effort to scrap the last trading day data from Yahoo Finance.
This is the most reliable way the get the data from Yahoo Finance. Keep in mind that you MUST start to get the stock prices after the market is closed. I higly recommend you to use this code after 4:30 PM when we are sure that the aftermarket hours is also closed. You also need to be careful to do not use this code in the weekend and holidays because using the getSymbols() provides you the up-to-date data itself .
In the following code I put a condition which scrap the data only if the time is after 4:00 PM. It is pretty reasonable because we do not have the true end of the day price when the market is still open.


   WorkingDirectory = ".... Set Your Own Drirectory here!!"

#setting our working directory
setwd(WorkingDirectory)
#================================================================================     
# This Function only scrapes the Last Trading Day Price
ScrapPrice = function(Symbol){
  # condition for scrapping after 4:00 PM
  if16 <= as.numeric(substr(Sys.time(),12,13))){
    # scrapping
    library(rvest)
    library(XML)
   
    makeAddress=paste('https://finance.yahoo.com/quote/',Symbol,'/history?p=',Symbol,sep = '')
    web = read_html(makeAddress)
    employers1 <- web %>%
      html_nodes("table") %>%
      .[1] %>%
      html_table()
   
    employers <- employers1[[1]]
    TodayPrice=employers[1,]
    return(TodayPrice)
  }
}

#=================
# This Functin Add the updated price to your working directory as .CSV file
AddPriceToday = function(Symb,Price = getSymbols(Symbols = Symb, src = "yahoo"from =c("2005-01-01") ,to = Sys.Date(), auto.assign = F,periodicity = "daily"),condition=TRUE)
{
  library(quantmod)
  library(tseries)
  #if(condition==TRUE){
  # after scrapping the data we need to format it in our favor
  # here I removed the " , " and the date column from the scrapped stock price
  TodayPrice = as.numeric(gsub(',','',ScrapPrice(Symb)[-1]))
  CLOSE=TodayPrice[5]
  # Price is the historical price which is downloaded by getSymblos() function
  OPEN = TodayPrice[1]
  HIGH=TodayPrice[2]
  LOW=TodayPrice[3]
  VOL=TodayPrice[6]
  My_Date=gsub('-','',time(Price)) # removing the"-" in the DATE data
  # Adding a new date column into the time series data
  Price=cbind(My_Date,Price)
  Price=Price[,-7# removing the ADJUSTED CLOSE
  #Todays Date
  Today=as.numeric(gsub('-','',Sys.Date()))
  CurrentPrice=cbind(Today,OPEN,HIGH,LOW,CLOSE,VOL)
  # rename the columns
  colnames(CurrentPrice) = c("<Date>","<OPEN>","<HIGH>","<LOW>","<CLOSE>","<VOL>")
  CurrentPrice=xts(CurrentPrice,Sys.Date())
  #putting the historical and current prices together
  Price = rbind(Price,CurrentPrice)
  # Save the file as .CSV in the working directory
  write.table(as.data.frame(Price),file = paste(Symb,".csv",sep=""),sep=",",col.names = T,row.names = F)
  # }
}
AddPriceToday(Symb = "IBM")

Comments

Popular posts from this blog

How to check if a distribution is normal?

Asset price Prediction Using Principal Component Analysis And Machine Learning Regression Model

How to scrap Zacks Rank signal in Python