3 Ways to Get Gold Prices in Microsoft Excel

Do you need to get gold prices into your Excel workbook?

Understanding the gold commodity market requires constant vigilance. This is often something market watchers want to track in Excel, but it’s not obvious this can be done in Excel without a manual process.

Excel does in fact offer methods to track commodity prices such as gold. This helps make tracking gold prices much more accessible and enables investors to assess market trends and make informed decisions.

This post is going to show you how to get the current and historical price per ounce of gold in your Excel spreadsheet.

Get Current Gold Prices with Stock Data Types

Excel has a stock data type that allows you to convert stock ticker names into rich data types that contain multiple points of information about the stock such as the price, volume, market cap, etc.

These stock data types do more than just stocks though. They can also give you financial information on currencies, cryptocurrencies, and commodities such as gold.

Unfortunately, these won’t be the spot price but rather the futures contract price.

A gold futures contract is an agreement for the sale of gold at a predetermined price at a specified date in the future.

The gold price that is determined through this process can provide investors with risk protection against any losses incurred from the fluctuating gold market.

This instrument can be used by gold investors and gold producers to hedge against price volatility and minimize the risk associated with changes in gold prices.

Here’s how to use the stock data type to get gold prices.

  1. Enter the text Gold in a cell. The actual ticker symbol for the gold futures contracts is @GC0Y but unfortunately Excel will recognize anything starting with the @ symbol as a function and won’t let you enter this.
  2. Go to the Data tab.
  3. Click on the Stocks data type found in the Data Types section of the ribbon.

Excel will attempt to convert this to a stock data type but will be unsuccessful and show you the Data Selector menu where you can search for the correct data type.

๐Ÿ’ก Tip: If you don’t see the Data Selector menu, you can always open it by right-clicking on a data type and then selecting the Data Type, and then choosing Change from the submenu.

  1. Click on Show more results at the bottom of the Data Selector pane. The gold futures contract data type might not show up in the search results at first.
  1. Click the Select button on the Gold Future – @GC0Y option.

This will convert the cell to the data type and you should see a small icon in the cell next to the word Gold.

When you click on the icon, this will show a data card with various data items that include the current price. These can be extracted into the grid.

  1. Select the cell with the data type. This will show the Extract button to the right of the cell.
  2. Click on the Extract button.
  3. Select the Price from the list of items you can extract into the grid.
=B2.Price

This will add the above formula into an adjacent empty cell that uses the dot notation to reference the Price field from the data type cell.

This data is connected to the financial market data provided by Refinitiv and will update with the latest data when you refresh the data type.

๐Ÿ’ก Tip: You can refresh a data type to get the latest gold prices by right-clicking on the cell โžœ choosing the Data Type option โžœ then selecting Refresh. You can also adjust the automatic refresh settings by selecting Refresh Settings.

Get Historical Gold Prices with the STOCKHISTORY Function

You can also get the historical gold futures prices using the STOCKHISTORY function in combination with the Stocks data type.

The STOCKHISTORY function will return historical stock prices and other financial metrics based on a stock ticker.

= STOCKHISTORY ( B2, TODAY() - 7, TODAY() )

The above formula will get the historical gold prices for the last 7 days based on the cell in B2 containing the gold futures contract data type.

In the most basic form, the STOCKHISTORY function requires a stock ticker or cell reference to a data type, start date, and end date to return historical prices.

The TODAY function is used to get today’s date and 7 days are subtracted to get the date seven days ago as the start date.

The end date is also entered with the TODAY function.

This results in the previous seven days of gold prices being returned by the STOCKHISTORY function.

Get Gold Prices with the WEBSERVICE Function

If your version of Excel doesn’t have data types or the STOCKHISTORY function, don’t worry, there is still an easy way to get the gold prices.

You can use an external API such as MetalPriceAPI along with the WEBSERVICE function to get the gold prices.

You will need to sign up to get your API key and use their free plan. Once you have an API key, you can use the following formulas to return the gold prices.

There are two API endpoints depending on if you want the current gold price or a historical gold price.

Current Gold Prices

=VALUE(
    INDEX(
        TEXTSPLIT(
            WEBSERVICE("https://api.metalpriceapi.com/v1/latest?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&base=XAU&currencies=USD"),
            {":","}"}
        ),
        1,
        6
    )
)

The above formula uses the latest endpoint and will result in the last available gold price in USD for 1 ounce of gold.

You will need to replace the xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx value with your own API key.

{"success":true,"base":"XAU","timestamp":1671307300,"rates":{"USD":1792.91990274}}

The WEBSERVICE function will return text similar to the above. This is JSON data and will include the price at the end. This will need to be parsed so it only returns the value.

You can parse this text with the TEXTSPLIT function based on the semicolon : and right curly brace } characters since the value of interest is between these two.

The TEXTSPLIT allows you to split text based on multiple characters, so passing an array of two delimiters {":","}"} will split the value into its own cell.

This will return an array of split values and you can use the INDEX function to get only the price which will be in the 1st row and 6th column of the split results.

The VALUE function is used to convert the resulting text into a number so it can be used in further numerical calculations.

Historical Gold Prices

=VALUE(
    INDEX(
        TEXTSPLIT(
            WEBSERVICE("https://api.metalpriceapi.com/v1/2020-09-15?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&base=XAU&currencies=USD"),
            {":","}"}
        ),
        1,
        6
    )
)

A similar formula can be used to return the historical price of gold from any given date. The formula uses the date endpoint of the API where the date is entered in yyyy-mm-dd format at the end of the API URL.

{"success":true,"base":"XAU","timestamp":1600127999,"rates":{"USD":1954.19001136}}

This results in a similar JSON text being returned, and it can be parsed in the same way previously shown.

=VALUE(
    INDEX(
        TEXTSPLIT(
            WEBSERVICE(
                "https://api.metalpriceapi.com/v1/" & TEXT(B2,"yyyy-mm-dd") & "?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&base=XAU&currencies=USD"
            ),
            {":","}"}
        ),
        1,
        6
    )
)

This formula can be adjusted to reference a cell with a date value as above. This will make it easier to get the historical price for any date!

This uses the TEXT function to ensure the date referenced in cell B2 is in the proper yyyy-mm-dd format for the API.

This will get you the historical gold price for any date!

Conclusions

The value of gold prices and other financial instruments are often tracked in Excel spreadsheets by investors.

There are a few easy ways to get these current and historical prices into your Excel workbook. Stocks data types, the STOCKHISTORY function, or even the WEBSERVICE function with an external API can be used.

How do you import gold prices to your Excel files? Let me know in the comments!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and qualified actuary with over 15 years of experience. He has worked in a variety of industries, including insurance, ad tech, and most recently Power Platform consulting. He is a keen problem solver and has a passion for using technology to make businesses more efficient.

Related Posts

Comments

2 Comments

  1. IVOR SHAER

    is there not a simple Excel 2021 vba macro that can fetch the current gold price from the internet and place it in cell A1 ?

    • John MacDougall

      Yes, you could also use VBA, Office Scripts, or Power Query in Excel to get the prices from the API.

Get the Latest Microsoft Excel Tips

Follow Us

Follow us to stay up to date with the latest in Microsoft Excel!

Subscribe for awesome Microsoft Excel videos ๐Ÿ˜ƒ