fix: correct P&L calculation in ton_trading_record_trade (issue #129)#130
Merged
xlabtg merged 3 commits intoxlabtg:mainfrom Apr 9, 2026
Merged
fix: correct P&L calculation in ton_trading_record_trade (issue #129)#130xlabtg 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#129
…g#129) The `exit_price_usd` parameter was semantically inconsistent with `entry_price_usd`. Both represent the USD price of `from_asset`, so the exit USD value must be calculated as `amount_in × exit_price_usd`, not `amount_out × exit_price_usd`. The old formula (`amount_out × exit_price_usd`) double-counted the price when closing a TON→USDT position: it multiplied USDT received by the TON exit price, producing wildly incorrect values like +$16.24 / +13,310% instead of the correct +$0.09 / +0.73%. Fixed formula: usdIn = amount_in × entry_price_usd usdOut = amount_in × exit_price_usd pnl = usdOut − usdIn Also updated `exit_price_usd` parameter description to clarify it refers to the from_asset (the asset that was sold), and added two regression tests covering the exact positions from the issue report. 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:
Total: (61.9K + 2.3M cached) input tokens, 23.3K output tokens, $1.277680 cost 🤖 Models used:
📎 Log file uploaded as Gist (1465KB)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 1509cd2.
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.
Root Cause
The
exit_price_usdparameter inton_trading_record_tradewas documented as "USD price of to_asset at exit", butentry_price_usdis "USD price of from_asset at entry". This asymmetry caused a double-counting bug when closing TON→USDT positions.For a trade like 10 TON → USDT at entry $1.240, exit $1.249:
amount_out=12.49(USDT received) andexit_price_usd=1.249(TON exit price, from market data)usdOut = amount_out × exit_price_usd = 12.49 × 1.249 = 15.60❌usdOut = amount_in × exit_price_usd = 10 × 1.249 = 12.49✅Fix
Changed
exit_price_usdsemantics to consistently refer to the from_asset price (same asentry_price_usd):This matches the formula requested in the issue:
Reproducing Tests Added
Two regression tests for the exact positions from the issue report:
All 108 tests pass.
Parameter Description Updated
exit_price_usdnow reads:Closes #129