-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (51 loc) · 2.03 KB
/
main.py
File metadata and controls
62 lines (51 loc) · 2.03 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import httpx
import csv
import asyncio
# Inputs
alpha_base = "https://www.alphavantage.co/query"
api_key = "your_api_key_here"
symbols = ["AAPL", "MSFT", "AMZN"]
interval = "5min"
async def fetch_intraday(client, symbol):
params = {
"function": "TIME_SERIES_INTRADAY",
"symbol": symbol,
"interval": interval,
"apikey": api_key
}
response = await client.get(alpha_base, params=params, timeout=10.0)
response.raise_for_status()
data = response.json()
return symbol, data
async def main():
# Fetch all symbols concurrently
async with httpx.AsyncClient() as client:
tasks = [fetch_intraday(client, symbol) for symbol in symbols]
results = await asyncio.gather(*tasks)
# Write everything into one CSV
with open('all_symbols.csv', 'w', newline='') as csvfile:
fieldnames = ['symbol', 'timestamp', 'open', 'high', 'low', 'close', 'volume']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for symbol, data in results:
print(f"Fetching intraday ({interval}) data for {symbol}")
print(symbol, "Keys: ", list(data.keys()))
time_series_key = f"Time Series ({interval})"
if time_series_key not in data:
print(f"WARNING: No '{time_series_key}' key for '{symbol}'. Got Keys: {list(data.keys())}")
continue
time_series_data = data[time_series_key]
for timestamp, ohlcv in time_series_data.items():
row = {
'symbol': symbol,
'timestamp': timestamp,
'open': float(ohlcv['1. open']),
'high': float(ohlcv['2. high']),
'low': float(ohlcv['3. low']),
'close': float(ohlcv['4. close']),
'volume': float(ohlcv['5. volume'])
}
writer.writerow(row)
print("All data saved to all_symbols.csv")
if __name__ == "__main__":
asyncio.run(main())