zhangkun9038@dingtalk.com 81fd93fb75 fix++
2025-10-28 02:17:38 +08:00
..
2025-10-28 00:46:12 +08:00
2025-10-28 02:17:38 +08:00
2025-10-27 10:25:36 +08:00
2025-10-27 10:25:36 +08:00
2025-10-28 00:46:12 +08:00
2025-10-27 09:01:04 +08:00

Go服务端实现

项目概述

本项目实现了hyperopt工作流的服务端基于Go语言开发。服务端通过Redis channel接收消息和发布结果不提供RESTful API接口。

目录结构

goflow/
├── model/      # 共享数据模型
├── server/     # 服务端代码
├── go.mod      # Go模块定义
└── README.md   # 使用说明

快速开始

启动服务端

cd goflow/server
go run server.go --redis-addr=localhost:6379 --redis-db=0

命令行参数

  • --redis-addr: Redis服务器地址默认为localhost:6379
  • --redis-password: Redis密码默认为空
  • --redis-db: Redis数据库索引默认为0

Redis频道

服务端使用以下Redis频道

订阅频道(接收任务)

  1. hyperopt_channel - 接收hyperopt任务
  2. backtest_channel - 接收回测任务

发布频道(结果输出)

  1. hyperopt_result_channel - 发布hyperopt结果
  2. hyperopt_response_channel - 发布hyperopt完整响应
  3. backtest_result_channel - 发布回测结果

数据模型

Hyperopt事件

{
  "task_id": "task-123",
  "strategy_name": "MyStrategy",
  "time_frame": "5m",
  "start_time": "2023-01-01T00:00:00Z",
  "properties_collections": [
    {
      "buy_rsi": {
        "optimize": true,
        "min": 20.0,
        "max": 40.0,
        "step": 1.0
      }
    }
  ]
}

回测事件

{
  "task_id": "task-456",
  "strategy_name": "MyStrategy",
  "time_frame": "5m",
  "start_time": "2023-01-01T00:00:00Z",
  "parameters": {
    "buy_rsi": 30.0,
    "buy_rsi_enabled": 1.0
  },
  "is_pre_test": true,
  "is_post_test": false
}

工作流程

  1. 服务端启动后连接到Redis服务器
  2. 订阅hyperopt_channel和backtest_channel频道
  3. 接收任务消息并异步处理
  4. 执行hyperopt或backtest任务当前为模拟实现
  5. 将结果发布到对应的结果频道

注意事项

  1. 当前实现为模拟版本实际使用时需要修改服务端代码集成到freqtrade的hyperopt和回测功能
  2. 支持多阶段优化策略每个fullHyperoptEvent会生成多个hyperoptResult子任务
  3. 所有子任务都关联到同一个fullHyperoptEvent ID
  4. 生产环境建议使用ElasticSearch存储历史结果
  5. 需要确保Redis服务器已安装并运行