Skip to content

fix(ton-trading-bot): use exit price for simulation balance P&L conversion (issue #133)#134

Merged
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-133-0c8646b244f5
Apr 11, 2026
Merged

fix(ton-trading-bot): use exit price for simulation balance P&L conversion (issue #133)#134
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-133-0c8646b244f5

Conversation

@konard
Copy link
Copy Markdown

@konard konard commented Apr 11, 2026

Summary

Fixes the simulation balance P&L calculation in ton_trading_record_trade where 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 to entry_price_usd when 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.

- const entryTonPriceUsd = entry.entry_price_usd ?? null;
- const creditTon = entryTonPriceUsd != null
-   ? entry.amount_in + pnl / entryTonPriceUsd   // ← used entry price (wrong)
+ const exitTonPriceUsd = exit_price_usd ?? entry.entry_price_usd ?? null;
+ const creditTon = exitTonPriceUsd != null
+   ? entry.amount_in + pnl / exitTonPriceUsd    // ← uses exit price (correct)
    : amount_out;

Verification (issue #133 example):

Formula Result
Buggy (entry $1.26) 15 + $1.995 / $1.26 +1.583 TON (over-credited)
Fixed (exit $1.393) 15 + $1.995 / $1.393 +1.432 TON ≈ ~1.43 TON

Test plan

  • New regression test: 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 price
  • Updated 2 existing balance-credit tests whose expected values were based on the wrong (entry-price) formula
  • All 268 tests pass locally (npm test)

Closes #133

🤖 Generated with Claude Code

konard and others added 2 commits April 11, 2026 18:14
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>
@konard konard changed the title [WIP] Trading Plugin P&L Calculation Error fix(ton-trading-bot): use exit price for simulation balance P&L conversion (issue #133) Apr 11, 2026
@konard konard marked this pull request as ready for review April 11, 2026 18:28
@konard
Copy link
Copy Markdown
Author

konard commented Apr 11, 2026

🤖 Solution Draft Log

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

💰 Cost: $3.463368

📊 Context and tokens usage:

  • Context window: 124.5K / 1M (12%) input tokens, 49.5K / 64K (77%) output tokens

Total: (130.2K + 7.4M cached) input tokens, 49.5K output tokens, $3.463368 cost

🤖 Models used:

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

📎 Log file uploaded as Gist (2515KB)


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 11, 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 dc22d0e into xlabtg:main Apr 11, 2026
8 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.

Trading Plugin P&L Calculation Error

2 participants