From 4114bc72dd13f39784ab7d93a6eba4bf99465c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=B1=E7=BA=AF=E5=87=80?= <1113655791@qq.com> Date: Mon, 17 Mar 2025 21:51:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B4=E6=97=B6=E8=A7=A3=E5=86=B3=E7=9B=98?= =?UTF-8?q?=E5=8F=A3=E4=BB=B7=E6=A0=BC=E4=B8=8D=E5=AF=B9=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98,=E6=84=9F=E8=A7=89=E4=B8=8D=E6=98=AF=E5=BE=88?= =?UTF-8?q?=E5=90=88=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 69 ++++++++++++++++++++++++++++++++++++------------------- codes.go | 48 ++++++++++++++++++++++++++------------ 2 files changed, 79 insertions(+), 38 deletions(-) diff --git a/client.go b/client.go index 9b025d4..3018d7c 100644 --- a/client.go +++ b/client.go @@ -239,7 +239,7 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) { } quotes := result.(protocol.QuotesResp) - { //临时处理下先,后续优化 + { //todo 临时处理下先,后续优化,感觉有问题 //判断长度和预期是否一致 if len(quotes) != len(codes) { return nil, fmt.Errorf("预期%d个,实际%d个", len(codes), len(quotes)) @@ -258,6 +258,13 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) { for ii, v := range quotes[i].BuyLevel { quotes[i].BuyLevel[ii].Price = m.Price(v.Price) } + quotes[i].K = protocol.K{ + Last: m.Price(quotes[i].K.Last), + Open: m.Price(quotes[i].K.Open), + High: m.Price(quotes[i].K.High), + Low: m.Price(quotes[i].K.Low), + Close: m.Price(quotes[i].K.Close), + } } } @@ -360,6 +367,12 @@ func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.Histo return resp, nil } +/* + + + + */ + // GetIndex 获取指数,接口是和k线一样的,但是解析不知道怎么区分(解析方式不一致),所以加一个方法 func (this *Client) GetIndex(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) { code = protocol.AddPrefix(code) @@ -380,7 +393,7 @@ func (this *Client) GetIndexUntil(Type uint8, code string, f func(k *protocol.Kl size := uint16(800) var last *protocol.Kline for start := uint16(0); ; start += size { - r, err := this.GetKline(Type, code, start, size) + r, err := this.GetIndex(Type, code, start, size) if err != nil { return nil, err } @@ -406,6 +419,11 @@ func (this *Client) GetIndexUntil(Type uint8, code string, f func(k *protocol.Kl return resp, nil } +// GetIndexAll 获取全部k线数据 +func (this *Client) GetIndexAll(Type uint8, code string) (*protocol.KlineResp, error) { + return this.GetIndexUntil(Type, code, func(k *protocol.Kline) bool { return false }) +} + func (this *Client) GetIndexDay(code string, start, count uint16) (*protocol.KlineResp, error) { return this.GetIndex(protocol.TypeKlineDay, code, start, count) } @@ -414,6 +432,31 @@ func (this *Client) GetIndexDayUntil(code string, f func(k *protocol.Kline) bool return this.GetIndexUntil(protocol.TypeKlineDay, code, f) } +func (this *Client) GetIndexDayAll(code string) (*protocol.KlineResp, error) { + return this.GetIndexAll(protocol.TypeKlineDay, code) +} + +func (this *Client) GetIndexWeekAll(code string) (*protocol.KlineResp, error) { + return this.GetIndexAll(protocol.TypeKlineWeek, code) +} + +func (this *Client) GetIndexMonthAll(code string) (*protocol.KlineResp, error) { + return this.GetIndexAll(protocol.TypeKlineMonth, code) +} + +func (this *Client) GetIndexQuarterAll(code string) (*protocol.KlineResp, error) { + return this.GetIndexAll(protocol.TypeKlineQuarter, code) +} + +func (this *Client) GetIndexYearAll(code string) (*protocol.KlineResp, error) { + return this.GetIndexAll(protocol.TypeKlineYear, code) +} + +/* + + + */ + // GetKline 获取k线数据,推荐收盘之后获取,否则会获取到当天的数据 func (this *Client) GetKline(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) { code = protocol.AddPrefix(code) @@ -462,27 +505,7 @@ func (this *Client) GetKlineUntil(Type uint8, code string, f func(k *protocol.Kl // GetKlineAll 获取全部k线数据 func (this *Client) GetKlineAll(Type uint8, code string) (*protocol.KlineResp, error) { - resp := &protocol.KlineResp{} - size := uint16(800) - var last *protocol.Kline - for start := uint16(0); ; start += size { - r, err := this.GetKline(Type, code, start, size) - if err != nil { - return nil, err - } - if last != nil && len(r.List) > 0 { - last.Last = r.List[len(r.List)-1].Close - } - if len(r.List) > 0 { - last = r.List[0] - } - resp.Count += r.Count - resp.List = append(r.List, resp.List...) - if r.Count < size { - break - } - } - return resp, nil + return this.GetKlineUntil(Type, code, func(k *protocol.Kline) bool { return false }) } // GetKlineMinute 获取一分钟k线数据,每次最多800条,最多只能获取24000条数据 diff --git a/codes.go b/codes.go index 4b4a3e1..2d1e4ee 100644 --- a/codes.go +++ b/codes.go @@ -40,7 +40,7 @@ func NewCodes(c *Client, filename string) (*Codes, error) { } update := new(UpdateModel) - { //插入一条数据 + { //查询或者插入一条数据 has, err := db.Get(update) if err != nil { return nil, err @@ -57,22 +57,38 @@ func NewCodes(c *Client, filename string) (*Codes, error) { Codes: nil, } - //设置定时器,每天早上9点更新数据 - task := cron.New(cron.WithSeconds()) - task.AddFunc("0 0 9 * * *", func() { - for i := 0; i < 3; i++ { - if err := cc.Update(); err == nil { - return + { //设置定时器,每天早上9点更新数据 + task := cron.New(cron.WithSeconds()) + task.AddFunc("0 0 9 * * *", func() { + for i := 0; i < 3; i++ { + if err := cc.Update(); err == nil { + return + } + logs.Err(err) + <-time.After(time.Minute * 5) + } + }) + task.Start() + } + + { //判断是否更新过,更新过则不更新 + now := time.Now() + node := time.Date(now.Year(), now.Month(), now.Day(), 9, 0, 0, 0, time.Local) + updateTime := time.Unix(update.Time, 0) + if now.Sub(node) > 0 { + //当前时间在9点之后,且更新时间在9点之前,需要更新 + if updateTime.Sub(node) < 0 { + return cc, cc.Update() + } + } else { + //当前时间在9点之前,且更新时间在上个节点之前 + if updateTime.Sub(node.Add(time.Hour*24)) < 0 { + return cc, cc.Update() } - logs.Err(err) - <-time.After(time.Minute * 5) } - }) - task.Start() - - //判断是否更新过,更新过则不更新 - //time.Unix(update.Time,0).A + } + //从缓存中加载 return cc, cc.Update(true) } @@ -118,7 +134,9 @@ func (this *Codes) Update(byCache ...bool) error { codeMap[code.Exchange+code.Code] = code } this.Codes = codeMap - return nil + //更新时间 + _, err = this.db.Update(&UpdateModel{Time: time.Now().Unix()}) + return err } // Code 更新股票并返回结果