How to retrieve KLSE/SGX stock and gold prices in Google Sheets (Updated for 2026Q1)

2026-01-09


My own portfolio Google Sheets

For Bursa Malaysia (KLSE) and Singapore Exchange (SGX), we can scrape from some other websites websites like growbeansprout and klsescreener. This is how.

KLSE

As of today, klsescreener still works for the Google Sheets scraper, albeit with an extra step that checks for presence of cookie. We can make it work by implementing a custom formula with Google App Script.

Step 1: At the top of your Google Sheets, click Extensions > App Script.

Step 2: Copy this script below, paste it in, save:

/**
 * Get KLSE stock price
 * @param {string} stockCode - Stock code (e.g., "5176")
 * @return Current price
 * @customfunction
 */
function KLSE(stockCode) {
  var chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
  var cookie = '';
  for (var i = 0; i < 26; i++) {
    cookie += chars.charAt(Math.floor(Math.random() * chars.length));
  }

  var response = UrlFetchApp.fetch(
    'https://www.klsescreener.com/v2/stocks/view/' + stockCode + '/',
    {
      headers: {'Cookie': 'KLSE=' + cookie},
      muteHttpExceptions: true
    }
  );

  var match = response.getContentText().match(/id="price"[^>]*data-value="([^"]+)"/);
  return match ? parseFloat(match[1]) : 'Error';
}

Step 3: Use this formula, replace 6888 with your own stock code, or assuming your stock code is in the cell B22:

=KLSE(6888)
=KLSE(B22)

SGX

And this is for SGX, just replace the stock code (e.g. G3B, CFA) with whatever ticker you want, or , or assuming your stock code is in the cell B22:

=IMPORTXML("https://growbeansprout.com/quote/G3B.SI", "//p[@class='text-3xl font-medium']")
=IMPORTXML("https://growbeansprout.com/quote/"&B22&".SI", "//p[@class='text-3xl font-medium']")

Gold Prices

You can use this formula to approximate USD per oz (approx. 31.1g):

=GOOGLEFINANCE("GLD", "price")*10/POW(0.996,YEARFRAC(TODAY(), "18/11/2004", 1))

GLD is the ticker for SPDR gold trust ETF, the largest one out there. They charge 0.4% per annum since inception.

On a fun note - 2026Q1

I have been updating this article over time, I first posted this a few years before LLM is a thing.

But these days, whenever the formulae in my own sheets broke, I turned to Google/Gemini, ChatGPT, and Claude asking for help. Sadly they often responded to me, either:

  1. GOOGLEFINANCE("MYX:xxx") - this counter doesn't exist, Google has definitely hallucinated.
  2. My own blog article which stopped working.
  3. Worse, my own blog article from years ago which Google doesn't seem to bother to reindex, and recommended at the top of AI suggestions.

They know my name is Anonoz, it did not occur to these AIs to wonder "why is anonoz asking these and maybe he wants to see smtg else".

Luckily, I am a software engineer who is well versed enough with web techs, and I can just get these LLM to come up with solutions based on my hunches, the KLSE script is one such example - generated by Claude Code.


Built with Ruby on Rails. Statically generated and hosted on Cloudflare Pages.