risk managment
This commit is contained in:
@ -303,13 +303,15 @@ class EnhancedCNNModel(nn.Module):
|
|||||||
batch_size, seq_len, features = x.shape
|
batch_size, seq_len, features = x.shape
|
||||||
|
|
||||||
# Reshape for processing: [batch, seq, features] -> [batch*seq, features]
|
# 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
|
# Input embedding
|
||||||
embedded = self.input_embedding(x_reshaped) # [batch*seq, base_channels]
|
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]
|
# 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
|
# Multi-scale feature extraction
|
||||||
path1 = self.conv_path1(embedded)
|
path1 = self.conv_path1(embedded)
|
||||||
|
@ -164,10 +164,10 @@ mexc_trading:
|
|||||||
simulation_account_usd: 100.0 # $100 simulation account balance
|
simulation_account_usd: 100.0 # $100 simulation account balance
|
||||||
|
|
||||||
# Risk management
|
# Risk management
|
||||||
max_daily_trades: 100
|
|
||||||
max_daily_loss_usd: 200.0
|
max_daily_loss_usd: 200.0
|
||||||
max_concurrent_positions: 3
|
max_concurrent_positions: 3
|
||||||
min_trade_interval_seconds: 5 # Reduced for testing and training
|
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
|
# Symbol restrictions - ETH ONLY
|
||||||
allowed_symbols: ["ETH/USDT"]
|
allowed_symbols: ["ETH/USDT"]
|
||||||
|
@ -104,6 +104,7 @@ class TradingExecutor:
|
|||||||
self.last_trade_time = {}
|
self.last_trade_time = {}
|
||||||
self.trading_enabled = self.mexc_config.get('enabled', False)
|
self.trading_enabled = self.mexc_config.get('enabled', False)
|
||||||
self.trading_mode = trading_mode
|
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}")
|
logger.debug(f"TRADING EXECUTOR: Initial trading_enabled state from config: {self.trading_enabled}")
|
||||||
|
|
||||||
@ -284,13 +285,13 @@ class TradingExecutor:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
# Check daily trade limit
|
# Check daily trade limit
|
||||||
max_daily_trades = self.mexc_config.get('max_trades_per_hour', 2) * 24
|
# max_daily_trades = self.mexc_config.get('max_daily_trades', 100)
|
||||||
if self.daily_trades >= max_daily_trades:
|
# if self.daily_trades >= max_daily_trades:
|
||||||
logger.warning(f"Daily trade limit reached: {self.daily_trades}")
|
# logger.warning(f"Daily trade limit reached: {self.daily_trades}")
|
||||||
return False
|
# return False
|
||||||
|
|
||||||
# Check trade interval
|
# 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)
|
last_trade = self.last_trade_time.get(symbol, datetime.min)
|
||||||
if (datetime.now() - last_trade).total_seconds() < min_interval:
|
if (datetime.now() - last_trade).total_seconds() < min_interval:
|
||||||
logger.info(f"Trade interval not met for {symbol}")
|
logger.info(f"Trade interval not met for {symbol}")
|
||||||
@ -440,6 +441,14 @@ class TradingExecutor:
|
|||||||
|
|
||||||
self.trade_history.append(trade_record)
|
self.trade_history.append(trade_record)
|
||||||
self.daily_loss += max(0, -pnl) # Add to daily loss if negative
|
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
|
# Remove position
|
||||||
del self.positions[symbol]
|
del self.positions[symbol]
|
||||||
@ -507,6 +516,14 @@ class TradingExecutor:
|
|||||||
self.trade_history.append(trade_record)
|
self.trade_history.append(trade_record)
|
||||||
self.daily_loss += max(0, -(pnl - fees)) # Add to daily loss if negative
|
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
|
# Remove position
|
||||||
del self.positions[symbol]
|
del self.positions[symbol]
|
||||||
self.last_trade_time[symbol] = datetime.now()
|
self.last_trade_time[symbol] = datetime.now()
|
||||||
@ -723,6 +740,14 @@ class TradingExecutor:
|
|||||||
self.trade_history.append(trade_record)
|
self.trade_history.append(trade_record)
|
||||||
self.daily_loss += max(0, -(pnl - fees)) # Add to daily loss if negative
|
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
|
# Remove position
|
||||||
del self.positions[symbol]
|
del self.positions[symbol]
|
||||||
self.last_trade_time[symbol] = datetime.now()
|
self.last_trade_time[symbol] = datetime.now()
|
||||||
@ -757,6 +782,11 @@ class TradingExecutor:
|
|||||||
# Apply leverage to get effective position size
|
# Apply leverage to get effective position size
|
||||||
leveraged_position_value = position_value * leverage
|
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}, "
|
logger.debug(f"Position calculation: account=${account_balance:.2f}, "
|
||||||
f"percent={position_percent*100:.1f}%, base=${position_value:.2f}, "
|
f"percent={position_percent*100:.1f}%, base=${position_value:.2f}, "
|
||||||
f"leverage={leverage}x, effective=${leveraged_position_value:.2f}, "
|
f"leverage={leverage}x, effective=${leveraged_position_value:.2f}, "
|
||||||
|
Reference in New Issue
Block a user