From 9c7923d939e5854db59db6c5bb215fd005bce72b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=B1=E7=BA=AF=E5=87=80?= <1113655791@qq.com> Date: Thu, 24 Oct 2024 22:23:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=A1=E5=AF=B9=E4=BA=86=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=EF=BC=8C=E4=B8=8A=E6=B5=B7=E7=9A=84=E6=B2=A1=E5=95=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E6=B7=B1=E5=9C=B3=E7=9A=84=E8=BF=98=E4=B8=8D?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 1 - protocol/const.go | 10 ------ protocol/model.go | 76 ++++++++++++++++++++++++++------------------- protocol/model_k.go | 8 +++-- protocol/types.go | 37 ++++++++++++++++++++++ protocol/unit.go | 56 ++++++++++++--------------------- 6 files changed, 107 insertions(+), 81 deletions(-) create mode 100644 protocol/types.go diff --git a/client.go b/client.go index 41d4437..fc22ac6 100644 --- a/client.go +++ b/client.go @@ -55,7 +55,6 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) { return } - logs.Debug(f.Type) switch f.Type { case protocol.TypeSecurityQuote: resp := protocol.MSecurityQuote.Decode(f.Data) diff --git a/protocol/const.go b/protocol/const.go index 22be980..b5ed4fb 100644 --- a/protocol/const.go +++ b/protocol/const.go @@ -1,15 +1,5 @@ package protocol -type Exchange uint8 - -func (this Exchange) Uint8() uint8 { return uint8(this) } - -const ( - ExchangeSH Exchange = iota //上海交易所 - ExchangeSZ //深圳交易所 - ExchangeBJ //北京交易所 -) - const ( Control = 0x01 ) diff --git a/protocol/model.go b/protocol/model.go index 6969c1a..42e11bf 100644 --- a/protocol/model.go +++ b/protocol/model.go @@ -3,7 +3,7 @@ package protocol import ( "errors" "fmt" - bytes2 "github.com/injoyai/base/bytes" + "strings" ) var ( @@ -85,27 +85,30 @@ func (securityList) Decode(bs []byte) (*SecurityListResp, error) { type SecurityQuotesResp []*SecurityQuote -type SecurityQuote struct { - Market uint8 // 市场 - Code string // 代码 - Active1 uint16 // 活跃度 - //Price float64 // 现价 - //Close float64 // 昨收 - //Open float64 // 开盘 - //High float64 // 最高 - //Low float64 // 最低 - K K //k线 +func (this SecurityQuotesResp) String() string { + ls := []string(nil) + for _, v := range this { + ls = append(ls, v.String()) + } + return strings.Join(ls, "\n") +} - ServerTime string // 时间 - ReversedBytes0 int // 保留(时间 ServerTime) - ReversedBytes1 int // 保留 - Vol int // 总量 - CurVol int // 现量 - Amount float64 // 总金额 - SVol int // 内盘 - BVol int // 外盘 - ReversedBytes2 int // 保留 - ReversedBytes3 int // 保留 +type SecurityQuote struct { + Exchange Exchange // 市场 + Code string // 代码 + Active1 uint16 // 活跃度 + K K //k线 + ServerTime string // 时间 + ReversedBytes0 int // 保留(时间 ServerTime) + ReversedBytes1 int // 保留 + TotalHand int // 总手(东财的盘口-总手) + Intuition int // 现量(东财的盘口-现量) + Amount float64 // 金额(东财的盘口-金额) + InsideDish int // 内盘(东财的盘口-外盘)(和东财对不上) + OuterDisc int // 外盘(东财的盘口-外盘)(和东财对不上) + + ReversedBytes2 int // 保留 + ReversedBytes3 int // 保留 BidLevels [5]PriceLevel AskLevels [5]PriceLevel Bid1 float64 @@ -138,6 +141,13 @@ type SecurityQuote struct { Active2 uint16 // 活跃度 } +func (this *SecurityQuote) String() string { + return this.K.String() + fmt.Sprintf(", 总量:%s, 现量:%s, 总金额:%s, 内盘:%s, 外盘:%s", + IntUnitString(this.TotalHand), IntUnitString(this.Intuition), FloatUnitString(this.Amount), + IntUnitString(this.InsideDish), IntUnitString(this.OuterDisc)) + "\n" + + fmt.Sprintf("%#v\n", this) +} + type securityQuote struct{} func (this securityQuote) Frame(m map[Exchange]string) (*Frame, error) { @@ -176,34 +186,36 @@ func (this securityQuote) Decode(bs []byte) SecurityQuotesResp { for i := uint16(0); i < number; i++ { sec := &SecurityQuote{ - Market: bs[0], - Code: string(UTF8ToGBK(bytes2.Reverse(bs[1:7]))), - Active1: Uint16(bs[7:9]), + Exchange: Exchange(bs[0]), + Code: string(UTF8ToGBK(bs[1:7])), + Active1: Uint16(bs[7:9]), } bs, sec.K = DecodeK(bs[9:]) bs, sec.ReversedBytes0 = CutInt(bs) sec.ServerTime = fmt.Sprintf("%d", sec.ReversedBytes0) bs, sec.ReversedBytes1 = CutInt(bs) - bs, sec.Vol = CutInt(bs) - bs, sec.CurVol = CutInt(bs) + bs, sec.TotalHand = CutInt(bs) + bs, sec.Intuition = CutInt(bs) sec.Amount = getVolume(Uint32(bs[:4])) - bs, sec.SVol = CutInt(bs[4:]) - bs, sec.BVol = CutInt(bs) + bs, sec.InsideDish = CutInt(bs[4:]) + bs, sec.OuterDisc = CutInt(bs) bs, sec.ReversedBytes2 = CutInt(bs) bs, sec.ReversedBytes3 = CutInt(bs) var p Price for i := 0; i < 5; i++ { bidele := PriceLevel{} + offerele := PriceLevel{} + bs, p = GetPrice(bs) bidele.Price = p + sec.K.Close - bs, bidele.Vol = CutInt(bs) - sec.BidLevels[i] = bidele - - offerele := PriceLevel{} bs, p = GetPrice(bs) offerele.Price = p + sec.K.Close + + bs, bidele.Vol = CutInt(bs) bs, offerele.Vol = CutInt(bs) + + sec.BidLevels[i] = bidele sec.AskLevels[i] = offerele } diff --git a/protocol/model_k.go b/protocol/model_k.go index 0272f95..d260b6e 100644 --- a/protocol/model_k.go +++ b/protocol/model_k.go @@ -17,8 +17,8 @@ func (this Price) String() string { } type PriceLevel struct { - Price Price - Vol int + Price Price //价 + Vol int //量,是否是成交量? } // K k线图 @@ -30,6 +30,10 @@ type K struct { Close Price //今日收盘价 } +func (this K) String() string { + return fmt.Sprintf("昨收:%0.2f, 今开:%0.2f, 最高:%0.2f, 最低:%0.2f, 今收:%0.2f", this.Last.Float64(), this.Open.Float64(), this.High.Float64(), this.Low.Float64(), this.Close.Float64()) +} + func DecodeK(bs []byte) ([]byte, K) { k := K{} diff --git a/protocol/types.go b/protocol/types.go new file mode 100644 index 0000000..fc740fa --- /dev/null +++ b/protocol/types.go @@ -0,0 +1,37 @@ +package protocol + +type Exchange uint8 + +func (this Exchange) Uint8() uint8 { return uint8(this) } + +func (this Exchange) String() string { + switch this { + case ExchangeSH: + return "sh" + case ExchangeSZ: + return "sz" + case ExchangeBJ: + return "bj" + default: + return "unknown" + } +} + +func (this Exchange) Name() string { + switch this { + case ExchangeSH: + return "上海" + case ExchangeSZ: + return "深圳" + case ExchangeBJ: + return "北京" + default: + return "未知" + } +} + +const ( + ExchangeSH Exchange = iota //上海交易所 + ExchangeSZ //深圳交易所 + ExchangeBJ //北京交易所 +) diff --git a/protocol/unit.go b/protocol/unit.go index b894718..5af69cf 100644 --- a/protocol/unit.go +++ b/protocol/unit.go @@ -2,6 +2,7 @@ package protocol import ( "bytes" + "fmt" bytes2 "github.com/injoyai/base/bytes" "github.com/injoyai/conv" "golang.org/x/text/encoding/simplifiedchinese" @@ -32,41 +33,24 @@ func UTF8ToGBK(text []byte) []byte { return bytes.ReplaceAll(content, []byte{0x00}, []byte{}) } -func getprice(b []byte, pos *int) int { - /* - 0x7f与常量做与运算实质是保留常量(转换为二进制形式)的后7位数,既取值区间为[0,127] - 0x3f与常量做与运算实质是保留常量(转换为二进制形式)的后6位数,既取值区间为[0,63] - - 0x80 1000 0000 - 0x7f 0111 1111 - 0x40 100 0000 - 0x3f 011 1111 - */ - posByte := 6 - bData := b[*pos] - data := int(bData & 0x3f) - bSign := false - if (bData & 0x40) > 0 { - bSign = true +func FloatUnit(f float64) (float64, string) { + m := []string{"万", "亿"} + unit := "" + for i := 0; f > 1e4 && i < len(m); f /= 1e4 { + unit = m[i] } - - if (bData & 0x80) > 0 { - for { - *pos += 1 - bData = b[*pos] - data += (int(bData&0x7f) << posByte) - - posByte += 7 - - if (bData & 0x80) <= 0 { - break - } - } - } - *pos++ - - if bSign { - data = -data - } - return data + return f, unit +} + +func FloatUnitString(f float64) string { + m := []string{"万", "亿"} + unit := "" + for i := 0; f > 1e4 && i < len(m); f /= 1e4 { + unit = m[i] + } + return fmt.Sprintf("%0.2f%s", f, unit) +} + +func IntUnitString(n int) string { + return FloatUnitString(float64(n)) }