Forum Discussion

Ancorato's avatar
Ancorato
Copper Contributor
Jul 24, 2024

Adding futures, indexes, and commodities into excel

I have a list of stocks that is every growing and have an excel formula that updates all the values on a weekly basis but I have issues linking to commodities, futures, and indices (ie. ng=f, cl.1, spxfp, ^rty, ^dji) and chatgpt and other resources haven't provided me with a great way to link to these. Can someone please help.  

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    Ancorato 

    To update futures, indexes, and commodities prices in Excel, you can use a combination of Excel's built-in functions and external data sources. Here are a few methods you can try:

    Method 1: Using Excel's Stock Data Feature (Excel 365)

    Excel 365 includes a feature that allows you to link stock data, including indices and commodities, directly in your worksheet. Here's how to do it:

    1. Enable Stock Data Type:
      • Select the cells containing the names of the futures, indices, or commodities you want to track.
      • Go to the Data tab on the Ribbon.
      • In the Data Types group, click Stocks.
      • Excel will try to convert the selected cells into linked data types.
    2. Insert Stock Data Fields:
      • Once converted, small icons will appear in the cells.
      • Click the icon to open a card showing more details about the data.
      • You can extract specific fields like price, change, and more by using the Insert Data button that appears next to the cell.

    Method 2: Using Web Queries

    If Excel's Stock Data feature does not cover certain commodities or futures, you can use web queries to pull in data from financial websites.

    1. Find a Reliable Data Source:
      • Websites like Yahoo Finance, Google Finance, or Bloomberg often provide updated data on futures, indices, and commodities.
      • For example, Yahoo Finance URLs for fetching data:
    2. Create a Web Query in Excel:
      • Go to the Data tab on the Ribbon.
      • Click From Web in the Get & Transform Data group.
      • Enter the URL of the data source (e.g., Yahoo Finance page for crude oil).
      • Follow the prompts to load the data into your worksheet.

    Method 3: Using Power Query

    Power Query (available in Excel 2010 and later versions) is a powerful tool for importing and transforming data from various sources.

    1. Open Power Query:
      • Go to the Data tab.
      • Click Get Data > From Web.
    2. Enter URL and Load Data:
      • Enter the URL of the webpage containing the data.
      • Power Query will open a navigator window allowing you to select the data you want to import.
    3. Transform and Load:
      • Use Power Query’s transformation tools to clean and shape the data.
      • Click Close & Load to load the data into your worksheet.

    Method 4: Using VBA for Custom Web Scraping

    For more control, you can use VBA to scrape data from websites and update your Excel sheet.

    1. Open the VBA Editor:
      • Press Alt + F11 to open the VBA editor.
      • Insert a new module: Insert > Module.
    2. Write VBA Code:
      • Here is a sample code to scrape data from Yahoo Finance:

    The VBA code is untested and serves as an example only, please backup your file in advance as a precaution.

    Sub GetStockData()
        Dim xml As Object
        Dim url As String
        Dim lastRow As Long
    
        ' Create XML object
        Set xml = CreateObject("MSXML2.XMLHTTP")
    
        ' Define URL
        url = "https://finance.yahoo.com/quote/^GSPC?p=^GSPC"
    
        ' Open connection
        xml.Open "GET", url, False
        xml.send
    
        ' Wait for response
        While xml.readyState <> 4
            DoEvents
        Wend
    
        ' Parse response
        Dim html As Object
        Set html = CreateObject("htmlfile")
        html.body.innerHTML = xml.responseText
    
        ' Find and extract data
        Dim result As String
        result = html.getElementsByClassName("Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)")(0).innerText
    
        ' Output result to Excel
        lastRow = Cells(Rows.Count, 1).End(xlUp).Row
        Cells(lastRow + 1, 1).Value = result
    
        ' Clean up
        Set xml = Nothing
        Set html = Nothing
    End Sub

         3. Run the Macro:

      • Press F5 to run the macro, which will fetch the data and place it into your worksheet.

    Conclusion

    By using these methods, you can dynamically update futures, indices, and commodities prices in Excel. The choice of method depends on your specific needs, data sources, and version of Excel. Excel 365’s Stock Data feature is the easiest, but for more control and customization, consider using web queries, Power Query, or VBA.

     

    NOTE: Since no one has answered it for at least one day or more, I entered your question in an AI (Otherwise it would have cost the author a lot of writing time). The text and the steps are the result of AI. The answers of an AI should be treated with caution, as they are not always correct. Maybe it will help you further in your project, if not please just ignore it.

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

Resources