增加GetETFs,用于获取基金代码

This commit is contained in:
injoyai
2025-05-29 08:33:08 +08:00
parent 84b0ec6f6c
commit ecaad0e85f
2 changed files with 37 additions and 3 deletions

View File

@@ -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]
}

View File

@@ -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 {