diff --git a/codes.go b/codes.go index 3bf6722..a26516c 100644 --- a/codes.go +++ b/codes.go @@ -30,8 +30,8 @@ func NewCodes(c *Client, filenames ...string) (*Codes, error) { //如果没有指定文件名,则使用默认 defaultFilename := filepath.Join(DefaultDatabaseDir, "codes.db") - filename := conv.Default[string](defaultFilename, filenames...) - filename = conv.Select[string](filename == "", defaultFilename, filename) + filename := conv.Default(defaultFilename, filenames...) + filename = conv.Select(filename == "", defaultFilename, filename) //如果文件夹不存在就创建 dir, _ := filepath.Split(filename) @@ -121,7 +121,7 @@ func (this *Codes) GetName(code string) string { // GetStocks 获取股票代码,sh6xxx sz0xx sz30xx func (this *Codes) GetStocks(limits ...int) []string { - limit := conv.Default[int](-1, limits...) + limit := conv.Default(-1, limits...) ls := []string(nil) for _, m := range this.list { code := m.FullCode() @@ -135,6 +135,22 @@ func (this *Codes) GetStocks(limits ...int) []string { return ls } +// GetETFs 获取基金代码,sz159xxx,sh510xxx,sh511xxx +func (this *Codes) GetETFs(limits ...int) []string { + limit := conv.Default(-1, limits...) + ls := []string(nil) + for _, m := range this.list { + code := m.FullCode() + if protocol.IsETF(code) { + ls = append(ls, code) + } + if limit > 0 && len(ls) >= limit { + break + } + } + return ls +} + func (this *Codes) Get(code string) *CodeModel { return this.Map[code] } diff --git a/protocol/unit.go b/protocol/unit.go index 9b3fa13..cb42e4e 100644 --- a/protocol/unit.go +++ b/protocol/unit.go @@ -213,6 +213,24 @@ func IsStock(code string) bool { return false } +// IsETF 是否是基金,示例sz159558 +func IsETF(code string) bool { + if len(code) != 8 { + return false + } + code = strings.ToLower(code) + switch { + case code[0:2] == ExchangeSH.String() && + (code[2:5] == "510" || code[2:5] == "511" || code[2:5] == "512" || code[2:5] == "513" || code[2:5] == "515"): + return true + + case code[0:2] == ExchangeSZ.String() && + (code[2:5] == "159"): + return true + } + return false +} + // AddPrefix 添加股票代码前缀,针对股票生效,例如000001,会增加前缀sz000001(平安银行),而不是sh000001(上证指数) func AddPrefix(code string) string { if len(code) == 6 {