import type { PriceDataPoint } from './src/types/index.js'; /** * 计算最近 N 个数据点的价格波动幅度 */ function calculatePriceRange(dataPoints: PriceDataPoint[], count: number = 10): number { const recentPoints = dataPoints.slice(-count); if (recentPoints.length < 2) return 0; const prices = recentPoints.map(p => p.price); const maxPrice = Math.max(...prices); const minPrice = Math.min(...prices); return maxPrice - minPrice; } /** * 测试波动幅度阈值逻辑 */ function testVolatilityThreshold() { console.log('========================================'); console.log('测试波动幅度阈值逻辑'); console.log('========================================\n'); // 测试场景 1:小幅波动(0.15) console.log('场景 1: 小幅波动'); const scenario1: PriceDataPoint[] = Array.from({ length: 10 }, (_, i) => ({ price: 22.0 + (i % 3) * 0.05 + Math.random() * 0.02, timestamp: new Date(Date.now() - (10 - i) * 60000).toISOString() })); const range1 = calculatePriceRange(scenario1, 10); console.log(` 波动幅度: ${range1.toFixed(3)}`); console.log(` 是否触发告警: ${range1 > 0.2 ? '是 ✅' : '否 ❌'}`); console.log(''); // 测试场景 2:中等波动(0.25) console.log('场景 2: 中等波动'); const scenario2: PriceDataPoint[] = Array.from({ length: 10 }, (_, i) => ({ price: 22.0 + (i % 5) * 0.06 + Math.random() * 0.02, timestamp: new Date(Date.now() - (10 - i) * 60000).toISOString() })); const range2 = calculatePriceRange(scenario2, 10); console.log(` 波动幅度: ${range2.toFixed(3)}`); console.log(` 是否触发告警: ${range2 > 0.2 ? '是 ✅' : '否 ❌'}`); console.log(''); // 测试场景 3:大幅波动(0.5) console.log('场景 3: 大幅波动'); const scenario3: PriceDataPoint[] = Array.from({ length: 10 }, (_, i) => ({ price: 22.0 + (i % 7) * 0.08 + Math.random() * 0.02, timestamp: new Date(Date.now() - (10 - i) * 60000).toISOString() })); const range3 = calculatePriceRange(scenario3, 10); console.log(` 波动幅度: ${range3.toFixed(3)}`); console.log(` 是否触发告警: ${range3 > 0.2 ? '是 ✅' : '否 ❌'}`); console.log(''); // 测试场景 4:模拟真实剧烈波动 console.log('场景 4: 模拟真实剧烈波动(22.0 → 22.6 → 22.2)'); const scenario4: PriceDataPoint[] = [ { price: 22.0, timestamp: new Date(Date.now() - 9 * 60000).toISOString() }, { price: 22.1, timestamp: new Date(Date.now() - 8 * 60000).toISOString() }, { price: 22.2, timestamp: new Date(Date.now() - 7 * 60000).toISOString() }, { price: 22.35, timestamp: new Date(Date.now() - 6 * 60000).toISOString() }, { price: 22.5, timestamp: new Date(Date.now() - 5 * 60000).toISOString() }, { price: 22.6, timestamp: new Date(Date.now() - 4 * 60000).toISOString() }, { price: 22.5, timestamp: new Date(Date.now() - 3 * 60000).toISOString() }, { price: 22.35, timestamp: new Date(Date.now() - 2 * 60000).toISOString() }, { price: 22.25, timestamp: new Date(Date.now() - 1 * 60000).toISOString() }, { price: 22.2, timestamp: new Date().toISOString() } ]; const range4 = calculatePriceRange(scenario4, 10); console.log(` 最高价: ${Math.max(...scenario4.map(p => p.price)).toFixed(3)}`); console.log(` 最低价: ${Math.min(...scenario4.map(p => p.price)).toFixed(3)}`); console.log(` 波动幅度: ${range4.toFixed(3)}`); console.log(` 是否触发告警: ${range4 > 0.2 ? '是 ✅' : '否 ❌'}`); console.log(''); console.log('========================================'); console.log('✅ 测试完成!'); console.log('========================================'); } testVolatilityThreshold();