-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_stocks.py
More file actions
36 lines (26 loc) · 1000 Bytes
/
Copy pathall_stocks.py
File metadata and controls
36 lines (26 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import json
import yfinance as yf
import pandas as pd
import time
# Load the JSON data
with open("vt.json", "r") as file:
data = json.load(file)
# Extract the first 5 holdings from the data
holdings = data.get("holding", [])
# Prepare lists to store tickers and market caps
tickers_list = []
# Loop through each holding and create a Ticker object using ISIN
for holding in holdings:
isin = holding.get("isin")
if isin: # Ensure ISIN is available
ticker_yahoo = yf.Ticker(isin) # Create the Ticker object for ISIN
# Append ticker and market cap to the lists
tickers_list.append(ticker_yahoo.ticker)
time.sleep(1) # Sleep to avoid hitting rate limits
# Create a DataFrame to store the tickers and market caps
df = pd.DataFrame({
"Ticker": tickers_list,
})
# Save the data to a CSV file
df.to_csv("stocks_ordered_by_market_cap.csv", index=False)
print("Stocks ordered by Market Cap saved to stocks_ordered_by_market_cap.csv")