API

How to get the latest commodity pricing in Google Sheet

Minhaz Vadakekara
Minhaz Vadakekara
May 23, 2022·20 min read

[caption id="" align="alignnone" width="694"] Screenshot of Google Sheet by Author using a custom hand written function.[/caption]

How to get the latest gold price in Google Sheet or How to get the latest metal price in Google Sheet in general.

Some existing approaches

Using GOOGLEFINANCE macro

ImportHtml and ImportXml

=IMPORTXML("https://sg.finance.yahoo.com/quote/RW0U.SI",
    "//span[@class='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)']")
=Dollar(Index(ImportHTML("http://www.apmex.com/spotprices/silver-price","table",8),2,2))

Getting data values with AppScript

[caption id="" align="alignnone" width="700"] Photo by Jason Briscoe on Unsplash.[/caption]

[caption id="" align="aligncenter" width="287"] Screenshot of Google Sheets by Author.[/caption]

[1] Create a new script and a stub function

[caption id="" align="aligncenter" width="422"] Response with the skeleton code so far. Screenshot by the Author.[/caption]

[2] Call the API and return the response

  • Register yourself with metals-api.com
  • Copy the API-Access key from the dashboard

[caption id="" align="aligncenter" width="478"] Result of running the new macro with updated script. Screenshot by the Author. Note the values are from when this article was originally written.[/caption]

[3] (Optional) Add caching support

[caption id="" align="aligncenter" width="610"] Result of running the new macro with updated script. Screenshot by the Author. Note the values are from when this article was originally written.[/caption]

What else can you do beyond this

  • Get the price of Gold in India in specific cities.
  • Get the price of some Stock on the Singapore Exchange.
  • Get the price of some stock in BSE or NSE.
  • Get the price of a certain Mutual Fund or ETF

Frequently Asked Questions

Can Google Sheets pull live commodity prices automatically?

Yes. Google Sheets supports the GOOGLEFINANCE function and IMPORTXML formula, which can fetch real-time and delayed commodity prices directly into your spreadsheet without any paid API. This works for gold, silver, oil, and major commodity ETFs with a simple formula setup.

What is the GOOGLEFINANCE function and how does it work for commodities?

GOOGLEFINANCE is a built-in Google Sheets formula that retrieves financial data from Google Finance. For commodities, you can use it with ticker symbols for commodity ETFs like GLD for gold or USO for oil. The syntax is =GOOGLEFINANCE("GLD","price") and it updates automatically during market hours.

Are commodity prices in Google Sheets real-time or delayed?

Google Finance data in Sheets is typically delayed by 15-20 minutes for most instruments. For professional trading decisions requiring real-time data, a paid API like Quandl or Alpha Vantage is recommended. For portfolio tracking and research purposes, the delayed data is sufficient.

What commodities can I track in Google Sheets for free?

Using GOOGLEFINANCE and IMPORTXML, you can track gold, silver, crude oil, natural gas, copper, wheat, corn, and most major commodities through their ETF proxies. Direct spot price feeds for some commodities require third-party data sources, but ETF prices serve as reliable proxies for most analytical purposes.

How do I set up automatic commodity price updates in Google Sheets?

Set up a Google Sheets trigger using Tools, Macros, Manage Macros to refresh your IMPORTXML formulas on a schedule. For GOOGLEFINANCE formulas, updates happen automatically. For external data sources, Google Apps Script can be used to fetch and refresh prices on a custom interval without manual intervention.

🎬DDI MediaNewDDI Smart Block

Turn this article into a video

Instantly repurpose any DDI article into a professionally produced short-form video.

Try DDI Media
Minhaz Vadakekara
Minhaz Vadakekara

I am Software Engineer who is looking to retire ASAP (FIRE). I invest a lot of my money on different derivatives and tend to write about data driven investment at times. This publication looks like a good fit to publish.

More in API