-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_ingestion.py
More file actions
367 lines (292 loc) · 12.8 KB
/
Copy pathrun_ingestion.py
File metadata and controls
367 lines (292 loc) · 12.8 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
#!/usr/bin/env python
"""
Data Ingestion Pipeline - Integration Test & Verification Script
This script verifies the end-to-end data ingestion pipeline:
1. Initialize CryptoDataLoader with exchange connection
2. Fetch historical OHLCV data from Binance
3. Store data in ArcticDB
4. Read back and verify data integrity
Usage:
python run_ingestion.py
python run_ingestion.py --symbol ETH/USDT --timeframe 4h --days 7
"""
from __future__ import annotations
import sys
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import TYPE_CHECKING
from loguru import logger
if TYPE_CHECKING:
import pandas as pd
# ═══════════════════════════════════════════════════════════════════════════════
# CONFIGURATION
# ═══════════════════════════════════════════════════════════════════════════════
# Default ingestion parameters
DEFAULT_SYMBOL = "BTC/USDT"
DEFAULT_TIMEFRAME = "1h"
DEFAULT_DAYS = 1
DEFAULT_EXCHANGE = "binance"
# Logging configuration
LOG_FORMAT = (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | "
"<level>{message}</level>"
)
# ═══════════════════════════════════════════════════════════════════════════════
# LOGGING SETUP
# ═══════════════════════════════════════════════════════════════════════════════
def configure_logging() -> None:
"""Configure loguru with structured output."""
logger.remove() # Remove default handler
logger.add(
sys.stderr,
format=LOG_FORMAT,
level="DEBUG",
colorize=True,
)
# Add file logging for audit trail
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)
logger.add(
log_dir / "ingestion_{time:YYYY-MM-DD}.log",
format=LOG_FORMAT,
level="INFO",
rotation="1 day",
retention="7 days",
compression="gz",
)
# ═══════════════════════════════════════════════════════════════════════════════
# PIPELINE COMPONENTS (Single Responsibility)
# ═══════════════════════════════════════════════════════════════════════════════
class DataFetcher:
"""Responsible for fetching data from exchange."""
def __init__(self, loader) -> None:
self._loader = loader
def fetch(
self,
symbol: str,
timeframe: str,
since: datetime,
until: datetime,
) -> "pd.DataFrame":
"""Fetch OHLCV data for the specified parameters."""
logger.info(
f"Fetching {symbol} {timeframe} | "
f"From: {since.isoformat()} | To: {until.isoformat()}"
)
df = self._loader.fetch_data(
symbol=symbol,
timeframe=timeframe,
since=since.isoformat(),
until=until.isoformat(),
)
logger.success(f"Fetched {len(df)} rows from exchange")
return df
class DataPersister:
"""Responsible for storing data to ArcticDB."""
def __init__(self, loader) -> None:
self._loader = loader
def store(self, df: "pd.DataFrame", symbol: str, timeframe: str) -> None:
"""Store DataFrame to ArcticDB."""
logger.info(f"Storing {len(df)} rows to ArcticDB...")
self._loader.store_data(df, symbol, timeframe)
logger.success(f"Data persisted to ArcticDB: {symbol} {timeframe}")
def retrieve(self, symbol: str, timeframe: str) -> "pd.DataFrame":
"""Retrieve DataFrame from ArcticDB."""
logger.info(f"Retrieving data from ArcticDB: {symbol} {timeframe}")
df = self._loader.get_data(symbol, timeframe)
logger.success(f"Retrieved {len(df)} rows from ArcticDB")
return df
class IntegrityVerifier:
"""Responsible for verifying data integrity."""
@staticmethod
def verify(original: "pd.DataFrame", retrieved: "pd.DataFrame") -> bool:
"""
Verify that retrieved data matches original data.
Returns:
True if verification passes, False otherwise.
"""
logger.info("Verifying data integrity...")
# Check row count
if len(original) != len(retrieved):
logger.error(
f"Row count mismatch: Original={len(original)}, Retrieved={len(retrieved)}"
)
return False
# Check column presence
if set(original.columns) != set(retrieved.columns):
logger.error(
f"Column mismatch: Original={list(original.columns)}, "
f"Retrieved={list(retrieved.columns)}"
)
return False
# Check data values (allow for floating point tolerance)
try:
import pandas as pd
pd.testing.assert_frame_equal(
original.reset_index(drop=True),
retrieved.reset_index(drop=True),
check_exact=False,
rtol=1e-5,
)
logger.success("Data integrity verification PASSED")
return True
except AssertionError as e:
logger.error(f"Data integrity verification FAILED: {e}")
return False
class ResultPresenter:
"""Responsible for presenting results to console."""
@staticmethod
def display(df: "pd.DataFrame", title: str) -> None:
"""Display DataFrame summary and sample rows."""
print("\n" + "═" * 80)
print(f" {title}")
print("═" * 80)
print(f"\n📊 DataFrame Shape: {df.shape[0]} rows × {df.shape[1]} columns")
print(f"📅 Date Range: {df.index.min()} → {df.index.max()}")
print(f"📋 Columns: {list(df.columns)}")
print("\n🔍 First 5 Rows:")
print("-" * 80)
print(df.head(5).to_string())
print("-" * 80)
# Show basic statistics
print("\n📈 Price Summary:")
print(f" Open - Min: {df['open'].min():.2f}, Max: {df['open'].max():.2f}")
print(f" Close - Min: {df['close'].min():.2f}, Max: {df['close'].max():.2f}")
print(f" Volume Total: {df['volume'].sum():.4f}")
print("═" * 80 + "\n")
# ═══════════════════════════════════════════════════════════════════════════════
# PIPELINE ORCHESTRATOR
# ═══════════════════════════════════════════════════════════════════════════════
class IngestionPipeline:
"""
Orchestrates the data ingestion pipeline.
Follows the Single Responsibility Principle by delegating
specific tasks to specialized components.
"""
def __init__(
self,
exchange_id: str = DEFAULT_EXCHANGE,
use_testnet: bool = True,
) -> None:
"""Initialize the pipeline with exchange connection."""
from src.data.loader import CryptoDataLoader
logger.info(f"Initializing pipeline | Exchange: {exchange_id} | Testnet: {use_testnet}")
self._loader = CryptoDataLoader(
exchange_id=exchange_id,
use_testnet=use_testnet,
)
self._fetcher = DataFetcher(self._loader)
self._persister = DataPersister(self._loader)
self._verifier = IntegrityVerifier()
self._presenter = ResultPresenter()
logger.success("Pipeline initialized successfully")
def run(
self,
symbol: str = DEFAULT_SYMBOL,
timeframe: str = DEFAULT_TIMEFRAME,
days: int = DEFAULT_DAYS,
) -> bool:
"""
Execute the full ingestion pipeline.
Args:
symbol: Trading pair (e.g., "BTC/USDT").
timeframe: Candle timeframe (e.g., "1h").
days: Number of days of historical data to fetch.
Returns:
True if pipeline succeeds, False otherwise.
"""
logger.info("=" * 60)
logger.info("STARTING DATA INGESTION PIPELINE")
logger.info("=" * 60)
try:
# Calculate date range
until = datetime.now(timezone.utc)
since = until - timedelta(days=days)
# Step 1: Fetch data from exchange
fetched_df = self._fetcher.fetch(symbol, timeframe, since, until)
if fetched_df.empty:
logger.error("No data fetched from exchange")
return False
# Step 2: Store to ArcticDB
self._persister.store(fetched_df, symbol, timeframe)
# Step 3: Retrieve from ArcticDB
retrieved_df = self._persister.retrieve(symbol, timeframe)
# Step 4: Verify integrity
# Note: Retrieved may have more rows if previous data exists
# We verify the latest fetched data is present
is_valid = len(retrieved_df) >= len(fetched_df)
if not is_valid:
logger.error("Data integrity check failed")
return False
# Step 5: Present results
self._presenter.display(retrieved_df, f"INGESTED DATA: {symbol} {timeframe}")
logger.success("=" * 60)
logger.success("DATA INGESTION PIPELINE COMPLETED SUCCESSFULLY")
logger.success("=" * 60)
return True
except Exception as e:
logger.exception(f"Pipeline failed with error: {e}")
return False
# ═══════════════════════════════════════════════════════════════════════════════
# CLI INTERFACE
# ═══════════════════════════════════════════════════════════════════════════════
def parse_args():
"""Parse command line arguments."""
import argparse
parser = argparse.ArgumentParser(
description="Data Ingestion Pipeline - Fetch, Store, and Verify OHLCV data",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--symbol",
type=str,
default=DEFAULT_SYMBOL,
help="Trading pair symbol (e.g., BTC/USDT)",
)
parser.add_argument(
"--timeframe",
type=str,
default=DEFAULT_TIMEFRAME,
choices=["1m", "5m", "15m", "1h", "4h", "1d"],
help="Candle timeframe",
)
parser.add_argument(
"--days",
type=int,
default=DEFAULT_DAYS,
help="Number of days of historical data",
)
parser.add_argument(
"--exchange",
type=str,
default=DEFAULT_EXCHANGE,
help="Exchange ID (ccxt)",
)
parser.add_argument(
"--live",
action="store_true",
help="Use live API instead of testnet",
)
return parser.parse_args()
def main() -> int:
"""Main entry point."""
configure_logging()
logger.info("╔════════════════════════════════════════════════════════════╗")
logger.info("║ ALGO TRADING BOT - DATA INGESTION VERIFICATION ║")
logger.info("╚════════════════════════════════════════════════════════════╝")
args = parse_args()
logger.info(f"Parameters: symbol={args.symbol}, timeframe={args.timeframe}, days={args.days}")
pipeline = IngestionPipeline(
exchange_id=args.exchange,
use_testnet=not args.live,
)
success = pipeline.run(
symbol=args.symbol,
timeframe=args.timeframe,
days=args.days,
)
return 0 if success else 1
if __name__ == "__main__":
sys.exit(main())