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:
@@ -45,20 +45,37 @@ export async function POST(request: NextRequest): Promise<NextResponse<RiskCheck
|
||||
// Check for existing positions on the same symbol
|
||||
const positionManager = await getInitializedPositionManager()
|
||||
const existingTrades = Array.from(positionManager.getActiveTrades().values())
|
||||
const duplicatePosition = existingTrades.find(trade => trade.symbol === body.symbol)
|
||||
const existingPosition = existingTrades.find(trade => trade.symbol === body.symbol)
|
||||
|
||||
if (duplicatePosition) {
|
||||
console.log('🚫 Risk check BLOCKED: Duplicate position exists', {
|
||||
if (existingPosition) {
|
||||
// Check if it's the SAME direction (duplicate - block it)
|
||||
if (existingPosition.direction === body.direction) {
|
||||
console.log('🚫 Risk check BLOCKED: Duplicate position (same direction)', {
|
||||
symbol: body.symbol,
|
||||
existingDirection: existingPosition.direction,
|
||||
requestedDirection: body.direction,
|
||||
existingEntry: existingPosition.entryPrice,
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
allowed: false,
|
||||
reason: 'Duplicate position',
|
||||
details: `Already have ${existingPosition.direction} position on ${body.symbol} (entry: $${existingPosition.entryPrice})`,
|
||||
})
|
||||
}
|
||||
|
||||
// OPPOSITE direction - this is a signal flip/reversal (ALLOW IT)
|
||||
console.log('🔄 Risk check: Signal flip detected', {
|
||||
symbol: body.symbol,
|
||||
existingDirection: duplicatePosition.direction,
|
||||
requestedDirection: body.direction,
|
||||
existingEntry: duplicatePosition.entryPrice,
|
||||
existingDirection: existingPosition.direction,
|
||||
newDirection: body.direction,
|
||||
note: 'Will close existing and open opposite',
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
allowed: false,
|
||||
reason: 'Duplicate position',
|
||||
details: `Already have ${duplicatePosition.direction} position on ${body.symbol} (entry: $${duplicatePosition.entryPrice})`,
|
||||
allowed: true,
|
||||
reason: 'Signal flip',
|
||||
details: `Signal reversed from ${existingPosition.direction} to ${body.direction} - will flip position`,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -68,7 +85,7 @@ export async function POST(request: NextRequest): Promise<NextResponse<RiskCheck
|
||||
// 3. Check cooldown period
|
||||
// 4. Check account health
|
||||
|
||||
console.log(`✅ Risk check PASSED: No duplicate positions`)
|
||||
console.log(`✅ Risk check PASSED: No existing positions`)
|
||||
|
||||
return NextResponse.json({
|
||||
allowed: true,
|
||||
|
||||
Reference in New Issue
Block a user