bybit ballance working

This commit is contained in:
Dobromir Popov
2025-07-14 23:20:01 +03:00
parent 02804ee64f
commit 5b2dd3b0b8
3 changed files with 152 additions and 46 deletions

View File

@ -99,6 +99,10 @@ class TradingExecutor:
self.primary_name = self.exchanges_config.get('primary', 'mexc')
self.primary_config = self.exchanges_config.get(self.primary_name, {})
# Set exchange config for compatibility (replaces mexc_config)
self.exchange_config = self.primary_config
self.mexc_config = self.primary_config # Legacy compatibility
# Initialize config synchronizer with the primary exchange
self.config_sync = ConfigSynchronizer(
config_path=config_path,
@ -1166,14 +1170,13 @@ class TradingExecutor:
logger.info("Daily trading statistics reset")
def get_account_balance(self) -> Dict[str, Dict[str, float]]:
"""Get account balance information from MEXC, including spot and futures.
"""Get account balance information from the primary exchange (universal method).
Returns:
Dict with asset balances in format:
{
'USDT': {'free': 100.0, 'locked': 0.0, 'total': 100.0, 'type': 'spot'},
'ETH': {'free': 0.5, 'locked': 0.0, 'total': 0.5, 'type': 'spot'},
'FUTURES_USDT': {'free': 500.0, 'locked': 50.0, 'total': 550.0, 'type': 'futures'}
...
}
"""
@ -1182,47 +1185,29 @@ class TradingExecutor:
logger.error("Exchange interface not available")
return {}
combined_balances = {}
# 1. Get Spot Account Info
spot_account_info = self.exchange.get_account_info()
if spot_account_info and 'balances' in spot_account_info:
for balance in spot_account_info['balances']:
asset = balance.get('asset', '')
free = float(balance.get('free', 0))
locked = float(balance.get('locked', 0))
if free > 0 or locked > 0:
combined_balances[asset] = {
'free': free,
'locked': locked,
'total': free + locked,
'type': 'spot'
}
# Use the universal get_all_balances method that works with all exchanges
if hasattr(self.exchange, 'get_all_balances'):
raw_balances = self.exchange.get_all_balances()
if raw_balances:
# Convert to the expected format with 'type' field
combined_balances = {}
for asset, balance_data in raw_balances.items():
if isinstance(balance_data, dict):
combined_balances[asset] = {
'free': balance_data.get('free', 0.0),
'locked': balance_data.get('locked', 0.0),
'total': balance_data.get('total', 0.0),
'type': 'spot' # Default to spot for now
}
logger.info(f"Retrieved balances for {len(combined_balances)} assets from {self.primary_name}")
return combined_balances
else:
logger.warning(f"No balances returned from {self.primary_name} exchange")
return {}
else:
logger.warning("Failed to get spot account info from MEXC or no balances found.")
# 2. Get Futures Account Info (commented out until futures API is implemented)
# futures_account_info = self.exchange.get_futures_account_info()
# if futures_account_info:
# for currency, asset_data in futures_account_info.items():
# # MEXC Futures API returns 'availableBalance' and 'frozenBalance'
# free = float(asset_data.get('availableBalance', 0))
# locked = float(asset_data.get('frozenBalance', 0))
# total = free + locked # total is the sum of available and frozen
# if free > 0 or locked > 0:
# # Prefix with 'FUTURES_' to distinguish from spot, or decide on a unified key
# # For now, let's keep them distinct for clarity
# combined_balances[f'FUTURES_{currency}'] = {
# 'free': free,
# 'locked': locked,
# 'total': total,
# 'type': 'futures'
# }
# else:
# logger.warning("Failed to get futures account info from MEXC or no futures assets found.")
logger.info(f"Retrieved combined balances for {len(combined_balances)} assets.")
return combined_balances
logger.error(f"Exchange {self.primary_name} does not support get_all_balances method")
return {}
except Exception as e:
logger.error(f"Error getting account balance: {e}")
@ -1426,7 +1411,11 @@ class TradingExecutor:
if sync_result.get('changes_made'):
logger.info("TRADING EXECUTOR: Reloading config after fee sync")
self.config = get_config(self.config_synchronizer.config_path)
self.mexc_config = self.config.get('mexc_trading', {})
# Update to use primary exchange config
self.exchanges_config = self.config.get('exchanges', {})
self.primary_name = self.exchanges_config.get('primary', 'mexc')
self.primary_config = self.exchanges_config.get(self.primary_name, {})
self.mexc_config = self.primary_config # Legacy compatibility
return sync_result