feat: Implement ATR-based TP/SL system for regime-agnostic trading

CRITICAL UPGRADE - Nov 17, 2025

Problem Solved:
- v6 shorts averaging +20.74% MFE but TP exits at +0.7% (leaving 95% on table)
- Fixed % targets don't adapt to bull/bear regime changes
- User must manually adjust settings when sentiment flips
- Market-regime bias in optimization (bearish now ≠ bullish later)

Solution - ATR-Based Dynamic TP/SL:
- TP1 = ATR × 2.0 (adaptive to volatility)
- TP2 = ATR × 4.0 (captures extended moves)
- SL = ATR × 3.0 (proportional risk)
- Safety bounds prevent extremes (min/max caps)

Example with SOL ATR = 0.45%:
- TP1: 0.45% × 2.0 = 0.90% (vs old fixed 0.4%)
- TP2: 0.45% × 4.0 = 1.80% (vs old fixed 0.7%)
- SL: 0.45% × 3.0 = 1.35% (vs old fixed 1.5%)

Benefits:
 Adapts automatically to bull/bear regime changes
 Asset-agnostic (SOL vs BTC have different ATR)
 Captures more profit in volatile conditions
 Tighter risk in calm conditions
 No manual intervention when sentiment shifts
 Consistent with existing ATR-based trailing stop

Implementation:
- Added TradingConfig fields: atrMultiplierTp1/Tp2/Sl with min/max bounds
- New calculatePercentFromAtr() helper function
- Execute endpoint calculates dynamic % from ATR, falls back to fixed % if unavailable
- ENV variables: ATR_MULTIPLIER_TP1/TP2/SL, MIN_TP1/TP2/SL_PERCENT, MAX_TP1/TP2/SL_PERCENT
- Updated .env with new defaults based on v6 MAE/MFE analysis

Configuration:
- USE_ATR_BASED_TARGETS=true (enabled by default)
- Runner: 40% (TAKE_PROFIT_1_SIZE_PERCENT=60)
- Trailing: 1.3x ATR (existing system, unchanged)
- Legacy fixed % used as fallback when ATR unavailable

Files Modified:
- config/trading.ts (interface + defaults + ENV reading)
- app/api/trading/execute/route.ts (ATR calculation logic)
- .env (new ATR multiplier variables)

Expected Impact:
- Capture 2-3x more profit per winning trade
- Maintain same risk management rigor
- Perform well in BOTH bull and bear markets
- Fix v6 underperformance (-$47.70 → positive)

Testing Required:
- Monitor first 10 trades with ATR-based targets
- Verify TP/SL prices match ATR calculations in logs
- Compare P&L to historical fixed-% performance
This commit is contained in:
mindesbunister
2025-11-17 11:41:13 +01:00
parent 9dfc6da449
commit 141022243a
3 changed files with 155 additions and 47 deletions

61
.env
View File

@@ -73,10 +73,10 @@ MAX_POSITION_SIZE_USD=210
# Higher leverage = bigger gains AND bigger losses
LEVERAGE=10
# Risk parameters (as percentages)
# Risk parameters (LEGACY FALLBACK - used when ATR unavailable)
# Stop Loss: Close 100% of position when price drops this much
# Example: -1.5% on 10x = -15% account loss
STOP_LOSS_PERCENT=-1
STOP_LOSS_PERCENT=-1.5
# ================================
# DUAL STOP SYSTEM (Advanced)
@@ -85,7 +85,7 @@ STOP_LOSS_PERCENT=-1
# When enabled, places TWO stop orders:
# 1. Soft Stop (TRIGGER_LIMIT) - Avoids false breakouts/wicks
# 2. Hard Stop (TRIGGER_MARKET) - Guarantees exit if price keeps falling
USE_DUAL_STOPS=true
USE_DUAL_STOPS=false
# Soft Stop (Primary, Stop-Limit)
# Triggers first, tries to avoid wicks
@@ -97,36 +97,53 @@ SOFT_STOP_BUFFER=0.4 # Buffer between trigger and limit (0.4% = limit at -1.9%)
# Guarantees exit during strong breakdowns
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.4
# Take Profit 1: Close X% of position at this profit level (FALLBACK)
# Example: +0.8% on 10x = +8% account gain
TAKE_PROFIT_1_PERCENT=0.8
# Take Profit 1 Size: What % of position to close at TP1
# Example: 50 = close 50% of position
TAKE_PROFIT_1_SIZE_PERCENT=70
# 60 = close 60%, leave 40% for runner
TAKE_PROFIT_1_SIZE_PERCENT=60
# Take Profit 2: Close remaining 50% at this profit level
# Example: +1.5% on 10x = +15% account gain
TAKE_PROFIT_2_PERCENT=0.7
# Take Profit 2: Trigger trailing stop at this profit level (FALLBACK)
# Example: +1.8% on 10x = +18% account gain
TAKE_PROFIT_2_PERCENT=1.8
# Take Profit 2 Size: What % of remaining position to close at TP2
# Example: 100 = close all remaining position
# 0 = don't close at TP2, activate trailing stop on full remaining position
TAKE_PROFIT_2_SIZE_PERCENT=0
# ATR-based dynamic targets (capture big moves like 4-5% drops)
# Enable dynamic TP2 based on market volatility
# ================================
# ATR-BASED TP/SL (PRIMARY SYSTEM - Nov 17, 2025)
# ================================
# Enable dynamic TP/SL based on market volatility (RECOMMENDED)
# ATR (Average True Range) adapts to current market conditions
# Bull market: Different ATR → Different targets automatically
# Bear market: Different ATR → Different targets automatically
# No manual intervention needed when market regime changes!
USE_ATR_BASED_TARGETS=true
# ATR multiplier for TP2 calculation (TP2 = ATR × this value)
# Example: ATR=0.8% × 2.0 = 1.6% TP2 target
ATR_MULTIPLIER_FOR_TP2=2
# ATR multipliers for TP1, TP2, and SL
# Example with SOL ATR = 0.45% of price:
# TP1 = 0.45% × 2.0 = 0.90% target
# TP2 = 0.45% × 4.0 = 1.80% target
# SL = 0.45% × 3.0 = 1.35% distance
ATR_MULTIPLIER_TP1=2.0
ATR_MULTIPLIER_TP2=4.0
ATR_MULTIPLIER_SL=3.0
# Minimum TP2 level regardless of ATR (safety floor)
MIN_TP2_PERCENT=0.7
# Safety bounds (prevent extreme values)
# TP1 bounds
MIN_TP1_PERCENT=0.5 # Never below +0.5%
MAX_TP1_PERCENT=1.5 # Never above +1.5%
# Maximum TP2 level cap (prevents excessive targets)
# Example: 3.0% = 30% account gain at 10x leverage
MAX_TP2_PERCENT=3
# TP2 bounds
MIN_TP2_PERCENT=1.0 # Never below +1.0%
MAX_TP2_PERCENT=3.0 # Never above +3.0%
# SL bounds
MIN_SL_PERCENT=0.8 # Never tighter than -0.8%
MAX_SL_PERCENT=2.0 # Never wider than -2.0%
# Emergency Stop: Hard stop if this level is breached
# Example: -2.0% on 10x = -20% account loss (rare but protects from flash crashes)