From 2f2a7665e73f04919709426d9cbf3cbee18dcd6f Mon Sep 17 00:00:00 2001 From: Celes Renata Date: Wed, 13 May 2026 20:19:47 +0000 Subject: [PATCH] fix: suppress take-profit when active buy signal has high confidence Profit-taking was selling positions that still had strong bullish signals (0.93 confidence buy recommendations), then the engine would immediately rebuy at a worse price. Now checks for recent high-confidence buy recommendations (>=0.80) before executing a take-profit sell. If the signal says keep holding, the take-profit is suppressed. Stop-losses still fire unconditionally. --- services/trading/engine.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/services/trading/engine.py b/services/trading/engine.py index 6f91f9a..a4f7cc1 100644 --- a/services/trading/engine.py +++ b/services/trading/engine.py @@ -946,6 +946,27 @@ class TradingEngine: if pos_match is None: continue + # Suppress take-profit when a strong buy signal is active + if trigger.trigger_type == "take_profit" and self.pool is not None: + try: + active_buy = await self.pool.fetchrow( + "SELECT confidence FROM recommendations " + "WHERE ticker = $1 AND action = 'buy' " + "AND mode IN ('paper_eligible', 'live_eligible') " + "AND generated_at > NOW() - INTERVAL '2 hours' " + "ORDER BY confidence DESC LIMIT 1", + trigger.ticker, + ) + if active_buy and float(active_buy["confidence"]) >= 0.80: + logger.info( + "Suppressing take-profit for %s — active buy signal (confidence=%.3f)", + trigger.ticker, + float(active_buy["confidence"]), + ) + continue + except Exception: + pass # On error, proceed with the take-profit + await self._submit_sell_order( trigger.ticker, pos_match.quantity,