I love using Google Sheet to track my stock purchases and portfolio. You can get a copy of my Portfolio Tracker here.
One thing I struggle with for Singapore stocks is that the stock data are not available on Google Finance. So then, how can I get the stock price without updating it manually on my google sheet?
Using the IMPORTXML function
Using the IMPORTXML
function, you can, in simple terms, scrap data from another website into your google sheet.
Syntax: IMPORTXML(url, xpath_query)
Example: Get DBS price without Google Finance for Singapore Stock
So in my case, I want to get the price of e.g. DBS.
- Find out what is the DBS stock code: D05.
- Find out a website that has DBS stock price. e.g.
https://sg.finance.yahoo.com/quote/D05.SI/history?p=D05.SI - Find out the XPATH of the price data. (This is a little technical, will go more into details later)
//span[@data-reactid="32"]
Using the above information, you can now get the updated price for Singapore stocks without using Google Finance.
You can access the example on googlesheet here. This is a view only link.
I have added 3 different sites in which you can scrape the price data of Singapore stock without google finance.
Just Make a copy of it and you will be able to edit it.
Why is the price not showing?
I do noticed that sometimes it takes a while of the price to load up. Or sometimes the site gets updated, so the XPATH has changed. Or other times, it could be that the site you are trying to scrape the data from doesn’t allow importxml anymore. This is not a fool proof way to get data, but I think its the best free way for me to manage my portfolio.
How to retrieve the XPATH?
Read on if you want to know the technical part.
I would suggest to read up a bit on XPATH so if you don’t know what XPATH is. Once you have a little bit of understanding, you can then try this. So for our example to retrieve the price for DBS, we have the URL which has the stock price.
Right click on the Price -> Inspect
This should open up the DEVTOOLS for chrome or Edge browser. And it will highlight which element that is.
So in this example, you can see that the price resides in the <span>
tag, and lucky for us, there is a attribute called data-reactid = "32"
.
You need to test out if the XPATH is unique by CTRL+F on the devtool and you will see this Find by string, selector or XPath search box appear. Make sure that the search result is 1 of 1 which means it’s unique in this page.
Drop me a comment if there are any other questions or if there are better ways to do it, I would love to hear about it.
This is really useful! I had the same problem for the longest time and could never understand why SGX does not allow its stock prices data to be available on Google Finance. I haven’t been able to figure out the IMPORTXML function to pull the stock prices into my Google Sheet.
I’m glad you wrote this post and it has been a great reference & learning experience for me. Just used it to update the stock prices on my Google Sheet and it’s nice to have live data now!
I’m glad you found it useful 🙂