Update on July 2nd 2017Unfortunately, the API described in this article has been changed and now it requires a session cookie to access the data in CSV format. I guess the guys on Yahoo do not like to display their data without having the chance of displaying their ads...
Still, you might download the data manually (example URL here).
In the meantime, I´m looking for an alternate source of prices, I´ll keep you posted.
In a previous article, we defined how to get new stock quotations as soon as they were published in Google Finance (of course, after a 20-minute delay, this is a free service after all).
However, it was hard to study this data without having any historical data to compare with, so we defined a manual process to load some historical prices.
This manual process is very inefficient, though. You need to do it manually and there is a data gap between the last file update and the current quotations.
Solution: Yahoo finance historical quotations
We know that this service is not suitable for "real-time" quotations in case of the Spanish stock exchange, however, it is perfectly good for getting historical quotations during a period of time and offering more information such as:
- Open, close, high and low prices
- Statistical data, such as mobile means, etc.
There seems to be no documentation for this interface at all (or at least, I have not been able to find it), however, I found some information about it buried in this 2009 blog article.
Basically, you need to compose a URL with the stock ticker and some time delimiters, for instance:
New Spring integration flow
As I had to replace the logic for reading the old static files, I created a new flow defined entirely in a separated XML. Basically what it does is:
- Reads from the application settings which stocks are we going to retrieve.
- Reads the time range to recover.
- Performs one request per Stock to the specific URL.
- Parses and converts the received CSV to our internal format.
- Publishes the new POJOs in to the topic so the consumer can use this information seamlessly (it does not really care how the POJOs were built as long as the contract is respected).
This class will receive a signal from Spring ApplicationContext as soon it is initialised (as it implements the interface ApplicationListener), it will parse the property containing the stocks to be retrieved and finally it will inject one message in the downstream channel per stock (Note that the ticker code is set as message header).
HTTP Outbound Gateway
This is the XML configuration for the component that will perform the request. Note how the target ticket is retrieved from the message header ticker and how the range of time is taken from the properties file.
And that´s it! As soon as you see the application running, a stream of data will be loaded into the ElasticSearch node so you can perform studies with wider statistical information.
As usual you can find the code in this GitHub repository. Feedback is always welcome!
- Live Stock data as input for your Spring application
- Implement a Spring Integration flow with Spring Boot
- Integrate the application with ElasticSearch
- Represent and search your data with Kibana
- Deploy your Spring Boot microservice in a Docker container