Skip to content

feat(ton-trading-bot): add ton_trading_check_stop_loss to query stop_loss_rules table#66

Merged
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-58-144d35d35259
Mar 27, 2026
Merged

feat(ton-trading-bot): add ton_trading_check_stop_loss to query stop_loss_rules table#66
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-58-144d35d35259

Conversation

@konard
Copy link
Copy Markdown

@konard konard commented Mar 27, 2026

Root cause

The stop_loss_rules table was written to by ton_trading_set_stop_loss (tool 18) but never queried anywhere, so the stop-loss feature was entirely non-functional — rules were stored and silently ignored.

Solution

Add a new atomic tool ton_trading_check_stop_loss (tool 19) that reads the stop_loss_rules table and checks whether the current market price has triggered any active rule:

  • Queries stop_loss_rules WHERE status = 'active' (optionally filtered by trade_id)
  • Computes stop_loss_price = entry_price × (1 − stop_loss_percent / 100) and take_profit_price for each rule
  • Annotates each rule with stop_loss_hit / take_profit_hit boolean flags
  • Separates triggered_rules from safe_rules so the LLM has unambiguous guidance
  • Returns a plain-language note directing the LLM to close affected trades via ton_trading_record_trade

This follows the same read-and-annotate pattern used by ton_trading_get_scheduled_trades (tool 23).

Changes

File Change
plugins/ton-trading-bot/index.js Add ton_trading_check_stop_loss tool; renumber tools 19→21, 20→22, 21→23
plugins/ton-trading-bot/manifest.json Register new tool
plugins/ton-trading-bot/tests/index.test.js Update tool count (21→22); add 6 unit tests

Tests

All 182 tests pass (npm test). Plugin validates with 22 tool(s) validated (npm run validate).

New tests cover:

  • Required parameter (current_price)
  • Category is data-bearing
  • Price within limits → no triggered rules
  • Price below stop-loss threshold → stop_loss action flagged
  • Price above take-profit threshold → take_profit action flagged
  • trade_id filter routes query correctly
  • Empty table → zero active rules returned

Fixes #58

konard and others added 2 commits March 27, 2026 00:42
Adding .gitkeep for PR creation (default mode).
This file will be removed when the task is complete.

Issue: xlabtg#58
…stop_loss_rules table

The stop_loss_rules table was written to but never read, making the
stop-loss feature non-functional. Add ton_trading_check_stop_loss (tool
19) that queries active rules and flags any triggered by the current
market price, closing the loop between set_stop_loss and trade closure.

- Query active stop-loss/take-profit rules from the database
- Annotate each rule with stop_loss_hit / take_profit_hit flags
- Separate triggered rules from safe ones for clear LLM guidance
- Support optional trade_id filter to check a single position
- Update manifest.json and file header to register the new tool
- Update tool count assertion and add 6 targeted unit tests

Fixes xlabtg#58

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@konard konard changed the title [WIP] bug: stop_loss_rules table written but never queried feat(ton-trading-bot): add ton_trading_check_stop_loss to query stop_loss_rules table Mar 27, 2026
@konard konard marked this pull request as ready for review March 27, 2026 00:48
@konard
Copy link
Copy Markdown
Author

konard commented Mar 27, 2026

🤖 Solution Draft Log

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

💰 Cost estimation:

  • Public pricing estimate: $1.034871
  • Calculated by Anthropic: $1.014199 USD
  • Difference: $-0.020671 (-2.00%)

🤖 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 (1656KB)


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 Mar 27, 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 0048ee6 into xlabtg:main Mar 27, 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: stop_loss_rules table written but never queried

2 participants