dynamic profitabiliy reward
This commit is contained in:
@ -176,13 +176,25 @@ class TradingExecutor:
|
||||
self.simulation_balance = self.trading_config.get('simulation_account_usd', 100.0)
|
||||
self.simulation_positions = {} # symbol -> position data with real entry prices
|
||||
|
||||
# Trading fees configuration (0.1% for both open and close)
|
||||
# Trading fees configuration (0.1% for both open and close - REVERTED TO NORMAL)
|
||||
self.trading_fees = {
|
||||
'open_fee_percent': 0.001, # 0.1% fee when opening position
|
||||
'close_fee_percent': 0.001, # 0.1% fee when closing position
|
||||
'total_round_trip_fee': 0.002 # 0.2% total for round trip
|
||||
}
|
||||
|
||||
# Dynamic profitability reward parameter - starts at 0, adjusts based on success rate
|
||||
self.profitability_reward_multiplier = 0.0 # Starts at 0, can be increased
|
||||
self.min_profitability_multiplier = 0.0 # Minimum value
|
||||
self.max_profitability_multiplier = 2.0 # Maximum 2x multiplier
|
||||
self.profitability_adjustment_step = 0.1 # Adjust by 0.1 each time
|
||||
|
||||
# Success rate tracking for profitability adjustment
|
||||
self.recent_trades_window = 20 # Look at last 20 trades
|
||||
self.success_rate_increase_threshold = 0.60 # Increase multiplier if >60% success
|
||||
self.success_rate_decrease_threshold = 0.51 # Decrease multiplier if <51% success
|
||||
self.last_profitability_adjustment = datetime.now()
|
||||
|
||||
logger.info(f"TradingExecutor initialized - Trading: {self.trading_enabled}, Mode: {self.trading_mode}")
|
||||
logger.info(f"Simulation balance: ${self.simulation_balance:.2f}")
|
||||
|
||||
@ -622,6 +634,83 @@ class TradingExecutor:
|
||||
logger.error(f"Error cancelling open orders for {symbol}: {e}")
|
||||
return 0
|
||||
|
||||
def _calculate_recent_success_rate(self) -> float:
|
||||
"""Calculate success rate of recent closed trades
|
||||
|
||||
Returns:
|
||||
float: Success rate (0.0 to 1.0) of recent trades
|
||||
"""
|
||||
try:
|
||||
if len(self.trade_records) < 5: # Need at least 5 trades
|
||||
return 0.0
|
||||
|
||||
# Get recent trades (up to the window size)
|
||||
recent_trades = self.trade_records[-self.recent_trades_window:]
|
||||
|
||||
if not recent_trades:
|
||||
return 0.0
|
||||
|
||||
# Count winning trades (net PnL > 0)
|
||||
winning_trades = sum(1 for trade in recent_trades if trade.net_pnl > 0)
|
||||
success_rate = winning_trades / len(recent_trades)
|
||||
|
||||
logger.debug(f"Recent success rate: {success_rate:.2%} ({winning_trades}/{len(recent_trades)} trades)")
|
||||
return success_rate
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error calculating success rate: {e}")
|
||||
return 0.0
|
||||
|
||||
def _adjust_profitability_reward_multiplier(self):
|
||||
"""Adjust profitability reward multiplier based on recent success rate"""
|
||||
try:
|
||||
# Only adjust every 5 minutes to avoid too frequent changes
|
||||
current_time = datetime.now()
|
||||
time_since_last_adjustment = (current_time - self.last_profitability_adjustment).total_seconds()
|
||||
|
||||
if time_since_last_adjustment < 300: # 5 minutes
|
||||
return
|
||||
|
||||
success_rate = self._calculate_recent_success_rate()
|
||||
|
||||
# Only adjust if we have enough trades
|
||||
if len(self.trade_records) < 10:
|
||||
return
|
||||
|
||||
old_multiplier = self.profitability_reward_multiplier
|
||||
|
||||
# Increase multiplier if success rate > 60%
|
||||
if success_rate > self.success_rate_increase_threshold:
|
||||
self.profitability_reward_multiplier = min(
|
||||
self.max_profitability_multiplier,
|
||||
self.profitability_reward_multiplier + self.profitability_adjustment_step
|
||||
)
|
||||
logger.info(f"🎯 SUCCESS RATE HIGH ({success_rate:.1%}) - Increased profitability multiplier: {old_multiplier:.1f} → {self.profitability_reward_multiplier:.1f}")
|
||||
|
||||
# Decrease multiplier if success rate < 51%
|
||||
elif success_rate < self.success_rate_decrease_threshold:
|
||||
self.profitability_reward_multiplier = max(
|
||||
self.min_profitability_multiplier,
|
||||
self.profitability_reward_multiplier - self.profitability_adjustment_step
|
||||
)
|
||||
logger.info(f"⚠️ SUCCESS RATE LOW ({success_rate:.1%}) - Decreased profitability multiplier: {old_multiplier:.1f} → {self.profitability_reward_multiplier:.1f}")
|
||||
|
||||
else:
|
||||
logger.debug(f"Success rate {success_rate:.1%} in acceptable range - keeping multiplier at {self.profitability_reward_multiplier:.1f}")
|
||||
|
||||
self.last_profitability_adjustment = current_time
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error adjusting profitability reward multiplier: {e}")
|
||||
|
||||
def get_profitability_reward_multiplier(self) -> float:
|
||||
"""Get current profitability reward multiplier
|
||||
|
||||
Returns:
|
||||
float: Current profitability reward multiplier
|
||||
"""
|
||||
return self.profitability_reward_multiplier
|
||||
|
||||
def _can_reenable_live_trading(self) -> bool:
|
||||
"""Check if trading performance has improved enough to re-enable live trading
|
||||
|
||||
@ -1198,7 +1287,11 @@ class TradingExecutor:
|
||||
)
|
||||
|
||||
self.trade_history.append(trade_record)
|
||||
self.trade_records.append(trade_record) # Add to trade records for success rate tracking
|
||||
self.daily_loss += max(0, -pnl) # Add to daily loss if negative
|
||||
|
||||
# Adjust profitability reward multiplier based on recent performance
|
||||
self._adjust_profitability_reward_multiplier()
|
||||
|
||||
# Update consecutive losses
|
||||
if pnl < -0.001: # A losing trade
|
||||
@ -1289,8 +1382,12 @@ class TradingExecutor:
|
||||
)
|
||||
|
||||
self.trade_history.append(trade_record)
|
||||
self.trade_records.append(trade_record) # Add to trade records for success rate tracking
|
||||
self.daily_loss += max(0, -(pnl - fees)) # Add to daily loss if negative
|
||||
|
||||
# Adjust profitability reward multiplier based on recent performance
|
||||
self._adjust_profitability_reward_multiplier()
|
||||
|
||||
# Update consecutive losses
|
||||
if pnl < -0.001: # A losing trade
|
||||
self.consecutive_losses += 1
|
||||
@ -1356,7 +1453,11 @@ class TradingExecutor:
|
||||
)
|
||||
|
||||
self.trade_history.append(trade_record)
|
||||
self.trade_records.append(trade_record) # Add to trade records for success rate tracking
|
||||
self.daily_loss += max(0, -pnl) # Add to daily loss if negative
|
||||
|
||||
# Adjust profitability reward multiplier based on recent performance
|
||||
self._adjust_profitability_reward_multiplier()
|
||||
|
||||
# Update consecutive losses
|
||||
if pnl < -0.001: # A losing trade
|
||||
@ -1428,8 +1529,12 @@ class TradingExecutor:
|
||||
)
|
||||
|
||||
self.trade_history.append(trade_record)
|
||||
self.trade_records.append(trade_record) # Add to trade records for success rate tracking
|
||||
self.daily_loss += max(0, -(pnl - fees)) # Add to daily loss if negative
|
||||
|
||||
# Adjust profitability reward multiplier based on recent performance
|
||||
self._adjust_profitability_reward_multiplier()
|
||||
|
||||
# Update consecutive losses
|
||||
if pnl < -0.001: # A losing trade
|
||||
self.consecutive_losses += 1
|
||||
|
Reference in New Issue
Block a user