Finding Connections with the Related Companies API
Aug 1, 2024
In this tutorial, we'll examine the newly released Related Companies API, designed to identify relationships between companies using data from news articles and financial returns data. We will look at the API's functionality by retrieving data for common stocks and visualizing these relationships using Python and vis.js.
Exploring the Related Companies API
The Related Companies API can help you discover relationships between companies that might span across multiple sectors such as technology, semiconductors, media, and many others. Beyond the obvious connections, this API can reveal subtle relationships often mentioned in news articles or financial data where companies from different sectors are mentioned together.
Let's start with a simple example to learn about these relationships. To get started, you'll need an API key, which can be obtainable upon signing up with Polygon.io. For this example, we'll use Python and the Polygon.io client-python library. Here’s how to retrieve the 10 companies most closely related to Apple:
This script then outputs the 10 most related companies to the AAPL ticker provided. Behind the scenes, we look at how often these companies are talked about together in news articles and financial data and then calculate a ranking. So, we provided the ticker symbol
AAPL
as the query parameter and you get back a list of mostly other related tech companies, but notable there is Tesla, Nvidia, Disney, and Berkshire Hathaway here too.
Now that you know the basics of the Related Companies API, let's try and fetch some common tickers, and then visualize this data, just to highlight some of the strong links between these companies.
Visualizing Related Companies
In this section, we will build on what we know already to construct a network graph to visualize the relationships between companies. Let’s start with the "Magnificent 7", those being Microsoft, Amazon, Meta, Apple, Alphabet, Nvidia, and Tesla. These companies are probably best known for their significant market performance, competition between each other, and frequent mentions in various contexts. By using vis.js, we can create dynamic graphs where each node represents a company, and the edges illustrate the strength and nature of their relationships.
This script retrieves related companies, then creates a network of
nodes
and
edges
, with each
node
representing a company, and the
edges
signify the relationships between them. The resulting relationship data is then saved in a JSON format to a file called
data.json
, ready for visualization through tools like vis.js.
from polygon import RESTClient
import json
defget_related_tickers(): client = RESTClient(trace=True)
# Fetch a limited list of tickers to keep the example manageable main_tickers = ["MSFT", "AMZN", "META", "AAPL", "GOOG", "NVDA", "TSLA", "HCP", "GME", "DIS"]
# Prepare data structures for nodes and edges nodes = []
edges = []
id_map = {}
current_id = 1# Iterate over each main ticker and find related tickersfor ticker in main_tickers:
if ticker notin id_map:
id_map[ticker] = current_id
nodes.append({'id': current_id, 'label': ticker})
current_id += 1
related_companies = client.get_related_companies(ticker)
for company in related_companies:
related_ticker = company.ticker
if related_ticker notin id_map:
id_map[related_ticker] = current_id
nodes.append({'id': current_id, 'label': related_ticker})
current_id += 1 edges.append({'from': id_map[ticker], 'to': id_map[related_ticker]})
# Save the nodes and edges to a JSON file for web visualizationwithopen('data.json', 'w') as f:
json.dump({'nodes': nodes, 'edges': edges}, f)
if __name__ == "__main__":
get_related_tickers()
The generated JSON file structures the data into two arrays:
nodes
and
edges
. Here’s a brief glimpse of what the JSON file looks like:
properties linking node IDs, indicating the relationship between companies. This format supports dynamic visualizations showing how different companies are interconnected through various data sources.
To render the graph, we'll use a basic HTML structure using vis.js using an example from here. Below is a simplified version of the HTML file, where you can add arrays for
nodes
and
edges
, which you should replace with actual data derived from your
data.json
file. For a complete example, that includes the Python script for generating this data, with the full HTML file, visit our GitHub repository.
After setting up the HTML as described and replacing the placeholder data with actual network data, you will be able to visualize the relationships between companies as an interactive network graph by opening the file in your web browser.
Try modifying the source tickers in the Python script, regenerate and copy over the data, then observe how the network changes. This example goes beyond the new Related Companies API but hopefully gives you some ideas on what you could build and how to create a visually appealing and interactive graph that helps you understand complex relationships between companies.
Next steps
In this tutorial, we've explored the capabilities of the newly released Related Companies API from Polygon.io, which provides a robust tool for identifying company relationships through news and financial data.
The examples provided, particularly the Python script to fetch related companies, highlight the API’s practical application and ease of integration. The visualization with vis.js serves as an additional demonstration of how you might utilize the data in a dynamic format. We encourage you to dive into the API, integrate it with your projects, and leverage its insights to enhance your data-driven analyses. For more detailed examples and to access the complete codebase, visit our GitHub repository.
We are excited to announce our integration with QuantConnect! This offering empowers users with state-of-the-art research, backtesting, parameter optimization, and live trading capabilities, all fueled by the robust market data APIs and WebSocket Streams of Polygon.io.
Polygon now includes daily historical Flat Files in all paid plans at no extra charge, featuring a new web-based File Browser and S3 access for simplified data exploration and integration.