-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_data.py
More file actions
518 lines (425 loc) · 18.1 KB
/
Copy pathfetch_data.py
File metadata and controls
518 lines (425 loc) · 18.1 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env python3
"""
Standalone multi-threaded data fetching script for EODHD API.
Downloads four datasets per ticker and saves to Parquet:
1. 1-minute intraday bars -> data/raw/1min/{TICKER}.parquet (RAW - all hours)
2. Daily EOD bars -> data/raw/eod/{TICKER}.parquet
3. Stock splits history -> data/raw/splits/{TICKER}.parquet
4. Dividends history -> data/raw/dividends/{TICKER}.parquet
Intraday data is fetched RAW (includes pre-market and after-hours).
Market hours filtering is applied during preprocessing (preprocessing.py)
using exchange calendar for accurate holiday/early-closure handling.
EODHD plan assumed: EOD+Intraday All World Extended.
Usage:
uv run python scripts/fetch_data.py # fetch everything (5 workers)
uv run python scripts/fetch_data.py --force # re-fetch and overwrite all
uv run python scripts/fetch_data.py --workers 10 # use 10 concurrent threads
"""
import argparse
import os
import sys
import threading
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timedelta
from pathlib import Path
import polars as pl
import requests
from tqdm import tqdm
# ──────────────────────────────────────────────────────────────────────
# Configuration – imported from utils.config (api_key read from EODHD_KEY env var)
# ──────────────────────────────────────────────────────────────────────
from utils.config import CONFIG, get_all_tickers # noqa: E402
# Thread-safe logging lock
_log_lock = threading.Lock()
# ──────────────────────────────────────────────────────────────────────
# Logging helper
# ──────────────────────────────────────────────────────────────────────
def _log(msg: str, log_path: Path | None = None) -> None:
"""Print to console and optionally append to log file (thread-safe)."""
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
line = f"[{ts}] {msg}"
with _log_lock:
print(line)
if log_path is not None:
with open(log_path, "a") as f:
f.write(line + "\n")
# ──────────────────────────────────────────────────────────────────────
# Date helpers
# ──────────────────────────────────────────────────────────────────────
def _date_chunks(
start: str,
end: str,
chunk_days: int,
) -> list[tuple[str, str]]:
"""Split a date range into consecutive chunks of at most *chunk_days*."""
fmt = "%Y-%m-%d"
s = datetime.strptime(start, fmt)
e = datetime.strptime(end, fmt)
chunks: list[tuple[str, str]] = []
while s <= e:
chunk_end = min(s + timedelta(days=chunk_days - 1), e)
chunks.append((s.strftime(fmt), chunk_end.strftime(fmt)))
s = chunk_end + timedelta(days=1)
return chunks
# ──────────────────────────────────────────────────────────────────────
# Generic request wrapper with retry
# ──────────────────────────────────────────────────────────────────────
def _api_get(url: str, timeout: int = 30, retries: int = 3) -> requests.Response | None:
"""GET with exponential-backoff retries on transient failures."""
for attempt in range(retries):
try:
resp = requests.get(url, timeout=timeout)
resp.raise_for_status()
return resp
except requests.RequestException:
if attempt < retries - 1:
time.sleep(2 ** (attempt + 1))
return None
# ──────────────────────────────────────────────────────────────────────
# 1. Intraday 1-min bars
# ──────────────────────────────────────────────────────────────────────
def fetch_1min_chunk(
ticker: str,
start: str,
end: str,
api_key: str,
exchange: str = "US",
) -> pl.DataFrame | None:
"""
Fetch a single <=120-day chunk of 1-minute bars.
Returns Polars DataFrame [timestamp, open, high, low, close, volume]
or None on failure / empty response.
"""
base = CONFIG.api.base_url
from_ts = int(datetime.strptime(start, "%Y-%m-%d").timestamp())
to_ts = int((datetime.strptime(end, "%Y-%m-%d") + timedelta(days=1)).timestamp())
url = (
f"{base}/intraday/{ticker}.{exchange}"
f"?api_token={api_key}&interval=1m"
f"&from={from_ts}&to={to_ts}&fmt=json"
)
resp = _api_get(url)
if resp is None:
return None
data = resp.json()
if not data or not isinstance(data, list):
return None
df = pl.DataFrame(data)
# Normalise timestamp column name
if "datetime" in df.columns:
df = df.drop(["timestamp"]).rename({"datetime": "timestamp"})
# Parse timestamp and convert to US/Eastern timezone
if df["timestamp"].dtype == pl.Utf8:
df = df.with_columns(
pl.col("timestamp")
.str.to_datetime(format="%Y-%m-%d %H:%M:%S", time_zone="UTC")
.dt.convert_time_zone("US/Eastern")
.alias("timestamp")
)
elif df["timestamp"].dtype in (pl.Int64, pl.UInt64, pl.Float64):
df = df.with_columns(
pl.from_epoch(pl.col("timestamp"), time_unit="s")
.dt.replace_time_zone("UTC")
.dt.convert_time_zone("US/Eastern")
.alias("timestamp")
)
keep = ["timestamp", "open", "high", "low", "close", "volume"]
df = df.select([c for c in keep if c in df.columns])
for c in ["open", "high", "low", "close"]:
if c in df.columns:
df = df.with_columns(pl.col(c).cast(pl.Float64))
if "volume" in df.columns:
df = df.with_columns(pl.col("volume").cast(pl.Int64))
return df if len(df) > 0 else None
def fetch_intraday_ticker(
ticker: str,
start_date: str,
end_date: str,
api_key: str,
exchange: str = "US",
chunk_days: int = 120,
delay: float = 0.35,
log_path: Path | None = None,
) -> pl.DataFrame | None:
"""
Fetch all 1-min bars for *ticker*, concatenating 120-day chunks.
Returns RAW intraday data (includes pre-market and after-hours).
Market hours filtering is handled by preprocessing.py.
Args:
ticker: Stock ticker symbol
start_date: Start date (YYYY-MM-DD)
end_date: End date (YYYY-MM-DD)
api_key: EODHD API key
exchange: Exchange code (default: US)
chunk_days: Days per API request chunk (default: 120)
delay: Delay between API requests in seconds
log_path: Optional path for logging
Returns:
DataFrame with raw intraday data (all hours) or None if no data
"""
chunks = _date_chunks(start_date, end_date, chunk_days)
frames: list[pl.DataFrame] = []
for i, (cs, ce) in enumerate(chunks):
_log(f" {ticker} 1min: chunk {i + 1}/{len(chunks)} {cs} -> {ce}", log_path)
df = fetch_1min_chunk(ticker, cs, ce, api_key, exchange)
if df is not None:
frames.append(df)
if i < len(chunks) - 1:
time.sleep(delay)
if not frames:
return None
# Concatenate all chunks and return raw data
# Market hours filtering happens in preprocessing.py
combined = pl.concat(frames).unique(subset=["timestamp"]).sort("timestamp")
return combined
# ──────────────────────────────────────────────────────────────────────
# 2. Daily EOD bars
# ──────────────────────────────────────────────────────────────────────
def fetch_eod_ticker(
ticker: str,
start_date: str,
end_date: str,
api_key: str,
exchange: str = "US",
) -> pl.DataFrame | None:
"""
Fetch daily EOD bars for a single ticker.
Returns [date, open, high, low, close, adjusted_close, volume].
"""
base = CONFIG.api.base_url
url = (
f"{base}/eod/{ticker}.{exchange}"
f"?api_token={api_key}&fmt=json"
f"&from={start_date}&to={end_date}"
)
resp = _api_get(url)
if resp is None:
return None
data = resp.json()
if not data or not isinstance(data, list):
return None
df = pl.DataFrame(data)
if "date" in df.columns:
df = df.with_columns(pl.col("date").str.to_date().alias("date"))
keep = ["date", "open", "high", "low", "close", "adjusted_close", "volume"]
df = df.select([c for c in keep if c in df.columns])
for c in ["open", "high", "low", "close", "adjusted_close"]:
if c in df.columns:
df = df.with_columns(pl.col(c).cast(pl.Float64))
if "volume" in df.columns:
df = df.with_columns(pl.col("volume").cast(pl.Int64))
return df if len(df) > 0 else None
# ──────────────────────────────────────────────────────────────────────
# 3. Splits
# ──────────────────────────────────────────────────────────────────────
def fetch_splits_ticker(
ticker: str,
start_date: str,
end_date: str,
api_key: str,
exchange: str = "US",
) -> pl.DataFrame | None:
"""
Fetch stock split history for a single ticker.
Returns [date, split] where split is a string like '4/1'.
"""
base = CONFIG.api.base_url
url = (
f"{base}/splits/{ticker}.{exchange}"
f"?api_token={api_key}&fmt=json"
f"&from={start_date}&to={end_date}"
)
resp = _api_get(url)
if resp is None:
return None
data = resp.json()
if not data or not isinstance(data, list):
return None
df = pl.DataFrame(data)
if len(df) == 0:
return None
if "date" in df.columns:
df = df.with_columns(pl.col("date").str.to_date().alias("date"))
return df
# ──────────────────────────────────────────────────────────────────────
# 4. Dividends
# ──────────────────────────────────────────────────────────────────────
def fetch_dividends_ticker(
ticker: str,
start_date: str,
end_date: str,
api_key: str,
exchange: str = "US",
) -> pl.DataFrame | None:
"""
Fetch dividend history for a single ticker.
Returns at minimum [date, value] (dividend amount per share).
"""
base = CONFIG.api.base_url
url = (
f"{base}/div/{ticker}.{exchange}"
f"?api_token={api_key}&fmt=json"
f"&from={start_date}&to={end_date}"
)
resp = _api_get(url)
if resp is None:
return None
data = resp.json()
if not data or not isinstance(data, list):
return None
df = pl.DataFrame(data)
if len(df) == 0:
return None
if "date" in df.columns:
df = df.with_columns(pl.col("date").str.to_date().alias("date"))
return df
# ──────────────────────────────────────────────────────────────────────
# Orchestrator
# ──────────────────────────────────────────────────────────────────────
def _save_if_present(
df: pl.DataFrame | None,
out_path: Path,
ticker: str,
label: str,
log_path: Path | None,
) -> None:
if df is None:
_log(f" {ticker} {label}: no data", log_path)
return
out_path.parent.mkdir(parents=True, exist_ok=True)
df.write_parquet(out_path)
_log(f" {ticker} {label}: {len(df)} rows -> {out_path}", log_path)
def process_ticker(
ticker: str,
start_date: str,
end_date: str,
api_key: str,
exchange: str,
chunk_days: int,
delay: float,
force: bool,
intraday_dir: Path,
eod_dir: Path,
splits_dir: Path,
div_dir: Path,
log_path: Path,
) -> str:
"""Process all data types for a single ticker. Returns ticker name when done."""
# ── EOD daily ──────────────────────────────────────
eod_file = eod_dir / f"{ticker}.parquet"
if eod_file.exists() and not force:
_log(f" {ticker} eod: SKIP (exists)", log_path)
else:
df = fetch_eod_ticker(ticker, start_date, end_date, api_key, exchange)
_save_if_present(df, eod_file, ticker, "eod", log_path)
time.sleep(delay)
# ── Splits ─────────────────────────────────────
split_file = splits_dir / f"{ticker}.parquet"
if split_file.exists() and not force:
_log(f" {ticker} splits: SKIP (exists)", log_path)
else:
df = fetch_splits_ticker(ticker, start_date, end_date, api_key, exchange)
_save_if_present(df, split_file, ticker, "splits", log_path)
time.sleep(delay)
# ── Dividends ──────────────────────────────────
div_file = div_dir / f"{ticker}.parquet"
if div_file.exists() and not force:
_log(f" {ticker} divs: SKIP (exists)", log_path)
else:
df = fetch_dividends_ticker(ticker, start_date, end_date, api_key, exchange)
_save_if_present(df, div_file, ticker, "divs", log_path)
time.sleep(delay)
# ── Intraday 1-min ─────────────────────────────────
intra_file = intraday_dir / f"{ticker}.parquet"
if intra_file.exists() and not force:
_log(f" {ticker} 1min: SKIP (exists)", log_path)
else:
df = fetch_intraday_ticker(
ticker,
start_date,
end_date,
api_key,
exchange,
chunk_days,
delay,
log_path,
)
_save_if_present(df, intra_file, ticker, "1min", log_path)
time.sleep(delay)
return ticker
def main() -> None:
parser = argparse.ArgumentParser(description="Fetch EODHD data")
parser.add_argument(
"--force",
action="store_true",
help="Re-fetch and overwrite existing data files",
)
parser.add_argument(
"--workers",
type=int,
default=CONFIG.api.max_workers,
help=f"Number of concurrent worker threads (default: {CONFIG.api.max_workers})",
)
args = parser.parse_args()
api_key = CONFIG.api.api_key
if api_key == "your_key_here":
print("ERROR: Set EODHD_KEY environment variable before running.")
sys.exit(1)
universe = get_all_tickers()
start_date = CONFIG.api.start_date
end_date = CONFIG.api.end_date
exchange = CONFIG.api.exchange
chunk_days = CONFIG.api.intraday_chunk_days
delay = CONFIG.api.request_delay
# Directories
data_root = Path("data")
intraday_dir = data_root / "raw" / "1min"
eod_dir = data_root / "raw" / "eod"
splits_dir = data_root / "raw" / "splits"
div_dir = data_root / "raw" / "dividends"
for d in [intraday_dir, eod_dir, splits_dir, div_dir]:
d.mkdir(parents=True, exist_ok=True)
log_path = data_root / "fetch_log.txt"
force = args.force
max_workers = args.workers
_log(
f"Starting fetch: {len(universe)} tickers, {start_date} to {end_date} "
f"[force={'Y' if force else 'N'}, workers={max_workers}]",
log_path,
)
# Use ThreadPoolExecutor for concurrent fetching
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# Submit all ticker processing tasks
futures = {
executor.submit(
process_ticker,
ticker,
start_date,
end_date,
api_key,
exchange,
chunk_days,
delay,
force,
intraday_dir,
eod_dir,
splits_dir,
div_dir,
log_path,
): ticker
for ticker in universe
}
# Track progress with tqdm as tasks complete
with tqdm(total=len(universe), desc="Fetching") as pbar:
for future in as_completed(futures):
ticker = futures[future]
try:
future.result()
pbar.update(1)
except Exception as exc:
_log(f" {ticker} ERROR: {exc}", log_path)
pbar.update(1)
_log("Fetch complete.", log_path)
if __name__ == "__main__":
main()