i think we fixed mexc interface at the end!!!

This commit is contained in:
Dobromir Popov
2025-07-04 02:14:29 +03:00
parent 978cecf0c5
commit cf91e090c8
7 changed files with 445 additions and 613 deletions

View File

@ -93,7 +93,6 @@ class TradingExecutor:
api_key=api_key,
api_secret=api_secret,
test_mode=exchange_test_mode,
trading_mode=trading_mode
)
# Trading state
@ -213,9 +212,15 @@ class TradingExecutor:
# Determine the quote asset (e.g., USDT, USDC) from the symbol
if '/' in symbol:
quote_asset = symbol.split('/')[1].upper() # Assuming symbol is like ETH/USDT
# Convert USDT to USDC for MEXC spot trading
if quote_asset == 'USDT':
quote_asset = 'USDC'
else:
# Fallback for symbols like ETHUSDT (assuming last 4 chars are quote)
quote_asset = symbol[-4:].upper()
quote_asset = symbol[-4:].upper()
# Convert USDT to USDC for MEXC spot trading
if quote_asset == 'USDT':
quote_asset = 'USDC'
# Calculate required capital for the trade
# If we are selling (to open a short position), we need collateral based on the position size
@ -779,13 +784,14 @@ class TradingExecutor:
logger.info("Daily trading statistics reset")
def get_account_balance(self) -> Dict[str, Dict[str, float]]:
"""Get account balance information from MEXC
"""Get account balance information from MEXC, including spot and futures.
Returns:
Dict with asset balances in format:
{
'USDT': {'free': 100.0, 'locked': 0.0},
'ETH': {'free': 0.5, 'locked': 0.0},
'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'}
...
}
"""
@ -794,28 +800,47 @@ class TradingExecutor:
logger.error("Exchange interface not available")
return {}
# Get account info from MEXC
account_info = self.exchange.get_account_info()
if not account_info:
logger.error("Failed to get account info from MEXC")
return {}
combined_balances = {}
balances = {}
for balance in account_info.get('balances', []):
asset = balance.get('asset', '')
free = float(balance.get('free', 0))
locked = float(balance.get('locked', 0))
# Only include assets with non-zero balance
if free > 0 or locked > 0:
balances[asset] = {
'free': free,
'locked': locked,
'total': free + locked
}
logger.info(f"Retrieved balances for {len(balances)} assets")
return 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'
}
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
except Exception as e:
logger.error(f"Error getting account balance: {e}")