Intrinio Financial Data in R and RStudio

Data analysts everywhere know that most of their time is spent gathering, cleaning, and formatting data. The actual analysis and interpreting the results is fun, fast, and easy once the data is structured how you need it. This blog shows how easy Intrinio makes it to complete the nasty part of analysis by using the Intrinio API to pull financial data into R.

Intrinio provides many data feeds via API and if you learn to use the API in conjunction with R, you can spend a lot more of your time running the analysis and analyzing the results and a lot less time on data entry.

If you don't use R this might not be the blog for you, but if you do, this blog will show you in step by step fashion how to save yourself a lot of money, and make yourself a lot of time, by using the Intrinio API in the R terminal or RStudio.

I prefer to work in R studio since I am really a beginner but the following instructions are just as valid in the terminal. I am in the process of creating a Github repository of this code and expect to have that available by December. For now, feel free to copy this code and please comment if you find more efficient ways of pulling in the data.

Step 1: Setting up your environment

Run these three lines of code in a new script:

The previous three commands clean up your environment to remove any objects from a previous session, install the httr package, and make sure you have the httr library active respectively. After the first time you install httr you can delete that line of code in your script.

Step 2: API Username And API Password

Go to intrinio.com/login and create an account. Its free and just requires an email. If you want more information about how Intrinio works, here is a blog explaining things at a high level.

Once you are logged in, you will see this-

screen-shot-2016-09-25-at-8-08-37-pm

You will see that you are automatically enrolled in three of Intrinio's data plans at the free level. This gives you lots of free data to use everyday. For now you should click the icons, one at a time, that have red arrows pointing to them. This will copy your API keys to the clip board- go ahead and paste them into R.

Your going to want to make them into objects, your code should look like this:

The only difference should be you are using your username and password, not mine.

3: Creating your API call

If you haven't used an API before, and haven't read through Intrinio's documentation, here is a blog about getting started with the Intrinio API that will help you make sense of this next step. Create the following objects- no changes this time, copy them just like this:

Go ahead and print call1 and call2 so you can look at them. What the paste function did was to combine each of the objects you created into two different API calls, one for Apple's close_price, or most recent stock price, and another for Apple's price to earnings ratio. The full API calls should look like this:

https://api.intrinio.com/data_point?ticker=AAPL&item=pricetoearnings

https://api.intrinio.com/data_point?ticker=AAPL&item=close_price

4. Making the API calls

Create the following objects:

Those objects combine your API calls with your username and password to use httr's GET function to send your request to Intrinio's database.

5. Parsing out the return value

The following code parses out the data sent over from Intrinio's server so you can analyze it:

Now that you have the data parsed, you can put it into a dataframe for analysis:

You end result should look like this except the values will reflect the values from the time of your API call:

screen-shot-2016-09-25-at-8-32-19-pm

If you followed along, what you will have in your dataframe are the latest stock price for Apple (EOD if after hours) as well as the latest price to earnings ratio.

I designed the syntax to make it easy to make more calls by varying the "item" object and using it to make different API calls. If you repeat that process, and learn the different tags that Intrinio uses, you can very quickly pull in historical data for any stock, valuation metrics, balance sheet data, or any other data type that Intrinio provides.

As you can see, this is just the tip of the iceberg. These calls used 2 of 500 free daily API calls. My plan is to build a dashboard next that prints a full analysis of a stock.

Update 10/10/16- Check out this blog showing how to create custom functions to pull historical data in R

Update 05/22/16- Check out this blog showing how to create a for loop in R to get multiple pages of data via API. This example shows the best way (known to the author) to parse JSON from an API in R.