Compare commits

...

11 Commits

16 changed files with 499 additions and 74 deletions

View File

@@ -60,43 +60,43 @@ func main() {
| IP | 测试时间 | 所属地区 | 运营商 |
|-----------------|------------|------|-----|
| 124.71.187.122 | 2025-03-15 | 上海 | 华为 |
| 122.51.120.217 | 2025-03-15 | 上海 | 腾讯 |
| 111.229.247.189 | 2025-03-15 | 上海 | 腾讯 |
| 124.70.176.52 | 2025-03-15 | 上海 | 华为 |
| 123.60.186.45 | 2025-03-15 | 上海 | 华为 |
| 122.51.232.182 | 2025-03-15 | 上海 | 腾讯 |
| 118.25.98.114 | 2025-03-15 | 上海 | 腾讯 |
| 124.70.199.56 | 2025-03-15 | 上海 | 华为 |
| 121.36.225.169 | 2025-03-15 | 上海 | 华为 |
| 123.60.70.228 | 2025-03-15 | 上海 | 华为 |
| 123.60.73.44 | 2025-03-15 | 上海 | 华为 |
| 124.70.133.119 | 2025-03-15 | 上海 | 华为 |
| 124.71.187.72 | 2025-03-15 | 上海 | 华为 |
| 123.60.84.66 | 2025-03-15 | 上海 | 华为 |
| 124.71.187.122 | 2025-05-21 | 上海 | 华为 |
| 122.51.120.217 | 2025-05-21 | 上海 | 腾讯 |
| 111.229.247.189 | 2025-05-21 | 上海 | 腾讯 |
| 124.70.176.52 | 2025-05-21 | 上海 | 华为 |
| 123.60.186.45 | 2025-05-21 | 上海 | 华为 |
| 122.51.232.182 | 2025-05-21 | 上海 | 腾讯 |
| 118.25.98.114 | 2025-05-21 | 上海 | 腾讯 |
| 124.70.199.56 | 2025-05-21 | 上海 | 华为 |
| 121.36.225.169 | 2025-05-21 | 上海 | 华为 |
| 123.60.70.228 | 2025-05-21 | 上海 | 华为 |
| 123.60.73.44 | 2025-05-21 | 上海 | 华为 |
| 124.70.133.119 | 2025-05-21 | 上海 | 华为 |
| 124.71.187.72 | 2025-05-21 | 上海 | 华为 |
| 123.60.84.66 | 2025-05-21 | 上海 | 华为 |
| | | | |
| 121.36.54.217 | 2025-03-15 | 北京 | 华为 |
| 121.36.81.195 | 2025-03-15 | 北京 | 华为 |
| 123.249.15.60 | 2025-03-15 | 北京 | 华为 |
| 124.70.75.113 | 2025-03-15 | 北京 | 华为 |
| 120.46.186.223 | 2025-03-15 | 北京 | 华为 |
| 124.70.22.210 | 2025-03-15 | 北京 | 华为 |
| 139.9.133.247 | 2025-03-15 | 北京 | 华为 |
| 121.36.54.217 | 2025-05-21 | 北京 | 华为 |
| 121.36.81.195 | 2025-05-21 | 北京 | 华为 |
| 123.249.15.60 | 2025-05-21 | 北京 | 华为 |
| 124.70.75.113 | 2025-05-21 | 北京 | 华为 |
| 120.46.186.223 | 2025-05-21 | 北京 | 华为 |
| 124.70.22.210 | 2025-05-21 | 北京 | 华为 |
| 139.9.133.247 | 2025-05-21 | 北京 | 华为 |
| | | | |
| 124.71.85.110 | 2025-03-15 | 广州 | 华为 |
| 139.9.51.18 | 2025-03-15 | 广州 | 华为 |
| 139.159.239.163 | 2025-03-15 | 广州 | 华为 |
| 124.71.9.153 | 2025-03-15 | 广州 | 华为 |
| 116.205.163.254 | 2025-03-15 | 广州 | 华为 |
| 116.205.171.132 | 2025-03-15 | 广州 | 华为 |
| 116.205.183.150 | 2025-03-15 | 广州 | 华为 |
| 111.230.186.52 | 2025-03-15 | 广州 | 腾讯 |
| 110.41.4.4 | 2025-03-15 | 广州 | 华为 |
| 110.41.2.72 | 2025-03-15 | 广州 | 华为 |
| 110.41.154.219 | 2025-03-15 | 广州 | 华为 |
| 110.41.147.114 | 2025-03-15 | 广州 | 华为 |
| 124.71.85.110 | 2025-05-21 | 广州 | 华为 |
| 139.9.51.18 | 2025-05-21 | 广州 | 华为 |
| 139.159.239.163 | 2025-05-21 | 广州 | 华为 |
| 124.71.9.153 | 2025-05-21 | 广州 | 华为 |
| 116.205.163.254 | 2025-05-21 | 广州 | 华为 |
| 116.205.171.132 | 2025-05-21 | 广州 | 华为 |
| 116.205.183.150 | 2025-05-21 | 广州 | 华为 |
| 111.230.186.52 | 2025-05-21 | 广州 | 腾讯 |
| 110.41.4.4 | 2025-05-21 | 广州 | 华为 |
| 110.41.2.72 | 2025-05-21 | 广州 | 华为 |
| 110.41.154.219 | 2025-05-21 | 广州 | 华为 |
| 110.41.147.114 | 2025-05-21 | 广州 | 华为 |
| | | | |
| 119.97.185.59 | 2025-03-15 | 武汉 | 电信 |
| 119.97.185.59 | 2025-05-21 | 武汉 | 电信 |

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

14
example/FastHosts/main.go Normal file
View File

@@ -0,0 +1,14 @@
package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
)
func main() {
ls := tdx.FastHosts(tdx.Hosts...)
for _, v := range ls {
logs.Debug(v)
}
logs.Debug("总数量:", len(ls))
}

