Thursday, October 17, 2019

Create stock portfolio on Google Sheets using SGX price data (Updated)

This is the update to my previous post as the revamp of SGX site actually break the previous script where the download link is updated with no date embedded in the link. As there is no date in the link, this script will need to rely on other information which is the running number embedded in the link to automate the download of new data everyday.

Few preparations needed to be done.

Firstly, you need to add in Singapore Calendar in your Google calendar. This can be done as shown below as the script will use this calendar to determine whether the script is run on holiday day:



Secondly, you need to create a new Google Sheets. You need to create 2 sheets with first sheet use to store your portfolio and the second sheet (named as "Data") use to store your downloaded data. In this "Data" sheet, update the last 2 column with the date and counter value. You can get these value by looking at the download link when you download the data from SGX website.


Go to Google Chrome Download menu, and you can find the actual download link, note the number in the link 


So for this example, the date is 17 Oct 2019 and counter is 5569. Please enter the value to the "data" sheet as shown below.



Once you are done with above, open "Script Editor" under Tools menu and paste the following script or you can download from this link.


Once the data is downloaded to the "Data" sheet, you can now use formula to match stock quotes (column O) and retrieve the share last done price (column G).


Below is the sample table structure for the portfolio sheet.


For the last done price, enter the following formula

=INDEX(Data!$G$1:$G$1091, MATCH (B2, Data!$O$1:$O$1091, 0))

Please note that 1091 is the total row in the "Data" sheet. As there will be new stock list or delist, you might need to adjust this number from time to time.


You can configure trigger to have this script run at certain time everyday. As SGX only release this data after 630pm, i set the trigger time to between 7pm to 8pm.







2 comments:

KelLiz said...

Hi fenderCat,

Thanks for the newer post and this time, I made a bit further. When running the script, it throw me some HTML codes within "Data" sheet and when I view on IE, it says "No Record Found" with timestamp.

I have went thru the script few times and ensure "Data" sheet has the date and counter under column Y and Z respectively (I have verified the counter from SGX download link is correct).

fenderCat said...

Hi KelLiz,

The Data Sheet should contain only the price list, if it contains HTML codes, it means the download link is probably not set correctly. The code in the blog might not be up to date, can you use the code from the Github link - https://github.com/aiyoh79/GAS-SGX-Pricelist-Download/blob/master/Code.gs