fix(ton-trading-bot): use exit price for simulation balance P&L conversion (issue #133)#134
Merged
xlabtg merged 3 commits intoxlabtg:mainfrom Apr 11, 2026
Merged
Conversation
Adding .gitkeep for PR creation (default mode). This file will be removed when the task is complete. Issue: xlabtg#133
…labtg#133) The simulation balance credit-back in ton_trading_record_trade was dividing the USD P&L by the entry price (USD/TON at trade open) to convert it back to TON, instead of using the exit price (current market rate at trade close). When TON appreciates significantly (e.g. +10.6% from $1.26 → $1.393), this caused the simulation balance to be over-credited: - Buggy (entry price $1.26): pnl $1.995 / $1.26 = +1.583 TON profit - Correct (exit price $1.393): pnl $1.995 / $1.393 = +1.432 TON profit ✓ The fix: use exit_price_usd (falling back to entry_price_usd when not provided) so the USD profit is converted to TON at the current exchange rate. Includes a new regression test for the exact scenario from issue xlabtg#133 (15 TON trade, $1.26 → $1.393) and updates two existing tests whose expected balance values were based on the wrong (entry-price) formula. 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: $3.463368📊 Context and tokens usage:
Total: (130.2K + 7.4M cached) input tokens, 49.5K output tokens, $3.463368 cost 🤖 Models used:
📎 Log file uploaded as Gist (2515KB)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 cf7f58a.
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.
Summary
Fixes the simulation balance P&L calculation in
ton_trading_record_tradewhere profit was being incorrectly reported for TON→USDT trades with significant price appreciation.Root cause: When crediting back the simulation balance on trade close, the USD P&L was divided by the entry price (USD/TON at trade open) instead of the exit price (current USD/TON rate). For a +10.6% TON price move ($1.26→$1.393), this over-credits the balance by 10.6%, yielding ~1.583 TON profit when the correct value is ~1.432 TON.
Fix: Use
exit_price_usd(falling back toentry_price_usdwhen not provided) to convert USD P&L back to TON units. This correctly reflects how much TON the USD profit can buy at the current market rate.Verification (issue #133 example):
15 + $1.995 / $1.2615 + $1.995 / $1.393Test plan
credits back correct profit for large price move (issue #133): uses exit_price not entry_price— reproduces the exact scenario from the issue (15 TON, $1.26→$1.393) and asserts the balance credit uses exit pricenpm test)Closes #133
🤖 Generated with Claude Code