critical: Fix ghost detection P&L compounding - delete from Map BEFORE check

Bug: Multiple monitoring loops detect ghost simultaneously
- Loop 1: has(tradeId) → true → proceeds
- Loop 2: has(tradeId) → true → ALSO proceeds (race condition)
- Both send Telegram notifications with compounding P&L

Real incident (Dec 2, 2025):
- Manual SHORT at $138.84
- 23 duplicate notifications
- P&L compounded: -$47.96 → -$1,129.24 (23× accumulation)
- Database shows single trade with final compounded value

Fix: Map.delete() returns true if key existed, false if already removed
- Call delete() FIRST
- Check return value
 proceeds
- All other loops get false → skip immediately
- Atomic operation prevents race condition

Pattern: This is variant of Common Pitfalls #48, #49, #59, #60, #61
- All had "check then delete" pattern
- All vulnerable to async timing issues
- Solution: "delete then check" pattern
- Map.delete() is synchronous and atomic

Files changed:
- lib/trading/position-manager.ts lines 390-410

Related: DUPLICATE PREVENTED message was working but too late
This commit is contained in:
mindesbunister
2025-12-02 18:25:56 +01:00
parent d156abc976
commit 93dd950821
7 changed files with 893 additions and 6 deletions

View File

@@ -618,3 +618,472 @@ if (currentADX > entryADX + 5) {
Every enhancement above depends on fresh 1-minute data. The foundation is SOLID and PROVEN. Now we build the optimizations layer by layer, validating each with real money results.
**Next Step:** Phase 2 (Smart Entry Timing) when ready - highest impact, proven concept from institutional trading.
---
# Strategic Enhancement Options (Dec 2025 Research) 🚀
**Context:** After completing Phases 1, 2, 7.1-7.3, comprehensive research conducted on next-generation improvements beyond 1-minute data enhancements. Four strategic options identified with varying complexity, timeline, and ROI potential.
---
## Option A: Regime-Based Filter (Conservative Enhancement)
**Goal:** Add market regime detection to filter trades in unfavorable conditions
**Expected Impact:** +20-30% profitability improvement
**Data Requirements:** ✅ 100% Available (No New Data Needed)
- Uses existing: ADX, ATR, volume ratio from TradingView
- No external APIs required
- No SDK enhancements needed
**How It Works:**
```
Identify 3 market regimes:
1. TRENDING (ADX > 25, ATR > 0.4%) → Full execution
2. CHOPPY (ADX < 15, ATR < 0.3%) → Block all signals
3. TRANSITIONAL (between thresholds) → Reduce position size 50%
Implementation:
- Add regime detection in check-risk endpoint
- Use rolling 20-bar ADX/ATR averages
- Save regime to Trade table for analysis
```
**Benefits:**
- Proven concept from institutional trading
- Low risk (simple logic, easy to disable)
- Fast implementation (1-2 weeks)
- Immediate profitability boost
**Drawbacks:**
- Incremental improvement, not revolutionary
- Misses opportunities in range-bound markets
- Doesn't address signal quality within regimes
**Implementation Priority:** HIGH (Quick win, proven concept, no dependencies)
**Timeline:** 1-2 weeks
---
## Option B: Multi-Strategy Portfolio (Balanced Growth)
**Goal:** Deploy multiple complementary strategies that profit in different market conditions
**Expected Impact:** +50-100% profitability improvement
**Data Requirements:** ✅ 100% Available (Same as Option A)
- Uses existing TradingView indicators
- No external APIs required
- No SDK enhancements needed
**Strategy Allocation:**
```
1. Trend Following (40% capital):
- v9 Money Line (current system)
- Catches strong directional moves
2. Mean Reversion (30% capital):
- RSI extremes + volume spikes
- Profits from oversold/overbought bounces
3. Breakout/Breakdown (30% capital):
- Range expansion + volume confirmation
- Captures volatility expansion moves
Risk Management:
- Each strategy has separate enable/disable toggle
- Individual quality thresholds
- Correlation tracking (avoid all strategies in same direction)
```
**Benefits:**
- Diversification reduces drawdown periods
- Profit in multiple market conditions
- Can disable underperforming strategies
- Proven institutional approach
**Drawbacks:**
- More complex codebase to maintain
- Requires separate backtesting for each strategy
- Capital allocation decisions needed
- 4-6 weeks implementation vs 1-2 weeks for Option A
**Implementation Priority:** MEDIUM (Higher ROI than A, proven concept, manageable complexity)
**Timeline:** 4-6 weeks
---
## Option C: Order Flow Revolution (Maximum Upside)
**Goal:** Add institutional-grade order flow indicators using real-time market microstructure data
**Expected Impact:** +200-500% profitability improvement (if fully implemented)
**Data Requirements:** 🔄 Partially Available
**Available via Drift SDK (Already Integrated):**
- ✅ Oracle price (`getOracleDataForPerpMarket()`)
- ✅ Funding rate (`getPerpMarketAccount().amm.lastFundingRate`)
- ✅ AMM reserves, pool parameters
- ✅ Liquidation events (via EventSubscriber)
**NOT Available via SDK - Requires External APIs:**
- ❌ Order book L2/L3 depth → **DLOB Server required**
- ❌ Open interest → **Data API required**
- ❌ 24h volume → **Data API required**
- ❌ Real-time trades feed → **DLOB WebSocket required**
**Implementation Paths:**
**Partial Implementation (40% viable, SDK only):**
- Use funding rate + liquidation events only
- Expected: +50-150% improvement
- Timeline: 2-3 weeks
- No external API integration needed
**Full Implementation (100% viable, with external APIs):**
- All 5 data sources (funding, liquidations, orderbook, OI, trades)
- Expected: +200-500% improvement
- Timeline: 8-12 weeks
- Requires significant infrastructure work
**External APIs Needed (Full Implementation):**
**1. DLOB Server (Order Book + Trades):**
```
REST Endpoints:
- GET https://dlob.drift.trade/l2?marketName=SOL-PERP&depth=10
Returns: Aggregated bid/ask depth
- GET https://dlob.drift.trade/l3?marketName=SOL-PERP
Returns: Individual orders with maker addresses
WebSocket:
- wss://dlob.drift.trade/ws
- Channels: "orderbook" (400ms updates), "trades" (real-time)
- Use case: Order flow imbalance, liquidity analysis
```
**2. Data API (Historical + Statistical):**
```
REST Endpoints:
- GET https://data.api.drift.trade/fundingRates?marketName=SOL-PERP
Returns: 30-day funding rate history
- GET https://data.api.drift.trade/contracts
Returns: Funding rate + open interest per market
- GET https://data.api.drift.trade/stats/markets/volume
Returns: 24h volume statistics
```
**Order Flow Indicators (Full Implementation):**
1. **Order Book Imbalance:**
```typescript
// Sum top 10 bid levels vs top 10 ask levels
const imbalance = (bidSize - askSize) / (bidSize + askSize)
// > 0.3: Strong buy pressure (LONG bias)
// < -0.3: Strong sell pressure (SHORT bias)
```
2. **Volume Delta:**
```typescript
// Track buys vs sells from trades feed
const volumeDelta = buyVolume - sellVolume
// Rising delta + price up: Confirmed uptrend
// Falling delta + price up: Divergence (potential reversal)
```
3. **Funding Rate Bias:**
```typescript
// Already have via SDK
if (fundingRate > 0.08) {
// Longs paying 8%+ annualized → SHORT bias
} else if (fundingRate < -0.02) {
// Shorts paying heavily → LONG bias
}
```
4. **Liquidation Clusters:**
```typescript
// Track liquidation events via EventSubscriber
// Identify price levels with high liquidation concentration
// Avoid entries near clusters (stop-hunt zones)
```
5. **Open Interest Changes:**
```typescript
// From Data API /contracts
const oiChange = (currentOI - previousOI) / previousOI
// Rising OI + price up: New longs entering (bullish)
// Falling OI + price up: Shorts covering (bearish)
```
**Implementation Requirements (Full):**
**New Code Components:**
```typescript
// lib/drift/dlob-client.ts (NEW - ~300 lines)
export class DLOBClient {
async getL2Orderbook(marketName: string, depth: number = 10)
async subscribeOrderbook(marketName: string, callback: Function)
async subscribeTrades(marketName: string, callback: Function)
}
// lib/drift/data-api-client.ts (NEW - ~200 lines)
export class DriftDataAPIClient {
async getFundingRateHistory(marketName: string)
async getContracts()
async getMarketVolume()
}
// lib/indicators/order-flow.ts (NEW - ~400 lines)
export class OrderFlowIndicators {
async calculateOrderImbalance(marketName: string): Promise<number>
async getFundingBias(marketName: string): Promise<string>
async getLiquidationClusters(marketName: string): Promise<number[]>
async getVolumeDelta(marketName: string): Promise<number>
}
```
**Infrastructure Effort:**
| Component | Complexity | Time |
|-----------|-----------|------|
| DLOB REST Client | Medium | 1 day |
| DLOB WebSocket Manager | High | 2 days |
| Data API Client | Medium | 1 day |
| Order Flow Indicators | High | 3 days |
| Integration Testing | Medium | 2 days |
| **Total** | **High** | **9 days** |
**Benefits:**
- Institutional-grade edge
- Maximum profitability potential (+200-500%)
- Detects hidden liquidity patterns
- Early warning of major moves
**Drawbacks:**
- Significant development effort (8-12 weeks)
- External API dependencies (rate limits, latency)
- Complexity increases maintenance burden
- Requires extensive validation
**Implementation Priority:** LOW-MEDIUM
- Start with partial (funding + liquidations only) if quick win desired
- Full implementation only after Options A/B validated
- Highest upside but highest risk/effort
**Timeline:**
- Partial: 2-3 weeks
- Full: 8-12 weeks
---
## Option D: Machine Learning Enhancement (Research Project)
**Goal:** Use ML to learn optimal entry timing, exit points, and position sizing from historical data
**Expected Impact:** Unknown (Potential 3-10× if successful)
**Data Requirements:** ✅ 100% Flexible
- Works with any available features
- Current data sufficient to start
- Can incorporate DLOB data later if Option C infrastructure built
**Approach:**
```
Phase 1: Feature Engineering (2 weeks)
- Extract 50+ features from historical trades
- Include: ADX, ATR, RSI, volume, price position, time of day, funding rate, etc.
- Calculate target: "If we had entered 1-5 minutes later, would P&L improve?"
Phase 2: Model Training (2 weeks)
- Try multiple algorithms: Gradient Boosting, Random Forest, Neural Networks
- Train on 1000+ historical signals
- Validate on hold-out test set (no look-ahead bias)
Phase 3: Backtesting (2 weeks)
- Run trained model on out-of-sample data
- Compare to baseline v9 Money Line
- Measure improvement in win rate, profit factor, drawdown
Phase 4: Paper Trading (4 weeks)
- Deploy model in parallel with live system
- Track predictions vs actual outcomes
- Don't execute, just observe
Phase 5: Live Deployment (2 weeks)
- If paper trading successful (>10% improvement), go live
- Start with 10-20% capital allocation
- Scale up if performance persists
```
**Example Features:**
- Technical: ADX, ATR, RSI, volume ratio, price position
- Market microstructure: Funding rate, mark-oracle spread, AMM depth
- Temporal: Time of day, day of week, days since last trade
- Historical: Recent win rate, consecutive wins/losses, drawdown depth
- Cross-asset: Correlation with BTC, ETH, market-wide metrics
**Benefits:**
- Learns non-obvious patterns humans miss
- Adapts to changing market conditions
- Can optimize entire workflow (entry, sizing, exits)
- Highest theoretical upside
**Drawbacks:**
- Uncertain ROI (could be +10% or +300%, or negative)
- Requires ML expertise
- Overfitting risk (backtests great, live fails)
- Black box (hard to debug when wrong)
- 2-3 month timeline before knowing if viable
**Implementation Priority:** LOW-MEDIUM
- Only after Options A/B deployed and validated
- Treat as research project, not guaranteed improvement
- Can run in parallel with other options
**Timeline:** 2-3 months (research + validation)
---
## Decision Framework 🎯
**Choose Based on Your Goals:**
**If prioritizing SPEED:**
**Option A** (Regime Filter)
- 1-2 weeks
- +20-30% improvement
- Low risk, proven concept
**If prioritizing BALANCE:**
**Option B** (Multi-Strategy)
- 4-6 weeks
- +50-100% improvement
- Diversification benefits
**If prioritizing UPSIDE (with time):**
**Option C Partial** (Funding + Liquidations)
- 2-3 weeks
- +50-150% improvement
- Foundation for full implementation later
**If prioritizing RESEARCH/LEARNING:**
**Option D** (Machine Learning)
- 2-3 months
- Unknown ROI (potentially 3-10×)
- Bleeding edge approach
**Recommended Path (Conservative Growth):**
```
Month 1: Option A (Regime Filter)
- Fast win, proven concept
- Validate +20-30% improvement
Month 2-3: Option B (Multi-Strategy)
- Build on proven foundation
- Diversify returns
- Aim for +50-100% total improvement
Month 4-5: Option C Partial (if desired)
- Add funding rate + liquidation indicators
- Test order flow concept
- Decide on full implementation
Month 6+: Option D (if research capacity)
- Parallel project
- Don't depend on results
- Could discover breakthrough edge
```
**Recommended Path (Aggressive Growth):**
```
Month 1: Option C Partial (Funding + Liquidations)
- Quick implementation (2-3 weeks)
- Test order flow concept
- +50-150% improvement potential
Month 2-4: Option C Full (if partial succeeds)
- Build DLOB + Data API infrastructure
- Deploy full order flow suite
- Aim for +200-500% improvement
Month 5+: Option B or D (diversification)
- Add multi-strategy for stability
- Or pursue ML for breakthrough edge
```
---
## Drift SDK Integration Status 📊
**Research Date:** Dec 2, 2025
**Current Implementation (lib/drift/client.ts):**
```typescript
getOraclePrice(marketIndex) - Line 342
Returns: Oracle price from Pyth network
getFundingRate(marketIndex) - Line 354
Returns: Current funding rate as percentage
getAccountHealth() - Line 376
Returns: Collateral, liability, free margin, margin ratio
```
**Available but NOT Used:**
- AMM Reserve Data: baseAssetReserve, quoteAssetReserve, sqrtK
- Pool Parameters: concentrationCoef, pegMultiplier
- Fee Metrics: totalFee, totalFeeWithdrawn
**External Resources:**
**DLOB Server Documentation:**
- Mainnet: `https://dlob.drift.trade/`
- WebSocket: `wss://dlob.drift.trade/ws`
- REST: `/l2`, `/l3`, `/topMakers` endpoints
- Update frequency: Orderbook every 400ms, trades real-time
**Data API Documentation:**
- Mainnet: `https://data.api.drift.trade/`
- Playground: `https://data.api.drift.trade/playground`
- Key endpoints: `/fundingRates`, `/contracts`, `/stats/markets/volume`
- Rate limited, cached responses
**SDK Documentation:**
- TypeScript: `https://drift-labs.github.io/v2-teacher/`
- Auto-generated: `https://drift-labs.github.io/protocol-v2/sdk/`
- Event Subscription: EventSubscriber class for liquidations, trades, funding updates
---
## Summary & Next Steps
**Current System:**
- ✅ v9 Money Line: $405.88 PnL, 60.98% WR, 569 trades (baseline)
- ✅ 1-minute data: Active collection, <60s fresh
- ✅ Phases 1, 2, 7.1-7.3: Deployed and operational
- ✅ EPYC cluster: Parameter optimization in progress (0/4,096 complete)
**Strategic Options Available:**
1. **Option A (Regime):** Quick win, proven, +20-30%, 1-2 weeks
2. **Option B (Multi-Strategy):** Balanced, diversified, +50-100%, 4-6 weeks
3. **Option C (Order Flow):** High upside, requires APIs, +50-500%, 2-12 weeks
4. **Option D (ML):** Research project, unknown ROI, 2-3 months
**When to Implement:**
- After EPYC cluster results analyzed (v9 parameter optimization)
- After validating optimized v9 baseline with 50-100 live trades
- User decision on strategic direction (A/B/C/D or combination)
**Data Availability Confirmed:**
- Options A, B, D: ✅ 100% viable with existing data
- Option C: 🔄 40% viable with SDK only, 100% viable with external APIs
**This research will be revisited when system is ready for next-generation enhancements.**