This commit is contained in:
Dobromir Popov
2025-09-08 13:41:22 +03:00
parent c9fba56622
commit 56e857435c
5 changed files with 27 additions and 295 deletions

View File

@@ -84,52 +84,10 @@ def test_win_rate_calculation():
trading_executor = TradingExecutor()
# Clear existing trades
trading_executor.trade_history = []
# Add test trades with meaningful P&L
logger.info("1. Adding test trades with meaningful P&L:")
# Add 3 winning trades
for i in range(3):
winning_trade = TradeRecord(
symbol='ETH/USDT',
side='LONG',
quantity=1.0,
entry_price=2500.0,
exit_price=2550.0,
entry_time=datetime.now(),
exit_time=datetime.now(),
pnl=50.0, # $50 profit with leverage
fees=1.0,
confidence=0.8,
hold_time_seconds=30.0 # 30 second hold
)
trading_executor.trade_history.append(winning_trade)
logger.info(f" Added winning trade #{i+1}: +$50.00 (30s hold)")
# Add 2 losing trades
for i in range(2):
losing_trade = TradeRecord(
symbol='ETH/USDT',
side='LONG',
quantity=1.0,
entry_price=2500.0,
exit_price=2475.0,
entry_time=datetime.now(),
exit_time=datetime.now(),
pnl=-25.0, # $25 loss with leverage
fees=1.0,
confidence=0.7,
hold_time_seconds=15.0 # 15 second hold
)
trading_executor.trade_history.append(losing_trade)
logger.info(f" Added losing trade #{i+1}: -$25.00 (15s hold)")
# Get statistics
# Get statistics from existing trades
stats = trading_executor.get_daily_stats()
logger.info("2. Calculated statistics:")
logger.info("1. Current trading statistics:")
logger.info(f" Total trades: {stats['total_trades']}")
logger.info(f" Winning trades: {stats['winning_trades']}")
logger.info(f" Losing trades: {stats['losing_trades']}")
@@ -137,21 +95,23 @@ def test_win_rate_calculation():
logger.info(f" Avg winning trade: ${stats['avg_winning_trade']:.2f}")
logger.info(f" Avg losing trade: ${stats['avg_losing_trade']:.2f}")
logger.info(f" Total P&L: ${stats['total_pnl']:.2f}")
# Verify calculations
expected_win_rate = 3/5 # 3 wins out of 5 trades = 60%
expected_avg_win = 50.0
expected_avg_loss = -25.0
logger.info("3. Verification:")
win_rate_ok = abs(stats['win_rate'] - expected_win_rate) < 0.01
avg_win_ok = abs(stats['avg_winning_trade'] - expected_avg_win) < 0.01
avg_loss_ok = abs(stats['avg_losing_trade'] - expected_avg_loss) < 0.01
logger.info(f" Win rate: Expected {expected_win_rate*100:.1f}%, Got {stats['win_rate']*100:.1f}% {'' if win_rate_ok else ''}")
logger.info(f" Avg win: Expected ${expected_avg_win:.2f}, Got ${stats['avg_winning_trade']:.2f} {'' if avg_win_ok else ''}")
logger.info(f" Avg loss: Expected ${expected_avg_loss:.2f}, Got ${stats['avg_losing_trade']:.2f} {'' if avg_loss_ok else ''}")
# If no trades, we can't verify calculations
if stats['total_trades'] == 0:
logger.info("2. No trades found - cannot verify calculations")
logger.info(" Run the system and execute real trades to test statistics")
return False
# Basic sanity checks on existing data
logger.info("2. Basic validation:")
win_rate_ok = 0.0 <= stats['win_rate'] <= 1.0
avg_win_ok = stats['avg_winning_trade'] >= 0 if stats['winning_trades'] > 0 else True
avg_loss_ok = stats['avg_losing_trade'] <= 0 if stats['losing_trades'] > 0 else True
logger.info(f" Win rate in valid range [0,1]: {'' if win_rate_ok else ''}")
logger.info(f" Avg win is positive when winning trades exist: {'' if avg_win_ok else ''}")
logger.info(f" Avg loss is negative when losing trades exist: {'' if avg_loss_ok else ''}")
return win_rate_ok and avg_win_ok and avg_loss_ok
def test_new_features():