mexc interface integrations REST API fixes
This commit is contained in:
@ -214,38 +214,40 @@ 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'
|
||||
# Keep USDT as USDT for MEXC spot trading (no conversion needed)
|
||||
else:
|
||||
# Fallback for symbols like ETHUSDT (assuming last 4 chars are quote)
|
||||
quote_asset = symbol[-4:].upper()
|
||||
# Convert USDT to USDC for MEXC spot trading
|
||||
if quote_asset == 'USDT':
|
||||
quote_asset = 'USDC'
|
||||
# Keep USDT as USDT for MEXC spot trading (no conversion needed)
|
||||
|
||||
# Calculate required capital for the trade
|
||||
# If we are selling (to open a short position), we need collateral based on the position size
|
||||
# For simplicity, assume required capital is the full position value in USD
|
||||
required_capital = self._calculate_position_size(confidence, current_price)
|
||||
|
||||
# Get available balance for the quote asset
|
||||
available_balance = self.exchange.get_balance(quote_asset)
|
||||
|
||||
# If USDC balance is insufficient, check USDT as fallback (for MEXC compatibility)
|
||||
if available_balance < required_capital and quote_asset == 'USDC':
|
||||
usdt_balance = self.exchange.get_balance('USDT')
|
||||
if usdt_balance >= required_capital:
|
||||
available_balance = usdt_balance
|
||||
quote_asset = 'USDT' # Use USDT instead
|
||||
logger.info(f"BALANCE CHECK: Using USDT fallback balance for {symbol}")
|
||||
# Get available balance for the quote asset (try USDT first, then USDC as fallback)
|
||||
if quote_asset == 'USDT':
|
||||
available_balance = self.exchange.get_balance('USDT')
|
||||
if available_balance < required_capital:
|
||||
# If USDT balance is insufficient, check USDC as fallback
|
||||
usdc_balance = self.exchange.get_balance('USDC')
|
||||
if usdc_balance >= required_capital:
|
||||
available_balance = usdc_balance
|
||||
quote_asset = 'USDC' # Use USDC instead
|
||||
logger.info(f"BALANCE CHECK: Using USDC fallback balance for {symbol}")
|
||||
else:
|
||||
available_balance = self.exchange.get_balance(quote_asset)
|
||||
|
||||
logger.info(f"BALANCE CHECK: Symbol: {symbol}, Action: {action}, Required: ${required_capital:.2f} {quote_asset}, Available: ${available_balance:.2f} {quote_asset}")
|
||||
|
||||
if available_balance < required_capital:
|
||||
# Allow some small precision tolerance (1 cent) and ensure sufficient balance
|
||||
balance_tolerance = 0.01
|
||||
if available_balance < (required_capital - balance_tolerance):
|
||||
logger.warning(f"Trade blocked for {symbol} {action}: Insufficient {quote_asset} balance. "
|
||||
f"Required: ${required_capital:.2f}, Available: ${available_balance:.2f}")
|
||||
return False
|
||||
else:
|
||||
logger.info(f"BALANCE CHECK PASSED: {quote_asset} balance sufficient for trade")
|
||||
elif self.simulation_mode:
|
||||
logger.debug(f"SIMULATION MODE: Skipping balance check for {symbol} {action} - allowing trade for model training")
|
||||
# --- End Balance check ---
|
||||
@ -765,34 +767,25 @@ class TradingExecutor:
|
||||
return False
|
||||
|
||||
def _calculate_position_size(self, confidence: float, current_price: float) -> float:
|
||||
"""Calculate position size based on percentage of account balance, confidence, and leverage"""
|
||||
"""Calculate position size - use 100% of account balance for short-term scalping"""
|
||||
# Get account balance (simulation or real)
|
||||
account_balance = self._get_account_balance_for_sizing()
|
||||
|
||||
# Get position sizing percentages
|
||||
max_percent = self.mexc_config.get('max_position_percent', 20.0) / 100.0
|
||||
min_percent = self.mexc_config.get('min_position_percent', 2.0) / 100.0
|
||||
base_percent = self.mexc_config.get('base_position_percent', 5.0) / 100.0
|
||||
leverage = self.mexc_config.get('leverage', 50.0)
|
||||
# Use 100% of account balance since we're holding for seconds/minutes only
|
||||
# Scale by confidence: 70-100% of balance based on confidence (0.7-1.0 range)
|
||||
confidence_multiplier = max(0.7, min(1.0, confidence))
|
||||
position_value = account_balance * confidence_multiplier
|
||||
|
||||
# Scale position size by confidence
|
||||
position_percent = min(max_percent, max(min_percent, base_percent * confidence))
|
||||
position_value = account_balance * position_percent
|
||||
|
||||
# Apply leverage to get effective position size
|
||||
leveraged_position_value = position_value * leverage
|
||||
|
||||
# Apply reduction based on consecutive losses
|
||||
# Apply reduction based on consecutive losses (risk management)
|
||||
reduction_factor = self.mexc_config.get('consecutive_loss_reduction_factor', 0.8)
|
||||
adjusted_reduction_factor = reduction_factor ** self.consecutive_losses
|
||||
leveraged_position_value *= adjusted_reduction_factor
|
||||
position_value *= adjusted_reduction_factor
|
||||
|
||||
logger.debug(f"Position calculation: account=${account_balance:.2f}, "
|
||||
f"percent={position_percent*100:.1f}%, base=${position_value:.2f}, "
|
||||
f"leverage={leverage}x, effective=${leveraged_position_value:.2f}, "
|
||||
f"confidence={confidence:.2f}")
|
||||
f"confidence_mult={confidence_multiplier:.2f}, "
|
||||
f"position=${position_value:.2f}, confidence={confidence:.2f}")
|
||||
|
||||
return leveraged_position_value
|
||||
return position_value
|
||||
|
||||
def _get_account_balance_for_sizing(self) -> float:
|
||||
"""Get account balance for position sizing calculations"""
|
||||
|
Reference in New Issue
Block a user