mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
增加GetETFs,用于获取基金代码
This commit is contained in:
22
codes.go
22
codes.go
@@ -30,8 +30,8 @@ func NewCodes(c *Client, filenames ...string) (*Codes, error) {
|
|||||||
|
|
||||||
//如果没有指定文件名,则使用默认
|
//如果没有指定文件名,则使用默认
|
||||||
defaultFilename := filepath.Join(DefaultDatabaseDir, "codes.db")
|
defaultFilename := filepath.Join(DefaultDatabaseDir, "codes.db")
|
||||||
filename := conv.Default[string](defaultFilename, filenames...)
|
filename := conv.Default(defaultFilename, filenames...)
|
||||||
filename = conv.Select[string](filename == "", defaultFilename, filename)
|
filename = conv.Select(filename == "", defaultFilename, filename)
|
||||||
|
|
||||||
//如果文件夹不存在就创建
|
//如果文件夹不存在就创建
|
||||||
dir, _ := filepath.Split(filename)
|
dir, _ := filepath.Split(filename)
|
||||||
@@ -121,7 +121,7 @@ func (this *Codes) GetName(code string) string {
|
|||||||
|
|
||||||
// GetStocks 获取股票代码,sh6xxx sz0xx sz30xx
|
// GetStocks 获取股票代码,sh6xxx sz0xx sz30xx
|
||||||
func (this *Codes) GetStocks(limits ...int) []string {
|
func (this *Codes) GetStocks(limits ...int) []string {
|
||||||
limit := conv.Default[int](-1, limits...)
|
limit := conv.Default(-1, limits...)
|
||||||
ls := []string(nil)
|
ls := []string(nil)
|
||||||
for _, m := range this.list {
|
for _, m := range this.list {
|
||||||
code := m.FullCode()
|
code := m.FullCode()
|
||||||
@@ -135,6 +135,22 @@ func (this *Codes) GetStocks(limits ...int) []string {
|
|||||||
return ls
|
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 {
|
func (this *Codes) Get(code string) *CodeModel {
|
||||||
return this.Map[code]
|
return this.Map[code]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -213,6 +213,24 @@ func IsStock(code string) bool {
|
|||||||
return false
|
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(上证指数)
|
// AddPrefix 添加股票代码前缀,针对股票生效,例如000001,会增加前缀sz000001(平安银行),而不是sh000001(上证指数)
|
||||||
func AddPrefix(code string) string {
|
func AddPrefix(code string) string {
|
||||||
if len(code) == 6 {
|
if len(code) == 6 {
|
||||||
|
|||||||
Reference in New Issue
Block a user