Downloading Stock Quotes with Stocks Tracker API
Today, a lot of people are discovering US stock markets. Some of those people work in the financial industry, while others are building their retiring nest by investing their savings into stocks. Other people trade stocks for a living.
If you are like me, a software developer, and follow a stock market, you have most likely thought about retrieving market information in your applications.
This article will show you how to retrieve stock quotes using simple calls to the existing Java library.
API Open Source
From time to time, I think about retrieving stock quotes for various purposes. One time it was in the .NET world, the other time is Objective-C for iOS calls. This time, it will be for the new Android app. Recently, I have started my open source project, “Stocks Tracker,” for Android devices. The Android app is still a work in progress, but I have already coded the API library for retrieving detailed information for individual stocks.
You won’t need to struggle with choosing stock quote sources on your own, because my library is working with 3 sources already: Yahoo Finance, Google Finance and MarkitOnDemand. All of these sources have their own pros and cons. I will show my findings for every source later, so you can decide for yourself which source you would like to use in your project.
Download the project from here.
The resulting class is called
Stock. The end result of a stock quote will be stored in this class. It has important fields, such as:
ticker– stock’s ticker
lastTrade– last trade price
change– daily change
changePercent– daily change in percent
Unfortunately, there is no general financial source which can fill all 84 fields of this class for free. We have to balance out which source to use according to the task requirements. Sometimes, it makes sense to use a combination of multiple sources. The latest is not a problem with
StockDownloader is a class which is intended to download individual stock information using the
mapper class. This
mapper class obeys
IStockMapping interface, so it can be injected into the
As of now, the library has 3 mapping classes:
StockYahooMapping (for getting information from Yahoo Finance),
StockGoogleMapping (for getting information from Google Finance) and
StockMerkitindemandMapping (for getting stock from MarkitOnDemand source).
The class diagram is shown below:
Financial Online Sources
There are 3 online sources coded into
StocksTracker API at the moment: Yahoo Finance, Google Finance and MarkitOnDemand. All of them are free to use.
Here are the pros and cons for each source:
Yahoo Finance provides very detailed information about individual stocks. You will receive a most extensive set of stock attributes if you use this source. However, you will not get real time data for the stocks prices for free with them. At least, you can use this source for your thorough analysis if it allows a moderate delay of 20 minutes.
Google Finance provides real time data for stocks. Unfortunately, it gives you bare bone set of attributes. This source is good for calculating a portfolio’s performance when you need multiple stocks’ quotes in real time. The bad thing about using Google Finance seems to be the lack of documentation. Basically, this source’s functionality is not documented by Google (at least from my experience). Google used to have some financial API years ago, but they discontinued this old API. I am afraid it is possible they will discontinue this unofficial financial source as well. As of now, it is a reliable source for me. I am using it in various projects.
MarkitOnDemand provides real time stock quotes. Their source’s API is well documented. It gives essential stocks data that you can use for stock portfolio calculations. A downside of this source is they seem to focus on providing support for JQuery apps. I have to do some minor workaround in order to write the mapping class for MarkitOnDemand.
Using the Code
You can easily code any of the sources in your project, thanks to a
StockDownloader class and to an
IStockMapping interface. After you import
stocksapi module into your project the following few lines of code will do the magic:
StockYahooMapping stockYahooMapping = new StockYahooMapping(); StockDownloader stockDownloader = new StockDownloader(stockYahooMapping); Stock stock = stockDownloader.Download("FB");
StockGoogleMapping stockMapping = new StockGoogleMapping(); StockDownloader stockDownloader = new StockDownloader(stockMapping); Stock stock = stockDownloader.Download("FB");
StockMarkitondemandMapping stockMapping = new StockMarkitondemandMapping(); StockDownloader stockDownloader = new StockDownloader(stockMapping); Stock stock = stockDownloader.Download("PAYC");
Just note that the stock ticker information is provided as an input parameter in the “
Download” method call.
Unit Tests Explained
I am using Android Studio 1.2.2 as my choice of IDE for Java development. This is still a project with a lot of features missing, but it is getting improvements with every release – Google is putting tremendous effort into this project. I decided to stick with Android Studio instead of Eclipse after watching the improvements over a few years.
Now thanks to Google, I can use embedded unit test functionality in the
StocksTracker API. However, the unit test wrapper for Android Studio is still buggy and slow. Hopefully, Google will improve on this area soon. I recommend to run unit tests first on your local computer before including the API module into your project.
If you take a look at the main unit test class,
StockVariousDownloadersTest, you will see 3 methods with the declarative tag “
@Test”. Those are the ones you want to launch, test, and potentially debug before using them in a real project. The unit tests help with the ongoing changes. You will find them very useful if you decide to implement something new or if you want to study an API more closely.
Running unit tests are a particularly simple step in the Android Studio. You can just focus the cursor on one of the unit test methods, then call the context menu by right mouse button click and select ‘Run…’ or ‘Debug…’ menu item. You will see the running result shortly in the message window.
If you decide to code unit tests in your own projects, I should mention that because of this new functionality, json support with latest
org.json.json library is still not working in Android Studio with Android project types. I had to switch the library’s version to the previous release. Please take a look at the build.gradle file for
stocksapi module. This is the line you have to pay attention to:
The open source project,
StocksTracker API, is an application of unifying free, online tools for retrieving the stock market’s information. It is free and simple to use in your own Java project.
The unit tests allow a developer to test and debug the existing API before adapting it.
If you have any recommendation or improvements for the library, feel free to contribute to this open source project.