View File

@@ -3,20 +3,18 @@ package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/example/common"
"github.com/injoyai/tdx/protocol"
)
func main() {
c, err := tdx.Dial("124.71.187.122:7709")
logs.PanicErr(err)
common.Test(func(c *tdx.Client) {
resp, err := c.GetCode(protocol.ExchangeSH, 369)
logs.PanicErr(err)
resp, err := c.GetCode(protocol.ExchangeSH, 369)
logs.PanicErr(err)
for i, v := range resp.List {
logs.Debug(i, v)
}
logs.Debug("总数:", resp.Count)
select {}
for i, v := range resp.List {
logs.Debug(i, v, v.LastPrice)
}
logs.Debug("总数:", resp.Count)
})
}

View File

@@ -0,0 +1,20 @@
package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/example/common"
)
func main() {
common.Test(func(c *tdx.Client) {
resp, err := c.GetKline30Minute("sz000001", 0, 20)
logs.PanicErr(err)
for _, v := range resp.List {
logs.Debug(v)
}
logs.Debug("总数:", resp.Count)
})
}

36
example/Income/main.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx/extend"
"time"
)
func main() {
code := "sz000001"
pull := extend.NewPullKline(extend.PullKlineConfig{
Codes: []string{code},
Tables: []string{extend.Day},
})
//m, err := tdx.NewManage(nil)
//logs.PanicErr(err)
//err = pull.Run(context.Background(), m)
//logs.PanicErr(err)
ks, err := pull.DayKlines(code)
logs.PanicErr(err)
t := time.Now().AddDate(0, -1, -9)
logs.Debug(t.Format(time.DateOnly))
ls := extend.DoIncomes(ks, t, 5, 10, 20)
logs.Debug(len(ls))
for _, v := range ls {
logs.Info(v)
}
}

65
extend/income.go Normal file
View File

@@ -0,0 +1,65 @@
package extend
import (
"fmt"
"github.com/injoyai/tdx/protocol"
"time"
)
func DoIncomes(ks Klines, startAt time.Time, days ...int) Incomes {
year, month, day := startAt.Date()
start := time.Date(year, month, day, 15, 0, 0, 0, startAt.Location()).Unix()
for i, v := range ks {
if v.Date >= start {
ks = ks[i:]
break
}
}
ls := Incomes{}
for _, v := range days {
if v < len(ks) {
x := ks[v]
ls = append(ls, &Income{
Offset: v,
Time: time.Unix(x.Date, 0),
Source: protocol.K{
Open: ks[0].Open,
High: ks[0].High,
Low: ks[0].Low,
Close: ks[0].Close,
},
Current: protocol.K{
Open: x.Open,
High: x.High,
Low: x.Low,
Close: x.Close,
},
})
}
}
return ls
}
type Incomes []*Income
type Income struct {
Offset int //偏移量
Time time.Time //时间
Source protocol.K //源
Current protocol.K //当前
}
func (this *Income) String() string {
return fmt.Sprintf("偏移: %d, 时间: %s, 涨幅: %.1f%%", this.Offset, this.Time.Format(time.DateOnly), this.RiseRate()*100)
}
func (this *Income) Rise() protocol.Price {
return this.Current.Close - this.Source.Close
}
func (this *Income) RiseRate() float64 {
return this.Rise().Float64() / this.Source.Close.Float64()
}

