Files
trading_bot_v4/EXIT_ORDERS_TEST_RESULTS.md
mindesbunister 4cc294baef feat: Add on-chain TP/SL order placement
- Add placeExitOrders() to create reduce-only LIMIT orders for TP1, TP2, and SL
- Orders now visible in Drift UI
- Tested with real tiny position (0 base x 5x = 0)
- All 3 exit orders placed successfully on-chain
- Position manager continues monitoring as backup
- Added test script and results documentation
2025-10-26 13:30:07 +01:00

3.6 KiB
Raw Blame History

Exit Orders Test Results

Test Details

  • Date: October 26, 2025
  • Position Size: $10 base × 5x leverage = $50 notional
  • Symbol: SOL-PERP
  • Direction: LONG
  • Entry Price: $197.4950

Results: SUCCESS

Transaction Signatures

Entry Order:

2Bio8oUhhNXkYxY9g5RsR3KUpb3mCX3ZWrQoqFTZ9DQY7rq5w7reCwu8qyHEq1cZdBK5TRo7n9qhC9nj7HtUvWKG

View on Solscan

TP1 Order (75% at +0.5%):

4G8rXJ5vhLZAhNaNJ46qwbDoMEhxab7kTibaQtrBHuSkzmd6FPtLyELbt7Lc8CpTLMtN1ut9sjz9F9o1FhRhgzLU

Target: $198.4825
View on Solscan

TP2 Order (100% at +2.5%):

5Zo56K8ZLkz3uEVVuQUakZQ3uMCQSXrkWG1EwtxSZVQB2pxQwKp2gbPUGEDyPZobyBv4TYMEBGf5kBpLWfCPYMEr

Target: $202.4324
View on Solscan

Stop Loss Order (at -0.9%):

5U9ZxYFyD99j8MXcthqqjy6DjACqedEWfidWsCb69RtQfSe7iBYvRWrFJVJ5PGe2nJYtvRrMo2szuDCD8ztBrebs

Target: $195.7175
View on Solscan

Verification

Check on Drift UI

Visit https://app.drift.trade/ and connect with wallet:

3dG7wayp7b9NBMo92D2qL2sy1curSC4TTmskFpaGDrtA

You should see:

  1. Active SOL-PERP position (~0.2532 SOL)
  2. Three open orders (TP1, TP2, SL) showing as "Limit" orders with "Reduce Only" flag
  3. Orders should be visible in the "Orders" tab

Implementation Details

What Changed

  1. lib/drift/orders.ts: Added placeExitOrders() function that creates reduce-only LIMIT orders
  2. app/api/trading/execute/route.ts: Calls placeExitOrders() after opening position
  3. config/trading.ts: Added takeProfit1SizePercent and takeProfit2SizePercent config fields

Order Types Used

  • Entry: Market order (immediate execution)
  • Exit orders: Reduce-only LIMIT orders
    • Reduce-only = can only close position, not increase it
    • LIMIT = visible in order book, executes when price reached
    • Alternative: Trigger-market orders (more guaranteed execution but may slip)

Position Manager

The bot still runs the position manager which:

  • Monitors price in real-time via Pyth
  • Will also close positions via market orders when targets hit
  • Acts as a backup/fallback if LIMIT orders don't fill

Risk Assessment

Safe for production with following considerations:

  1. LIMIT order risk: May not fill if market gaps past price (e.g., flash crash through stop loss)
  2. Solution: Position manager provides backup via market orders
  3. Recommendation: For more safety-critical stops, consider implementing trigger-market orders

Next Steps (Optional Enhancements)

  1. Add trigger-market for SL: More guaranteed execution on stop loss
  2. Order cancellation: Cancel old orders when position closes manually
  3. Multiple timeframes: Support different TP/SL per timeframe
  4. Dynamic sizing: Adjust TP sizes based on signal strength

Test Command

cd /home/icke/traderv4
./test-exit-orders.sh

Monitoring

# Check logs
docker logs trading-bot-v4 -f

# Check position manager status
curl http://localhost:3001/api/status

Status: TESTED AND WORKING

The implementation successfully places on-chain TP/SL orders that are visible in the Drift UI. All three exit orders were placed successfully in the live test with real funds.