Files
hedge/test_binance.py

98 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""
Test script for Binance API integration
"""
import time
from market import (
get_binance_price,
get_all_binance_prices,
get_market_arbitrage_opportunities,
FLEXTRADE_TO_BINANCE_MAPPING
)
def test_single_binance_price():
"""Test fetching a single price from Binance"""
print("🧪 Testing single Binance price fetch...")
symbol = "SOLUSDC" # SOL-USD
price = get_binance_price(symbol, debug=True)
if price:
print(f"✅ Successfully fetched {symbol}: ${price:.4f}")
else:
print(f"❌ Failed to fetch price for {symbol}")
print("-" * 50)
def test_all_binance_prices():
"""Test fetching all mapped Binance prices"""
print("🧪 Testing all Binance prices fetch...")
prices = get_all_binance_prices(debug=True)
print(f"\n📊 Retrieved {len(prices)} prices:")
for market, data in prices.items():
print(f" {market}: ${data['price']:.4f} (via {data['binance_symbol']})")
print("-" * 50)
def test_price_comparison_simulation():
"""Simulate price comparison with mock FlexTrade data"""
print("🧪 Testing price comparison simulation...")
# Get real Binance prices
binance_prices = get_all_binance_prices(debug=False)
# Create mock FlexTrade prices (slightly different for demonstration)
mock_flextrade_prices = {}
for market, binance_data in binance_prices.items():
# Add 0.5% to 2% difference to simulate price variations
import random
price_modifier = random.uniform(0.995, 1.02) # -0.5% to +2%
mock_price = binance_data['price'] * price_modifier
# Convert market name to FlexTrade format (remove hyphens)
flextrade_market = market.replace('-', '')
mock_flextrade_prices[flextrade_market] = {
'price': mock_price,
'timestamp_str': binance_data['timestamp_str'],
'source': 'flextrade_mock'
}
# Import comparison function
from market import compare_prices
print("\n📈 Price Comparison (Mock FlexTrade vs Real Binance):")
comparison = compare_prices(mock_flextrade_prices, binance_prices, debug=True)
print(f"\n🔍 Found {len(comparison)} price comparisons")
print("-" * 50)
def main():
"""Run all tests"""
print("🚀 Starting Binance API Integration Tests")
print("=" * 80)
# Display mapping information
print("📋 FlexTrade to Binance Symbol Mapping:")
for ft_market, bn_symbol in FLEXTRADE_TO_BINANCE_MAPPING.items():
print(f" {ft_market:<12}{bn_symbol}")
print("-" * 50)
# Run tests
test_single_binance_price()
time.sleep(1)
test_all_binance_prices()
time.sleep(1)
test_price_comparison_simulation()
print("✅ All tests completed!")
print("=" * 80)
if __name__ == '__main__':
main()