-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_validate_market_data.py
More file actions
259 lines (197 loc) · 8.28 KB
/
tool_validate_market_data.py
File metadata and controls
259 lines (197 loc) · 8.28 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
import os
import polars as pl
from datetime import datetime, timedelta
REQUIRED_COLUMNS = [
'timestamp', 'open', 'high', 'low', 'close', 'volume',
'quote_asset_volume', 'number_of_trades',
'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume'
]
# Map intervals to their timedelta for continuity checks
INTERVAL_DELTAS = {
'15m': timedelta(minutes=15),
'30m': timedelta(minutes=30),
'1h': timedelta(hours=1),
'2h': timedelta(hours=2),
'4h': timedelta(hours=4),
'6h': timedelta(hours=6),
'8h': timedelta(hours=8),
'12h': timedelta(hours=12),
}
def validate_file(filepath, interval):
"""
Universal validation function for all intervals.
Checks for missing columns, nulls, duplicates, and continuity.
"""
try:
df = pl.read_csv(filepath)
filename = os.path.basename(filepath)
# Check if all required columns exist
missing_cols = [col for col in REQUIRED_COLUMNS if col not in df.columns]
if missing_cols:
return f"{filename}: ❌ Missing columns: {missing_cols}"
# Parse timestamps with consistent precision
df = df.with_columns([
pl.col("timestamp")
.str.strptime(pl.Datetime, "%Y-%m-%d %H:%M:%S", strict=False)
.cast(pl.Datetime("us")) # Force microsecond precision
.alias("timestamp")
])
# Check for malformed timestamps
if df["timestamp"].null_count() > 0:
return f"{filename}: ❌ Malformed timestamps"
# Sort by timestamp
df = df.sort("timestamp")
# Check for nulls in any data columns
null_counts = df.select(pl.exclude("timestamp").null_count())
total_nulls = null_counts.to_numpy().sum()
if total_nulls > 0:
return f"{filename}: ❌ Contains {total_nulls} nulls in data columns"
# Check for duplicate timestamps
if df["timestamp"].is_duplicated().any():
duplicate_count = df["timestamp"].is_duplicated().sum()
return f"{filename}: ❌ {duplicate_count} duplicate timestamps found"
# Check data validity (prices should be positive, volumes non-negative)
price_cols = ['open', 'high', 'low', 'close']
volume_cols = ['volume', 'quote_asset_volume', 'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume']
# Check for non-positive prices
for col in price_cols:
if (df[col] <= 0).any():
return f"{filename}: ❌ Non-positive values found in {col}"
# Check for negative volumes
for col in volume_cols:
if (df[col] < 0).any():
return f"{filename}: ❌ Negative values found in {col}"
# Check OHLC relationships (high >= low, open/close between high/low)
if (df["high"] < df["low"]).any():
return f"{filename}: ❌ High < Low detected"
if (df["open"] > df["high"]).any() or (df["open"] < df["low"]).any():
return f"{filename}: ❌ Open price outside High/Low range"
if (df["close"] > df["high"]).any() or (df["close"] < df["low"]).any():
return f"{filename}: ❌ Close price outside High/Low range"
# Check continuity - ensure no missing timestamps
if interval in INTERVAL_DELTAS:
delta = INTERVAL_DELTAS[interval]
ts_list = df["timestamp"].to_list()
if len(ts_list) > 1:
# Generate expected timeline
start_time = ts_list[0]
end_time = ts_list[-1]
expected = []
current = start_time
while current <= end_time:
expected.append(current)
current += delta
actual_set = set(ts_list)
expected_set = set(expected)
missing = sorted(expected_set - actual_set)
if missing:
sample = [d.strftime("%Y-%m-%d %H:%M") for d in missing[:3]]
return f"{filename}: ❌ Missing {len(missing)} timestamps (examples: {sample})"
# Check for reasonable data ranges
row_count = len(df)
if row_count == 0:
return f"{filename}: ❌ Empty file"
elif row_count < 10:
return f"{filename}: ⚠️ Very small dataset ({row_count} rows)"
# All checks passed
date_range = f"{df['timestamp'].min().strftime('%Y-%m-%d')} to {df['timestamp'].max().strftime('%Y-%m-%d')}"
return f"{filename}: ✅ Passed ({row_count} rows, {date_range})"
except Exception as e:
return f"{os.path.basename(filepath)}: ❌ Exception — {str(e)}"
# Individual checker functions for each interval
def check_15m_file(filepath):
return validate_file(filepath, '15m')
def check_30m_file(filepath):
return validate_file(filepath, '30m')
def check_1h_file(filepath):
return validate_file(filepath, '1h')
def check_2h_file(filepath):
return validate_file(filepath, '2h')
def check_4h_file(filepath):
return validate_file(filepath, '4h')
def check_6h_file(filepath):
return validate_file(filepath, '6h')
def check_8h_file(filepath):
return validate_file(filepath, '8h')
def check_12h_file(filepath):
return validate_file(filepath, '12h')
CHECKERS = {
'15m': check_15m_file,
'30m': check_30m_file,
'1h': check_1h_file,
'2h': check_2h_file,
'4h': check_4h_file,
'6h': check_6h_file,
'8h': check_8h_file,
'12h': check_12h_file,
}
def scan_directory(base_dir):
"""
Scan all interval directories and validate CSV files.
"""
results = []
summary = {
'total_files': 0,
'passed': 0,
'failed': 0,
'warnings': 0,
'by_interval': {}
}
for interval, checker in CHECKERS.items():
folder = os.path.join(base_dir, interval)
if not os.path.exists(folder):
continue
interval_stats = {'total': 0, 'passed': 0, 'failed': 0, 'warnings': 0}
print(f"\n--- Validating {interval} files ---")
csv_files = [f for f in os.listdir(folder) if f.endswith(".csv")]
csv_files.sort()
for filename in csv_files:
filepath = os.path.join(folder, filename)
result = checker(filepath)
if result:
results.append(result)
print(result)
# Update statistics
summary['total_files'] += 1
interval_stats['total'] += 1
if "✅ Passed" in result:
summary['passed'] += 1
interval_stats['passed'] += 1
elif "⚠️" in result:
summary['warnings'] += 1
interval_stats['warnings'] += 1
else:
summary['failed'] += 1
interval_stats['failed'] += 1
summary['by_interval'][interval] = interval_stats
print(
f" {interval} summary: {interval_stats['passed']} passed, {interval_stats['failed']} failed, {interval_stats['warnings']} warnings")
return results, summary
def print_summary(summary):
"""
Print a detailed summary of validation results.
"""
print("\n" + "=" * 60)
print("VALIDATION SUMMARY")
print("=" * 60)
print(f"Total files validated: {summary['total_files']}")
print(f"✅ Passed: {summary['passed']}")
print(f"❌ Failed: {summary['failed']}")
print(f"⚠️ Warnings: {summary['warnings']}")
if summary['failed'] > 0:
print(f"\n🚨 {summary['failed']} files have issues that need attention!")
elif summary['warnings'] > 0:
print(f"\n⚠️ All files passed validation, but {summary['warnings']} have warnings.")
else:
print(f"\n🎉 All {summary['passed']} files passed validation!")
print("\nBy interval:")
for interval, stats in summary['by_interval'].items():
if stats['total'] > 0:
success_rate = (stats['passed'] / stats['total']) * 100
print(f" {interval}: {stats['passed']}/{stats['total']} passed ({success_rate:.1f}%)")
if __name__ == "__main__":
base_directory = "datasets/raw/full_scope"
print("🔍 Starting comprehensive data validation...")
print(f"📁 Scanning directory: {base_directory}")
results, summary = scan_directory(base_directory)
print_summary(summary)