How to create a new stock quote source for Gnucash using Finance::Quote

Note that I no longer use github.

This post is for technical users. The objective is to share how the MumbaiStock.pm module was created. Visit this other post to know how to add the MumbaiStock.pm to Gnucash and use it.

Problem:
A friend of mine uses Gnucash to manage their personal finances. They configured and used Gnucash to retrieve online price quotes from the NSE or the BSE through Yahoo Finance. Unfortunately since the past few months, they could not retrieve these stock quotes to get a correct picture of their Portfolio.

This meant they had to either decide to enter the Quotes manually (very labor intensive), or to migrate away from Gnucash to a different personal finance tool. The constraint being they wanted a tool that worked both on Linux & Windows.

Diagnosis:
They approached me to see if there was a method to get away from Yahoo Finance and use something else like Google Finance. I know of one product called JStock, that I recommended they use as a stop-gap till a solution is found for Gnucash. JStock is available on many platforms. All they had to do was to enter their list of stocks into it, and track it, away from the core Gnucash they were used to. They could manually enter the closing prices into the Price Editor in Gnucash from JStock. Inefficient, but it works.

A drawback is that they could not monitor Mutual Fund NAVs as it did not retrieve those values from the AMFI website, but retrieved what it could from Google Finance. Therefore, to overcome this situation, here is what I did for them.

Looked over the internet to see if there is any other source that can provide the stock quotes. Found that the Bombay Stock Exchange (BSE) website had something called a bhavcopy. Bhavcopy is nothing but stock information at end of a trading day. At end of day, the BSE puts up an updated bhavcopy at this link http://www.bseindia.com/download/BhavCopy/Equity/eq141013_csv.zip. You can download this file to get a comma-delimited record for each BSE stock from EQ141013.CSV as on 14th October, 2013.

On viewing the EQ141013.CSV file, we find that stock quotes are available for all stocks in a comma-delimited format. This now implies that the stock prices are available in a similar style as the AMFI India Mutual Fund NAVs text file. Only, the AMFI NAV text file is delimited by semi-colons. We can use this information to at least get the closing prices for all the stocks in one go and then fill them into the prices for Gnucash.

Technical Solution (The Beauty of Open Source):
Based on above information, and a hunt through the internet, and major thanks to this post http://stephan.paukner.cc/syslog/archives/401-How-to-add-new-quote-sources-to-GnuCash.html by Stephan, we can create our own perl module to use the Bombay Stock Exchange’s bhavcopy through Get Quotes. If you look more closely at the IndiaMutual.pm perl module within the /usr/share/perl5/Finance/IndiaMutual.pm code, you will find that you can easily adapt it, because, IndiaMutual.pm also relies on a text file from the amfiindia.com website published by them at end of day’s trading. With a bit of understanding, here is what was done. Below is the method intended for those who know what they are doing and have the technical knowledge to do so. If you are an end user, please go to this other blog post.

1 Backup the files Quote.pm and create MumbaiStock.pm from IndiaMutual.pm
$ cd /usr/share/perl5/Finance <enter>
$ sudo cp Quote.pm Quote.backup <enter>
$ cd /usr/share/perl5/Finance/Quote <enter>
$ sudo cp IndiaMutual.pm MumbaiStock.pm <enter>
2 Make a copy of MumbaiStock.pm in your local home folder for modification
$ cp /usr/share/perl5/Finance/Quote/MumbaiStock.pm /home/user <enter>
Open MumbaiStock.pm for editing using mousepad / gedit.
3 Download the following bhavcopy file from the BSE India website to your home folder
http://www.bseindia.com/download/BhavCopy/Equity/eq141013_csv.zipOpen the file using mousepad / gedit.
4 Replace AMFI strings in MumbaiStock.pm as follows:
a. Locate AMFI (Capitals) and replace with MSTK.
b. Locate amfi (small) and replace with mstk.
c. Locate IndiaMutual and replace with MumbaiStock.
5 Modify /home/user/MumbaiStock.pm
You can refer to the https://github.com/geetuvaswani/mumbaistock to view the source code of MumbaiStock.pm.
6 Save MumbaiStock.pm
We will copy it over to /usr/share/perl5/Finance/Quote later.
7 Modify Quote.pm
Not much to change here, other than to add MumbaiStock to the list of available modules. Open the file using mousepad or gedit.
$ sudo mousepad /usr/share/perl5/Finance/Quote.pm <enter>
Search for @modules.Just before IndiaMutual, add MumbaiStock, save the file and you are done.
8 Copy MumbaiStock.pm into Finance/Quotes
$ sudo /home/user/MumbaiStock.pm /usr/share/perl5/Finance/Quote/MumbaiStock.pm <enter>Make sure there are no errors in the code by:
$ perl -e 'use Finance::Quote::MumbaiStock' <enter>If it just displays the next line, you are good to test the module next.
9 Test MumbaiStock.pm without starting Gnucash
To test the module without starting Gnucash, open a terminal window and enter following command:$ gnc-fq-dump mumbaistock 500010 <enter>
where 500010 is a stock symbol of HDFC on the Bombay Stock Exchange.If all works, you will see:Finance::Quote fields Gnucash uses:
symbol: 500010 <=== required
date: 10/14/2013 <=== required
currency: INR <=== required
last: <=/
nav: 803.80 <=== one of these
price: <=/
timezone: <=== optional

You can also add printf statements in the file MumbaiStock.pm to debug variable values as you test. Do not forget to remove the printf statements when done.You should also open another terminal window and look for the files coming into the /tmp folder.Remember, we are to look for mumbaistocklist.txt or mumbaistocklist.zip just to make sure we get the right data. Only after you confirm it works fine, should you then modify the Security Codes in Gnucash Security or Price Editor.

10 Once satisfied, you can start up Gnucash and change the stocks quote source in Security Editor for MumbaiStock and retrieve them using Price Editor. This module does not retrieve real-time quotes. Look elsewhere if you need real-time updates.

2 comments to How to create a new stock quote source for Gnucash using Finance::Quote

  • Thank you for fixing it for Windows. I deleted my Windows 8 partitions as I was forcibly sold my chosen configuration Lenovo laptop forcibly with Windows 8 down here in India. They now expect me to expend bandwidth to extent of 3 GB at least, to upgrade to Windows 8.1. I do not hate Windows, but I do not like being forced. I’ll attribute the fix for Windows to you on Finance::Quotes bugslist. Oops, sorry… the github site where I put the Linux code.

  • Anil

    Thank for the code for retrieving stock quotes from BSE.

    Initially it did not work in Windows7. Had to remove /tmp/ from lines 53 and 54 to make it work.

Leave a Reply

  

  

  

Blue Captcha Image
Refresh

*

September 2018
M T W T F S S
« Jul    
 12
3456789
10111213141516
17181920212223
24252627282930