fix(ton-trading-bot): restore simulation balance on trade close (issue #96)#97
Merged
xlabtg merged 3 commits intoxlabtg:mainfrom Apr 5, 2026
Merged
fix(ton-trading-bot): restore simulation balance on trade close (issue #96)#97xlabtg merged 3 commits intoxlabtg:mainfrom
xlabtg merged 3 commits intoxlabtg:mainfrom
Conversation
Adding .gitkeep for PR creation (default mode). This file will be removed when the task is complete. Issue: xlabtg#96
…xlabtg#96) When a simulation trade opened with TON was closed via ton_trading_record_trade, the virtual balance was never credited back. The deduction happened at open (amount_in subtracted) but the matching credit was silently skipped because the guard `entry.to_asset === "TON"` never matched TON→USDT trades (the common case). Fix: switch the guard to `entry.from_asset === "TON"` and credit back `amount_in + pnl_in_ton`. When USD prices are available the PnL is converted to TON via the entry price; when no prices are stored (same- currency TON→TON trade) amount_out is credited directly as before. Also add two new simulation management tools requested in the issue: - ton_trading_reset_simulation_balance — reset balance to a starting amount for a fresh paper-trading session - ton_trading_set_simulation_balance — manually set the balance to align the simulation with a real portfolio Tests: 104 pass (4 new regression tests for the bug fix, 4 for new tools). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Author
🤖 Solution Draft LogThis log file contains the complete execution trace of the AI solution draft process. 💰 Cost estimation:
📊 Context and tokens usage:Claude Sonnet 4.6:
Total input tokens: 69.2K + 2.3M cached Claude Haiku 4.5:
Total input tokens: 70.8K + 472.2K cached 🤖 Models used:
📎 Log file uploaded as Gist (1488KB)Now working session is ended, feel free to review and add any feedback on the solution draft. |
Author
✅ Ready to mergeThis pull request is now ready to be merged:
Monitored by hive-mind with --auto-restart-until-mergeable flag |
This reverts commit 2a5d6b9.
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.
Problem
The simulation balance in `ton_trading_simulate_trade` is decremented when a trade is opened (`amount_in` is subtracted from the virtual balance), but was never incremented when the trade was closed via `ton_trading_record_trade`. This caused the virtual balance to drain to zero regardless of trading performance.
Root cause: The credit-back guard in `ton_trading_record_trade` checked `entry.to_asset === "TON"`, which only matched TON→TON round-trips. For the common case of TON→USDT trades, `to_asset` is USDT so the credit was silently skipped.
Fix
Changed the guard from `entry.to_asset === "TON"` to `entry.from_asset === "TON"` (TON was what was deducted at open) and updated the credit calculation to return principal + PnL in TON:
New tools (feature requests from issue)
How to reproduce the bug (before fix)
```
```
Tests
4 new regression tests for the balance credit-back fix:
4 new tests for the two new tools.
All 104 tests pass locally (`node --test plugins/ton-trading-bot/tests/index.test.js`).
Fixes #96