You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
4.6 KiB
184 lines
4.6 KiB
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
// 1. 加载账号配置
|
|
fmt.Println("正在加载账号配置...")
|
|
config, err := LoadAccountConfig("account.json") // 注意:现在是.json,不是.txt
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 2. 加载Alpha表达式
|
|
fmt.Println("正在加载Alpha表达式...")
|
|
alphaList, err := LoadAlphaList("alphas.json")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if len(alphaList) == 0 {
|
|
fmt.Println("alphas.json 文件为空")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 3. 提取表达式(类似Python的提取逻辑)
|
|
var expressions []map[string]interface{}
|
|
for _, item := range alphaList {
|
|
expr := map[string]interface{}{
|
|
"expression": item["regular"],
|
|
"settings": item["settings"],
|
|
"type": item["type"],
|
|
}
|
|
expressions = append(expressions, expr)
|
|
}
|
|
|
|
// 4. 创建客户端并登录
|
|
client := NewBrainClient(config.Username, config.Password)
|
|
|
|
fmt.Println("\n正在登录Brain API...")
|
|
err = client.Login()
|
|
if err != nil {
|
|
fmt.Printf("登录失败: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 5. 开始模拟
|
|
fmt.Println("\n开始Alpha因子模拟...")
|
|
totalStart := time.Now()
|
|
var results []*SimulationResult
|
|
|
|
for i, item := range expressions {
|
|
fmt.Printf("\n%s\n", "============================================")
|
|
fmt.Printf("开始第 %d 个因子模拟 (共 %d 个)\n", i+1, len(expressions))
|
|
fmt.Printf("因子: %v\n", item)
|
|
fmt.Printf("%s\n", "============================================")
|
|
|
|
// 获取表达式和设置
|
|
expression, _ := item["expression"].(string)
|
|
settings, _ := item["settings"].(map[string]interface{})
|
|
|
|
// 执行模拟
|
|
result, err := client.SimulateAlpha(expression, settings)
|
|
if err != nil {
|
|
// 处理错误
|
|
result = &SimulationResult{
|
|
Status: "failed",
|
|
Expression: expression,
|
|
Message: err.Error(),
|
|
TimeCost: 0,
|
|
FormattedTime: "0秒",
|
|
Timestamp: time.Now().Format("2006-01-02 15:04:05"),
|
|
}
|
|
}
|
|
|
|
results = append(results, result)
|
|
|
|
// 打印结果
|
|
printResult(result)
|
|
|
|
// 最后一个不等待
|
|
if i < len(expressions)-1 {
|
|
sleepTime := 3.0 // 固定等待3秒
|
|
fmt.Printf("\n等待 %.2f 秒后开始下一个...\n", sleepTime)
|
|
time.Sleep(time.Duration(sleepTime) * time.Second)
|
|
}
|
|
}
|
|
|
|
totalTime := time.Since(totalStart).Seconds()
|
|
|
|
// 6. 打印汇总
|
|
printSummary(results, totalTime)
|
|
|
|
// 7. 保存结果
|
|
saveResults(results)
|
|
}
|
|
|
|
// 打印单个结果
|
|
func printResult(r *SimulationResult) {
|
|
if r.Status == "success" {
|
|
fmt.Printf("✓ 因子模拟成功: %s\n", r.Expression)
|
|
fmt.Printf(" 耗时: %s,Alpha ID: %s\n", r.FormattedTime, r.AlphaID)
|
|
|
|
if r.Metrics != nil {
|
|
fmt.Println(" 关键指标:")
|
|
if r.Metrics.SharpeRatio != nil {
|
|
fmt.Printf(" 夏普比率: %.4f\n", *r.Metrics.SharpeRatio)
|
|
}
|
|
if r.Metrics.AnnualReturn != nil {
|
|
fmt.Printf(" 年化收益: %.4f\n", *r.Metrics.AnnualReturn)
|
|
}
|
|
}
|
|
} else {
|
|
fmt.Printf("✗ 因子模拟失败: %s\n", r.Expression)
|
|
fmt.Printf(" 耗时: %s,错误: %s\n", r.FormattedTime, r.Message)
|
|
}
|
|
}
|
|
|
|
// 打印汇总
|
|
func printSummary(results []*SimulationResult, totalTime float64) {
|
|
fmt.Printf("\n%s\n", "============================================")
|
|
fmt.Println("模拟结果汇总")
|
|
fmt.Printf("%s\n", "============================================")
|
|
|
|
success := 0
|
|
failed := 0
|
|
for _, r := range results {
|
|
if r.Status == "success" {
|
|
success++
|
|
} else {
|
|
failed++
|
|
}
|
|
}
|
|
|
|
fmt.Printf("总模拟因子数: %d\n", len(results))
|
|
fmt.Printf("成功: %d 个\n", success)
|
|
fmt.Printf("失败: %d 个\n", failed)
|
|
fmt.Printf("总耗时: %s\n", formatTime(totalTime))
|
|
fmt.Printf("%s\n", "============================================")
|
|
|
|
for i, r := range results {
|
|
mark := "✓"
|
|
if r.Status != "success" {
|
|
mark = "✗"
|
|
}
|
|
fmt.Printf("%d. %s %s\n", i+1, mark, r.Expression)
|
|
fmt.Printf(" 状态: %s\n", r.Status)
|
|
fmt.Printf(" 耗时: %s\n", r.FormattedTime)
|
|
fmt.Printf(" Alpha ID: %s\n", r.AlphaID)
|
|
if r.Status != "success" {
|
|
fmt.Printf(" 原因: %s\n", r.Message)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
// 保存结果
|
|
func saveResults(results []*SimulationResult) {
|
|
// 创建result目录
|
|
os.MkdirAll("./result", 0755)
|
|
|
|
filename := fmt.Sprintf("result/simulation_results-%d.json", time.Now().Unix())
|
|
|
|
// 转换为JSON并保存
|
|
data, err := json.MarshalIndent(results, "", " ")
|
|
if err != nil {
|
|
fmt.Printf("保存结果失败: %v\n", err)
|
|
return
|
|
}
|
|
|
|
err = os.WriteFile(filename, data, 0644)
|
|
if err != nil {
|
|
fmt.Printf("保存结果失败: %v\n", err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("结果已保存到 %s\n", filename)
|
|
}
|
|
|