Trade memory: what is stored and what is retrieved
An LLM without history repeats its own mistakes with fresh confidence. This article describes the storage, the retrieval rules, and where the design is still thin.
Why there is memory at all
The first version of the engine (December 2025) was a stateless prompt pipeline: every cycle computed indicators, formatted them and asked the model for a decision. A position opened in one cycle was unknown to the next one, because nothing recorded why it had been opened. The first fix was a local JSON state file; it was fragile and lost data when the process stopped mid-write.
State fixed continuity but not learning. Two files handle that now: SQLite (trade_history.db) is the record of what happened, and ChromaDB is what can be searched by similarity. The rest of this article is about the second one.
What is stored
Closed-trade experiences are embedded locally with BAAI/bge-base-en-v1.5 — 768 dimensions, no embedding API call, no trade data leaving the machine. Three ChromaDB collections hold the memory:
trading_experiences— closed trades: the market context, the decision, the outcome and the P&L;semantic_rules— rules the reflection pass has written from results, in plain language;system_constraints_rejections— signals that were refused, with the reason.
Confidence statistics are not a fourth collection. They are computed on demand from the same embeddings (compute_confidence_stats, src/trading/vector_memory_analytics.py), so there is one source of truth for "how is this going?" and it cannot disagree with the stored trades.
What is retrieved before a decision
The brain assembles a bounded context for every cycle (src/trading/brain_context.py):
- 3 most similar past trades — over-fetched, then re-scored 70% similarity / 30% recency, each with a match breakdown;
- win rate and average P&L over the 20 nearest trades in the current market context;
- 3 learned rules that match the current regime (trend, ADX, volatility label);
- up to 5 recent post-mortems written by the model after closed trades.
That is the whole memory block — nothing else is injected, and the prompt budget silently drops whole lower-priority sections when the context would overflow ([prompt_budget]). When the sample is too thin, the prompt says so in capitals: "⚠️ LIMITED DATA: treat these as ANECDOTES, not as an established pattern". With a dozen closed trades, most retrievals are in that state, and it would be dishonest to present them otherwise.
The surprise ratio
Not every outcome is equally informative. A trade that won because of a random headline teaches the wrong lesson about the setup. Each closed trade therefore gets a surprise score:
Expected is the move to take-profit for that position, not a forecast of the exit price. The max(…, 0.01) floor keeps a near-zero target from turning the ratio into a division by zero. A ratio near zero means the trade did what the thesis said; a high ratio means it did not — and that can be a lucky win as easily as an unlucky loss. A group average above 1.5 puts a ⚠️ high surprise annotation on the rule the reflection pass writes, so the model discounts the lesson. Code: src/trading/brain_experience.py, src/trading/brain_reflection.py.
Rules, not just examples
Reflection runs every N closed trades, where N follows the timeframe: 5 on the 4-hour chart, 3 on daily, 7 on intraday and 10 on scalping (src/trading/brain.py). Loss rules are grouped by direction and regime only; the exit reason and the execution profile stay as labels, never as the group, because a group defined by its exit selects trades by the outcome the rule then claims to explain. A group needs at least five losses and a win rate below the closed-trade baseline, and only a one-sided exact binomial test (α = 0.05, with a baseline of at least ten closed trades) tags it anti_pattern ([⚠️ AVOID]); otherwise it is stored as corrective, or dropped when it does not underperform at all. Best-practice rules need five wins, three on the same pattern and a 60% win rate. Every rule prints its sample size and the baseline it was compared against, so a thin sample is visible where the model reads it (src/trading/brain_reflection.py, src/trading/brain_patterns.py).
Rules are not deleted by age; they decay. Freshness is an exponential score with a half-life of round(timeframe_minutes / 17) days, capped at 30 — about 14 days on the 4-hour chart — and past four half-lives a rule is labelled legacy and keeps losing score, but no code path expires it. On top of age, retrieval scores each rule on evidence (sample size, validations) and subtracts a contradiction penalty, so a rule that keeps being wrong falls faster than it ages (src/trading/vector_memory_rules.py). A rule is retired explicitly, through deactivate_semantic_rule(), which sets active: false, keeps the text and records the reason. The first rule retired that way was an anti_pattern whose group was keyed on the exit reason: it read "LONG in BULLISH, 0 wins, 0% win rate" while the same history held a +4.07% take-profit, because no winning trade could survive the filter. The final ordering is done in the prompt assembler (src/trading/brain_context.py), which marks anti-patterns [⚠️ AVOID] and correctives [⚡ IMPROVE].
The learned thresholds are bounded: the brain can raise the entry R/R floor, never lower it below the configured min_rr_entry (src/trading/rr_policy.py). A model that writes its own rules should not be able to talk itself into worse risk.
What this does not prove
- The sample is small. The ledger window holds a handful of closed trades — the results page counts them — so retrieval is closer to anecdote lookup than to a track record, and the prompt labels it that way.
- Similarity is not equivalence. Two setups that are close in embedding space are close in the text used to describe them; that does not make them the same market situation.
- Rules are advice, not code. The model can read a learned rule and ignore it. Only guards and the R/R policy are enforced.
- No controlled comparison. There is no experiment here showing that memory improves results; the memory exists because the stateless version could not explain its own decisions.
The dashboard exposes the memory side of every decision: which trades were retrieved, which rules matched, which signals were refused.
Live dashboard ↗Next: order execution →