bybit ballance working
This commit is contained in:
81
NN/exchanges/bybit/debug/test_bybit_balance.py
Normal file
81
NN/exchanges/bybit/debug/test_bybit_balance.py
Normal file
@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import sys
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
# Add project root to path
|
||||
project_root = Path(__file__).parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
from NN.exchanges.bybit_interface import BybitInterface
|
||||
|
||||
async def test_bybit_balance():
|
||||
"""Test if we can read real balance from Bybit"""
|
||||
|
||||
print("Testing Bybit Balance Reading...")
|
||||
print("=" * 50)
|
||||
|
||||
# Initialize Bybit interface
|
||||
bybit = BybitInterface()
|
||||
|
||||
try:
|
||||
# Connect to Bybit
|
||||
print("Connecting to Bybit...")
|
||||
success = await bybit.connect()
|
||||
|
||||
if not success:
|
||||
print("ERROR: Failed to connect to Bybit")
|
||||
return
|
||||
|
||||
print("✓ Connected to Bybit successfully")
|
||||
|
||||
# Test get_balance for USDT
|
||||
print("\nTesting get_balance('USDT')...")
|
||||
usdt_balance = await bybit.get_balance('USDT')
|
||||
print(f"USDT Balance: {usdt_balance}")
|
||||
|
||||
# Test get_all_balances
|
||||
print("\nTesting get_all_balances()...")
|
||||
all_balances = await bybit.get_all_balances()
|
||||
print(f"All Balances: {all_balances}")
|
||||
|
||||
# Check if we have any non-zero balances
|
||||
print("\nBalance Analysis:")
|
||||
if isinstance(all_balances, dict):
|
||||
for symbol, balance in all_balances.items():
|
||||
if isinstance(balance, (int, float)) and balance > 0:
|
||||
print(f" {symbol}: {balance}")
|
||||
elif isinstance(balance, dict):
|
||||
# Handle nested balance structure
|
||||
total = balance.get('total', 0) or balance.get('available', 0)
|
||||
if total > 0:
|
||||
print(f" {symbol}: {total}")
|
||||
|
||||
# Test account info if available
|
||||
print("\nTesting account info...")
|
||||
try:
|
||||
if hasattr(bybit, 'client') and bybit.client:
|
||||
# Try to get account info
|
||||
account_info = bybit.client.get_wallet_balance(accountType="UNIFIED")
|
||||
print(f"Account Info: {account_info}")
|
||||
except Exception as e:
|
||||
print(f"Account info error: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"ERROR: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
finally:
|
||||
# Cleanup
|
||||
if hasattr(bybit, 'client') and bybit.client:
|
||||
try:
|
||||
await bybit.client.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run the test
|
||||
asyncio.run(test_bybit_balance())
|
@ -168,7 +168,20 @@ class BybitInterface(ExchangeInterface):
|
||||
coins = account.get('coin', [])
|
||||
for coin in coins:
|
||||
if coin.get('coin', '').upper() == asset.upper():
|
||||
available_balance = float(coin.get('availableToWithdraw', 0))
|
||||
# Try availableToWithdraw first, then equity, then walletBalance
|
||||
available_str = coin.get('availableToWithdraw', '')
|
||||
if available_str:
|
||||
available_balance = float(available_str)
|
||||
else:
|
||||
# Use equity if availableToWithdraw is empty
|
||||
equity_str = coin.get('equity', '')
|
||||
if equity_str:
|
||||
available_balance = float(equity_str)
|
||||
else:
|
||||
# Fall back to walletBalance
|
||||
wallet_str = coin.get('walletBalance', '0')
|
||||
available_balance = float(wallet_str) if wallet_str else 0.0
|
||||
|
||||
logger.debug(f"Balance for {asset}: {available_balance}")
|
||||
return available_balance
|
||||
|
||||
@ -198,6 +211,14 @@ class BybitInterface(ExchangeInterface):
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting account summary: {e}")
|
||||
return {}
|
||||
|
||||
def get_account_info(self) -> Dict[str, Any]:
|
||||
"""Get account information (alias for get_account_summary for compatibility).
|
||||
|
||||
Returns:
|
||||
Dictionary with account information
|
||||
"""
|
||||
return self.get_account_summary()
|
||||
|
||||
def get_all_balances(self) -> Dict[str, Dict[str, float]]:
|
||||
"""Get all account balances in the format expected by trading executor.
|
||||
@ -217,8 +238,23 @@ class BybitInterface(ExchangeInterface):
|
||||
asset = coin.get('coin', '')
|
||||
if asset:
|
||||
# Convert Bybit balance format to MEXC-compatible format
|
||||
available = float(coin.get('availableToWithdraw', 0))
|
||||
locked = float(coin.get('locked', 0))
|
||||
# Handle empty string values that cause conversion errors
|
||||
available_str = coin.get('availableToWithdraw', '')
|
||||
locked_str = coin.get('locked', '')
|
||||
equity_str = coin.get('equity', '')
|
||||
wallet_str = coin.get('walletBalance', '')
|
||||
|
||||
# Use equity or walletBalance if availableToWithdraw is empty
|
||||
if available_str:
|
||||
available = float(available_str)
|
||||
elif equity_str:
|
||||
available = float(equity_str)
|
||||
elif wallet_str:
|
||||
available = float(wallet_str)
|
||||
else:
|
||||
available = 0.0
|
||||
|
||||
locked = float(locked_str) if locked_str else 0.0
|
||||
|
||||
balances[asset] = {
|
||||
'free': available,
|
||||
|
Reference in New Issue
Block a user