Skip to content

fix: correct P&L calculation in ton_trading_record_trade (issue #129)#130

Merged
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-129-bdc522ef04f6
Apr 9, 2026
Merged

fix: correct P&L calculation in ton_trading_record_trade (issue #129)#130
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-129-bdc522ef04f6

Conversation

@konard
Copy link
Copy Markdown

@konard konard commented Apr 8, 2026

Root Cause

The exit_price_usd parameter in ton_trading_record_trade was documented as "USD price of to_asset at exit", but entry_price_usd is "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:

  • The user passes amount_out=12.49 (USDT received) and exit_price_usd=1.249 (TON exit price, from market data)
  • Old formula: usdOut = amount_out × exit_price_usd = 12.49 × 1.249 = 15.60
  • New formula: usdOut = amount_in × exit_price_usd = 10 × 1.249 = 12.49

Fix

Changed exit_price_usd semantics to consistently refer to the from_asset price (same as entry_price_usd):

usdIn  = amount_in × entry_price_usd
usdOut = amount_in × exit_price_usd
pnl    = usdOut − usdIn

This matches the formula requested in the issue:

pnl_usd = (amount_in × exit_price_usd) - (amount_in × entry_price_usd)
pnl_pct = ((exit_price - entry_price) / entry_price) × 100

Reproducing Tests Added

Two regression tests for the exact positions from the issue report:

Position amount_in entry_price exit_price Expected P&L Old (buggy)
#28 10 TON $1.240 $1.249 +$0.09 / +0.73% +$3.20 / +25.8%
#29 50 TON $1.252 $1.251 −$0.05 / −0.08% +$15.65 / +25.0%

All 108 tests pass.

Parameter Description Updated

exit_price_usd now reads:

"USD price of from_asset at trade exit. Must match the same asset as entry_price_usd (e.g. if you sold TON, provide TON's USD price at the time of closing)."

Closes #129

konard and others added 2 commits April 8, 2026 22:58
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>
@konard konard changed the title [WIP] [BUG] Incorrect P&L calculation in TON Trading Bot report fix: correct P&L calculation in ton_trading_record_trade (issue #129) Apr 8, 2026
@konard konard marked this pull request as ready for review April 8, 2026 23:04
@konard
Copy link
Copy Markdown
Author

konard commented Apr 8, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $1.277680
  • Calculated by Anthropic: $1.277680 USD
  • Difference: $0.000000 (0.00%)

📊 Context and tokens usage:

  • Context window: 71.9K / 1M (7%) input tokens, 23.3K / 64K (36%) output tokens

Total: (61.9K + 2.3M cached) input tokens, 23.3K output tokens, $1.277680 cost

🤖 Models used:

  • Tool: Anthropic Claude Code
  • Requested: sonnet
  • Model: Claude Sonnet 4.6 (claude-sonnet-4-6)

📎 Log file uploaded as Gist (1465KB)


Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Copy Markdown
Author

konard commented Apr 8, 2026

✅ Ready to merge

This pull request is now ready to be merged:

  • All CI checks have passed
  • No merge conflicts
  • No pending changes

Monitored by hive-mind with --auto-restart-until-mergeable flag

@xlabtg xlabtg merged commit 421c734 into xlabtg:main Apr 9, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Incorrect P&L calculation in TON Trading Bot report

2 participants