Skip to content

fix(ton-trading-bot): restore simulation balance on trade close (issue #96)#97

Merged
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-96-2e05601cd414
Apr 5, 2026
Merged

fix(ton-trading-bot): restore simulation balance on trade close (issue #96)#97
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-96-2e05601cd414

Conversation

@konard
Copy link
Copy Markdown

@konard konard commented Apr 5, 2026

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:

  • When USD prices are available: `credit_ton = amount_in + pnl_usd / entry_price_usd`
  • When no prices are stored (same-currency TON→TON trade): `credit_ton = amount_out` (same behaviour as before)

New tools (feature requests from issue)

  • `ton_trading_reset_simulation_balance` — reset the virtual balance to a starting amount (defaults to `pluginConfig.simulationBalance`) for a fresh paper-trading session
  • `ton_trading_set_simulation_balance` — manually set the virtual balance to any amount to align with a real portfolio

How to reproduce the bug (before fix)

```

  1. Start with balance = 1000 TON
  2. simulate_trade(from_asset="TON", to_asset="USDT_ADDR", amount_in=100, ...) → balance 900
  3. record_trade(trade_id=1, amount_out=105, exit_price_usd=1) → balance stays 900 (bug)
  4. Expected: balance ≈ 1000 + pnl_ton
    ```

Tests

4 new regression tests for the balance credit-back fix:

  • credits principal + profit (TON→USDT with USD prices)
  • credits `amount_out` directly for same-currency TON→TON trades
  • does NOT credit for non-TON from_asset trades (USDT→TON)
  • correctly handles losing trades (principal - loss in TON)

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

konard and others added 2 commits April 5, 2026 08:47
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>
@konard konard changed the title [WIP] [Bug] Simulation balance not updated after closing trades (principal + PnL not returned) fix(ton-trading-bot): restore simulation balance on trade close (issue #96) Apr 5, 2026
@konard konard marked this pull request as ready for review April 5, 2026 08:53
@konard
Copy link
Copy Markdown
Author

konard commented Apr 5, 2026

🤖 Solution Draft Log

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

💰 Cost estimation:

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

📊 Context and tokens usage:

Claude Sonnet 4.6:

  • Max context window: 79.2K / 1M input tokens (8%)
  • Max output tokens: 16.0K / 64K output tokens (25%)

Total input tokens: 69.2K + 2.3M cached
Total output tokens: 16.0K output
Cost: $1.174467

Claude Haiku 4.5:

  • Context window: 542.9K / 200K tokens (271%)
  • Max output tokens: 5.3K / 64K output tokens (8%)

Total input tokens: 70.8K + 472.2K cached
Total output tokens: 5.3K output
Cost: $0.162317

🤖 Models used:

  • Tool: Anthropic Claude Code
  • Requested: sonnet
  • Main model: Claude Sonnet 4.6 (claude-sonnet-4-6)
  • Additional models:
    • Claude Haiku 4.5 (claude-haiku-4-5-20251001)

📎 Log file uploaded as Gist (1488KB)


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 5, 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 1afb3f7 into xlabtg:main Apr 5, 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] Simulation balance not updated after closing trades (principal + PnL not returned)

2 participants