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
This commit is contained in:
mindesbunister
2025-10-26 13:30:07 +01:00
parent 993ae64c64
commit 4cc294baef
6 changed files with 365 additions and 17 deletions

107
EXIT_ORDERS_TEST_RESULTS.md Normal file
View File

@@ -0,0 +1,107 @@
# 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](https://solscan.io/tx/2Bio8oUhhNXkYxY9g5RsR3KUpb3mCX3ZWrQoqFTZ9DQY7rq5w7reCwu8qyHEq1cZdBK5TRo7n9qhC9nj7HtUvWKG)
**TP1 Order (75% at +0.5%):**
```
4G8rXJ5vhLZAhNaNJ46qwbDoMEhxab7kTibaQtrBHuSkzmd6FPtLyELbt7Lc8CpTLMtN1ut9sjz9F9o1FhRhgzLU
```
Target: $198.4825
[View on Solscan](https://solscan.io/tx/4G8rXJ5vhLZAhNaNJ46qwbDoMEhxab7kTibaQtrBHuSkzmd6FPtLyELbt7Lc8CpTLMtN1ut9sjz9F9o1FhRhgzLU)
**TP2 Order (100% at +2.5%):**
```
5Zo56K8ZLkz3uEVVuQUakZQ3uMCQSXrkWG1EwtxSZVQB2pxQwKp2gbPUGEDyPZobyBv4TYMEBGf5kBpLWfCPYMEr
```
Target: $202.4324
[View on Solscan](https://solscan.io/tx/5Zo56K8ZLkz3uEVVuQUakZQ3uMCQSXrkWG1EwtxSZVQB2pxQwKp2gbPUGEDyPZobyBv4TYMEBGf5kBpLWfCPYMEr)
**Stop Loss Order (at -0.9%):**
```
5U9ZxYFyD99j8MXcthqqjy6DjACqedEWfidWsCb69RtQfSe7iBYvRWrFJVJ5PGe2nJYtvRrMo2szuDCD8ztBrebs
```
Target: $195.7175
[View on Solscan](https://solscan.io/tx/5U9ZxYFyD99j8MXcthqqjy6DjACqedEWfidWsCb69RtQfSe7iBYvRWrFJVJ5PGe2nJYtvRrMo2szuDCD8ztBrebs)
## Verification
### Check on Drift UI
Visit [https://app.drift.trade/](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
```bash
cd /home/icke/traderv4
./test-exit-orders.sh
```
## Monitoring
```bash
# 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.