Hi mate. Hopefully this will make sense and work for you. It should be a lot quicker. I’ve tested it a couple of times and it seems to work well so fingers crossed!
First I found this website: https://www.listcorp.com/asx which seems to have all ASX tickers available for download.
When I clicked the Download button, I had to register with them, but it only took a minute and was free to do (there were paid options but it seems you can download this info using the free option!). Once registered, I had to click back a few times to get back to the download page, hit download again and it then downloaded the list, which looked like this:
I then got rid of all the other data, and the headings, leaving just a list of tickers:
I saved this file in my working directory as “ASXTickers.CSV”, as we will be using this to import in to RStudio below. Alternatively, you could skip this step and just create or use your own list of tickers.
Next, copy and paste the entire code chunk below titled “Code Chunk to Copy” in to your RStudio workspace and run the code. Notice that this chunk of code:
### /// THIS CHUNK OF CODE SHOULD ONLY BE RUN THE FIRST TIME YOU ARE CREATING THE FILE IN GOOGLE DRIVE \\\ ###
#This sets the parameters for uploading to Google Drive then uploads the previously saved .csv file
GDrive.parameters <- drive_upload('ASXDailyData.csv')
# This code saves these parameters to your working directory so they can easily be loaded in to the Global
# Environment the next day/time RStudio is started
save(GDrive.parameters, file = "GDrive.parameters.RData")
### /// ---------------------------------------------------------------------------------------------- \\\ ###
…only needs to be run the first time, as this chunk uploads the first set of data to google drive. On all subsequent days you want to update this file, not upload it again so you can hash out the two lines so they don’t run like this:
### /// THIS CHUNK OF CODE SHOULD ONLY BE RUN THE FIRST TIME YOU ARE CREATING THE FILE IN GOOGLE DRIVE \\\ ###
#This sets the parameters for uploading to Google Drive then uploads the previously saved .csv file
#GDrive.parameters <- drive_upload('ASXDailyData.csv')
# This code saves these parameters to your working directory so they can easily be loaded in to the Global
# Environment the next day/time RStudio is started
#save(GDrive.parameters, file = "GDrive.parameters.RData")
### /// ---------------------------------------------------------------------------------------------- \\\ ###
Copy all of the text below to your RStudio workspace and run it all. The longest part is the ax.data <- BatchGetSymbols chunk as this is where it downloads the OHLC etc data for each ticker. This took between 10 and 20 minutes for me so not too bad. Any code in R that has a # at the start of the line means that line of code will not be run, so these are comments explaining what each line of code is doing.
# Installs and loads the required packages
if (!require(BatchGetSymbols)) install.packages('BatchGetSymbols')
library(BatchGetSymbols)
if (!require(googledrive)) install.packages("googledrive")
library(googledrive)
# Here we set your working directory. It can be any local folder you like but needs to be the same
# throughout this whole code. This is where you should have saved your "ASXTickers.CSV" file.
setwd("C:/Users/Owner/Documents/")
# Reads in your list of ASX ticker symbols with no column headers separated by commas
asx.stock.tickers <- read.csv("ASXTickers.CSV", header = FALSE, sep = ",")
#This ads .AX on to the end of each ticker with no space in between the join
asx.stock.tickers <- paste(asx.stock.tickers$V1,".AX",sep="")
#This transposes from column to row
asx.stock.tickers <- t(asx.stock.tickers)
# This removes everything before the : in each ticker leaving us with usable tickers
asx.stock.tickers <- gsub(".*:", "", asx.stock.tickers)
# Here we import the last 5 days OHLC and volume data for each ticker in the above dataset
# Notice the thresh.bad.data = 0.75, which means that if it cannot retrieve 75% of the data
# it will not import the data for that ticker. Great for excluding tickers no longer on
# the exchange. You can change thresh.bad.data, first.date and last.date
# Sys.Date() is today
ax.data <- BatchGetSymbols(tickers = asx.stock.tickers,
first.date = Sys.Date()-5,
last.date = Sys.Date(),
freq.data = 'daily',
thresh.bad.data = 0.75,
cache.folder = file.path(tempdir(),
'BGS_Cache') ) # cache in tempdir()
# This gives you your output dataset ax.data.out which you can view in the Global Environment
ax.data.out <- as_tibble(ax.data$df.tickers)
# This code takes the ax.data.out and exports it to a .csv file in your working directory
write.csv(ax.data.out,'ASXDailyData.csv')
### /// THIS CHUNK OF CODE SHOULD ONLY BE RUN THE FIRST TIME YOU ARE CREATING THE FILE IN GOOGLE DRIVE \\\ ###
#This sets the parameters for uploading to Google Drive then uploads the previously saved .csv file
GDrive.parameters <- drive_upload('ASXDailyData.csv')
# This code saves these parameters to your working directory so they can easily be loaded in to the Global
# Environment the next day/time RStudio is started
save(GDrive.parameters, file = "GDrive.parameters.RData")
### /// ---------------------------------------------------------------------------------------------- \\\ ###
# This loads the parameters back in to your Global Environment if you have closed and reopened RStudio
load("GDrive.parameters.RData")
# This takes today's data which has been exported to the local .csv file and updates the original Google Drive doc
GDrive.parameters <- GDrive.parameters %>% drive_update('ASXDailyData.csv')
In theory, this should be all you need to get a full list of tickers, download the data, save it to a .csv and update your google drive file with the new data each day.
If you wanted to test it with just a few tickers so it doesn’t take 20 minutes to test it, you can create a small list of tickers and run it like this instead, as this will only take a few seconds to download:
ax.data <- BatchGetSymbols(tickers = c("CSL.AX","CBA.AX","BHP.AX","NAB.AX"),
first.date = Sys.Date()-5,
last.date = Sys.Date(),
freq.data = 'daily',
thresh.bad.data = 0.75,
cache.folder = file.path(tempdir(),
'BGS_Cache') ) # cache in tempdir()
Hope this helps and works for you! Give me a message if you have any problems!