From 64fe4a18b07901b3f943d8df2eed51ef1dee8104 Mon Sep 17 00:00:00 2001 From: "zhangkun9038@dingtalk.com" Date: Fri, 7 Mar 2025 22:34:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E6=94=B9=E6=88=90st?= =?UTF-8?q?ring=E5=8F=AF=E8=AF=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- okx/candleList.go | 54 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/okx/candleList.go b/okx/candleList.go index d62d04d..5b0024d 100644 --- a/okx/candleList.go +++ b/okx/candleList.go @@ -9,6 +9,15 @@ import ( "time" ) +// formatTimestamp 将毫秒时间戳字符串转换为可读格式 +func formatTimestamp(ts string) (string, error) { + millis, err := strconv.ParseInt(ts, 10, 64) + if err != nil { + return "", err + } + return time.UnixMilli(millis).Format("2006-01-02 15:04:05"), nil +} + // CandleList 封装Candle数组并提供序列化方法 type CandleList struct { Candles []*Candle @@ -46,13 +55,35 @@ func MakeCandleList(instId string, period string, startTime time.Time, endTime t // ToJson 将Candle数组序列化为JSON字符串 func (cl *CandleList) ToJson() (string, error) { - // 调试信息 - fmt.Printf("Number of candles: %d\n", len(cl.Candles)) - // for i, candle := range cl.Candles { - // fmt.Printf("Writing candle %d: %+v\n", i, candle) - // } - // - jsonData, err := json.Marshal(cl.Candles) + // 创建临时结构体用于格式化时间戳 + type FormattedCandle struct { + Timestamp string `json:"timestamp"` + Open string `json:"open"` + High string `json:"high"` + Low string `json:"low"` + Close string `json:"close"` + Volume string `json:"volume"` + VolumeCcy string `json:"volumeCcy"` + } + + var formattedCandles []FormattedCandle + for _, candle := range cl.Candles { + formattedTs, err := formatTimestamp(candle.Timestamp) + if err != nil { + return "", err + } + formattedCandles = append(formattedCandles, FormattedCandle{ + Timestamp: formattedTs, + Open: candle.Open, + High: candle.High, + Low: candle.Low, + Close: candle.Close, + Volume: candle.Volume, + VolumeCcy: candle.VolumeCcy, + }) + } + + jsonData, err := json.Marshal(formattedCandles) if err != nil { return "", err } @@ -74,9 +105,12 @@ func (cl *CandleList) ToCsv() (string, error) { fmt.Printf("Number of candles: %d\n", len(cl.Candles)) for _, candle := range cl.Candles { - // fmt.Printf("Debug: Writing candle %d with Timestamp: %s\n", i, candle.Timestamp) + formattedTs, err := formatTimestamp(candle.Timestamp) + if err != nil { + return "", err + } record := []string{ - candle.Timestamp, + formattedTs, candle.Open, candle.High, candle.Low, @@ -87,8 +121,6 @@ func (cl *CandleList) ToCsv() (string, error) { if err := writer.Write(record); err != nil { return "", err } - // 调试信息 - // fmt.Printf("Writing candle %d: %+v\n", i, candle) } writer.Flush()