npressfetimg-3980.png

Fetching Fundamental and Technical Stock Data in C++ – DataDrivenInvestor

C++ Tutorials

Support my content by becoming a referred Medium Member or learning more about C++ programming or AI in finance.

Photo by Luke Chesser on Unsplash

When investing in the stock market good data sources are paramount. Whether your investment decisions are based on fundamental analysis, technical analysis, or a combination of the two, it can be challenging to find the requisite data. Fortunately for me, when I was building my stock market research website, ntrinsically.com, I stumbled on a rich (and affordable) data source that provided both fundamental and technical data, FMP Cloud. With a free tier that offers plenty of monthly requests for the average user and a starter tier offering unlimited requests for only $14/month, in my mind, access to this API was essential.

One problem with any API when building computational tools is easily and efficiently accessing the data. In my case, I have many upcoming finance-focused projects using C++ and thought it would be extremely useful to have access to FMP Cloud’s API endpoints so I’ve built a wrapper around the API that abstracts access to its endpoints and returns RapidJSON Document objects. I’ve written before about building API wrappers in Python and parsing JSON data in C++ using RapidJSON, readers interested in either of these topics are encouraged to read those blog posts for more information.

In this post, I will introduce FMPCloudAPI, a C++ header-only wrapper around the FMP Cloud API. Rather than showing how the wrapper was constructed (which was pretty tedious) I will provide example usage in C++. More example usage can be found in the test and example directories on the project’s GitHub repository. The wrapper implements almost every endpoint in the API so the API documentation is a good place to start to see exactly what data is available.

For starters, anyone wanting to use the API wrapper will need an API key. These are provided by FMP Cloud. Readers interested in trying out the API can sign up for the free tier and get 250 requests per month. I don’t believe any of the endpoints are hidden behind a paywall so any API key, free or otherwise, should have full access to the API functionality.

As most of the method names in the wrapper class’ header file are self-documenting, below I share the class method declarations. As seen in this code snippet there is a lot of data available in the API which, again, is more easily seen in FMP Cloud’s documentation.

This is probably not the best way to display this information but with so much data available this post would be an hour long just describing what each endpoint does. Instead, I’ll jump right into some examples that demonstrate some key endpoints (note that the examples below are just what I find most useful in the API, each user will find varying amounts of utility from these endpoints).

Before using the API wrapper, cURL will need to be installed on your system of choice (I’m using Debian Linux). Additionally, RapidJSON is a header-only C++ JSON parsing library so that will need to be downloaded from their GitHub as well. RapidJSON can also be installed via CMake if you prefer to use the dependency that way, don’t forget to include the linker flag when compiling your code if you take that route.

The source code for these examples can be found in the project’s GitHub repository under the examples directory.

Basic Company Information

The first example will fetch company profile and stock peer information.

The company profile returns important information about the company such as the name, a description of the company, its website’s URL, the CEO, the sector it belongs to, etc.

Company profile information from FMP Cloud.

The next API call fetches the peers for a particular company provided the stock’s ticker symbol. A company’s peers are other companies that are deemed to be most related to the company.

Stock peers for Apple Computer (symbol AAPL).

Price Quotes

No stock market data API would be complete without basic price quote information. FMP Cloud offers many endpoints to fetch stock pricing information including current quotes, historical price data, and batch end-of-day pricing data. Some of this functionality is demonstrated in this example.

Fetching stock price quotes for a ticker and a batch of tickers as well as hourly and minutely historical data.

For starters, a single stock price quote is fetched for Intel Corporation (INTC) resulting in the following JSON.

Stock quote for INTC.

Next, a vector of ticker symbols is provided to the same function call to fetch quote data for many tickers at once.

A batch of stock quotes for ticker symbols: INTC, AAPL, QCOM, PARA, BABA, AMD, TSLA, WMT, and WM.

The final two examples fetch historical minute-by-minute price data and hourly price data. These two calls return a LOT of data so, for brevity, only a small amount is shown below.

Hourly stock data, condensed version.

Minute-by-minute stock price data, condensed version.

Technical Indicators

Other useful trading strategies are created with technical indicators such as various moving averages, RSI, and standard deviation. This information is easily retrieved from the API. The indicators available are SMA, EMA, WMA, DEMA, TEMA, WILLIAMS, RSI, ADX, and STDDEV. The example below demonstrates fetching this data.

As with the historical data above, a lot of data is returned from the API for these technical indicator calls so a small snippet of the data is shown below.

A 10-period exponential moving average (EMA) for Fastly (FSLY).

A snippet of data returned when querying for a 5-period simple moving average (SMA) for Pinterest (PINS).

Financial Statements and SEC Documents

In my opinion, some of the most useful information from FMP Cloud is the financial statement data. These endpoints return data from the income, cash flow, and balance sheet statements which is essential for fundamental analysis. Since so much data is returned via these API calls, only the code used to fetch the data is shown below.

The logic used to fetch financial statements and SEC filing data.

Forex and Crypto

Finally, FMP Cloud offers limited foreign exchange and cryptocurrency data. This data consists of current and historical price quotes for the currency exchanges.

Cryptocurrency and foreign exchange data examples.

Again, for brevity’s sake, the output from these calls is not included in this post. Interested readers are invited to run the examples in the GitHub repository with the provided Makefile, or, better yet, to develop applications of their own using the API wrapper.

In conclusion, the FMP Cloud API is an excellent and affordable resource for any type of stock market investing. The purpose of this blog post was to introduce a C++ header-only wrapper around the API which makes the functionality much more accessible to C++ developers. The wrapper depends on the cURL and RapidJSON libraries. This post provided some example usage of the API wrapper but the demonstrations barely scratch the surface of what is available from FMP Cloud and, thusly, from this API wrapper.

Source: https://medium.datadriveninvestor.com/fetching-fundamental-and-technical-stock-data-in-c-ec2a970bb14e