tanya/test/okxAli/candleList_test.go
zhangkun9038@dingtalk.com 446185f780 up
2025-03-07 12:44:09 +08:00

113 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package okx
import (
"fmt"
. "gitea.zjmud.xyz/phyer/tanya/okx"
"os"
"strconv"
"testing"
"time"
)
func makeCandleList(t *testing.T) (*CandleList, error) {
service := NewOkxPublicDataService()
layout := "2006-01-02 15:04:05"
// 使用2024年作为测试年份因为它是闰年
startTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
endTime := time.Date(2024, 12, 31, 0, 0, 0, 0, time.UTC)
t.Logf("Start Time: %s, End Time: %s", startTime.Format(layout), endTime.Format(layout))
var allCandles []*Candle
currentTime := endTime
for currentTime.After(startTime) || currentTime.Equal(startTime) {
t.Logf("Fetching candles after: %s", currentTime.Format(layout))
req := CandlesRequest{
InstID: "BTC-USDT",
Bar: "1D",
After: strconv.FormatInt(currentTime.UnixMilli(), 10),
Limit: "50",
}
t.Logf("Fetching candles with request: %+v", req)
candles, err := service.GetCandles(req)
if err != nil {
return nil, err
}
t.Logf("Fetched %d candles", len(candles))
// for _, candle := range candles {
// t.Logf("Candle: %+v", candle)
// }
if len(candles) == 0 {
t.Logf("No more candles to fetch")
break
}
allCandles = append(allCandles, candles...)
// 更新currentTime为最后一条数据的时间戳
lastCandleTime, _ := strconv.ParseInt(candles[len(candles)-1].Timestamp, 10, 64)
newCurrentTime := time.UnixMilli(lastCandleTime).Add(time.Millisecond)
if newCurrentTime.Equal(currentTime) || newCurrentTime.After(endTime) || len(allCandles) >= 366 {
t.Logf("No new candles fetched, reached end time, or got 366 candles, stopping the loop")
break
}
currentTime = newCurrentTime
t.Logf("Fetched %d candles, last candle time: %s", len(candles), time.UnixMilli(lastCandleTime).Format(layout))
}
fmt.Println("lens of allCandles: ", len(allCandles))
// for _, v := range allCandles {
// timestamp, _ := strconv.ParseInt(v.Timestamp, 10, 64)
// dateString := time.UnixMilli(timestamp).Format("2006-01-02 15:04:05")
// fmt.Printf("k, v: %d, %s\n", k, dateString)
// }
cl := &CandleList{Candles: allCandles}
return cl, nil
}
func TestCandleList_ToJson(t *testing.T) {
cl, err := makeCandleList(t)
if err != nil {
t.Fatalf("ToJson failed: %v", err)
}
jsonStr, err := cl.ToJson()
if err != nil {
t.Fatalf("ToJson failed: %v", err)
}
// Write to temp file
tmpFile := "/tmp/candle_test.json"
err = os.WriteFile(tmpFile, []byte(jsonStr), 0644)
if err != nil {
t.Fatalf("Failed to write json file: %v", err)
}
t.Logf("JSON output written to %s", tmpFile)
}
func TestCandleList_ToCsv(t *testing.T) {
cl, err := makeCandleList(t)
if err != nil {
t.Fatalf("ToJson failed: %v", err)
}
csvStr, err := cl.ToCsv()
if err != nil {
t.Fatalf("ToCsv failed: %v", err)
}
// Write to temp file
tmpFile := "/tmp/candle_test.csv"
err = os.WriteFile(tmpFile, []byte(csvStr), 0644)
if err != nil {
t.Fatalf("Failed to write csv file: %v", err)
}
t.Logf("CSV output written to %s", tmpFile)
}