R – Import historic ASX data for selected companies
On the Yahoo.com finance site, you can select the company of interest over a selected period of time. For example, this link shows daily data for ASX over a 12 month period: https://au.finance.yahoo.com/quote/ASX.AX/history/?period1=1700345906&period2=1731968156
To import the data directly into R, I had the best results with the ‘yfR’ package: https://cran.r-project.org/web/packages/yfR/yfR.pdf
install.packages('yfR') library(yfR)
It has various functionality but to import the stock market data for specific companies, I use ‘yf_get’.
Companies listed in the Australian stock exchange (ASX) get ‘.AX’ postpended to the company code. So BHP becomes BHP.AX.
An individual company can be specified or a list can be created as in this example below.
tickers <- c("BHP.AX", "AGL.AX") first_date <- Sys.Date() - 30 last_date <- Sys.Date() df_yf <- yf_get( tickers = TicketList, first_date = first_date, last_date = last_date) print(df_yf)
Dates need to be entered in this format “YYY-MM-DD” (ie first_date <- "2001-01-01"
)
I used this approach to import data for 17 companies over a period of approximately ten years without any difficulty. Other packages like rvest and httr were producing errors.