Stock Analysis

Author

Logan Younker

Introduction

The data used for this exercise comes from financial modeling prep. The data is hosted at https://site.financialmodelingprep.com/developer/docs and requests for a API Key can also be made at this site. Once an API key is granted, the user can gather lots of different information about companies of their choosing. In this document, I will be using their stock screener, which a gathers a magnitude of information about multiple companies.

Variables

Some of the variables this API will use, include, but are not limited to:

Symbol

Sector

Price

Volume

Market Cap

Setting up the API

The necessary packages, as well as the api endpoint, are required. This is what it should look like

library(tidyverse)
library(jsonlite)
library(magrittr)

#API Endpoint
stocks_endpoint <- "https://financialmodelingprep.com/api/v3/"

Now that the necessary packages and endpoint is loaded in, you will need to define your API key. Any example of this would look like:

apikey <- "apikey=YOUR_API_KEY"

Defining the fields

Making a call to the API requires that specific fields are outlined. In this example, the stock screener is selected by:

fields <- paste("stock-screener?")

Final API Call

Once your endpoint, api key, and chosen field are all defined you will be able to make a request for the data. This will give you your final api call URL. The results will look like this:

#Creating final API call URL
stocks_api_GET <- 
  paste(stocks_endpoint,fields,apikey, sep ="")

#Get data
stocks_api_GET
[1] "https://financialmodelingprep.com/api/v3/stock-screener?apikey=YOUR_API_KEY"

Your final API URL should look like this:

https://financialmodelingprep.com/api/v3/stock-screener?apikey=YOUR_API_KEY

Data Frame and Visualization

After generating this URL, the data can be retrieved in R-Studio using the JSON document that has been created. The eval() function is used to convert the data to R-Studio in order to be viewed as a data frame. The result will look like this:

stocks_data <- 
  stocks_api_GET %>% 
  eval() %>% 
  fromJSON() 

Now using the data frame, lets create a visual of the average stock price of each sector

#Calculate average stock price by sector
average_stock_prices <- stocks_data %>%
  group_by(sector) %>%
  summarise(avg_price = mean(price, na.rm = TRUE)) %>%
  arrange(desc(avg_price))

#Filter out rows with missing or blank sector values
filtered_average_stock_prices <- average_stock_prices %>%
  filter(!is.na(sector) & sector != "")

#Visualization
ggplot(filtered_average_stock_prices, aes(x = reorder(sector, avg_price), y = avg_price)) +
  geom_bar(stat = "identity", fill = "blue") +
  labs(title = "Average Stock Price by Sector",
       x = "Sector",
       y = "Average Stock Price") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

From this chart we see that the financial services sector has the highest average stock price. This may be a little misleading since the financial services sector has a stock that is valued at $622,380. Doing a more detailed analysis you may choose to keep or remove it, but in this case I will just be keeping it in.

Summary

The Financial modeling prep stock screener API offers a large and extensive data set for providing detailed information about a vast array of stocks. The accessibility, and amount of data available make it an excellent tool for many different types of projects.