dev 2
This commit is contained in:
@@ -2,8 +2,10 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -12,23 +14,22 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
BaseURL = "http://127.0.0.1:10086"
|
||||
Token = "QMTbyYanweidong"
|
||||
BaseURL = "http://127.0.0.1:10086"
|
||||
Token = "QMTbyYanweidong"
|
||||
AccountType = "stock"
|
||||
PassCodes = []string{}
|
||||
Timeout = 15 * time.Second
|
||||
Timeout = 15 * time.Second
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := sdk.New(BaseURL, Token, AccountType, Timeout)
|
||||
client := sdk.New(BaseURL, Token, Timeout).SetAccountType(AccountType)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), Timeout)
|
||||
defer cancel()
|
||||
|
||||
assets, err := client.Assets(ctx, AccountType)
|
||||
assets, err := client.Assets(ctx)
|
||||
if err != nil {
|
||||
fatal("获取资产失败: %v", err)
|
||||
}
|
||||
positions, err := client.Positions(ctx, AccountType)
|
||||
positions, err := client.Positions(ctx)
|
||||
if err != nil {
|
||||
fatal("获取持仓失败: %v", err)
|
||||
}
|
||||
@@ -54,12 +55,38 @@ func main() {
|
||||
)
|
||||
}
|
||||
|
||||
printTicks(client, Timeout, PassCodes)
|
||||
codes := loadPassCodes()
|
||||
printTicks(client, codes)
|
||||
// if _, err := client.Shutdown(ctx); err != nil {
|
||||
// fatal("关闭服务失败: %v", err)
|
||||
// }
|
||||
// fmt.Println("【服务】已关闭")
|
||||
}
|
||||
|
||||
func printTicks(client *sdk.Client, timeout time.Duration, codes []string) {
|
||||
func loadPassCodes() []string {
|
||||
dir := strings.TrimSpace(os.Getenv("QMT_DATA_DIR"))
|
||||
if dir == "" {
|
||||
fatal("环境变量 QMT_DATA_DIR 为空")
|
||||
}
|
||||
path := filepath.Join(dir, "pass_codes.json")
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
fatal("读取 %s 失败: %v", path, err)
|
||||
}
|
||||
var codes []string
|
||||
if err := json.Unmarshal(raw, &codes); err != nil {
|
||||
fatal("解析 %s 失败: %v", path, err)
|
||||
}
|
||||
return codes
|
||||
}
|
||||
|
||||
func printTicks(client *sdk.Client, codes []string) {
|
||||
fmt.Println(strings.Repeat("-", 80))
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
if len(codes) == 0 {
|
||||
fmt.Println("【行情】pass_codes.json 为空,跳过")
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), Timeout)
|
||||
defer cancel()
|
||||
ticks, err := client.FullTick(ctx, codes)
|
||||
if err != nil {
|
||||
@@ -83,18 +110,6 @@ func printTicks(client *sdk.Client, timeout time.Duration, codes []string) {
|
||||
}
|
||||
}
|
||||
|
||||
func splitCSV(s string) []string {
|
||||
parts := strings.Split(s, ",")
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
p = strings.TrimSpace(p)
|
||||
if p != "" {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func rawStr(m map[string]any, names ...string) string {
|
||||
for _, name := range names {
|
||||
if v, ok := m[name]; ok && v != nil {
|
||||
@@ -104,13 +119,6 @@ func rawStr(m map[string]any, names ...string) string {
|
||||
return "-"
|
||||
}
|
||||
|
||||
func envOr(key, fallback string) string {
|
||||
if v := strings.TrimSpace(os.Getenv(key)); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func fatal(format string, args ...any) {
|
||||
fmt.Fprintf(os.Stderr, format+"\n", args...)
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user