diff --git a/.env b/.env index f4942a9..e8d122f 100644 --- a/.env +++ b/.env @@ -370,7 +370,7 @@ TRAILING_STOP_ACTIVATION=0.4 MIN_QUALITY_SCORE=65 SOLANA_ENABLED=true SOLANA_POSITION_SIZE=100 -SOLANA_LEVERAGE=20 +SOLANA_LEVERAGE=15 SOLANA_USE_PERCENTAGE_SIZE=true ETHEREUM_ENABLED=false ETHEREUM_POSITION_SIZE=50 diff --git a/ATR_BASED_TP_ROADMAP.md b/ATR_BASED_TP_ROADMAP.md new file mode 100644 index 0000000..912016c --- /dev/null +++ b/ATR_BASED_TP_ROADMAP.md @@ -0,0 +1,278 @@ +# ATR-Based Take Profit System - Roadmap + +## Current Status: 📋 PLANNING (Needs more data) + +**Date Added:** November 12, 2025 +**Priority:** Medium (after 50+ trades collected with ATR data) + +## Concept + +Replace fixed percentage TP1/TP2 targets with ATR-based dynamic targets that adapt to market volatility. + +### Current System (Fixed %) +``` +TP1: Entry + 0.4% (always) +TP2: Entry + 0.7% (always) +SL: Entry - 1.0% (always) +``` + +### Proposed System (ATR Multipliers) +``` +TP1: Entry + (ATR × TP1_MULTIPLIER) +TP2: Entry + (ATR × TP2_MULTIPLIER) +SL: Entry - (ATR × SL_MULTIPLIER) +``` + +**Example with ATR = 0.26% at $160.62 entry:** +- 1.5x ATR TP1: $161.25 (+0.39%) ← Close to current 0.4%! +- 2.5x ATR TP2: $161.66 (+0.65%) ← Close to current 0.7%! +- 2.0x ATR SL: $159.79 (-0.52%) ← Tighter than current -1.0% + +## Benefits + +✅ **Adapts to volatility automatically** +- Calm market (ATR 0.15%): Tighter targets, faster profit locks +- Volatile market (ATR 0.50%): Wider targets, more room to run + +✅ **Data already available** +- ATR saved in database (`atrAtEntry` field) +- TradingView signals include ATR value +- 159 historical trades to backtest against + +✅ **Aligns with existing systems** +- Already using ATR for trailing stop calculation +- Consistent risk management approach +- Easy to add as config toggle + +## Implementation Plan + +### Phase 1: Data Collection (IN PROGRESS ✅) +**Status:** Currently collecting ATR data with every trade +- ✅ `atrAtEntry` field added to database (Nov 11) +- ✅ ATR-based trailing stop implemented (Nov 11) +- ✅ TradingView signals passing ATR value +- 🔄 Need 50+ trades with ATR data for meaningful backtest + +**Current progress:** 1 trade with ATR tracking (need 49 more) + +### Phase 2: Backtest Analysis (NEXT - After 50+ trades) +**Goal:** Determine optimal ATR multipliers through historical data + +**Backtest queries to run:** +```sql +-- Test different TP1 multipliers (1.0x, 1.5x, 2.0x, 2.5x) +WITH atr_tp1_simulation AS ( + SELECT + id, + symbol, + direction, + entryPrice, + "atrAtEntry", + "maxFavorableExcursion" as mfe, + realizedPnL, + -- Simulate TP1 at different ATR multipliers + (entryPrice * "atrAtEntry" / 100 * 1.0) as tp1_1x_distance, + (entryPrice * "atrAtEntry" / 100 * 1.5) as tp1_15x_distance, + (entryPrice * "atrAtEntry" / 100 * 2.0) as tp1_2x_distance, + -- Check if MFE would have hit each TP1 level + CASE WHEN "maxFavorableExcursion" >= ("atrAtEntry" * 1.0) THEN 1 ELSE 0 END as hit_1x, + CASE WHEN "maxFavorableExcursion" >= ("atrAtEntry" * 1.5) THEN 1 ELSE 0 END as hit_15x, + CASE WHEN "maxFavorableExcursion" >= ("atrAtEntry" * 2.0) THEN 1 ELSE 0 END as hit_2x + FROM "Trade" + WHERE "atrAtEntry" IS NOT NULL + AND "exitReason" IS NOT NULL +) +SELECT + 'TP1 at 1.0x ATR' as strategy, + COUNT(*) as total_trades, + SUM(hit_1x) as tp1_hits, + ROUND(100.0 * SUM(hit_1x) / COUNT(*), 1) as hit_rate, + ROUND(AVG("atrAtEntry" * 1.0), 2) as avg_target_pct +FROM atr_tp1_simulation +UNION ALL +SELECT + 'TP1 at 1.5x ATR' as strategy, + COUNT(*) as total_trades, + SUM(hit_15x) as tp1_hits, + ROUND(100.0 * SUM(hit_15x) / COUNT(*), 1) as hit_rate, + ROUND(AVG("atrAtEntry" * 1.5), 2) as avg_target_pct +FROM atr_tp1_simulation +UNION ALL +SELECT + 'TP1 at 2.0x ATR' as strategy, + COUNT(*) as total_trades, + SUM(hit_2x) as tp1_hits, + ROUND(100.0 * SUM(hit_2x) / COUNT(*), 1) as hit_rate, + ROUND(AVG("atrAtEntry" * 2.0), 2) as avg_target_pct +FROM atr_tp1_simulation; +``` + +**Metrics to compare:** +- TP1 hit rate (target 80%+) +- Average profit per TP1 hit +- Comparison vs current fixed 0.4% system +- Win rate impact +- Total P&L impact + +### Phase 3: Configuration Implementation +**Add new ENV variables:** +```env +# ATR-based targets (optional, defaults to fixed %) +USE_ATR_BASED_TARGETS=false # Toggle feature +TP1_ATR_MULTIPLIER=1.5 # TP1 = entry + (ATR × 1.5) +TP2_ATR_MULTIPLIER=2.5 # TP2 = entry + (ATR × 2.5) +SL_ATR_MULTIPLIER=2.0 # SL = entry - (ATR × 2.0) +``` + +**Update trading config:** +```typescript +// config/trading.ts +export const DEFAULT_TRADING_CONFIG = { + // ... existing config + + // ATR-based targets (optional override for fixed %) + useAtrBasedTargets: false, + tp1AtrMultiplier: 1.5, + tp2AtrMultiplier: 2.5, + slAtrMultiplier: 2.0, +} +``` + +**Update execute endpoint:** +```typescript +// app/api/trading/execute/route.ts +if (config.useAtrBasedTargets && body.atr) { + const atrValue = body.atr / 100 // Convert % to decimal + const atrInDollars = entryPrice * atrValue + + tp1Price = entryPrice + (atrInDollars * config.tp1AtrMultiplier) + tp2Price = entryPrice + (atrInDollars * config.tp2AtrMultiplier) + stopLossPrice = entryPrice - (atrInDollars * config.slAtrMultiplier) + + console.log(`📊 ATR-based targets (ATR=${body.atr}%):`) + console.log(` TP1: $${tp1Price.toFixed(4)} (${config.tp1AtrMultiplier}x ATR)`) + console.log(` TP2: $${tp2Price.toFixed(4)} (${config.tp2AtrMultiplier}x ATR)`) + console.log(` SL: $${stopLossPrice.toFixed(4)} (${config.slAtrMultiplier}x ATR)`) +} else { + // Fallback to fixed % (current system) + tp1Price = calculatePrice(entryPrice, config.takeProfit1Percent, body.direction) + // ... existing logic +} +``` + +### Phase 4: A/B Testing (After backtest shows promise) +Run parallel comparison for 20-30 trades: +- 50% trades: Fixed % targets (control group) +- 50% trades: ATR-based targets (test group) +- Compare: Win rate, profit factor, avg P&L + +### Phase 5: Full Deployment +If A/B test shows improvement: +- Enable by default for all new trades +- Keep fixed % as fallback when ATR unavailable +- Document in copilot-instructions.md + +## Expected Multipliers (Based on initial analysis) + +**Conservative (High win rate focus):** +``` +TP1: 1.0-1.5x ATR +TP2: 2.0-2.5x ATR +SL: 2.0-3.0x ATR +``` + +**Balanced (Current ~equivalent):** +``` +TP1: 1.5-2.0x ATR +TP2: 2.5-3.5x ATR +SL: 2.5-3.5x ATR +``` + +**Aggressive (Larger winners):** +``` +TP1: 2.0-3.0x ATR +TP2: 3.5-5.0x ATR +SL: 3.0-4.0x ATR +``` + +## Risks & Considerations + +⚠️ **Potential issues:** +1. Very low ATR (<0.15%) might create targets too tight +2. Very high ATR (>0.8%) might create targets unreachable +3. Need min/max clamping to prevent extreme values +4. ATR from TradingView must be fresh (<5min old) + +**Mitigation:** +```typescript +// Clamp ATR to reasonable range +const clampedAtr = Math.max(0.15, Math.min(0.8, body.atr)) + +// Or use hybrid: max of fixed % or ATR-based +const tp1Price = Math.max( + calculatePrice(entryPrice, 0.4, direction), // Fixed minimum + entryPrice + (atrInDollars * config.tp1AtrMultiplier) // ATR-based +) +``` + +## Dependencies + +✅ Already implemented: +- ATR data collection from TradingView +- `atrAtEntry` field in database +- ATR-based trailing stop logic + +⏳ Need before implementation: +- 50+ trades with ATR data (currently 1/50) +- Backtest analysis results +- Optimal multiplier determination + +## Success Metrics + +System is successful if ATR-based approach shows: +- ✅ TP1 hit rate ≥ 75% (vs current ~70%) +- ✅ Win rate maintained or improved (target 60%+) +- ✅ Profit factor > 1.5 (vs current varies) +- ✅ Better performance in volatile vs calm markets +- ✅ Total P&L improvement of 10%+ + +## Timeline Estimate + +**Phase 1 (Data Collection):** 2-4 weeks +- Depends on signal frequency (3-5 trades/day = 10-17 days) +- Need indicator v6 deployed to production + +**Phase 2 (Backtest):** 1-2 days +- Run SQL analysis +- Generate comparison reports +- Determine optimal multipliers + +**Phase 3 (Implementation):** 2-3 hours +- Add config options +- Update execute endpoint +- Add settings UI controls + +**Phase 4 (A/B Testing):** 2-3 weeks +- Run parallel comparison +- Statistical significance testing + +**Total:** ~6-8 weeks from today + +## Notes + +- This complements existing ATR-based trailing stop (already working well) +- Creates unified risk management: entry, TP, SL, and trailing all based on ATR +- Could be symbol-specific (SOL might need different multipliers than ETH) +- Aligns with Phase 1 goals (prove system works with data-driven decisions) + +## References + +- ATR Trailing Stop Implementation: `ATR_TRAILING_STOP_FIX.md` +- Trade Database Schema: `prisma/schema.prisma` (line 74: `atrAtEntry Float?`) +- Position Scaling Roadmap: `POSITION_SCALING_ROADMAP.md` (similar data-driven approach) +- Current Execute Logic: `app/api/trading/execute/route.ts` (lines 280-310) + +--- + +**Status:** Documented and ready for Phase 2 when sufficient data collected. +**Next Action:** Wait for 50+ trades with ATR data, then run backtest analysis.