-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_enhanced_parsing.py
More file actions
457 lines (381 loc) · 19.2 KB
/
Copy pathtest_enhanced_parsing.py
File metadata and controls
457 lines (381 loc) · 19.2 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
#!/usr/bin/env python3
"""
Comprehensive test script for enhanced candle data parsing and WebSocket functionality.
This script demonstrates the handling of the specific data formats you provided and validates
the enhanced parsing logic in both async and sync versions.
"""
import asyncio
import sys
import os
import json
from datetime import datetime, timezone
# Add the project root to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from BinaryOptionsToolsAsync.platforms.pocketoption.stable_api import PocketOption as AsyncPocketOption
from BinaryOptionsTools.platforms.pocketoption.stable_api import PocketOption as SyncPocketOption
def format_timestamp(timestamp):
"""Convert timestamp to readable format."""
if timestamp and timestamp > 0:
dt = datetime.fromtimestamp(timestamp / 1000, tz=timezone.utc)
return dt.strftime('%Y-%m-%d %H:%M:%S UTC')
return "Invalid timestamp"
def validate_candle_data(candles, expected_count=None):
"""Validate candle data structure and content."""
if not isinstance(candles, list):
return False, "Candles data is not a list"
if expected_count is not None and len(candles) != expected_count:
return False, f"Expected {expected_count} candles, got {len(candles)}"
required_fields = ['time', 'open', 'close', 'high', 'low', 'volume']
for i, candle in enumerate(candles):
if not isinstance(candle, dict):
return False, f"Candle {i} is not a dictionary"
for field in required_fields:
if field not in candle:
return False, f"Candle {i} missing field: {field}"
# Validate OHLC logic
if candle['high'] < max(candle['open'], candle['close'], candle['low']):
return False, f"Candle {i} has invalid high value"
if candle['low'] > min(candle['open'], candle['close'], candle['high']):
return False, f"Candle {i} has invalid low value"
return True, "All candles valid"
async def test_async_enhanced_parsing():
"""Test enhanced parsing with async PocketOption."""
print("=" * 60)
print("Testing Async Enhanced Data Parsing")
print("=" * 60)
try:
# Create API instance (no need to connect for parsing tests)
api = AsyncPocketOption("test_ssid", demo=True)
# Initialize required attributes
if not hasattr(api, 'history_data'):
api.history_data = None
if not hasattr(api, 'real_time_candles'):
api.real_time_candles = {}
if not hasattr(api, 'websocket_client'):
# Create a mock websocket client for testing
class MockWebSocketClient:
def __init__(self, api_ref):
self.api = api_ref
self.history_data_ready = False
import logging
self.logger = logging.getLogger(__name__)
async def on_message(self, message):
# Simplified version of the WebSocket client logic for testing
if isinstance(message, bytes):
message = message.decode('utf-8')
message = json.loads(message)
if self.history_data_ready and isinstance(message, dict):
self.history_data_ready = False
if "data" in message:
data = message["data"]
if isinstance(data, dict) and "candles" in data:
candles = data["candles"]
formatted_candles = []
for candle in candles:
if len(candle) >= 5:
formatted_candle = {
"time": int(candle[0]) if candle[0] else 0,
"open": float(candle[1]) if candle[1] else 0.0,
"close": float(candle[2]) if candle[2] else 0.0,
"high": float(candle[3]) if candle[3] else 0.0,
"low": float(candle[4]) if candle[4] else 0.0,
"volume": 0
}
formatted_candles.append(formatted_candle)
self.api.history_data = formatted_candles
elif isinstance(message, list) and len(message) > 0:
for item in message:
if isinstance(item, list) and len(item) >= 3:
try:
asset = str(item[0])
timestamp = int(item[1]) if item[1] else 0
price = float(item[2]) if item[2] else 0.0
if asset not in self.api.real_time_candles:
self.api.real_time_candles[asset] = {}
period = 1
if period not in self.api.real_time_candles[asset]:
self.api.real_time_candles[asset][period] = {}
candle_data = {
"time": timestamp,
"open": price,
"close": price,
"high": price,
"low": price,
"volume": 0
}
self.api.real_time_candles[asset][period][timestamp] = candle_data
except (ValueError, IndexError, TypeError):
pass
api.websocket_client = MockWebSocketClient(api)
# Test 1: Historical data format
print("\n1. Testing Historical Data Format:")
historical_data = {
"data": {
"asset": "AEDCNY_otc",
"period": 5,
"history": [
[1735145825000, 0.74115],
[1735145830000, 0.74116],
[1735145835000, 0.74114]
],
"candles": [
[1735145825000, 0.74115, 0.74116, 0.74117, 0.74114],
[1735145830000, 0.74116, 0.74114, 0.74118, 0.74113],
[1735145835000, 0.74114, 0.74115, 0.74116, 0.74112]
]
}
}
# Simulate WebSocket message processing
api.websocket_client.history_data_ready = True
await api.websocket_client.on_message(json.dumps(historical_data).encode('utf-8'))
candles = api.history_data
valid, message = validate_candle_data(candles, 3)
print(f" Received {len(candles) if candles else 0} candles")
print(f" Validation: {message}")
if valid and candles:
for i, candle in enumerate(candles):
print(f" Candle {i+1}: {format_timestamp(candle['time'])} | O:{candle['open']} C:{candle['close']} H:{candle['high']} L:{candle['low']}")
# Test 2: Real-time data format
print("\n2. Testing Real-time Data Format:")
realtime_data = [
["EURUSD_otc", 1735145825000, 1.04223],
["GBPUSD_otc", 1735145825100, 1.25647],
["AUDUSD_otc", 1735145825200, 0.63891]
]
# Simulate WebSocket message processing
await api.websocket_client.on_message(json.dumps(realtime_data).encode('utf-8'))
if hasattr(api, 'real_time_candles') and api.real_time_candles:
print(f" Stored real-time data for {len(api.real_time_candles)} assets")
for asset, periods in api.real_time_candles.items():
for period, candles in periods.items():
print(f" {asset} ({period}s): {len(candles)} candles")
for timestamp, candle in list(candles.items())[:2]: # Show first 2
print(f" {format_timestamp(timestamp)}: ${candle['close']}")
return True
except Exception as e:
print(f" Error: {e}")
return False
def test_sync_enhanced_parsing():
"""Test enhanced parsing with sync PocketOption."""
print("=" * 60)
print("Testing Sync Enhanced Data Parsing")
print("=" * 60)
try:
# Create API instance (no need to connect for parsing tests)
api = SyncPocketOption("test_ssid", demo=True)
# Initialize required attributes
if not hasattr(api, 'history_data'):
api.history_data = None
if not hasattr(api, 'real_time_candles'):
api.real_time_candles = {}
if not hasattr(api, 'websocket_client'):
# Create a mock websocket client for testing
class MockWebSocketClient:
def __init__(self, api_ref):
self.api = api_ref
self.history_data_ready = False
import logging
self.logger = logging.getLogger(__name__)
async def on_message(self, message):
# Simplified version of the WebSocket client logic for testing
if isinstance(message, bytes):
message = message.decode('utf-8')
message = json.loads(message)
if self.history_data_ready and isinstance(message, dict):
self.history_data_ready = False
if "data" in message:
data = message["data"]
if isinstance(data, dict) and "candles" in data:
candles = data["candles"]
formatted_candles = []
for candle in candles:
if len(candle) >= 5:
formatted_candle = {
"time": int(candle[0]) if candle[0] else 0,
"open": float(candle[1]) if candle[1] else 0.0,
"close": float(candle[2]) if candle[2] else 0.0,
"high": float(candle[3]) if candle[3] else 0.0,
"low": float(candle[4]) if candle[4] else 0.0,
"volume": 0
}
formatted_candles.append(formatted_candle)
self.api.history_data = formatted_candles
elif isinstance(message, list) and len(message) > 0:
for item in message:
if isinstance(item, list) and len(item) >= 3:
try:
asset = str(item[0])
timestamp = int(item[1]) if item[1] else 0
price = float(item[2]) if item[2] else 0.0
if asset not in self.api.real_time_candles:
self.api.real_time_candles[asset] = {}
period = 1
if period not in self.api.real_time_candles[asset]:
self.api.real_time_candles[asset][period] = {}
candle_data = {
"time": timestamp,
"open": price,
"close": price,
"high": price,
"low": price,
"volume": 0
}
self.api.real_time_candles[asset][period][timestamp] = candle_data
except (ValueError, IndexError, TypeError):
pass
api.websocket_client = MockWebSocketClient(api)
# Test 1: Historical data format
print("\n1. Testing Historical Data Format:")
historical_data = {
"data": {
"asset": "AEDCNY_otc",
"period": 5,
"history": [
[1735145825000, 0.74115],
[1735145830000, 0.74116],
[1735145835000, 0.74114]
],
"candles": [
[1735145825000, 0.74115, 0.74116, 0.74117, 0.74114],
[1735145830000, 0.74116, 0.74114, 0.74118, 0.74113],
[1735145835000, 0.74114, 0.74115, 0.74116, 0.74112]
]
}
}
# Simulate WebSocket message processing
import asyncio
api.websocket_client.history_data_ready = True
asyncio.run(api.websocket_client.on_message(json.dumps(historical_data).encode('utf-8')))
candles = api.history_data
valid, message = validate_candle_data(candles, 3)
print(f" Received {len(candles) if candles else 0} candles")
print(f" Validation: {message}")
if valid and candles:
for i, candle in enumerate(candles):
print(f" Candle {i+1}: {format_timestamp(candle['time'])} | O:{candle['open']} C:{candle['close']} H:{candle['high']} L:{candle['low']}")
# Test 2: Real-time data format
print("\n2. Testing Real-time Data Format:")
realtime_data = [
["EURUSD_otc", 1735145825000, 1.04223],
["GBPUSD_otc", 1735145825100, 1.25647],
["AUDUSD_otc", 1735145825200, 0.63891]
]
# Simulate WebSocket message processing
asyncio.run(api.websocket_client.on_message(json.dumps(realtime_data).encode('utf-8')))
if hasattr(api, 'real_time_candles') and api.real_time_candles:
print(f" Stored real-time data for {len(api.real_time_candles)} assets")
for asset, periods in api.real_time_candles.items():
for period, candles in periods.items():
print(f" {asset} ({period}s): {len(candles)} candles")
for timestamp, candle in list(candles.items())[:2]: # Show first 2
print(f" {format_timestamp(timestamp)}: ${candle['close']}")
return True
except Exception as e:
print(f" Error: {e}")
return False
def test_data_format_compatibility():
"""Test compatibility with various data formats."""
print("=" * 60)
print("Testing Data Format Compatibility")
print("=" * 60)
test_cases = [
{
"name": "Standard OHLC format",
"data": {"data": {"candles": [[1735145825000, 1.0422, 1.0423, 1.0424, 1.0421]]}},
"expected": 1
},
{
"name": "Multiple assets real-time",
"data": [["EUR", 1735145825000, 1.04], ["GBP", 1735145825100, 1.25]],
"expected": 2
},
{
"name": "Empty candles",
"data": {"data": {"candles": []}},
"expected": 0
},
{
"name": "Malformed candle data",
"data": {"data": {"candles": [[1735145825000, 1.0422]]}}, # Missing OHLC
"expected": 0
}
]
all_passed = True
for test_case in test_cases:
print(f"\nTesting: {test_case['name']}")
try:
# Count expected results based on data format
data = test_case['data']
count = 0
if isinstance(data, dict) and "data" in data and "candles" in data["data"]:
# Historical format
for candle in data["data"]["candles"]:
if len(candle) >= 5:
count += 1
elif isinstance(data, list):
# Real-time format
for item in data:
if isinstance(item, list) and len(item) >= 3:
count += 1
passed = count == test_case['expected']
print(f" Expected: {test_case['expected']}, Got: {count}, Passed: {'✅' if passed else '❌'}")
if not passed:
all_passed = False
except Exception as e:
print(f" Error: {e}")
all_passed = False
return all_passed
async def main():
"""Run all enhanced parsing tests."""
print("Enhanced WebSocket Data Parsing Test Suite")
print("Testing compatibility with new data formats")
print("=" * 80)
tests = [
("Data Format Compatibility", test_data_format_compatibility),
("Sync Enhanced Parsing", test_sync_enhanced_parsing),
]
async_tests = [
("Async Enhanced Parsing", test_async_enhanced_parsing),
]
results = []
# Run sync tests
for test_name, test_func in tests:
try:
result = test_func()
results.append((test_name, result, None))
except Exception as e:
results.append((test_name, False, str(e)))
# Run async tests
for test_name, test_func in async_tests:
try:
result = await test_func()
results.append((test_name, result, None))
except Exception as e:
results.append((test_name, False, str(e)))
# Summary
print("\n" + "=" * 80)
print("TEST RESULTS SUMMARY")
print("=" * 80)
passed = 0
total = len(results)
for test_name, result, error in results:
status = "PASS" if result else "FAIL"
print(f"{test_name:<40} {status}")
if error:
print(f" Error: {error}")
if result:
passed += 1
print(f"\nPassed: {passed}/{total}")
if passed == total:
print("✅ All enhanced parsing tests passed!")
print("\nThe WebSocket client can now properly handle:")
print(" • Historical candle data format: {\"data\": {\"candles\": [[timestamp, open, close, high, low], ...]}}")
print(" • Real-time streaming format: [[\"ASSET\", timestamp, price], ...]")
print(" • Enhanced error handling and data validation")
print(" • Robust type conversion and edge case handling")
return 0
else:
print("❌ Some tests failed. Please check the implementation.")
return 1
if __name__ == "__main__":
exit(asyncio.run(main()))