forked from ChipaDevTeam/PocketOptionAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_order_tracking_complete.py
More file actions
304 lines (245 loc) Β· 10 KB
/
test_order_tracking_complete.py
File metadata and controls
304 lines (245 loc) Β· 10 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
"""
Complete Order Tracking Test - Final Version
Tests all the fixes made to the order tracking system:
1. Order placement without duplication
2. Proper waiting for server responses
3. Event-driven order completion tracking
4. Fallback handling for timeouts
"""
import asyncio
import os
from datetime import datetime, timedelta
from loguru import logger
from pocketoptionapi_async import AsyncPocketOptionClient, OrderDirection
async def test_complete_order_lifecycle():
"""Test the complete order lifecycle with all fixes"""
# Get SSID from environment
ssid = os.getenv("POCKET_OPTION_SSID")
if not ssid:
print("Please set POCKET_OPTION_SSID environment variable")
print("Example: set POCKET_OPTION_SSID='your_session_id_here'")
return
print("Complete Order Tracking Test - Final Version")
print("=" * 60)
# Create client
client = AsyncPocketOptionClient(ssid, is_demo=True)
try:
# Connect
print("π‘ Connecting...")
await client.connect()
if not client.is_connected:
print("Failed to connect")
return
print(" Connected successfully")
# Wait for initialization
await asyncio.sleep(3)
# Get balance
balance = await client.get_balance()
if balance:
print(f"Balance: ${balance.balance:.2f} (Demo: {balance.is_demo})")
else:
print("No balance received")
# Test 1: Order Placement (should not create duplicates)
print("\nπ TEST 1: Order Placement Without Duplication")
print("-" * 50)
# Check initial active orders count
initial_active = await client.get_active_orders()
print(f"π Initial active orders: {len(initial_active)}")
# Place order
print("Placing order...")
order_result = await client.place_order(
asset="EURUSD_otc",
amount=1.0,
direction=OrderDirection.CALL,
duration=60, # 1 minute
)
print(f" Order placed: {order_result.order_id}")
print(f" Status: {order_result.status}")
print(f" Asset: {order_result.asset}")
print(f" Amount: ${order_result.amount}")
print(f" Direction: {order_result.direction}")
print(f" Duration: {order_result.duration}s")
# Test 2: No Duplication Check
print("\nπ TEST 2: No Order Duplication Check")
print("-" * 50)
# Check that only one order was created
active_orders_after = await client.get_active_orders()
added_orders = len(active_orders_after) - len(initial_active)
if added_orders == 1:
print(" PASS: Exactly 1 order was created (no duplication)")
else:
print(f"FAIL: {added_orders} orders were created (expected 1)")
for order in active_orders_after:
print(f" - {order.order_id}: {order.status}")
# Test 3: Order Tracking
print("\nπ TEST 3: Order Tracking and Result Checking")
print("-" * 50)
# Immediate check
immediate_result = await client.check_order_result(order_result.order_id)
if immediate_result:
print(" Order immediately found in tracking system")
print(f" ID: {immediate_result.order_id}")
print(f" Status: {immediate_result.status}")
else:
print("Order NOT found in tracking system - this is a problem!")
return
# Test 4: Event-Based Order Completion Monitoring
print("\nπ TEST 4: Event-Based Order Completion")
print("-" * 50)
# Set up event callback to detect completion
completed_orders = []
def on_order_closed(order_result):
completed_orders.append(order_result)
status = (
"WIN"
if order_result.profit > 0
else "LOSE"
if order_result.profit < 0
else "EVEN"
)
print(
f"ORDER COMPLETED via EVENT: {status} - Profit: ${order_result.profit:.2f}"
)
client.add_event_callback("order_closed", on_order_closed)
# Test 5: Wait for Trade Completion
print("\nπ TEST 5: Waiting for Trade Completion")
print("-" * 50)
print(
f"β±οΈ Waiting for trade to complete (up to {order_result.duration + 30} seconds)..."
)
start_time = datetime.now()
max_wait = timedelta(
seconds=order_result.duration + 30
) # Trade duration + buffer
last_status = None
while datetime.now() - start_time < max_wait:
result = await client.check_order_result(order_result.order_id)
if result:
# Only print status changes to avoid spam
if result.status != last_status:
status_emoji = (
"π’"
if result.status == "active"
else "π΄"
if result.status in ["win", "lose"]
else "π‘"
)
print(f" {status_emoji} Order status: {result.status}")
last_status = result.status
# Check if order completed
if result.profit is not None:
win_lose = (
"WIN"
if result.profit > 0
else "LOSE"
if result.profit < 0
else "EVEN"
)
print("\nTRADE COMPLETED!")
print(f" Result: {win_lose}")
print(f" Profit/Loss: ${result.profit:.2f}")
if result.payout:
print(f" Payout: ${result.payout:.2f}")
# Calculate percentage return
if result.profit != 0:
percentage = (result.profit / order_result.amount) * 100
print(f" Return: {percentage:.1f}%")
break
# Check if status indicates completion but no profit yet
elif result.status in ["win", "lose", "closed"]:
print(
f" π Order marked as {result.status} but no profit data yet..."
)
else:
print(" Order disappeared from tracking system")
break
await asyncio.sleep(2) # Check every 2 seconds
# Test 6: Event vs Polling Comparison
print("\nπ TEST 6: Event vs Polling Results")
print("-" * 50)
# Check if we completed via event callback
if completed_orders:
print(" Order completion detected via EVENT callback!")
final_order_event = completed_orders[0]
print(f" Event Result - Profit: ${final_order_event.profit:.2f}")
else:
print("No completion event received")
# Check final polling result
final_result_poll = await client.check_order_result(order_result.order_id)
if final_result_poll:
print(" Order completion detected via POLLING!")
print(
f" Polling Result - Profit: ${final_result_poll.profit:.2f if final_result_poll.profit is not None else 'None'}"
)
else:
print("Order not found via polling")
# Test 7: Final System State
print("\nπ TEST 7: Final System State")
print("-" * 50)
# Check final counts
final_active_orders = await client.get_active_orders()
print(f"π Final active orders: {len(final_active_orders)}")
for order in final_active_orders:
print(f" Active: {order.order_id} - {order.status}")
# Show test summary
print("\nπ TEST SUMMARY")
print("=" * 60)
tests_passed = 0
total_tests = 7
# Test results
if added_orders == 1:
print(" Order Placement (No Duplication): PASS")
tests_passed += 1
else:
print("Order Placement (No Duplication): FAIL")
if immediate_result:
print(" Order Tracking: PASS")
tests_passed += 1
else:
print("Order Tracking: FAIL")
if completed_orders:
print(" Event-Based Completion: PASS")
tests_passed += 1
else:
print("Event-Based Completion: FAIL")
if final_result_poll and final_result_poll.profit is not None:
print(" Polling-Based Completion: PASS")
tests_passed += 1
else:
print("Polling-Based Completion: FAIL")
# Additional checks
if len(final_active_orders) < len(active_orders_after):
print(" Order Movement (Active -> Completed): PASS")
tests_passed += 1
else:
print("Order Movement (Active -> Completed): FAIL")
if balance:
print(" Balance Retrieval: PASS")
tests_passed += 1
else:
print("Balance Retrieval: FAIL")
print(f"\nOVERALL RESULT: {tests_passed}/{total_tests} tests passed")
if tests_passed >= 5:
print("π ORDER TRACKING SYSTEM IS WORKING WELL!")
elif tests_passed >= 3:
print("Order tracking is partially working, some improvements needed")
else:
print("Major issues with order tracking system")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
finally:
# Disconnect
print("\nDisconnecting...")
await client.disconnect()
print(" Test completed")
if __name__ == "__main__":
# Configure logging to be less verbose
logger.remove()
logger.add(
lambda msg: print(msg, end=""),
format="<level>{level}</level> | {message}",
level="ERROR", # Only show errors from the library to keep output clean
)
asyncio.run(test_complete_order_lifecycle())