Commit ·
b64ba5a
1
Parent(s): 99a1635
fix(signals): anchor entry to live price, rescale stop/target
Browse files228 unmodified lines22923023123223323423523623723823924024124224324424522 unmodified lines268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337228 unmodified linessignal: TradingSignal,raw_output: str,) -> TradingSignal:"""Swap out a parsed signal whose entry is wildly off the live price.Qwen3-14B occasionally invents prices at synthesis time that happento satisfy the BUY/SELL inequalities — e.g. returning a \$10.00entry for NVDA on a day it traded at \$215. Those pass the parserbut mislead the user. We fetch the live price via:func:`get_price_change` and, if the parsed entry differs by morethan 30 %, replace the whole signal with the deterministicsynthesis from :meth:`_synthesize_from_tools`. If the entry is inrange we also back-fill any missing stop/target so HOLD cardsrender with real numbers instead of N/A."""parsed_entry = signal.entry_price22 unmodified linesif live_entry is None or live_entry <= 0:return self._backfill_missing_prices(signal)# If the parsed entry is missing or drifts by more than 30 %,# replace with the deterministic synthesis.drifted = (parsed_entry is Noneor parsed_entry <= 0or abs(parsed_entry - live_entry) / live_entry > 0.30)if drifted:synthesized = self._synthesize_from_tools(ticker, raw_output)if synthesized is not None:return synthesized# If synthesis failed too, at least swap the entry.return TradingSignal(ticker=signal.ticker,action=signal.action,confidence=signal.confidence,entry_price=live_entry,stop_loss=signal.stop_loss,target_price=signal.target_price,reasoning=signal.reasoning,)# Parsed entry is close to live price — keep the LLM signal and# back-fill any missing stop/target fields, plus swap out any# stop/target that are wildly off (more than 20 % from entry).# Small LLMs occasionally emit numbers that are credible-looking# on their own but nonsensical for the ticker (e.g. BTC-USD# at \$80 782 with a \$79 stop-loss — the model dropped the "k").entry = signal.entry_pricestop = signal.stop_losstarget = signal.target_pricedef _far_from_entry(v: Optional[float]) -> bool:if v is None or v <= 0:return Truereturn abs(v - entry) / entry > 0.20def _too_close_to_entry(v: Optional[float]) -> bool:"""True if a stop/target is within 0.5 % of entry — effectively adegenerate zero-risk / zero-reward number that the LLM sometimesemits when it conflates HOLD with 'no action'."""if v is None or v <= 0:return Truereturn abs(v - entry) / entry < 0.005if _far_from_entry(stop) or _too_close_to_entry(stop):stop = None # trigger back-fill belowif _far_from_entry(target) or _too_close_to_entry(target):target = Noneif stop is None or target is None:return self._backfill_missing_prices(TradingSignal(ticker=signal.ticker,action=signal.action,confidence=signal.confidence,entry_price=entry,stop_loss=stop,target_price=target,reasoning=signal.reasoning,))return signal@staticmethoddef _backfill_missing_prices(signal: TradingSignal) -> TradingSignal:228 unmodified lines22923023123223323423523623723823924024124224324424524624724824925025125225325425525622 unmodified lines279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352228 unmodified linessignal: TradingSignal,raw_output: str,) -> TradingSignal:r"""Anchor every signal's entry price to the live quote.Small LLMs frequently emit training-era prices (Qwen3-14B oncehanded back \$203 for NVDA when it was trading at \$215) thatpass the parser because the BUY/SELL inequalities still holdrelative to the invented entry. The card then shows a staleprice — useless for a trading signal.The fix is absolute: we fetch the current quote via:func:`get_price_change`, force ``entry_price`` to that value,and **rescale** stop / target by the same ratio so the LLM'srisk / reward geometry is preserved. Example: LLM emitsentry=\$200, stop=\$194, target=\$210 for an NVDA card whilethe live price is \$215 — we scale 1.075× so the card rendersentry=\$215, stop=\$208.52, target=\$225.75. The narrativereasoning is kept as-is.When the parsed entry is missing, zero, or more than 50 % offthe live price we fall back to the deterministic tool-groundedsynthesis in :meth:`_synthesize_from_tools`, which picks aBUY / SELL / HOLD from the day's % change and derives bandsfrom scratch."""parsed_entry = signal.entry_price22 unmodified linesif live_entry is None or live_entry <= 0:return self._backfill_missing_prices(signal)# Parsed entry is missing or grossly off (>50 %): the model# probably fabricated the entire signal, so re-synthesise from# tools. Below 50 % drift we keep the LLM's narrative but# rescale the numbers.badly_drifted = (parsed_entry is Noneor parsed_entry <= 0or abs(parsed_entry - live_entry) / live_entry > 0.50)if badly_drifted:synthesized = self._synthesize_from_tools(ticker, raw_output)if synthesized is not None:return synthesized# If synthesis failed too, at least swap the entry.return self._backfill_missing_prices(TradingSignal(ticker=signal.ticker,action=signal.action,confidence=signal.confidence,entry_price=live_entry,stop_loss=None,target_price=None,reasoning=signal.reasoning,))# Anchor to live price and rescale stop / target to preserve# the LLM's risk-reward geometry. If either side is missing or# degenerate, let the back-fill derive a fresh band.scale = live_entry / parsed_entrystop = signal.stop_losstarget = signal.target_pricedef _bad(v: Optional[float]) -> bool:"""Treat very-close-to-entry (< 0.5 %) stops / targets asdegenerate zero-risk numbers the model sometimes emits onHOLD calls."""if v is None or v <= 0:return Truereturn abs(v - parsed_entry) / parsed_entry < 0.005new_stop = None if _bad(stop) else round(stop * scale, 2)new_target = None if _bad(target) else round(target * scale, 2)# Extra guard: after rescaling, stop / target should still be# within a reasonable band of the new entry (tight stop < 10 %,# target < 15 %). If the LLM emitted something implausibly wide# we let the back-fill replace it.def _unreasonable(v: Optional[float]) -> bool:if v is None:return Truereturn abs(v - live_entry) / live_entry > 0.15if _unreasonable(new_stop):new_stop = Noneif _unreasonable(new_target):new_target = Nonerescaled = TradingSignal(ticker=signal.ticker,action=signal.action,confidence=signal.confidence,entry_price=round(live_entry, 2),stop_loss=new_stop,target_price=new_target,reasoning=signal.reasoning,)return self._backfill_missing_prices(rescaled)@staticmethoddef _backfill_missing_prices(signal: TradingSignal) -> TradingSignal:
crew/crew.py+72-57