feat: Implement ATR-based dynamic TP2 system and fix P&L calculation

- Add ATR-based dynamic TP2 scaling from 0.7% to 3.0% based on volatility
- New config options: useAtrBasedTargets, atrMultiplierForTp2, minTp2Percent, maxTp2Percent
- Enhanced settings UI with ATR controls and updated risk calculator
- Fix external closure P&L calculation using unrealized P&L instead of volatile current price
- Update execute and test endpoints to use calculateDynamicTp2() function
- Maintain 25% runner system for capturing extended moves (4-5% targets)
- Add environment variables for ATR-based configuration
- Better P&L accuracy for manual position closures
This commit is contained in:
mindesbunister
2025-11-07 17:01:22 +01:00
parent 5acc61cf66
commit 6d5991172a
7 changed files with 208 additions and 19 deletions

View File

@@ -34,6 +34,12 @@ interface TradingSettings {
TRAILING_STOP_PERCENT: number
TRAILING_STOP_ACTIVATION: number
// ATR-based Dynamic Targets
USE_ATR_BASED_TARGETS: boolean
ATR_MULTIPLIER_FOR_TP2: number
MIN_TP2_PERCENT: number
MAX_TP2_PERCENT: number
// Position Scaling
ENABLE_POSITION_SCALING: boolean
MIN_SCALE_QUALITY_SCORE: number
@@ -167,10 +173,21 @@ export default function SettingsPage() {
// Calculate gains/losses for risk calculator
const tp1Gain = size * lev * (settings.TAKE_PROFIT_1_PERCENT / 100) * (settings.TAKE_PROFIT_1_SIZE_PERCENT / 100)
const tp2RunnerSize = size * (1 - settings.TAKE_PROFIT_1_SIZE_PERCENT / 100) // 25% remaining after TP1
const runnerValue = tp2RunnerSize * lev * (settings.TAKE_PROFIT_2_PERCENT / 100) // Full 25% runner value at TP2
// Use ATR-based TP2 if enabled, otherwise use static
const tp2Percent = settings.USE_ATR_BASED_TARGETS
? `${settings.MIN_TP2_PERCENT}-${settings.MAX_TP2_PERCENT}% (ATR-based)`
: `${settings.TAKE_PROFIT_2_PERCENT}% (static)`
// For calculation, use max potential TP2 if ATR-based
const tp2CalcPercent = settings.USE_ATR_BASED_TARGETS
? settings.MAX_TP2_PERCENT
: settings.TAKE_PROFIT_2_PERCENT
const runnerValue = tp2RunnerSize * lev * (tp2CalcPercent / 100) // Full 25% runner value at TP2
const fullWin = tp1Gain + runnerValue
return { maxLoss, tp1Gain, runnerValue, fullWin }
return { maxLoss, tp1Gain, runnerValue, fullWin, tp2Percent }
}
if (loading) {
@@ -228,6 +245,7 @@ export default function SettingsPage() {
<div className="bg-green-500/10 border border-green-500/50 rounded-lg p-4">
<div className="text-green-400 text-sm mb-1">Runner Value (25%)</div>
<div className="text-white text-2xl font-bold">+${risk.runnerValue.toFixed(2)}</div>
<div className="text-xs text-green-300 mt-1">{risk.tp2Percent}</div>
</div>
<div className="bg-purple-500/10 border border-purple-500/50 rounded-lg p-4">
<div className="text-purple-400 text-sm mb-1">Full Win</div>
@@ -455,6 +473,54 @@ export default function SettingsPage() {
/>
</Section>
{/* ATR-based Dynamic Targets */}
<Section title="📈 ATR-Based Dynamic Targets" description="Automatically scale TP2 based on market volatility to capture big moves">
<div className="mb-4 p-3 bg-green-500/10 border border-green-500/30 rounded-lg">
<p className="text-sm text-green-400 mb-2">
<strong>🎯 Capture Big Moves:</strong> When ATR is high (volatile markets), TP2 automatically scales higher to catch 4-5% moves instead of exiting early at 0.7%.
</p>
<p className="text-xs text-gray-400">
Example: If ATR = 1.2% and multiplier = 2.0, then TP2 = 2.4% (instead of fixed 0.7%). Perfect for trending markets!
</p>
</div>
<Setting
label="Enable ATR-Based Targets"
value={(settings as any).USE_ATR_BASED_TARGETS ? 1 : 0}
onChange={(v) => updateSetting('USE_ATR_BASED_TARGETS', v === 1)}
min={0}
max={1}
step={1}
description="Enable dynamic TP2 based on Average True Range (market volatility). 0 = fixed TP2, 1 = adaptive TP2."
/>
<Setting
label="ATR Multiplier for TP2"
value={(settings as any).ATR_MULTIPLIER_FOR_TP2 || 2.0}
onChange={(v) => updateSetting('ATR_MULTIPLIER_FOR_TP2', v)}
min={1.0}
max={4.0}
step={0.1}
description="Multiply ATR by this value to get TP2 target. Higher = more aggressive targets in volatile markets."
/>
<Setting
label="Minimum TP2 (%)"
value={(settings as any).MIN_TP2_PERCENT || 0.7}
onChange={(v) => updateSetting('MIN_TP2_PERCENT', v)}
min={0.3}
max={2.0}
step={0.1}
description="Safety floor - TP2 will never go below this level even in low-volatility markets."
/>
<Setting
label="Maximum TP2 (%)"
value={(settings as any).MAX_TP2_PERCENT || 3.0}
onChange={(v) => updateSetting('MAX_TP2_PERCENT', v)}
min={1.0}
max={5.0}
step={0.1}
description="Safety cap - TP2 will never exceed this level. Example: 3.0% = 30% account gain at 10x leverage."
/>
</Section>
{/* Dynamic Adjustments */}
<Section title="🎯 Dynamic Stop Loss" description="Automatically adjust SL as trade moves in profit">
<Setting