Skip to content

Commit 5a4e204

Browse files
Add related companies demo (#717)
* Added related companies example * Added image to readme
1 parent c2f955e commit 5a4e204

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"nodes": [{"id": 1, "label": "MSFT"}, {"id": 2, "label": "GOOGL"}, {"id": 3, "label": "NVDA"}, {"id": 4, "label": "AMZN"}, {"id": 5, "label": "GOOG"}, {"id": 6, "label": "META"}, {"id": 7, "label": "TSLA"}, {"id": 8, "label": "AAPL"}, {"id": 9, "label": "CRM"}, {"id": 10, "label": "ORCL"}, {"id": 11, "label": "AMD"}, {"id": 12, "label": "NFLX"}, {"id": 13, "label": "WMT"}, {"id": 14, "label": "DIS"}, {"id": 15, "label": "SNAP"}, {"id": 16, "label": "SHOP"}, {"id": 17, "label": "INTC"}, {"id": 18, "label": "ANET"}, {"id": 19, "label": "RIVN"}, {"id": 20, "label": "GM"}, {"id": 21, "label": "F"}, {"id": 22, "label": "LCID"}, {"id": 23, "label": "WBD"}, {"id": 24, "label": "CMCSA"}, {"id": 25, "label": "PARA"}, {"id": 26, "label": "T"}, {"id": 27, "label": "ROKU"}], "edges": [{"from": 1, "to": 2}, {"from": 1, "to": 3}, {"from": 1, "to": 4}, {"from": 1, "to": 5}, {"from": 1, "to": 6}, {"from": 1, "to": 7}, {"from": 1, "to": 8}, {"from": 1, "to": 9}, {"from": 1, "to": 10}, {"from": 1, "to": 11}, {"from": 4, "to": 1}, {"from": 4, "to": 2}, {"from": 4, "to": 5}, {"from": 4, "to": 8}, {"from": 4, "to": 7}, {"from": 4, "to": 3}, {"from": 4, "to": 6}, {"from": 4, "to": 12}, {"from": 4, "to": 13}, {"from": 4, "to": 14}, {"from": 6, "to": 5}, {"from": 6, "to": 2}, {"from": 6, "to": 1}, {"from": 6, "to": 4}, {"from": 6, "to": 8}, {"from": 6, "to": 7}, {"from": 6, "to": 3}, {"from": 6, "to": 15}, {"from": 6, "to": 12}, {"from": 6, "to": 11}, {"from": 8, "to": 1}, {"from": 8, "to": 2}, {"from": 8, "to": 4}, {"from": 8, "to": 5}, {"from": 8, "to": 7}, {"from": 8, "to": 3}, {"from": 8, "to": 6}, {"from": 8, "to": 12}, {"from": 8, "to": 14}, {"from": 8, "to": 11}, {"from": 5, "to": 2}, {"from": 5, "to": 1}, {"from": 5, "to": 6}, {"from": 5, "to": 4}, {"from": 5, "to": 8}, {"from": 5, "to": 7}, {"from": 5, "to": 3}, {"from": 5, "to": 15}, {"from": 5, "to": 12}, {"from": 5, "to": 16}, {"from": 3, "to": 11}, {"from": 3, "to": 6}, {"from": 3, "to": 2}, {"from": 3, "to": 7}, {"from": 3, "to": 5}, {"from": 3, "to": 1}, {"from": 3, "to": 8}, {"from": 3, "to": 4}, {"from": 3, "to": 17}, {"from": 3, "to": 18}, {"from": 7, "to": 19}, {"from": 7, "to": 2}, {"from": 7, "to": 4}, {"from": 7, "to": 20}, {"from": 7, "to": 21}, {"from": 7, "to": 22}, {"from": 7, "to": 5}, {"from": 7, "to": 6}, {"from": 7, "to": 8}, {"from": 7, "to": 3}, {"from": 14, "to": 12}, {"from": 14, "to": 23}, {"from": 14, "to": 4}, {"from": 14, "to": 24}, {"from": 14, "to": 25}, {"from": 14, "to": 8}, {"from": 14, "to": 2}, {"from": 14, "to": 26}, {"from": 14, "to": 5}, {"from": 14, "to": 27}]}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Vis Network | Related Companies</title>
5+
<style type="text/css">
6+
#mynetwork {
7+
width: 2400px;
8+
height: 1200px;
9+
border: 1px solid lightgray;
10+
}
11+
</style>
12+
<script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
13+
<script type="text/javascript">
14+
var nodes = null;
15+
var edges = null;
16+
var network = null;
17+
function draw() {
18+
nodes = [{"id": 1, "label": "MSFT"}, {"id": 2, "label": "GOOGL"}, {"id": 3, "label": "NVDA"}, {"id": 4, "label": "AMZN"}, {"id": 5, "label": "GOOG"}, {"id": 6, "label": "META"}, {"id": 7, "label": "TSLA"}, {"id": 8, "label": "AAPL"}, {"id": 9, "label": "CRM"}, {"id": 10, "label": "ORCL"}, {"id": 11, "label": "AMD"}, {"id": 12, "label": "NFLX"}, {"id": 13, "label": "WMT"}, {"id": 14, "label": "DIS"}, {"id": 15, "label": "SNAP"}, {"id": 16, "label": "SHOP"}, {"id": 17, "label": "INTC"}, {"id": 18, "label": "ANET"}, {"id": 19, "label": "RIVN"}, {"id": 20, "label": "GM"}, {"id": 21, "label": "F"}, {"id": 22, "label": "LCID"}, {"id": 23, "label": "GME"}, {"id": 24, "label": "AMC"}, {"id": 25, "label": "HOOD"}, {"id": 26, "label": "BB"}, {"id": 27, "label": "CHWY"}, {"id": 28, "label": "CLOV"}, {"id": 29, "label": "PLTR"}, {"id": 30, "label": "SNDL"}, {"id": 31, "label": "WBD"}, {"id": 32, "label": "CMCSA"}, {"id": 33, "label": "PARA"}, {"id": 34, "label": "T"}, {"id": 35, "label": "ROKU"}];
19+
edges = [{"from": 1, "to": 2}, {"from": 1, "to": 3}, {"from": 1, "to": 4}, {"from": 1, "to": 5}, {"from": 1, "to": 6}, {"from": 1, "to": 7}, {"from": 1, "to": 8}, {"from": 1, "to": 9}, {"from": 1, "to": 10}, {"from": 1, "to": 11}, {"from": 4, "to": 1}, {"from": 4, "to": 2}, {"from": 4, "to": 5}, {"from": 4, "to": 8}, {"from": 4, "to": 7}, {"from": 4, "to": 3}, {"from": 4, "to": 6}, {"from": 4, "to": 12}, {"from": 4, "to": 13}, {"from": 4, "to": 14}, {"from": 6, "to": 5}, {"from": 6, "to": 2}, {"from": 6, "to": 1}, {"from": 6, "to": 4}, {"from": 6, "to": 8}, {"from": 6, "to": 7}, {"from": 6, "to": 3}, {"from": 6, "to": 15}, {"from": 6, "to": 12}, {"from": 6, "to": 11}, {"from": 8, "to": 1}, {"from": 8, "to": 2}, {"from": 8, "to": 4}, {"from": 8, "to": 5}, {"from": 8, "to": 7}, {"from": 8, "to": 3}, {"from": 8, "to": 6}, {"from": 8, "to": 12}, {"from": 8, "to": 14}, {"from": 8, "to": 11}, {"from": 5, "to": 2}, {"from": 5, "to": 1}, {"from": 5, "to": 6}, {"from": 5, "to": 4}, {"from": 5, "to": 8}, {"from": 5, "to": 7}, {"from": 5, "to": 3}, {"from": 5, "to": 15}, {"from": 5, "to": 12}, {"from": 5, "to": 16}, {"from": 3, "to": 11}, {"from": 3, "to": 6}, {"from": 3, "to": 2}, {"from": 3, "to": 7}, {"from": 3, "to": 5}, {"from": 3, "to": 1}, {"from": 3, "to": 8}, {"from": 3, "to": 4}, {"from": 3, "to": 17}, {"from": 3, "to": 18}, {"from": 7, "to": 19}, {"from": 7, "to": 2}, {"from": 7, "to": 4}, {"from": 7, "to": 20}, {"from": 7, "to": 21}, {"from": 7, "to": 22}, {"from": 7, "to": 5}, {"from": 7, "to": 6}, {"from": 7, "to": 8}, {"from": 7, "to": 3}, {"from": 23, "to": 24}, {"from": 23, "to": 7}, {"from": 23, "to": 25}, {"from": 23, "to": 26}, {"from": 23, "to": 27}, {"from": 23, "to": 28}, {"from": 23, "to": 4}, {"from": 23, "to": 29}, {"from": 23, "to": 2}, {"from": 23, "to": 30}, {"from": 14, "to": 12}, {"from": 14, "to": 31}, {"from": 14, "to": 4}, {"from": 14, "to": 32}, {"from": 14, "to": 33}, {"from": 14, "to": 8}, {"from": 14, "to": 2}, {"from": 14, "to": 34}, {"from": 14, "to": 5}, {"from": 14, "to": 35}];
20+
var container = document.getElementById("mynetwork");
21+
var data = { nodes: nodes, edges: edges };
22+
var options = { nodes: { shape: 'dot' } };
23+
network = new vis.Network(container, data, options);
24+
}
25+
</script>
26+
</head>
27+
<body onload="draw()">
28+
<div id="mynetwork"></div>
29+
</body>
30+
</html>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See Connections with the Related Companies API
2+
3+
This repository contains the Python script and HTML file used in our tutorial to demonstrate how to identify and visualize relationships between companies using Polygon.io's Related Companies API. The tutorial showcases how to fetch related company data and create a dynamic network graph using Python and vis.js, providing insights into the interconnected corporate landscape.
4+
5+
![Related Companies](./related-companies.png)
6+
7+
Please see the [tutorial](https://polygon.io/blog/related-companies-api) for more details.
8+
9+
### Prerequisites
10+
11+
- Python 3.8+
12+
- Have Polygon.io's [python client](https://github.com/polygon-io/client-python) installed
13+
- An active Polygon.io account with an API key
14+
15+
### Repository Contents
16+
17+
- `related-companies-demo.py`: Python script to fetch and process data from the Related Companies API.
18+
- `index.html`: HTML file for visualizing the data as a network graph using vis.js.
19+
20+
### Running the Example
21+
22+
To run the Python script, ensure you have Python installed and your API key ready. Execute the following command:
23+
24+
```
25+
python related-companies-demo.py
26+
```
27+
28+
The script will generate a `data.json` file, which contains the nodes and edges for the network graph.
29+
30+
To visualize the relationships:
31+
32+
1. Take the `nodes` and `edges` from the `data.json` file and replace them in the `index.html` file
33+
2. Open `index.html` in your web browser.
34+
3. The web page should display the network graph.
35+
36+
For a complete step-by-step guide on setting up and exploring the capabilities of the Related Companies API, refer to our detailed [tutorial](https://polygon.io/blog/related-companies-api).
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from polygon import RESTClient
2+
import json
3+
4+
5+
def get_related_tickers():
6+
client = RESTClient(trace=True)
7+
8+
# Fetch a limited list of tickers to keep the example manageable
9+
main_tickers = ["MSFT", "AMZN", "META", "AAPL", "GOOG", "NVDA", "TSLA", "DIS"]
10+
11+
# Prepare data structures for nodes and edges
12+
nodes = []
13+
edges = []
14+
id_map = {}
15+
current_id = 1
16+
17+
# Iterate over each main ticker and find related tickers
18+
for ticker in main_tickers:
19+
if ticker not in id_map:
20+
id_map[ticker] = current_id
21+
nodes.append({"id": current_id, "label": ticker})
22+
current_id += 1
23+
24+
related_companies = client.get_related_companies(ticker)
25+
for company in related_companies:
26+
related_ticker = company.ticker
27+
if related_ticker not in id_map:
28+
id_map[related_ticker] = current_id
29+
nodes.append({"id": current_id, "label": related_ticker})
30+
current_id += 1
31+
32+
edges.append({"from": id_map[ticker], "to": id_map[related_ticker]})
33+
34+
# Save the nodes and edges to a JSON file for web visualization
35+
with open("data.json", "w") as f:
36+
json.dump({"nodes": nodes, "edges": edges}, f)
37+
38+
39+
if __name__ == "__main__":
40+
get_related_tickers()
Loading

0 commit comments

Comments
 (0)