-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_arbitrage_cycle.py
More file actions
137 lines (110 loc) · 4.25 KB
/
test_arbitrage_cycle.py
File metadata and controls
137 lines (110 loc) · 4.25 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
#!/usr/bin/env python3
"""Test script to verify arbitrage cycle structure compatibility."""
from cross_rate_matrix import CrossRateMatrix
from arbitrage_trade import ArbitrageTrade
import time
def test_cycle_structure():
"""Test that profit_info from CrossRateMatrix works with ArbitrageTrade."""
print("=" * 60)
print("Testing Arbitrage Cycle Structure Compatibility")
print("=" * 60)
# Create cross-rate matrix
matrix = CrossRateMatrix()
# Simulate some price data
test_prices = {
'BTC/USDT': {'bid': 45000, 'ask': 45050, 'timestamp': time.time() * 1000},
'ETH/USDT': {'bid': 3000, 'ask': 3005, 'timestamp': time.time() * 1000},
'ETH/BTC': {'bid': 0.066, 'ask': 0.0661, 'timestamp': time.time() * 1000},
}
test_markets = {
'BTC/USDT': {'base': 'BTC', 'quote': 'USDT'},
'ETH/USDT': {'base': 'ETH', 'quote': 'USDT'},
'ETH/BTC': {'base': 'ETH', 'quote': 'BTC'},
}
# Update matrix
matrix.update_from_symbol_prices(test_prices, test_markets)
# Find arbitrage cycles
cycles = matrix.find_arbitrage_cycles("USDT", min_profit_threshold=-0.1) # Allow negative for testing
if not cycles:
print("\n❌ No cycles found! Check price data.")
return False
print(f"\n✅ Found {len(cycles)} cycles")
# Get first cycle
cycle = cycles[0]
print("\n" + "=" * 60)
print("Cycle Structure from CrossRateMatrix:")
print("=" * 60)
print(f"Cycle path: {cycle['cycle']}")
print(f"Expected profit: {cycle['profit_percentage']:.4f}%")
print(f"\nSteps ({len(cycle['steps'])}):")
for i, step in enumerate(cycle['steps'], 1):
print(f"\n Step {i}:")
print(f" Symbol: {step['symbol']}")
print(f" Side: {step.get('side', 'MISSING!')}")
print(f" From: {step['from']} -> To: {step['to']}")
print(f" Rate: {step['rate']}")
print(f" Fee: {step['fee']}")
print(f" Amount before: {step['amount_before']:.8f}")
print(f" Amount after: {step['amount_after']:.8f}")
# Check required fields for ArbitrageTrade
print("\n" + "=" * 60)
print("Verification for ArbitrageTrade Compatibility:")
print("=" * 60)
required_cycle_fields = ['cycle', 'profit_percentage', 'steps']
required_step_fields = ['symbol', 'side', 'from', 'to', 'rate', 'fee']
all_ok = True
# Check cycle fields
for field in required_cycle_fields:
if field in cycle:
print(f"✅ Cycle has '{field}'")
else:
print(f"❌ Cycle MISSING '{field}'")
all_ok = False
# Check step fields
print(f"\nChecking {len(cycle['steps'])} steps:")
for i, step in enumerate(cycle['steps'], 1):
print(f"\n Step {i}:")
for field in required_step_fields:
if field in step:
print(f" ✅ Has '{field}': {step[field]}")
else:
print(f" ❌ MISSING '{field}'")
all_ok = False
# Test with ArbitrageTrade (dry run)
print("\n" + "=" * 60)
print("Testing with ArbitrageTrade (DRY RUN):")
print("=" * 60)
try:
trader = ArbitrageTrade(
exchange='binance',
api_key='test_key',
api_secret='test_secret'
)
success, results, final_amount = trader.execute_cycle(
cycle=cycle,
initial_amount=1.0,
dry_run=True
)
if success:
print(f"\n✅ Dry run SUCCESSFUL!")
print(f" Final amount: {final_amount:.8f}")
print(f" Executed {len(results)} trades")
else:
print(f"\n❌ Dry run FAILED!")
all_ok = False
except Exception as e:
print(f"\n❌ Exception during dry run: {e}")
import traceback
traceback.print_exc()
all_ok = False
# Final result
print("\n" + "=" * 60)
if all_ok:
print("✅ ALL TESTS PASSED - Structure is compatible!")
else:
print("❌ TESTS FAILED - Structure needs fixes!")
print("=" * 60)
return all_ok
if __name__ == "__main__":
success = test_cycle_structure()
exit(0 if success else 1)