- Add Pyth Network price monitoring (WebSocket + polling fallback) - Add Position Manager with automatic exit logic (TP1/TP2/SL) - Implement dynamic stop-loss adjustment (breakeven + profit lock) - Add real-time P&L tracking and multi-position support - Create comprehensive test suite (3 test scripts) - Add 5 detailed documentation files (2500+ lines) - Update configuration to $50 position size for safe testing - All Phase 2 features complete and tested Core Components: - v4/lib/pyth/price-monitor.ts - Real-time price monitoring - v4/lib/trading/position-manager.ts - Autonomous position management - v4/app/api/trading/positions/route.ts - Query positions endpoint - v4/test-*.ts - Comprehensive testing suite Documentation: - PHASE_2_COMPLETE_REPORT.md - Implementation summary - v4/PHASE_2_SUMMARY.md - Detailed feature overview - v4/TESTING.md - Testing guide - v4/QUICKREF_PHASE2.md - Quick reference - install-phase2.sh - Automated installation script
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
/**
|
|
* Risk Check API Endpoint
|
|
*
|
|
* Called by n8n workflow before executing trade
|
|
* POST /api/trading/check-risk
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getMergedConfig } from '@/v4/config/trading'
|
|
|
|
export interface RiskCheckRequest {
|
|
symbol: string
|
|
direction: 'long' | 'short'
|
|
}
|
|
|
|
export interface RiskCheckResponse {
|
|
allowed: boolean
|
|
reason?: string
|
|
details?: string
|
|
}
|
|
|
|
export async function POST(request: NextRequest): Promise<NextResponse<RiskCheckResponse>> {
|
|
try {
|
|
// Verify authorization
|
|
const authHeader = request.headers.get('authorization')
|
|
const expectedAuth = `Bearer ${process.env.API_SECRET_KEY}`
|
|
|
|
if (!authHeader || authHeader !== expectedAuth) {
|
|
return NextResponse.json(
|
|
{
|
|
allowed: false,
|
|
reason: 'Unauthorized',
|
|
},
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const body: RiskCheckRequest = await request.json()
|
|
|
|
console.log('🔍 Risk check for:', body)
|
|
|
|
const config = getMergedConfig()
|
|
|
|
// TODO: Implement actual risk checks:
|
|
// 1. Check daily drawdown
|
|
// 2. Check trades per hour limit
|
|
// 3. Check cooldown period
|
|
// 4. Check account health
|
|
// 5. Check existing positions
|
|
|
|
// For now, always allow (will implement in next phase)
|
|
const allowed = true
|
|
const reason = allowed ? undefined : 'Risk limit exceeded'
|
|
|
|
console.log(`✅ Risk check: ${allowed ? 'PASSED' : 'BLOCKED'}`)
|
|
|
|
return NextResponse.json({
|
|
allowed,
|
|
reason,
|
|
details: allowed ? 'All risk checks passed' : undefined,
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('❌ Risk check error:', error)
|
|
|
|
return NextResponse.json(
|
|
{
|
|
allowed: false,
|
|
reason: 'Risk check failed',
|
|
details: error instanceof Error ? error.message : 'Unknown error',
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|