View File

@@ -26,33 +26,32 @@ const (
Month = "month"
Quarter = "quarter"
Year = "year"
tableMinute = "MinuteKline"
table5Minute = "Minute5Kline"
table15Minute = "Minute15Kline"
table30Minute = "Minute30Kline"
tableHour = "HourKline"
tableDay = "DayKline"
tableWeek = "WeekKline"
tableMonth = "MonthKline"
tableQuarter = "QuarterKline"
tableYear = "YearKline"
)
var (
AllKlineType = []string{Minute, Minute5, Minute15, Minute30, Hour, Day, Week, Month, Quarter, Year}
KlineTableMap = map[string]*KlineTable{
Minute: NewKlineTable("MinuteKline", func(c *tdx.Client) KlineHandler { return c.GetKlineMinuteUntil }),
Minute5: NewKlineTable("Minute5Kline", func(c *tdx.Client) KlineHandler { return c.GetKline5MinuteUntil }),
Minute15: NewKlineTable("Minute15Kline", func(c *tdx.Client) KlineHandler { return c.GetKline15MinuteUntil }),
Minute30: NewKlineTable("Minute30Kline", func(c *tdx.Client) KlineHandler { return c.GetKline30MinuteUntil }),
Hour: NewKlineTable("HourKline", func(c *tdx.Client) KlineHandler { return c.GetKlineHourUntil }),
Day: NewKlineTable("DayKline", func(c *tdx.Client) KlineHandler { return c.GetKlineDayUntil }),
Week: NewKlineTable("WeekKline", func(c *tdx.Client) KlineHandler { return c.GetKlineWeekUntil }),
Month: NewKlineTable("MonthKline", func(c *tdx.Client) KlineHandler { return c.GetKlineMonthUntil }),
Quarter: NewKlineTable("QuarterKline", func(c *tdx.Client) KlineHandler { return c.GetKlineQuarterUntil }),
Year: NewKlineTable("YearKline", func(c *tdx.Client) KlineHandler { return c.GetKlineYearUntil }),
}
AllKlineTables = []string{
"MinuteKline",
"Minute5Kline",
"Minute15Kline",
"Minute30Kline",
"HourKline",
"DayKline",
"WeekKline",
"MonthKline",
"QuarterKline",
"YearKline",
Minute: NewKlineTable(tableMinute, func(c *tdx.Client) KlineHandler { return c.GetKlineMinuteUntil }),
Minute5: NewKlineTable(table5Minute, func(c *tdx.Client) KlineHandler { return c.GetKline5MinuteUntil }),
Minute15: NewKlineTable(table15Minute, func(c *tdx.Client) KlineHandler { return c.GetKline15MinuteUntil }),
Minute30: NewKlineTable(table30Minute, func(c *tdx.Client) KlineHandler { return c.GetKline30MinuteUntil }),
Hour: NewKlineTable(tableHour, func(c *tdx.Client) KlineHandler { return c.GetKlineHourUntil }),
Day: NewKlineTable(tableDay, func(c *tdx.Client) KlineHandler { return c.GetKlineDayUntil }),
Week: NewKlineTable(tableWeek, func(c *tdx.Client) KlineHandler { return c.GetKlineWeekUntil }),
Month: NewKlineTable(tableMonth, func(c *tdx.Client) KlineHandler { return c.GetKlineMonthUntil }),
Quarter: NewKlineTable(tableQuarter, func(c *tdx.Client) KlineHandler { return c.GetKlineQuarterUntil }),
Year: NewKlineTable(tableYear, func(c *tdx.Client) KlineHandler { return c.GetKlineYearUntil }),
}
)
@@ -69,6 +68,12 @@ func NewPullKline(cfg PullKlineConfig) *PullKline {
for _, v := range cfg.Tables {
_tables = append(_tables, KlineTableMap[v])
}
if cfg.Limit <= 0 {
cfg.Limit = 1
}
if len(cfg.Dir) == 0 {
cfg.Dir = filepath.Join(tdx.DefaultDatabaseDir, "kline")
}
return &PullKline{
tables: _tables,
Config: cfg,
@@ -84,6 +89,21 @@ func (this *PullKline) Name() string {
return "拉取k线数据"
}
func (this *PullKline) DayKlines(code string) (Klines, error) {
//连接数据库
db, err := xorm.NewEngine("sqlite", filepath.Join(this.Config.Dir, code+".db"))
if err != nil {
return nil, err
}
db.SetMapper(core.SameMapper{})
db.DB().SetMaxOpenConns(1)
defer db.Close()
data := Klines{}
err = db.Table(tableDay).Asc("date").Find(&data)
return data, err
}
func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
limit := chans.NewWaitLimit(uint(this.Config.Limit))
@@ -112,6 +132,7 @@ func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
logs.Err(err)
return
}
defer db.Close()
db.SetMapper(core.SameMapper{})
db.DB().SetMaxOpenConns(1)

118
extend/spider_ths.go Normal file
View File

@@ -0,0 +1,118 @@
package extend
import (
"bytes"
"encoding/json"
"fmt"
"github.com/injoyai/conv"
"github.com/injoyai/tdx/protocol"
"io"
"net/http"
"strings"
"time"
)
const (
UrlTHSDayKline = "http://d.10jqka.com.cn/v6/line/hs_%s/0%d/all.js"
THS_QFQ uint8 = 1 //前复权
THS_HFQ uint8 = 2 //后复权
)
/*
GetTHSDayKline
前复权,和通达信对的上,和东方财富对不上
后复权,和通达信,东方财富都对不上
*/
func GetTHSDayKline(code string, _type uint8) ([]*Kline, error) {
if _type != THS_QFQ && _type != THS_HFQ {
return nil, fmt.Errorf("数据类型错误,例如:前复权1或后复权2")
}
code = protocol.AddPrefix(code)
if len(code) != 8 {
return nil, fmt.Errorf("股票代码错误,例如:SZ000001或000001")
}
u := fmt.Sprintf(UrlTHSDayKline, code[2:], _type)
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
/*
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/90.0.4430.212 Safari/537.36',
'Referer': 'http://stockpage.10jqka.com.cn/',
'DNT': '1',
*/
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.54")
req.Header.Set("Referer", "http://stockpage.10jqka.com.cn/")
req.Header.Set("DNT", "1")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bs, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
n := bytes.IndexByte(bs, '(')
bs = bs[n+1 : len(bs)-1]
m := map[string]any{}
err = json.Unmarshal(bs, &m)
if err != nil {
return nil, err
}
total := conv.Int(m["total"])
priceFactor := conv.Float64(m["priceFactor"])
prices := strings.Split(conv.String(m["price"]), ",")
dates := strings.Split(conv.String(m["dates"]), ",")
volumes := strings.Split(conv.String(m["volumn"]), ",")
start := conv.String(m["start"])
t, err := time.Parse("20060102", start)
if err != nil {
return nil, err
}
//好像到了22点,总数量会比实际多1
if total == len(dates)+1 && total == len(volumes)+1 {
total -= 1
}
//判断数量是否对应
if total*4 != len(prices) || total != len(dates) || total != len(volumes) {
return nil, fmt.Errorf("total=%d prices=%d dates=%d volumns=%d", total, len(prices), len(dates), len(volumes))
}
ls := []*Kline(nil)
year := t.Year()
lastDate := ""
for i := 0; i < total; i++ {
//当日前变小时(12xx变01xx),说明过了1年,除非该股票停牌了1年多则数据错误
if dates[i] < lastDate {
year++
}
lastDate = dates[i]
x, err := time.Parse("0102", dates[i])
if err != nil {
return nil, err
}
x = time.Date(year, x.Month(), x.Day(), 15, 0, 0, 0, time.Local)
low := protocol.Price(conv.Float64(prices[i*4+0]) * 1000 / priceFactor)
ls = append(ls, &Kline{
Code: protocol.AddPrefix(code),
Date: x.Unix(),
Open: protocol.Price(conv.Float64(prices[i*4+1])*1000/priceFactor) + low,
High: protocol.Price(conv.Float64(prices[i*4+2])*1000/priceFactor) + low,
Low: low,
Close: protocol.Price(conv.Float64(prices[i*4+3])*1000/priceFactor) + low,
Volume: (conv.Int64(volumes[i]) + 50) / 100,
})
}
return ls, nil
}

16
extend/spider_ths_test.go Normal file
View File

@@ -0,0 +1,16 @@
package extend
import (
"testing"
)
func TestNewSpiderTHS(t *testing.T) {
ls, err := GetTHSDayKline("sz000001", THS_HFQ)
if err != nil {
t.Error(err)
return
}
for _, v := range ls {
t.Log(v)
}
}

View File

@@ -1,5 +1,12 @@
package tdx
import (
"github.com/injoyai/logs"
"net"
"strings"
"sync"
)
var (
// Hosts 所有服务器地址(2024-11-30测试通过)
@@ -67,3 +74,31 @@ var (
"119.97.185.59", //电信
}
)
// FastHosts 通过tcp(ping不可用)的方式筛选可用的地址,并排序(有点误差)
func FastHosts(hosts ...string) []string {
wg := sync.WaitGroup{}
wg.Add(len(hosts))
mu := sync.Mutex{}
ls := []string(nil)
for _, host := range hosts {
go func(host string) {
defer wg.Done()
addr := host
if !strings.Contains(addr, ":") {
addr += ":7709"
}
c, err := net.Dial("tcp", addr)
if err != nil {
logs.Err(err)
return
}
defer c.Close()
mu.Lock()
ls = append(ls, host)
mu.Unlock()
}(host)
}
wg.Wait()
return ls
}

View File

@@ -49,7 +49,7 @@ func (code) Decode(bs []byte) (*CodeResp, error) {
Multiple: Uint16(bs[6:8]),
Name: string(UTF8ToGBK(bs[8:16])),
Decimal: int8(bs[20]),
LastPrice: getVolume(Uint32(bs[21:25])),
LastPrice: getVolume2(Uint32(bs[21:25])),
}
//logs.Debug(bs[25:29]) //26和28字节 好像是枚举(基本是44,45和34,35)
bs = bs[29:]

View File

@@ -177,7 +177,7 @@ func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) {
resp.List = append(resp.List, k)
}
resp.List = FixKlineTime(resp.List)
return resp, nil
}
@@ -185,3 +185,27 @@ type KlineCache struct {
Type uint8 //1分钟,5分钟,日线等
Kind string //指数,个股等
}
// FixKlineTime 修复盘内下午(13~15点)拉取数据的时候,11.30的时间变成13.00
func FixKlineTime(ks []*Kline) []*Kline {
if len(ks) == 0 {
return ks
}
now := time.Now()
//只有当天下午13~15点之间才会出现的时间问题
node1 := time.Date(now.Year(), now.Month(), now.Day(), 13, 0, 0, 0, now.Location())
node2 := time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, now.Location())
if ks[len(ks)-1].Time.Unix() < node1.Unix() || ks[len(ks)-1].Time.Unix() > node2.Unix() {
return ks
}
ls := ks
if len(ls) >= 120 {
ls = ls[len(ls)-120:]
}
for i, v := range ls {
if v.Time.Unix() == node1.Unix() {
ls[i].Time = time.Date(now.Year(), now.Month(), now.Day(), 11, 30, 0, 0, now.Location())
}
}
return ks
}

