fix(sim): two rules of one kind on one asset get a row each (#829) - #832
Merged
Merged
Conversation
edge_table and accumulation_table keyed rows "{rule.name}:{asset}", so a
second rule of the same kind on the same asset overwrote the first: the
deployment's $50/week BTC DCA sleeve disappeared behind a $5 one.
group_trades_by_class looked both rules up by the shared key, so G2's
class pool counted the survivor twice and the other rule not at all.
One key function, rule_keys, now serves all three: a unique key is
unchanged; colliding rules are told apart by rules.id ("dca#19:BTC"), or
by position when a hand-built rule has none.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #829.
What was wrong
report.edge_tableandreport.accumulation_table(#825) keyed each row"{rule.name}:{asset}". When two rules of the same kind sit on the same asset, the later rule's row overwrote the earlier one's.~/keel/keel.dbhas two paper BTC DCA rules, rule 19 at $50/week and rule 20 at $5/week.keel simulateprinted a single row, the $5 rule's, which looked like a ~$5/week DCA. The $50 sleeve was invisible.group_trades_by_classlooked both rules up by that shared key. G2's per-class pool counted the surviving rule's trades twice and the other rule's not at all. The test below shows a class pool of 0 trades where the pooled row has 1. The pooled row itself was always right, because it extends every rule's trades before keying.The fix
One function,
report.rule_keys(rules), now keys all three tables:"turtle_breakout:BTC"), so existing readers and tests are untouched.rules.id("dca#19:BTC","dca#20:BTC"), or by position when a hand-built rule has no id. No row is ever lost.ruleslist, whichsimulatealready does. The docstrings say so.Verification
tests/sim/test_rule_keys.pyhas 6 tests. Five failed first on the old code; the "unique key unchanged" test is a regression pin that passes throughout. They cover:Five mutants were each checked to have changed the source, and each was killed:
rule_idReal data, on a copy of the paper database over the 5-year window:
dca#19:BTCdca#20:BTCBefore the fix only the second row printed.
Full suite: 6775 passed, 3 skipped.
mypy,ruff checkandruff format --checkare clean.🤖 Generated with Claude Code