Add trailing stop feature for runner position + fix settings persistence

- Implemented trailing stop logic in Position Manager for remaining position after TP2
- Added new ActiveTrade fields: tp2Hit, trailingStopActive, peakPrice
- New config settings: useTrailingStop, trailingStopPercent, trailingStopActivation
- Added trailing stop UI section in settings page with explanations
- Fixed env file parsing regex to support numbers in variable names (A-Z0-9_)
- Settings now persist correctly across container restarts
- Added back arrow navigation on settings page
- Updated all API endpoints and test files with new fields
- Trailing stop activates when runner reaches configured profit level
- SL trails below peak price by configurable percentage
This commit is contained in:
mindesbunister
2025-10-27 12:11:10 +01:00
parent d3c04ea9c9
commit 4ae9c38ad8
8 changed files with 193 additions and 22 deletions

View File

@@ -26,6 +26,11 @@ export interface TradingConfig {
profitLockTriggerPercent: number // When to lock in profit
profitLockPercent: number // How much profit to lock
// Trailing stop for runner (after TP2)
useTrailingStop: boolean // Enable trailing stop for remaining position
trailingStopPercent: number // Trail by this % below peak
trailingStopActivation: number // Activate when runner profits exceed this %
// DEX specific
priceCheckIntervalMs: number // How often to check prices
slippageTolerance: number // Max acceptable slippage (%)
@@ -70,10 +75,15 @@ export const DEFAULT_TRADING_CONFIG: TradingConfig = {
hardStopPercent: -2.5, // Hard stop (TRIGGER_MARKET)
// Dynamic adjustments
breakEvenTriggerPercent: 0.4, // Move SL to breakeven at +0.4%
breakEvenTriggerPercent: 0.4, // Move SL to this profit level after TP1 hits
profitLockTriggerPercent: 1.0, // Lock profit at +1.0%
profitLockPercent: 0.4, // Lock +0.4% profit
// Trailing stop for runner (after TP2)
useTrailingStop: true, // Enable trailing stop for remaining position after TP2
trailingStopPercent: 0.3, // Trail by 0.3% below peak price
trailingStopActivation: 0.5, // Activate trailing when runner is +0.5% in profit
// DEX settings
priceCheckIntervalMs: 2000, // Check every 2 seconds
slippageTolerance: 1.0, // 1% max slippage on market orders
@@ -86,8 +96,8 @@ export const DEFAULT_TRADING_CONFIG: TradingConfig = {
// Execution
useMarketOrders: true, // Use market orders for reliable fills
confirmationTimeout: 30000, // 30 seconds max wait
takeProfit1SizePercent: 75,
takeProfit2SizePercent: 100,
takeProfit1SizePercent: 75, // Close 75% at TP1 to lock in profit
takeProfit2SizePercent: 100, // Close remaining 25% at TP2
}
// Supported markets on Drift Protocol
@@ -200,6 +210,24 @@ export function getConfigFromEnv(): Partial<TradingConfig> {
takeProfit2SizePercent: process.env.TAKE_PROFIT_2_SIZE_PERCENT
? parseFloat(process.env.TAKE_PROFIT_2_SIZE_PERCENT)
: undefined,
breakEvenTriggerPercent: process.env.BREAKEVEN_TRIGGER_PERCENT
? parseFloat(process.env.BREAKEVEN_TRIGGER_PERCENT)
: undefined,
profitLockTriggerPercent: process.env.PROFIT_LOCK_TRIGGER_PERCENT
? parseFloat(process.env.PROFIT_LOCK_TRIGGER_PERCENT)
: undefined,
profitLockPercent: process.env.PROFIT_LOCK_PERCENT
? parseFloat(process.env.PROFIT_LOCK_PERCENT)
: undefined,
useTrailingStop: process.env.USE_TRAILING_STOP
? process.env.USE_TRAILING_STOP === 'true'
: undefined,
trailingStopPercent: process.env.TRAILING_STOP_PERCENT
? parseFloat(process.env.TRAILING_STOP_PERCENT)
: undefined,
trailingStopActivation: process.env.TRAILING_STOP_ACTIVATION
? parseFloat(process.env.TRAILING_STOP_ACTIVATION)
: undefined,
maxDailyDrawdown: process.env.MAX_DAILY_DRAWDOWN
? parseFloat(process.env.MAX_DAILY_DRAWDOWN)
: undefined,