volume_experiment.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/usr/bin/env tsx
  2. /**
  3. * 长时间刷量交易实验
  4. * 用于验证刷量策略的长期效果
  5. */
  6. import { CompleteTradingSystem } from './src/main-complete.js'
  7. class VolumeExperiment {
  8. private system?: CompleteTradingSystem
  9. private startTime = Date.now()
  10. private experimentDuration = 10 * 60 * 1000 // 10分钟实验
  11. private logInterval = 30 * 1000 // 每30秒记录一次统计
  12. private experimentStats = {
  13. startTime: Date.now(),
  14. totalExperimentTrades: 0,
  15. totalExperimentVolume: 0,
  16. tradeHistory: [] as any[],
  17. hourlyStats: [] as any[],
  18. }
  19. async runExperiment() {
  20. console.log(`
  21. 🧪 长期刷量交易实验开始
  22. ===============================================
  23. 实验时长: ${this.experimentDuration / 60000}分钟
  24. 数据记录间隔: ${this.logInterval / 1000}秒
  25. 实验目标: 验证刷量交易策略的持续效果
  26. ===============================================
  27. `)
  28. try {
  29. // 启动完整交易系统
  30. this.system = new CompleteTradingSystem()
  31. // 监听交易事件
  32. this.system.on('trade_executed', (data: any) => {
  33. this.recordTrade(data)
  34. })
  35. await this.system.start()
  36. // 定期记录统计数据
  37. const statsTimer = setInterval(() => {
  38. this.logExperimentStats()
  39. }, this.logInterval)
  40. // 设置实验结束定时器
  41. setTimeout(() => {
  42. clearInterval(statsTimer)
  43. this.endExperiment()
  44. }, this.experimentDuration)
  45. console.log('✅ 长期实验已启动,系统将自动运行...')
  46. console.log(`⏰ 实验将在 ${this.experimentDuration / 60000} 分钟后自动结束`)
  47. } catch (error: any) {
  48. console.error('❌ 实验启动失败:', error.message)
  49. process.exit(1)
  50. }
  51. }
  52. private recordTrade(trade: any) {
  53. this.experimentStats.tradeHistory.push({
  54. timestamp: Date.now(),
  55. ...trade,
  56. })
  57. this.experimentStats.totalExperimentTrades++
  58. this.experimentStats.totalExperimentVolume += parseFloat(trade.amount || '0')
  59. }
  60. private logExperimentStats() {
  61. const runtime = Date.now() - this.startTime
  62. const runtimeMinutes = Math.floor(runtime / 60000)
  63. const systemStats = this.system?.getSystemStats()
  64. const currentStats = {
  65. timestamp: Date.now(),
  66. runtime: runtimeMinutes,
  67. totalTrades: systemStats?.trading.totalTrades || 0,
  68. successfulTrades: systemStats?.trading.successfulTrades || 0,
  69. totalVolume: systemStats?.trading.totalVolume || 0,
  70. successRate: systemStats?.trading.successRate || 0,
  71. tradesPerMinute: systemStats?.trading.totalTrades
  72. ? (systemStats.trading.totalTrades / Math.max(runtimeMinutes, 1)).toFixed(2)
  73. : '0.00',
  74. }
  75. this.experimentStats.hourlyStats.push(currentStats)
  76. // 清屏并显示实验统计
  77. process.stdout.write('\u001B[2J\u001B[0;0f')
  78. console.log(`🧪 长期刷量交易实验 - 实时统计`)
  79. console.log('='.repeat(80))
  80. console.log(`⏰ 实验进度`)
  81. console.log(` 运行时间: ${runtimeMinutes}/${this.experimentDuration / 60000}分钟`)
  82. console.log(` 完成度: ${((runtime / this.experimentDuration) * 100).toFixed(1)}%`)
  83. console.log(`\n📊 累计交易统计`)
  84. console.log(` 总交易数: ${currentStats.totalTrades}`)
  85. console.log(` 成功交易: ${currentStats.successfulTrades}`)
  86. console.log(` 失败交易: ${currentStats.totalTrades - currentStats.successfulTrades}`)
  87. console.log(` 总交易量: ${currentStats.totalVolume} BTC`)
  88. console.log(` 成功率: ${currentStats.successRate}%`)
  89. console.log(`\n⚡ 交易效率`)
  90. console.log(` 每分钟交易数: ${currentStats.tradesPerMinute}`)
  91. console.log(` 预期小时交易数: ${(parseFloat(currentStats.tradesPerMinute) * 60).toFixed(0)}`)
  92. if (this.experimentStats.hourlyStats.length > 1) {
  93. const recent = this.experimentStats.hourlyStats.slice(-2)
  94. const recentTradeDiff = recent[1].totalTrades - recent[0].totalTrades
  95. const timeDiff = (recent[1].timestamp - recent[0].timestamp) / 1000 / 60
  96. const recentRate = (recentTradeDiff / timeDiff).toFixed(2)
  97. console.log(` 最近${this.logInterval / 1000}s交易率: ${recentRate}笔/分钟`)
  98. }
  99. console.log(`\n🎯 刷量效果评估`)
  100. const expectedMinimumTrades = Math.floor(runtimeMinutes * 2) // 期望每分钟至少2笔交易
  101. const actualTrades = currentStats.totalTrades
  102. const efficiency = actualTrades >= expectedMinimumTrades ? '✅ 达标' : '⚠️ 低于预期'
  103. console.log(` 预期最少交易数: ${expectedMinimumTrades}`)
  104. console.log(` 实际交易数: ${actualTrades}`)
  105. console.log(` 刷量效率: ${efficiency}`)
  106. if (runtimeMinutes > 0) {
  107. const volumePerMinute = (currentStats.totalVolume / runtimeMinutes).toFixed(4)
  108. console.log(` 每分钟交易量: ${volumePerMinute} BTC`)
  109. console.log(` 预期小时交易量: ${(parseFloat(volumePerMinute) * 60).toFixed(2)} BTC`)
  110. }
  111. console.log(`\n📈 趋势分析`)
  112. if (this.experimentStats.hourlyStats.length >= 3) {
  113. const last3Stats = this.experimentStats.hourlyStats.slice(-3)
  114. const tradeGrowth = last3Stats.map(s => s.totalTrades)
  115. const isIncreasing = tradeGrowth[2] > tradeGrowth[1] && tradeGrowth[1] > tradeGrowth[0]
  116. console.log(` 交易趋势: ${isIncreasing ? '📈 上升' : '📊 稳定'}`)
  117. const avgRate = last3Stats.map(s => parseFloat(s.tradesPerMinute)).reduce((a, b) => a + b) / 3
  118. console.log(` 平均交易率: ${avgRate.toFixed(2)}笔/分钟`)
  119. }
  120. console.log(`\n⏰ 最后更新: ${new Date().toLocaleTimeString()}`)
  121. console.log('='.repeat(80))
  122. console.log(`💡 实验持续进行中... 按 Ctrl+C 提前结束`)
  123. }
  124. private async endExperiment() {
  125. console.log('\n🏁 实验完成!正在生成最终报告...')
  126. const finalStats = this.system?.getSystemStats()
  127. const totalRuntime = Date.now() - this.startTime
  128. const totalMinutes = totalRuntime / 60000
  129. console.log(`
  130. 🧪 长期刷量交易实验 - 最终报告
  131. ===============================================
  132. 实验时长: ${totalMinutes.toFixed(1)}分钟
  133. 开始时间: ${new Date(this.startTime).toLocaleString()}
  134. 结束时间: ${new Date().toLocaleString()}
  135. ===============================================
  136. 📊 总体统计:
  137. 总交易数: ${finalStats?.trading.totalTrades || 0}
  138. 成功交易: ${finalStats?.trading.successfulTrades || 0}
  139. 失败交易: ${(finalStats?.trading.totalTrades || 0) - (finalStats?.trading.successfulTrades || 0)}
  140. 总交易量: ${finalStats?.trading.totalVolume || 0} BTC
  141. 成功率: ${finalStats?.trading.successRate || 0}%
  142. ⚡ 效率分析:
  143. 平均每分钟: ${((finalStats?.trading.totalTrades || 0) / totalMinutes).toFixed(2)} 笔
  144. 预期每小时: ${(((finalStats?.trading.totalTrades || 0) / totalMinutes) * 60).toFixed(0)} 笔
  145. 每分钟交易量: ${((finalStats?.trading.totalVolume || 0) / totalMinutes).toFixed(4)} BTC
  146. 预期小时交易量: ${(((finalStats?.trading.totalVolume || 0) / totalMinutes) * 60).toFixed(2)} BTC
  147. 🎯 刷量效果:
  148. 期望最少交易: ${Math.floor(totalMinutes * 2)}
  149. 实际完成交易: ${finalStats?.trading.totalTrades || 0}
  150. 效率达成率: ${(((finalStats?.trading.totalTrades || 0) / Math.max(Math.floor(totalMinutes * 2), 1)) * 100).toFixed(
  151. 1,
  152. )}%
  153. 💡 结论:
  154. 刷量策略: ${(finalStats?.trading.totalTrades || 0) >= Math.floor(totalMinutes * 2) ? '✅ 有效' : '⚠️ 需优化'}
  155. 系统稳定性: ${(finalStats?.trading.successRate || 0) >= 90 ? '✅ 稳定' : '⚠️ 需改进'}
  156. 风险控制: ${finalStats?.trading.totalTrades ? '✅ 正常' : '❌ 异常'}
  157. ===============================================
  158. `)
  159. // 保存详细统计到文件
  160. const reportData = {
  161. experimentConfig: {
  162. duration: this.experimentDuration,
  163. startTime: this.startTime,
  164. endTime: Date.now(),
  165. },
  166. finalStats,
  167. hourlyStats: this.experimentStats.hourlyStats,
  168. tradeHistory: this.experimentStats.tradeHistory,
  169. }
  170. // 保存到 JSON 文件
  171. const fs = require('fs')
  172. const reportPath = `./volume_experiment_${new Date().getTime()}.json`
  173. fs.writeFileSync(reportPath, JSON.stringify(reportData, null, 2))
  174. console.log(`📁 详细报告已保存到: ${reportPath}`)
  175. if (this.system) {
  176. await this.system.shutdown()
  177. }
  178. process.exit(0)
  179. }
  180. }
  181. // 启动实验
  182. if (import.meta.url === `file://${process.argv[1]}`) {
  183. const experiment = new VolumeExperiment()
  184. experiment.runExperiment().catch(console.error)
  185. }