Bargain Hunter
Programming Snapshot – Stock Quote API
Financial wizard Mike Schilli is annoyed that some web services have discontinued serving up real-time stock market data. To keep an eye on his investment dollars, Mike taps into a little known interface for stock prices.
For users of stock-quote-serving APIs, the fruit is currently hanging higher. First, Google discontinued its web service [1] for real-time stock market data (Figure 1); then, Yahoo followed suit and pulled the plug. With the end of their undocumented but widely used share price interface in CSV format [2], Yahoo, perhaps unknowingly, also pulled the rug from under a number of open source projects, such as the Perl Yahoo::Finance CPAN module and the Python yahoo-finance package. A few open source portfolio-tracking applications using these popular packages were dragged into the abyss along with them.
On the GitHub sites of these projects, the Issues tabs are now overflowing with comments, there's quite a bit of wailing and gnashing of teeth from users, and much wringing of hands of those who hope to find a replacement for the vanished free real-time (or at least time-delayed) stock market price data source.
Free Is Cheap Enough
For a hefty monthly fee, of course, you can find dozens of business-level stock market data providers, such as Bloomberg, but only a few competitors offer free quotes, often only to registered users who are adding an issued API key to every request. It is probably only a matter of time before the free source presented today also dries up, but I'm working on the assumption that, in the future, options for retrieving the data free of charge will still be around.
Developers should therefore be prepared for the fact that an application's data back end can change at short notice. Thanks to a design abstracting away the share price source, this should be easy to handle with a simple upgrade of the quote library for applications; the caravan of thirsty camels, after only finding dried out water holes at the old oasis, will move on to more refreshing destinations.
Listing 1 [3] uses a still open Yahoo source to obtain the current market prices of equities on the world's major stock exchanges. For example, the amzn ticker symbol in dollars reveals the daily value of the Amazon share on the New York Stock Exchange. Those who want to explore Amazon's share price on the German Xetra Stock Exchange in euros enter amz.de instead.
Listing 1
myquote.py
To do this, the quote()
function awaits the ticker symbol and sends a request to the web API that returns a data structure encoded in JSON. Under the regularMarketPrice
key is the numerical value for the price in the currency customary on the market where it is traded.
Cash Check in the Money Bin
Banks and stock brokerage houses offer portfolio management functions and calculate the net value of the deposit for their clients. Google and Yahoo also offer portfolios, although they are in the process of change [2]; both seem to be looking for new ways to make money off the service somehow.
The advantage of a home-grown script is that it adds all items across accounts or brokerages. For this purpose, the user has to keep a YAML file with the items in their portfolio up to date manually, as shown in Listing 2. However, this approach preserves the investors' privacy more reliably than third-party applications that have a monetary interest in mining user data and using it for marketing purposes or, worse, selling it to the highest bidder.
Listing 2
portfolio.yaml
Listing 2 shows two fictitious accounts, one at Wells Fargo and one at Citibank, with 100 Amazon and 50 Google shares, the latter of which are now traded under the "Alphabet" name, although the company's ticker symbol on Nasdaq is still goog. Both accounts also have a Cash
item. The mymoney.py
script in Listing 3 adds the cash at face value but determines the current value of the share items via the API and multiplies the stock price by the number of shares in the deposit.
Listing 3
mymoney.py Calculations
To perform this calculation, Listing 4 (line 3) includes the yaml
module, which you must have installed previously by typing:
pip install --user pyyaml
Listing 4
mymoney.py
It provides the load()
method used in line 8 to parse the account data from the portfolio file in Listing 2. Line 30 iterates across the accounts found; line 32 iterates across the items stored therein. In the current version, the script supports cash (cash
) and shares (stock
) but could easily be extended to include bonds, options, or items in foreign currencies, whose exchange rate the script then can retrieve in a similar fashion at run time. The cash_dump()
function from line 18 determines the value of a cash item. There's no magic being performed here; it only returns a tuple with the name Cash
and the amount.
To determine the value of a stock item, on the other hand, Listing 4 uses the myquote.py
module from Listing 1 and calls its quote()
function with the ticker symbol as a parameter. It multiplies the returned market price by the number of shares in the securities account and feeds this back to the main program together with a table key, all wrapped into a tuple. The main program only needs to add the line items across all accounts and output the total net value for the user (Listing 3).
Rounding, but Correctly
One thing to keep in mind is that computers are notoriously bad at adding floating-point numbers. If you add 1.23 and 4.56 together, you may get a result that deviates from the expected value of 5.79, because the CPU does not represent 1.23 exactly as 1 and 23 hundredths, but approximates it in binary format. In a past article, I discussed how even eBay can't add properly and that their monthly bill regularly is off by a few cents [4].
When it comes to adding money amounts, it is smarter to compute in cents rather than dollars. In other words, simply multiply the dollar amount by 100 and add it as a rounded integer; when displaying the result, inject a decimal point in front of the last two digits – Listing 4 does just that.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.