Add /close command and auto-flip logic with order cleanup

- Added /close Telegram command for full position closure
- Updated /reduce to accept 10-100% (was 10-90%)
- Implemented auto-flip logic: automatically closes opposite position when signal reverses
- Fixed risk check to allow opposite direction trades (signal flips)
- Enhanced Position Manager to cancel orders when removing trades
- Added startup initialization for Position Manager (restores trades on restart)
- Fixed analytics to show stopped-out trades (manual DB update for orphaned trade)
- Updated reduce endpoint to route 100% closes through closePosition for proper cleanup
- All position closures now guarantee TP/SL order cancellation on Drift
This commit is contained in:
mindesbunister
2025-10-27 23:27:48 +01:00
parent a07bf9f4b2
commit 9bf83260c4
7 changed files with 264 additions and 19 deletions

View File

@@ -0,0 +1,33 @@
/**
* Position Manager Startup Initialization
*
* Ensures Position Manager starts monitoring on bot startup
* This prevents orphaned trades when the bot restarts
*/
import { getInitializedPositionManager } from '../trading/position-manager'
let initStarted = false
export async function initializePositionManagerOnStartup() {
if (initStarted) {
return
}
initStarted = true
console.log('🚀 Initializing Position Manager on startup...')
try {
const manager = await getInitializedPositionManager()
const status = manager.getStatus()
console.log(`✅ Position Manager ready - ${status.activeTradesCount} active trades`)
if (status.activeTradesCount > 0) {
console.log(`📊 Monitoring: ${status.symbols.join(', ')}`)
}
} catch (error) {
console.error('❌ Failed to initialize Position Manager on startup:', error)
}
}