risk managment

This commit is contained in:
Dobromir Popov
2025-07-04 20:52:40 +03:00
parent ed42e7c238
commit e8b9c05148
3 changed files with 40 additions and 8 deletions

View File

@ -104,6 +104,7 @@ class TradingExecutor:
self.last_trade_time = {}
self.trading_enabled = self.mexc_config.get('enabled', False)
self.trading_mode = trading_mode
self.consecutive_losses = 0 # Track consecutive losing trades
logger.debug(f"TRADING EXECUTOR: Initial trading_enabled state from config: {self.trading_enabled}")
@ -284,13 +285,13 @@ class TradingExecutor:
return False
# Check daily trade limit
max_daily_trades = self.mexc_config.get('max_trades_per_hour', 2) * 24
if self.daily_trades >= max_daily_trades:
logger.warning(f"Daily trade limit reached: {self.daily_trades}")
return False
# max_daily_trades = self.mexc_config.get('max_daily_trades', 100)
# if self.daily_trades >= max_daily_trades:
# logger.warning(f"Daily trade limit reached: {self.daily_trades}")
# return False
# Check trade interval
min_interval = self.mexc_config.get('min_trade_interval_seconds', 300)
min_interval = self.mexc_config.get('min_trade_interval_seconds', 5)
last_trade = self.last_trade_time.get(symbol, datetime.min)
if (datetime.now() - last_trade).total_seconds() < min_interval:
logger.info(f"Trade interval not met for {symbol}")
@ -440,6 +441,14 @@ class TradingExecutor:
self.trade_history.append(trade_record)
self.daily_loss += max(0, -pnl) # Add to daily loss if negative
# Update consecutive losses
if pnl < -0.001: # A losing trade
self.consecutive_losses += 1
elif pnl > 0.001: # A winning trade
self.consecutive_losses = 0
else: # Breakeven trade
self.consecutive_losses = 0
# Remove position
del self.positions[symbol]
@ -507,6 +516,14 @@ class TradingExecutor:
self.trade_history.append(trade_record)
self.daily_loss += max(0, -(pnl - fees)) # Add to daily loss if negative
# Update consecutive losses
if pnl < -0.001: # A losing trade
self.consecutive_losses += 1
elif pnl > 0.001: # A winning trade
self.consecutive_losses = 0
else: # Breakeven trade
self.consecutive_losses = 0
# Remove position
del self.positions[symbol]
self.last_trade_time[symbol] = datetime.now()
@ -723,6 +740,14 @@ class TradingExecutor:
self.trade_history.append(trade_record)
self.daily_loss += max(0, -(pnl - fees)) # Add to daily loss if negative
# Update consecutive losses
if pnl < -0.001: # A losing trade
self.consecutive_losses += 1
elif pnl > 0.001: # A winning trade
self.consecutive_losses = 0
else: # Breakeven trade
self.consecutive_losses = 0
# Remove position
del self.positions[symbol]
self.last_trade_time[symbol] = datetime.now()
@ -757,6 +782,11 @@ class TradingExecutor:
# Apply leverage to get effective position size
leveraged_position_value = position_value * leverage
# Apply reduction based on consecutive losses
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
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}, "