Repository navigation
Expand file tree
/
Copy pathvalidate.py
More file actions
563 lines (476 loc) · 24 KB
/
Copy pathvalidate.py
File metadata and controls
563 lines (476 loc) · 24 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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import argparse
import os
import time
from collections import defaultdict
from datetime import datetime
from typing import List, Optional, Dict, Any, Tuple
import ccxt
import pandas as pd
from src.timeframe_manager import TimeframeManager
class MultiExchangeGapFiller:
def __init__(self) -> None:
self.exchanges = {
'binance': ccxt.binance({'enableRateLimit': True}),
'binanceusdm': ccxt.binanceusdm({'enableRateLimit': True}),
'binanceus': ccxt.binanceus({'enableRateLimit': True}),
'bitget': ccxt.bitget({'enableRateLimit': True}),
'bitmart': ccxt.bitmart({'enableRateLimit': True}),
'bitmex': ccxt.bitmex({'enableRateLimit': True}),
'bybit': ccxt.bybit({'enableRateLimit': True}),
'coinbase': ccxt.coinbase({'enableRateLimit': True}),
'coinex': ccxt.coinex({'enableRateLimit': True}),
'cryptocom': ccxt.cryptocom({'enableRateLimit': True}),
'gate': ccxt.gate({'enableRateLimit': True}),
'htx': ccxt.htx({'enableRateLimit': True}),
'kucoin': ccxt.kucoin({'enableRateLimit': True}),
'kucoinfutures': ccxt.kucoinfutures({'enableRateLimit': True}),
'mexc': ccxt.mexc({'enableRateLimit': True}),
'okx': ccxt.okx({'enableRateLimit': True}),
'woo': ccxt.woo({'enableRateLimit': True}),
'ascendex': ccxt.ascendex({'enableRateLimit': True}),
'bitfinex': ccxt.bitfinex({'enableRateLimit': True}),
'bitstamp': ccxt.bitstamp({'enableRateLimit': True}),
'bitrue': ccxt.bitrue({'enableRateLimit': True}),
'bitvavo': ccxt.bitvavo({'enableRateLimit': True}),
'blockchaincom': ccxt.blockchaincom({'enableRateLimit': True}),
'coinbaseinternational': ccxt.coinbaseinternational({'enableRateLimit': True}),
'deribit': ccxt.deribit({'enableRateLimit': True}),
'gemini': ccxt.gemini({'enableRateLimit': True}),
'kraken': ccxt.kraken({'enableRateLimit': True}),
'krakenfutures': ccxt.krakenfutures({'enableRateLimit': True}),
'lbank': ccxt.lbank({'enableRateLimit': True}),
'phemex': ccxt.phemex({'enableRateLimit': True}),
'poloniex': ccxt.poloniex({'enableRateLimit': True}),
'upbit': ccxt.upbit({'enableRateLimit': True}),
'whitebit': ccxt.whitebit({'enableRateLimit': True})
}
def fetch_candles(self, exchange_name: str, pair: str, timeframe: str,
since: int, limit: int = 1000) -> List[List[Any]]:
try:
exchange = self.exchanges[exchange_name]
if timeframe not in exchange.timeframes:
print(f"Exchange {exchange_name} does not support timeframe {timeframe}")
return []
ohlcv = exchange.fetch_ohlcv(
pair,
timeframe,
since=since,
limit=limit
)
return ohlcv
except Exception as e:
print(f"Error fetching from {exchange_name}: {str(e)}")
return []
class ValidationRunner:
def __init__(self, directory: str, timeframe: Optional[str] = None,
pair: Optional[str] = None) -> None:
self.directory = directory
self.timeframe = timeframe
self.pair = pair
self.csv_files = self._get_csv_files()
self.validation_results: Dict[str, Dict] = {}
self.gap_filler = MultiExchangeGapFiller()
def _get_csv_files(self) -> List[str]:
all_files = os.listdir(self.directory)
csv_files = [
f for f in all_files
if f.endswith('.csv') and
(self.timeframe is None or f'_{self.timeframe}_' in f) and
(self.pair is None or f.startswith(self.pair))
]
return csv_files
def _extract_pair_name(self, filename: str) -> str:
# More robust pair name extraction
try:
# Split filename and remove extension
name_parts = os.path.splitext(filename)[0].split('_')
# Find the timeframe part index
timeframe_idx = -1
for i, part in enumerate(name_parts):
if part in TimeframeManager.TIMEFRAME_TO_SECONDS:
timeframe_idx = i
break
if timeframe_idx > 0:
# Pair is everything before the timeframe
base_quote = '_'.join(name_parts[:timeframe_idx])
return base_quote
else:
# Fallback to original method
return f"{name_parts[0]}_{name_parts[1]}"
except Exception as e:
print(f"Error extracting pair name from {filename}: {str(e)}")
# Fallback to original method
parts = filename.split('_')
return f"{parts[0]}_{parts[1]}" if len(parts) > 1 else parts[0]
def run_validation(self) -> None:
for file in self.csv_files:
file_path = os.path.join(self.directory, file)
# First check if the file is corrupted
integrity_check = self._check_file_integrity(file_path)
if not integrity_check['is_valid']:
print(f"\n⚠️ File integrity issues found in {file}:")
for issue in integrity_check['issues']:
print(f" - {issue}")
continue
timeframe = self._extract_timeframe(file)
if timeframe:
print(f"\nValidating {file}...")
validator = TimeframeManager(file_path, timeframe)
result = validator.validate()
pair_name = self._extract_pair_name(file)
self.validation_results[file] = {
'pair': pair_name,
'timeframe': timeframe,
'gaps_count': result['gaps_count'],
'gaps': result['gaps'],
'invalid_intervals': result['invalid_intervals_count'],
'integrity': integrity_check
}
if not result['is_valid']:
print(f"\nValidation issues found in {file}:")
if result['gaps']:
print(f"Gaps found: {result['gaps_count']}")
if result['invalid_intervals']:
print(f"Invalid intervals found: {result['invalid_intervals_count']}")
else:
print(f"Could not extract timeframe from filename: {file}")
if not self.pair:
self._print_summary()
def _check_file_integrity(self, file_path: str) -> Dict[str, Any]:
"""
Check if file is corrupted or has incomplete data due to interruptions (CTRL+C)
"""
issues = []
is_valid = True
try:
# Check 1: Can the file be opened and read?
if not os.path.exists(file_path):
issues.append("File does not exist")
return {'is_valid': False, 'issues': issues}
if os.path.getsize(file_path) == 0:
issues.append("File is empty")
return {'is_valid': False, 'issues': issues}
# Check 2: Parse as CSV without errors
try:
df = pd.read_csv(file_path)
except pd.errors.ParserError:
issues.append("File is not a valid CSV format - possible corruption")
return {'is_valid': False, 'issues': issues}
except pd.errors.EmptyDataError:
issues.append("File contains no data")
return {'is_valid': False, 'issues': issues}
# Check 3: Verify expected columns
expected_columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume']
missing_columns = [col for col in expected_columns if col not in df.columns]
if missing_columns:
issues.append(f"Missing required columns: {', '.join(missing_columns)}")
is_valid = False
# Check 4: Check for NaN values which might indicate truncated records
nan_counts = df.isna().sum()
nan_columns = [col for col in df.columns if nan_counts[col] > 0]
if nan_columns:
issues.append(f"Found NaN values in columns: {', '.join(nan_columns)} - possible incomplete write")
is_valid = False
# Check 5: Verify data consistency
if all(col in df.columns for col in ['open', 'high', 'low', 'close']):
# Check high >= low (should always be true)
invalid_hl = (df['high'] < df['low']).sum()
if invalid_hl > 0:
issues.append(f"Found {invalid_hl} records where 'high' is less than 'low' - data corruption")
is_valid = False
# Check close is within high-low range
invalid_close = ((df['close'] > df['high']) | (df['close'] < df['low'])).sum()
if invalid_close > 0:
issues.append(f"Found {invalid_close} records where 'close' is outside high-low range - data corruption")
is_valid = False
# Check 6: Verify timestamps are sorted (indicates potential missing end due to CTRL+C)
if 'timestamp' in df.columns and len(df) > 1:
if not all(df['timestamp'].diff().iloc[1:] > 0):
issues.append("Timestamps are not in ascending order - possible file corruption")
is_valid = False
# Check 7: Check for suspiciously truncated file (last record might be partially written)
# If file doesn't end with newline or has an unexpected EOF marker
with open(file_path, 'rb') as f:
f.seek(0, os.SEEK_END)
last_position = f.tell()
f.seek(max(0, last_position - 100), os.SEEK_SET) # Get last 100 bytes
last_bytes = f.read()
if not last_bytes.endswith(b'\n'):
issues.append("File doesn't end with a newline - possible incomplete write")
is_valid = False
return {'is_valid': is_valid, 'issues': issues}
except Exception as e:
issues.append(f"Error checking file integrity: {str(e)}")
return {'is_valid': False, 'issues': issues}
def _print_summary(self) -> None:
if not self.validation_results:
print("\nNo files were validated.")
return
pairs_with_issues = defaultdict(lambda: {'gaps': 0, 'files': []})
for file, result in self.validation_results.items():
if result['gaps'] > 0 or result['invalid_intervals'] > 0:
pair = result['pair']
pairs_with_issues[pair]['gaps'] += result['gaps']
pairs_with_issues[pair]['files'].append(file)
if pairs_with_issues:
print("\nSummary of pairs with issues:")
for pair, data in sorted(pairs_with_issues.items()):
print(f"\n{pair}:")
print(f" Total gaps: {data['gaps']}")
print(" Affected files:")
for file in data['files']:
print(f" - {file}")
else:
print("\nNo issues found in any pairs.")
def fill_gaps(self) -> None:
if not self.validation_results:
print("No validation results available. Run validation first.")
return
for file, result in self.validation_results.items():
if result['gaps_count'] > 0:
print(f"\nAttempting to fill gaps in {file}")
pair = result['pair'].replace('_', '/')
timeframe = result['timeframe']
gaps_list = result['gaps']
if not gaps_list:
continue
self._fill_file_gaps(file, pair, timeframe, gaps_list)
def _fill_file_gaps(self, file: str, pair: str, timeframe: str,
gaps_list: List[Dict[str, int]]) -> None:
exchanges = list(self.gap_filler.exchanges.keys())
file_path = os.path.join(self.directory, file)
# Try both standard pair format and alternative formats
pair_variations = self._get_pair_variations(pair)
for gap in gaps_list:
gap_filled = False
interval_ms = TimeframeManager.get_timeframe_in_seconds(timeframe) * 1000
num_candles = (gap['end'] - gap['start']) // interval_ms
gap_start_year = datetime.fromtimestamp(gap['start'] / 1000).year
coverage_threshold = 50 if gap_start_year < 2020 else 80
print(f"\nAttempting to fill gap from {datetime.fromtimestamp(gap['start'] / 1000)} "
f"to {datetime.fromtimestamp(gap['end'] / 1000)} "
f"(need {num_candles} candles)")
for exchange_name in exchanges:
try:
exchange = self.gap_filler.exchanges[exchange_name]
if timeframe not in exchange.timeframes:
continue
# Try each pair variation until one works
exchange_candles = []
used_pair = None
for pair_variant in pair_variations:
try:
print(f"Trying to fill gap using {exchange_name} with pair {pair_variant}")
# Check if pair exists on exchange
markets = exchange.load_markets()
if pair_variant not in markets:
continue
# Try to fetch a single candle to verify pair works
test_candle = self.gap_filler.fetch_candles(
exchange_name,
pair_variant,
timeframe,
gap['start'],
1
)
if test_candle:
used_pair = pair_variant
break
except Exception as e:
# Just continue without logging
continue
if not used_pair:
print(f"No valid pair format found for {pair} on {exchange_name}")
continue
current_start = gap['start']
remaining_attempts = 3 # Limit retries for a specific exchange
while current_start < gap['end'] and remaining_attempts > 0:
try:
remaining_ms = gap['end'] - current_start
remaining_candles = remaining_ms // interval_ms
chunk_size = min(remaining_candles, 500)
ohlcv = self.gap_filler.fetch_candles(
exchange_name,
used_pair,
timeframe,
current_start,
int(chunk_size)
)
if not ohlcv:
print(f"No data returned from {exchange_name}")
remaining_attempts -= 1
time.sleep(2)
continue
valid_candles = [
candle for candle in ohlcv
if gap['start'] <= candle[0] <= gap['end']
]
if valid_candles:
exchange_candles.extend(valid_candles)
current_start = valid_candles[-1][0] + interval_ms
else:
current_start += interval_ms * chunk_size
# Respect rate limits
time.sleep(exchange.rateLimit / 1000 * 1.5)
except ccxt.RateLimitExceeded:
print(f"Rate limit exceeded for {exchange_name}, waiting 60 seconds")
time.sleep(60)
remaining_attempts -= 1
continue
except Exception as e:
print(f"Error fetching chunk from {exchange_name}: {str(e)}")
remaining_attempts -= 1
time.sleep(5)
continue
if exchange_candles:
success = self._process_gap_candles(file_path, exchange_name, exchange_candles, gap, interval_ms, coverage_threshold)
if success:
gap_filled = True
break
except Exception as e:
print(f"Error with exchange {exchange_name}: {str(e)}")
continue
if not gap_filled:
print("Failed to fill gap with any exchange")
def _process_gap_candles(self, file_path: str, exchange_name: str, exchange_candles: List,
gap: Dict[str, int], interval_ms: int, coverage_threshold: float) -> bool:
"""Process downloaded candles and update the file if coverage is sufficient"""
try:
df = pd.DataFrame(
exchange_candles,
columns=['timestamp', 'open', 'high', 'low', 'close', 'volume']
)
df = df.drop_duplicates(subset=['timestamp']).sort_values('timestamp')
expected_timestamps = set(range(
gap['start'],
gap['end'] + interval_ms,
interval_ms
))
actual_timestamps = set(df['timestamp'].values)
missing_timestamps = expected_timestamps - actual_timestamps
coverage = (len(expected_timestamps) - len(missing_timestamps)) / len(expected_timestamps) * 100
print(f"Gap coverage from {exchange_name}: {coverage:.2f}% ({len(missing_timestamps)} missing candles)")
if coverage >= coverage_threshold:
self._update_csv_with_gap_data(file_path, df.values.tolist())
print(f"Successfully filled gap with {coverage:.2f}% coverage using {exchange_name}")
return True
return False
except Exception as e:
print(f"Error processing gap candles: {str(e)}")
return False
def _get_pair_variations(self, pair: str) -> List[str]:
"""Generate possible variations of the pair format for different exchanges"""
# Original format like "BTC_USDT"
if '_' not in pair:
return [pair]
base, quote = pair.split('_', 1)
variations = [
f"{base}/{quote}", # BTC/USDT - standard CCXT format
pair.replace('_', '/'), # Also BTC/USDT
pair, # Original format BTC_USDT
f"{base}{quote}", # BTCUSDT - some exchanges use this format
f"{base}-{quote}" # BTC-USDT - some exchanges use this format
]
return variations
@staticmethod
def _extract_timeframe(filename: str) -> Optional[str]:
try:
parts = filename.split('_')
for part in parts:
part_lower = part.lower()
if part_lower.endswith(('m', 'h', 'd', 'w')):
if part_lower in TimeframeManager.TIMEFRAME_TO_SECONDS:
return part_lower
return None
except Exception:
return None
def _scan_for_corrupted_files(self) -> None:
"""
Scan all CSV files for corruption without performing timeframe validation
"""
corrupted_files = []
print(f"\nScanning {len(self.csv_files)} files for corruption...")
for file in self.csv_files:
file_path = os.path.join(self.directory, file)
integrity_check = self._check_file_integrity(file_path)
if not integrity_check['is_valid']:
corrupted_files.append((file, integrity_check['issues']))
print(f"\n⚠️ File integrity issues found in {file}:")
for issue in integrity_check['issues']:
print(f" - {issue}")
if not corrupted_files:
print("\n✅ No file corruption detected. All files appear valid.")
else:
print(f"\n⚠️ Found {len(corrupted_files)} corrupted files out of {len(self.csv_files)} scanned files.")
print("\nSuggested repair steps:")
print("1. For empty or severely corrupted files: redownload the data")
print("2. For files with NaN values or out-of-order timestamps: run validation with --fill-gaps")
print("3. For files with minor corruption: try opening in pandas and rewriting with df.to_csv()")
def _update_csv_with_gap_data(self, file_path: str, new_data: List[List[Any]]) -> None:
"""
Updates the CSV file with new data retrieved to fill gaps
"""
try:
df = pd.read_csv(file_path)
new_df = pd.DataFrame(
new_data,
columns=['timestamp', 'open', 'high', 'low', 'close', 'volume']
)
combined_df = pd.concat([df, new_df])
combined_df = combined_df.drop_duplicates(subset=['timestamp'])
combined_df = combined_df.sort_values('timestamp')
# Back up the original file before overwriting
backup_path = f"{file_path}.bak"
print(f"Creating backup of original file at {backup_path}")
df.to_csv(backup_path, index=False)
combined_df.to_csv(file_path, index=False)
print(f"Updated {file_path} with {len(new_df)} candles to fill gaps")
except Exception as e:
print(f"Error updating file with gap data: {str(e)}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Validate OHLCV data files timestamp spacing')
parser.add_argument(
'--directory', '-d',
type=str,
default='./csv_ohlcv',
help='Directory containing CSV files'
)
parser.add_argument(
'--timeframe', '-t',
type=str,
choices=['1m', '3m', '5m', '15m', '30m', '1h', '2h', '3h', '4h', '6h', '12h', '1d', '1w'],
help='Specific timeframe to validate (optional)'
)
parser.add_argument(
'--pair', '-p',
type=str,
help='Trading pair to validate (e.g., BTC_USDT) (optional)'
)
parser.add_argument(
'--fill-gaps', '-f',
action='store_true',
help='Attempt to fill gaps in the data by downloading missing candles'
)
# Add integrity-only flag
parser.add_argument(
'--integrity-only', '-i',
action='store_true',
help='Only check file integrity without validating timeframes'
)
args = parser.parse_args()
print(f"Starting validation of files in {args.directory}")
if args.timeframe:
print(f"Filtering for timeframe: {args.timeframe}")
if args.pair:
print(f"Filtering for pair: {args.pair}")
if args.integrity_only:
print("Performing integrity checks only")
runner = ValidationRunner(args.directory, args.timeframe, args.pair)
if args.integrity_only:
# In this case we're only interested in file corruption
runner._scan_for_corrupted_files()
else:
runner.run_validation()
if args.fill_gaps:
print("\nAttempting to fill gaps in the data...")
runner.fill_gaps()