Fix: Critical Position Manager monitoring issues

**Root Cause:** Position Manager didn't detect when on-chain TP/SL orders closed positions externally, causing endless error loops and stale position data.

**Issues Fixed:**
1. Position Manager now checks if on-chain position still exists before attempting to close
2. Detects external closures (by on-chain orders) and updates database accordingly
3. Determines likely exit reason based on price vs TP/SL levels
4. Automatically cancels leftover orders when position detected as closed
5. Analytics now properly shows stopped-out trades

**Technical Changes:**
- Added position existence check at start of checkTradeConditions()
- Calls DriftService.getPosition() to verify on-chain state
- Updates database with exitPrice, exitReason, realizedPnL when external closure detected
- Removes trade from monitoring after external closure
- Handles size mismatches for partial closes (TP1 hit externally)

**Database Fix:**
- Manually closed orphaned trade (stopped out 9 hours ago but still marked 'open')
- Calculated and set realizedPnL = -$12.00 for stopped-out SHORT position
- Analytics now shows 3 total trades instead of missing the SL exit

**Testing:**
- Bot starts cleanly with no error loops
- Position monitoring active with 0 trades (as expected)
- Analytics correctly shows stopped-out trade in statistics
This commit is contained in:
mindesbunister
2025-10-28 07:51:40 +01:00
parent a72ddd8f0e
commit 19f5b7ab14
2 changed files with 84 additions and 5 deletions

8
.env
View File

@@ -61,7 +61,7 @@ PYTH_HERMES_URL=https://hermes.pyth.network
# Position sizing
# Base position size in USD (default: 50 for safe testing)
# Example: 50 with 10x leverage = $500 notional position
MAX_POSITION_SIZE_USD=80
MAX_POSITION_SIZE_USD=78
# Leverage multiplier (1-20, default: 10)
# Higher leverage = bigger gains AND bigger losses
@@ -70,7 +70,7 @@ LEVERAGE=10
# Risk parameters (as percentages)
# Stop Loss: Close 100% of position when price drops this much
# Example: -1.5% on 10x = -15% account loss
STOP_LOSS_PERCENT=-1.5
STOP_LOSS_PERCENT=-1.1
# ================================
# DUAL STOP SYSTEM (Advanced)
@@ -93,7 +93,7 @@ HARD_STOP_PERCENT=-2.5
# Take Profit 1: Close 50% of position at this profit level
# Example: +0.7% on 10x = +7% account gain
TAKE_PROFIT_1_PERCENT=0.7
TAKE_PROFIT_1_PERCENT=0.4
# Take Profit 1 Size: What % of position to close at TP1
# Example: 50 = close 50% of position
@@ -101,7 +101,7 @@ TAKE_PROFIT_1_SIZE_PERCENT=75
# Take Profit 2: Close remaining 50% at this profit level
# Example: +1.5% on 10x = +15% account gain
TAKE_PROFIT_2_PERCENT=1.1
TAKE_PROFIT_2_PERCENT=0.7
# Take Profit 2 Size: What % of remaining position to close at TP2
# Example: 100 = close all remaining position