From e8b9c05148e1fecf827475256ef8cfd4dbb8791f Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Fri, 4 Jul 2025 20:52:40 +0300 Subject: [PATCH] risk managment --- NN/models/cnn_model_pytorch.py | 6 +++-- config.yaml | 2 +- core/trading_executor.py | 40 +++++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/NN/models/cnn_model_pytorch.py b/NN/models/cnn_model_pytorch.py index bc1372d..2c166ad 100644 --- a/NN/models/cnn_model_pytorch.py +++ b/NN/models/cnn_model_pytorch.py @@ -303,13 +303,15 @@ class EnhancedCNNModel(nn.Module): batch_size, seq_len, features = x.shape # Reshape for processing: [batch, seq, features] -> [batch*seq, features] - x_reshaped = x.view(-1, features) + x_reshaped = x.reshape(-1, features) + x_reshaped = self._memory_barrier(x_reshaped) # Input embedding embedded = self.input_embedding(x_reshaped) # [batch*seq, base_channels] + embedded = self._memory_barrier(embedded) # Reshape back for conv1d: [batch*seq, channels] -> [batch, channels, seq] - embedded = embedded.view(batch_size, seq_len, -1).transpose(1, 2) + embedded = embedded.reshape(batch_size, seq_len, -1).transpose(1, 2).contiguous() # Multi-scale feature extraction path1 = self.conv_path1(embedded) diff --git a/config.yaml b/config.yaml index 3234f87..c96ec12 100644 --- a/config.yaml +++ b/config.yaml @@ -164,10 +164,10 @@ mexc_trading: simulation_account_usd: 100.0 # $100 simulation account balance # Risk management - max_daily_trades: 100 max_daily_loss_usd: 200.0 max_concurrent_positions: 3 min_trade_interval_seconds: 5 # Reduced for testing and training + consecutive_loss_reduction_factor: 0.8 # Reduce position size by 20% after each consecutive loss # Symbol restrictions - ETH ONLY allowed_symbols: ["ETH/USDT"] diff --git a/core/trading_executor.py b/core/trading_executor.py index e08b267..b96723c 100644 --- a/core/trading_executor.py +++ b/core/trading_executor.py @@ -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}, "