View File

@@ -19,7 +19,10 @@ func Test_stockKline_Decode(t *testing.T) {
t.Error(err)
return
}
resp, err := MKline.Decode(bs, 9)
resp, err := MKline.Decode(bs, KlineCache{
Type: 9,
Kind: "",
})
if err != nil {
t.Error(err)
return

View File

@@ -195,6 +195,46 @@ func getVolume(val uint32) (volume float64) {
return
}
func getVolume2(val uint32) float64 {
ivol := int32(val)
logpoint := ivol >> 24 // 提取最高字节原8*3移位
hleax := (ivol >> 16) & 0xff // 提取次高字节
lheax := (ivol >> 8) & 0xff // 提取第三字节
lleax := ivol & 0xff // 提取最低字节
dwEcx := logpoint*2 - 0x7f // 基础指数计算
dbl_xmm6 := math.Exp2(float64(dwEcx)) // 核心指数计算仅一次
// 计算dbl_xmm4
var dbl_xmm4 float64
if hleax > 0x80 {
// 高位分支:合并指数计算
dbl_xmm4 = dbl_xmm6 * (64.0 + float64(hleax&0x7f)) / 64.0
} else {
// 低位分支:复用核心指数
dbl_xmm4 = dbl_xmm6 * float64(hleax) / 128.0
}
// 计算缩放因子
scale := 1.0
if (hleax & 0x80) != 0 {
scale = 2.0
}
// 预计算常量的倒数,优化除法
const (
inv32768 = 1.0 / 32768.0 // 2^15
inv8388608 = 1.0 / 8388608.0 // 2^23
)
// 计算低位分量
dbl_xmm3 := dbl_xmm6 * float64(lheax) * inv32768 * scale
dbl_xmm1 := dbl_xmm6 * float64(lleax) * inv8388608 * scale
// 合计最终结果
return dbl_xmm6 + dbl_xmm4 + dbl_xmm3 + dbl_xmm1
}
// IsStock 是否是股票,示例sz000001
func IsStock(code string) bool {
if len(code) != 8 {
@@ -213,6 +253,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 {

View File

@@ -20,5 +20,6 @@ func TestUTF8ToGBK(t *testing.T) {
func Test_getVolume(t *testing.T) {
t.Log(getVolume(1237966432))
t.Log(getVolume(1237966432))
t.Log(getVolume2(1237966432))
}