From 1f0958b53efe6742dfd2193b79efbaf957932c03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=B1=E7=BA=AF=E5=87=80?= <1113655791@qq.com> Date: Wed, 12 Mar 2025 20:41:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E4=BB=A3=E7=A0=81=E4=BC=A0?= =?UTF-8?q?=E5=8F=82=E6=96=B9=E5=BC=8F,=E4=B8=AA=E8=82=A1=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E5=BF=BD=E7=95=A5=E4=BA=A4=E6=98=93=E6=89=80=E6=A0=87?= =?UTF-8?q?=E8=AF=86,=E4=BE=8Bsz000001,=E5=8F=AF=E4=BB=A5=E4=BC=A0000001?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 3 +++ protocol/unit.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/client.go b/client.go index 771d820..1f684e1 100644 --- a/client.go +++ b/client.go @@ -233,6 +233,7 @@ func (this *Client) GetMinute(code string) (*protocol.MinuteResp, error) { // GetMinuteTrade 获取分时交易详情,服务器最多返回1800条,count-start<=1800 func (this *Client) GetMinuteTrade(code string, start, count uint16) (*protocol.MinuteTradeResp, error) { + code = protocol.AddPrefix(code) f, err := protocol.MMinuteTrade.Frame(code, start, count) if err != nil { return nil, err @@ -267,6 +268,7 @@ func (this *Client) GetMinuteTradeAll(code string) (*protocol.MinuteTradeResp, e // 只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0 // 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则 func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16) (*protocol.HistoryMinuteTradeResp, error) { + code = protocol.AddPrefix(code) f, err := protocol.MHistoryMinuteTrade.Frame(date, code, start, count) if err != nil { return nil, err @@ -299,6 +301,7 @@ func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.Histo // GetKline 获取k线数据,推荐收盘之后获取,否则会获取到当天的数据 func (this *Client) GetKline(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) { + code = protocol.AddPrefix(code) f, err := protocol.MKline.Frame(Type, code, start, count) if err != nil { return nil, err diff --git a/protocol/unit.go b/protocol/unit.go index 156711a..c6218e8 100644 --- a/protocol/unit.go +++ b/protocol/unit.go @@ -50,6 +50,7 @@ func UTF8ToGBK(text []byte) []byte { } func DecodeCode(code string) (Exchange, string, error) { + code = AddPrefix(code) if len(code) != 8 { return 0, "", fmt.Errorf("股票代码长度错误,例如:SZ000001") } @@ -212,3 +213,18 @@ func IsStock(code string) bool { } return false } + +// AddPrefix 添加股票代码前缀,针对股票生效,例如000001,会增加前缀sz000001(平安银行),而不是sh000001(上证指数) +func AddPrefix(code string) string { + if len(code) == 6 { + switch { + case code[:1] == "6": + code = ExchangeSH.String() + code + case code[:1] == "0": + code = ExchangeSZ.String() + code + case code[:2] == "30": + code = ExchangeSZ.String() + code + } + } + return code +}