This commit is contained in:
2026-08-25 18:59:18 +08:00
parent fa62436a73
commit ec58641d09
23 changed files with 563 additions and 944 deletions

39
go-client/libs/http.go Normal file
View File

@@ -0,0 +1,39 @@
package libs
func getJSON(rawURL string, params url.Values, timeout time.Duration) (map[string]any, error) {
if timeout <= 0 {
timeout = 5 * time.Second
}
if params != nil {
if strings.Contains(rawURL, "?") {
rawURL += "&" + params.Encode()
} else {
rawURL += "?" + params.Encode()
}
}
req, err := http.NewRequest(http.MethodGet, rawURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "big-qmt-go/1")
client := &http.Client{Timeout: timeout}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("http %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
}
out := map[string]any{}
if err := json.Unmarshal(body, &out); err != nil {
return nil, err
}
return out, nil
}

58
go-client/libs/market.go Normal file
View File

@@ -0,0 +1,58 @@
package libs
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
)
// AllowOpen 每次开仓或补仓前取 60 分钟大盘信号,只有 UP 才放行。
func AllowOpen(apiHost string, timeout time.Duration) bool {
rawURL := strings.TrimRight(apiHost, "/") + "/a/market"
payload, err := getJSON(rawURL, url.Values{"period": {"60m"}}, timeout)
if err != nil {
log.Printf("[ERROR] 获取60m大盘信号失败: %s %v", rawURL, err)
return false
}
status := Status(payload)
log.Printf("[INFO] 大盘信号: url=%s status=%s", rawURL, status)
return status == "UP"
}
// Status 兼容常见响应结构;无法识别的值统一按 UNKNOWN 处理。
func Status(payload map[string]any) string {
var value any = payload
if m, ok := value.(map[string]any); ok {
if d, exists := m["data"]; exists {
value = d
}
}
if arr, ok := value.([]any); ok {
if len(arr) == 0 {
value = nil
} else {
value = arr[len(arr)-1]
}
}
if m, ok := value.(map[string]any); ok {
if v, exists := m["action"]; exists {
value = v
} else if v, exists := m["status"]; exists {
value = v
} else if v, exists := m["signal"]; exists {
value = v
}
}
s := strings.ToUpper(strings.TrimSpace(fmt.Sprint(value)))
switch s {
case "UP", "DOWN", "NEUTRAL":
return s
default:
return "UNKNOWN"
}
}