Commit ·
eb4ef03
1
Parent(s): 1d4dd5c
Add deterministic sanity-fix for Strategist's runaway prices
Browse filesThe 14B Strategist occasionally emits wild numbers at synthesis time
(e.g. a $50 000 target on a $290 stock, or stop on the wrong side of
entry). Rather than surface those to the user, the parser now runs
_sanity_fix_prices after extracting the fields: if a stop/target is
more than 20 percent away from the entry or sits on the wrong side of
entry for the BUY/SELL direction, replace it with a conservative
default derived arithmetically from the entry (±3 percent for stop,
±5 percent for target). Valid prices pass through untouched.
49 unmodified lines5051525354553 unmodified lines59606162636465666749 unmodified lines"""Parse raw output into a TradingSignal.Attempts primary pattern first, falls back to heuristic extraction.Args:raw_output: Raw text from Chief Strategist agent3 unmodified linesTradingSignal if parsing succeeds, None if output is unparseable"""signal = self._parse_primary(raw_output, ticker)if signal is not None:return signalreturn self._parse_fallback(raw_output, ticker)def _parse_primary(self, raw_output: str, ticker: str) -> Optional[TradingSignal]:"""Attempt to parse using the primary structured format."""49 unmodified lines5051525354555657583 unmodified lines626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614749 unmodified lines"""Parse raw output into a TradingSignal.Attempts primary pattern first, falls back to heuristic extraction.After parsing, prices are sanity-checked so the Strategist can'tship obviously-wrong numbers (e.g. $50 000 target on a $290 stock,or stop-loss on the wrong side of entry for the given action).Args:raw_output: Raw text from Chief Strategist agent3 unmodified linesTradingSignal if parsing succeeds, None if output is unparseable"""signal = self._parse_primary(raw_output, ticker)if signal is None:signal = self._parse_fallback(raw_output, ticker)if signal is None:return Nonereturn self._sanity_fix_prices(signal)@staticmethoddef _sanity_fix_prices(signal: TradingSignal) -> TradingSignal:"""Clamp runaway prices and enforce BUY/SELL inequality semantics.Small LLMs occasionally emit obvious absurdities at synthesis time(e.g. target that is 100× entry, or stop_loss on the wrong sideof entry for a BUY). Rather than surface nonsense to the user,detect these and replace with conservative defaults derived fromthe entry price:* For BUY: stop = entry × 0.97, target = entry × 1.05* For SELL: stop = entry × 1.03, target = entry × 0.95* For HOLD: defaults left as-is.We only override a field when it fails a plausibility test(>20 % away from entry, or on the wrong side of entry for thecurrent action). Valid prices from the model are preserved."""entry = signal.entry_price# No entry price → nothing to clamp against.if entry is None or entry <= 0:return signalstop = signal.stop_losstarget = signal.target_priceaction = signal.actiondef _out_of_band(value: Optional[float]) -> bool:"""True if ``value`` is missing, non-positive, or >20 % from entry."""if value is None or value <= 0:return Truereturn abs(value - entry) / entry > 0.20def _wrong_side_stop(value: Optional[float]) -> bool:"""Stop on the wrong side of entry for the current action."""if value is None:return Falseif action == Action.BUY and value >= entry:return Trueif action == Action.SELL and value <= entry:return Truereturn Falsedef _wrong_side_target(value: Optional[float]) -> bool:"""Target on the wrong side of entry for the current action."""if value is None:return Falseif action == Action.BUY and value <= entry:return Trueif action == Action.SELL and value >= entry:return Truereturn Falseif action == Action.BUY:if _out_of_band(stop) or _wrong_side_stop(stop):stop = round(entry * 0.97, 2)if _out_of_band(target) or _wrong_side_target(target):target = round(entry * 1.05, 2)elif action == Action.SELL:if _out_of_band(stop) or _wrong_side_stop(stop):stop = round(entry * 1.03, 2)if _out_of_band(target) or _wrong_side_target(target):target = round(entry * 0.95, 2)# HOLD: leave as-is; the Strategist can describe a range.return TradingSignal(ticker=signal.ticker,action=signal.action,confidence=signal.confidence,entry_price=entry,stop_loss=stop,target_price=target,reasoning=signal.reasoning,)def _parse_primary(self, raw_output: str, ticker: str) -> Optional[TradingSignal]:"""Attempt to parse using the primary structured format."""
crew/signals.py+82-2