| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #!/usr/bin/env tsx
- /**
- * 长时间刷量交易实验
- * 用于验证刷量策略的长期效果
- */
- import { CompleteTradingSystem } from './src/main-complete.js'
- class VolumeExperiment {
- private system?: CompleteTradingSystem
- private startTime = Date.now()
- private experimentDuration = 10 * 60 * 1000 // 10分钟实验
- private logInterval = 30 * 1000 // 每30秒记录一次统计
- private experimentStats = {
- startTime: Date.now(),
- totalExperimentTrades: 0,
- totalExperimentVolume: 0,
- tradeHistory: [] as any[],
- hourlyStats: [] as any[],
- }
- async runExperiment() {
- console.log(`
- 🧪 长期刷量交易实验开始
- ===============================================
- 实验时长: ${this.experimentDuration / 60000}分钟
- 数据记录间隔: ${this.logInterval / 1000}秒
- 实验目标: 验证刷量交易策略的持续效果
- ===============================================
- `)
- try {
- // 启动完整交易系统
- this.system = new CompleteTradingSystem()
- // 监听交易事件
- this.system.on('trade_executed', (data: any) => {
- this.recordTrade(data)
- })
- await this.system.start()
- // 定期记录统计数据
- const statsTimer = setInterval(() => {
- this.logExperimentStats()
- }, this.logInterval)
- // 设置实验结束定时器
- setTimeout(() => {
- clearInterval(statsTimer)
- this.endExperiment()
- }, this.experimentDuration)
- console.log('✅ 长期实验已启动,系统将自动运行...')
- console.log(`⏰ 实验将在 ${this.experimentDuration / 60000} 分钟后自动结束`)
- } catch (error: any) {
- console.error('❌ 实验启动失败:', error.message)
- process.exit(1)
- }
- }
- private recordTrade(trade: any) {
- this.experimentStats.tradeHistory.push({
- timestamp: Date.now(),
- ...trade,
- })
- this.experimentStats.totalExperimentTrades++
- this.experimentStats.totalExperimentVolume += parseFloat(trade.amount || '0')
- }
- private logExperimentStats() {
- const runtime = Date.now() - this.startTime
- const runtimeMinutes = Math.floor(runtime / 60000)
- const systemStats = this.system?.getSystemStats()
- const currentStats = {
- timestamp: Date.now(),
- runtime: runtimeMinutes,
- totalTrades: systemStats?.trading.totalTrades || 0,
- successfulTrades: systemStats?.trading.successfulTrades || 0,
- totalVolume: systemStats?.trading.totalVolume || 0,
- successRate: systemStats?.trading.successRate || 0,
- tradesPerMinute: systemStats?.trading.totalTrades
- ? (systemStats.trading.totalTrades / Math.max(runtimeMinutes, 1)).toFixed(2)
- : '0.00',
- }
- this.experimentStats.hourlyStats.push(currentStats)
- // 清屏并显示实验统计
- process.stdout.write('\u001B[2J\u001B[0;0f')
- console.log(`🧪 长期刷量交易实验 - 实时统计`)
- console.log('='.repeat(80))
- console.log(`⏰ 实验进度`)
- console.log(` 运行时间: ${runtimeMinutes}/${this.experimentDuration / 60000}分钟`)
- console.log(` 完成度: ${((runtime / this.experimentDuration) * 100).toFixed(1)}%`)
- console.log(`\n📊 累计交易统计`)
- console.log(` 总交易数: ${currentStats.totalTrades}`)
- console.log(` 成功交易: ${currentStats.successfulTrades}`)
- console.log(` 失败交易: ${currentStats.totalTrades - currentStats.successfulTrades}`)
- console.log(` 总交易量: ${currentStats.totalVolume} BTC`)
- console.log(` 成功率: ${currentStats.successRate}%`)
- console.log(`\n⚡ 交易效率`)
- console.log(` 每分钟交易数: ${currentStats.tradesPerMinute}`)
- console.log(` 预期小时交易数: ${(parseFloat(currentStats.tradesPerMinute) * 60).toFixed(0)}`)
- if (this.experimentStats.hourlyStats.length > 1) {
- const recent = this.experimentStats.hourlyStats.slice(-2)
- const recentTradeDiff = recent[1].totalTrades - recent[0].totalTrades
- const timeDiff = (recent[1].timestamp - recent[0].timestamp) / 1000 / 60
- const recentRate = (recentTradeDiff / timeDiff).toFixed(2)
- console.log(` 最近${this.logInterval / 1000}s交易率: ${recentRate}笔/分钟`)
- }
- console.log(`\n🎯 刷量效果评估`)
- const expectedMinimumTrades = Math.floor(runtimeMinutes * 2) // 期望每分钟至少2笔交易
- const actualTrades = currentStats.totalTrades
- const efficiency = actualTrades >= expectedMinimumTrades ? '✅ 达标' : '⚠️ 低于预期'
- console.log(` 预期最少交易数: ${expectedMinimumTrades}`)
- console.log(` 实际交易数: ${actualTrades}`)
- console.log(` 刷量效率: ${efficiency}`)
- if (runtimeMinutes > 0) {
- const volumePerMinute = (currentStats.totalVolume / runtimeMinutes).toFixed(4)
- console.log(` 每分钟交易量: ${volumePerMinute} BTC`)
- console.log(` 预期小时交易量: ${(parseFloat(volumePerMinute) * 60).toFixed(2)} BTC`)
- }
- console.log(`\n📈 趋势分析`)
- if (this.experimentStats.hourlyStats.length >= 3) {
- const last3Stats = this.experimentStats.hourlyStats.slice(-3)
- const tradeGrowth = last3Stats.map(s => s.totalTrades)
- const isIncreasing = tradeGrowth[2] > tradeGrowth[1] && tradeGrowth[1] > tradeGrowth[0]
- console.log(` 交易趋势: ${isIncreasing ? '📈 上升' : '📊 稳定'}`)
- const avgRate = last3Stats.map(s => parseFloat(s.tradesPerMinute)).reduce((a, b) => a + b) / 3
- console.log(` 平均交易率: ${avgRate.toFixed(2)}笔/分钟`)
- }
- console.log(`\n⏰ 最后更新: ${new Date().toLocaleTimeString()}`)
- console.log('='.repeat(80))
- console.log(`💡 实验持续进行中... 按 Ctrl+C 提前结束`)
- }
- private async endExperiment() {
- console.log('\n🏁 实验完成!正在生成最终报告...')
- const finalStats = this.system?.getSystemStats()
- const totalRuntime = Date.now() - this.startTime
- const totalMinutes = totalRuntime / 60000
- console.log(`
- 🧪 长期刷量交易实验 - 最终报告
- ===============================================
- 实验时长: ${totalMinutes.toFixed(1)}分钟
- 开始时间: ${new Date(this.startTime).toLocaleString()}
- 结束时间: ${new Date().toLocaleString()}
- ===============================================
- 📊 总体统计:
- 总交易数: ${finalStats?.trading.totalTrades || 0}
- 成功交易: ${finalStats?.trading.successfulTrades || 0}
- 失败交易: ${(finalStats?.trading.totalTrades || 0) - (finalStats?.trading.successfulTrades || 0)}
- 总交易量: ${finalStats?.trading.totalVolume || 0} BTC
- 成功率: ${finalStats?.trading.successRate || 0}%
- ⚡ 效率分析:
- 平均每分钟: ${((finalStats?.trading.totalTrades || 0) / totalMinutes).toFixed(2)} 笔
- 预期每小时: ${(((finalStats?.trading.totalTrades || 0) / totalMinutes) * 60).toFixed(0)} 笔
- 每分钟交易量: ${((finalStats?.trading.totalVolume || 0) / totalMinutes).toFixed(4)} BTC
- 预期小时交易量: ${(((finalStats?.trading.totalVolume || 0) / totalMinutes) * 60).toFixed(2)} BTC
- 🎯 刷量效果:
- 期望最少交易: ${Math.floor(totalMinutes * 2)}
- 实际完成交易: ${finalStats?.trading.totalTrades || 0}
- 效率达成率: ${(((finalStats?.trading.totalTrades || 0) / Math.max(Math.floor(totalMinutes * 2), 1)) * 100).toFixed(
- 1,
- )}%
- 💡 结论:
- 刷量策略: ${(finalStats?.trading.totalTrades || 0) >= Math.floor(totalMinutes * 2) ? '✅ 有效' : '⚠️ 需优化'}
- 系统稳定性: ${(finalStats?.trading.successRate || 0) >= 90 ? '✅ 稳定' : '⚠️ 需改进'}
- 风险控制: ${finalStats?.trading.totalTrades ? '✅ 正常' : '❌ 异常'}
- ===============================================
- `)
- // 保存详细统计到文件
- const reportData = {
- experimentConfig: {
- duration: this.experimentDuration,
- startTime: this.startTime,
- endTime: Date.now(),
- },
- finalStats,
- hourlyStats: this.experimentStats.hourlyStats,
- tradeHistory: this.experimentStats.tradeHistory,
- }
- // 保存到 JSON 文件
- const fs = require('fs')
- const reportPath = `./volume_experiment_${new Date().getTime()}.json`
- fs.writeFileSync(reportPath, JSON.stringify(reportData, null, 2))
- console.log(`📁 详细报告已保存到: ${reportPath}`)
- if (this.system) {
- await this.system.shutdown()
- }
- process.exit(0)
- }
- }
- // 启动实验
- if (import.meta.url === `file://${process.argv[1]}`) {
- const experiment = new VolumeExperiment()
- experiment.runExperiment().catch(console.error)
- }
|