From b00f1b65d54a8b9d7f5781c6811a414030cfd9d5 Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Wed, 16 Apr 2025 10:44:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=8C=89=E8=82=A1=E7=A5=A8?= =?UTF-8?q?=E5=A4=84=E7=90=86,=E4=B8=8D=E7=94=A8=E9=85=8D=E7=BD=AEDefaultC?= =?UTF-8?q?odes,=E5=9F=BA=E9=87=91=E6=89=8D=E9=9C=80=E8=A6=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 51 ++++++++++++++++++++++------------------- codes.go | 3 ++- protocol/model_quote.go | 6 ++--- protocol/types_price.go | 7 ++++++ 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/client.go b/client.go index 50a2d72..9556751 100644 --- a/client.go +++ b/client.go @@ -235,12 +235,18 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp, // GetQuote 获取盘口五档报价 func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) { - if DefaultCodes == nil { - return nil, errors.New("DefaultCodes未初始化") - } for i := range codes { - codes[i] = DefaultCodes.AddExchange(codes[i]) + //如果是股票代码,则加上前缀 + codes[i] = protocol.AddPrefix(codes[i]) + if !protocol.IsStock(codes[i]) { + if DefaultCodes == nil { + return nil, errors.New("DefaultCodes未初始化") + } + //不是股票代码的话,根据codes的信息加上前缀 + codes[i] = DefaultCodes.AddExchange(codes[i]) + } } + f, err := protocol.MQuote.Frame(codes...) if err != nil { return nil, err @@ -256,26 +262,25 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) { if len(quotes) != len(codes) { return nil, fmt.Errorf("预期%d个,实际%d个", len(codes), len(quotes)) } - if DefaultCodes == nil { - return nil, errors.New("DefaultCodes未初始化") - } for i, code := range codes { - m := DefaultCodes.Get(code) - if m == nil { - return nil, fmt.Errorf("未查询到代码[%s]相关信息", code) - } - for ii, v := range quotes[i].SellLevel { - quotes[i].SellLevel[ii].Price = m.Price(v.Price) - } - 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), + if !protocol.IsStock(code) { + m := DefaultCodes.Get(code) + if m == nil { + return nil, fmt.Errorf("未查询到代码[%s]相关信息", code) + } + for ii, v := range quotes[i].SellLevel { + quotes[i].SellLevel[ii].Price = m.Price(v.Price) + } + 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), + } } } } diff --git a/codes.go b/codes.go index e660f4a..2a02f9d 100644 --- a/codes.go +++ b/codes.go @@ -314,7 +314,8 @@ func (this *CodeModel) FullCode() string { } func (this *CodeModel) Price(p protocol.Price) protocol.Price { - return p * protocol.Price(math.Pow10(int(3-this.Decimal))) + return protocol.Price(float64(p) * math.Pow10(int(2-this.Decimal))) + return p * protocol.Price(math.Pow10(int(2-this.Decimal))) } func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error { diff --git a/protocol/model_quote.go b/protocol/model_quote.go index 874011c..6794e37 100644 --- a/protocol/model_quote.go +++ b/protocol/model_quote.go @@ -47,7 +47,7 @@ type Quote struct { func (this *Quote) String() string { return fmt.Sprintf(`%s%s %s -总量:%s, 现量:%s, 总金额:%s, 内盘:%s, 外盘:%s +总手:%s, 现量:%s, 总金额:%s, 内盘:%s, 外盘:%s %s%s `, this.Exchange.String(), this.Code, this.K, @@ -142,9 +142,9 @@ func (this quote) Decode(bs []byte) QuotesResp { sellLevel := PriceLevel{} bs, p = GetPrice(bs) - buyLevel.Price = p + sec.K.Close + buyLevel.Price = p*10 + sec.K.Close bs, p = GetPrice(bs) - sellLevel.Price = p + sec.K.Close + sellLevel.Price = p*10 + sec.K.Close bs, buyLevel.Number = CutInt(bs) bs, sellLevel.Number = CutInt(bs) diff --git a/protocol/types_price.go b/protocol/types_price.go index 6547d7e..490a73c 100644 --- a/protocol/types_price.go +++ b/protocol/types_price.go @@ -78,6 +78,13 @@ func DecodeK(bs []byte) ([]byte, K) { bs, k.Low = GetPrice(bs) k.Low += k.Close + //默认按股票展示 + k.Last *= 10 + k.Open *= 10 + k.Close *= 10 + k.High *= 10 + k.Low *= 10 + return bs, k }