Compare commits

...

15 Commits

Author SHA1 Message Date
钱纯净
0a1a6194af 升级引用库版本 2025-08-24 20:20:53 +08:00
钱纯净
8b3ff3af01 升级引用库版本 2025-08-24 20:09:38 +08:00
钱纯净
c4c0d1dfa4 Merge remote-tracking branch 'origin/master' 2025-08-18 22:31:17 +08:00
钱纯净
73b80857cc 修复未交易(停牌等情况)超过一年复权数据不对的bug,例000633 2025-08-18 22:30:07 +08:00
injoyai
e7e8c6a46a 用60Minute来替代Hour,保留Hour作为60Minute的别名 2025-07-10 09:47:17 +08:00
injoyai
2e17db7faf 增加GetStockAll和GetETFAll,用于实时获取代码 2025-07-08 10:45:37 +08:00
injoyai
accda98f3b protocol.HistoryTradeResp替换成protocol.TradeResp 2025-07-02 15:53:38 +08:00
injoyai
1783643f47 升级引用库版本 2025-07-01 16:13:06 +08:00
injoyai
b9f0951b15 Manage的Codes和Workday共用一个客户端 2025-07-01 16:01:55 +08:00
injoyai
2e4ecd034c Manage的Codes和Workday共用一个客户端 2025-07-01 16:00:16 +08:00
injoyai
9269bca388 增加Manage.RangeStocks和增加Manage.RangeETFs 2025-07-01 15:46:44 +08:00
injoyai
fc1f25c6c9 优化FastHosts,增加Spend(耗时) 2025-06-18 13:51:45 +08:00
injoyai
705f6e4e3a 优化GetTrade示例 2025-06-18 13:50:50 +08:00
injoyai
12079f1ee2 优化FastHosts,增加Spend(耗时) 2025-06-18 13:50:23 +08:00
injoyai
34701c4197 优化PullTrade 2025-06-10 15:34:14 +08:00
18 changed files with 272 additions and 121 deletions

View File

@@ -87,16 +87,18 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
c.SetOption(op...) //自定义选项
c.Event.OnReadFrom = protocol.ReadFrom //分包
c.Event.OnDealMessage = cli.handlerDealMessage //解析数据并处理
//无数据超时时间是60秒,30秒发送一个心跳包
c.GoTimerWriter(30*time.Second, func(w ios.MoreWriter) error {
bs := protocol.MHeart.Frame().Bytes()
_, err := w.Write(bs)
return err
})
f := protocol.MConnect.Frame()
if _, err = c.Write(f.Bytes()); err != nil {
c.Close()
c.Event.OnConnected = func(c *client.Client) error {
//无数据超时时间是60秒,30秒发送一个心跳包
c.GoTimerWriter(30*time.Second, func(w ios.MoreWriter) error {
bs := protocol.MHeart.Frame().Bytes()
_, err := w.Write(bs)
return err
})
f := protocol.MConnect.Frame()
if _, err = c.Write(f.Bytes()); err != nil {
c.Close()
}
return nil
}
})
if err != nil {
@@ -233,6 +235,40 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp,
return resp, nil
}
// GetStockAll 获取所有股票代码
func (this *Client) GetStockAll() ([]string, error) {
ls := []string(nil)
for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
resp, err := this.GetCodeAll(ex)
if err != nil {
return nil, err
}
for _, v := range resp.List {
if protocol.IsStock(v.Code) {
ls = append(ls, v.Code)
}
}
}
return ls, nil
}
// GetETFAll 获取所有ETF代码
func (this *Client) GetETFAll() ([]string, error) {
ls := []string(nil)
for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
resp, err := this.GetCodeAll(ex)
if err != nil {
return nil, err
}
for _, v := range resp.List {
if protocol.IsETF(v.Code) {
ls = append(ls, v.Code)
}
}
}
return ls, nil
}
// GetQuote 获取盘口五档报价
func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
for i := range codes {
@@ -360,14 +396,14 @@ func (this *Client) GetMinuteTradeAll(code string) (*protocol.TradeResp, error)
return resp, nil
}
func (this *Client) GetHistoryTrade(date, code string, start, count uint16) (*protocol.HistoryTradeResp, error) {
func (this *Client) GetHistoryTrade(date, code string, start, count uint16) (*protocol.TradeResp, error) {
return this.GetHistoryMinuteTrade(date, code, start, count)
}
// GetHistoryMinuteTrade 获取历史分时交易
// 只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16) (*protocol.HistoryTradeResp, error) {
func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16) (*protocol.TradeResp, error) {
code = protocol.AddPrefix(code)
f, err := protocol.MHistoryTrade.Frame(date, code, start, count)
if err != nil {
@@ -380,17 +416,17 @@ func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16
if err != nil {
return nil, err
}
return result.(*protocol.HistoryTradeResp), nil
return result.(*protocol.TradeResp), nil
}
func (this *Client) GetHistoryTradeAll(date, code string) (*protocol.HistoryTradeResp, error) {
func (this *Client) GetHistoryTradeAll(date, code string) (*protocol.TradeResp, error) {
return this.GetHistoryMinuteTradeAll(date, code)
}
// GetHistoryMinuteTradeAll 获取历史分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.HistoryTradeResp, error) {
resp := &protocol.HistoryTradeResp{}
func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.TradeResp, error) {
resp := &protocol.TradeResp{}
size := uint16(2000)
for start := uint16(0); ; start += size {
r, err := this.GetHistoryMinuteTrade(date, code, start, size)
@@ -603,18 +639,32 @@ func (this *Client) GetKline30MinuteUntil(code string, f func(k *protocol.Kline)
return this.GetKlineUntil(protocol.TypeKline30Minute, code, f)
}
// GetKline60Minute 获取60分钟k线数据
func (this *Client) GetKline60Minute(code string, start, count uint16) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKline60Minute, code, start, count)
}
// GetKlineHour 获取小时k线数据
func (this *Client) GetKlineHour(code string, start, count uint16) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKlineHour, code, start, count)
return this.GetKline(protocol.TypeKline60Minute, code, start, count)
}
// GetKline60MinuteAll 获取60分钟k线全部数据
func (this *Client) GetKline60MinuteAll(code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKline60Minute, code)
}
// GetKlineHourAll 获取小时k线全部数据
func (this *Client) GetKlineHourAll(code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKlineHour, code)
return this.GetKlineAll(protocol.TypeKline60Minute, code)
}
func (this *Client) GetKline60MinuteUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
return this.GetKlineUntil(protocol.TypeKline60Minute, code, f)
}
func (this *Client) GetKlineHourUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
return this.GetKlineUntil(protocol.TypeKlineHour, code, f)
return this.GetKlineUntil(protocol.TypeKline60Minute, code, f)
}
// GetKlineDay 获取日k线数据

View File

@@ -70,7 +70,7 @@ func NewCodes(c *Client, filenames ...string) (*Codes, error) {
{ //设置定时器,每天早上9点更新数据
task := cron.New(cron.WithSeconds())
task.AddFunc("0 0 9 * * *", func() {
task.AddFunc("10 0 9 * * *", func() {
for i := 0; i < 3; i++ {
if err := cc.Update(); err == nil {
return

View File

@@ -8,7 +8,7 @@ import (
func main() {
ls := tdx.FastHosts(tdx.Hosts...)
for _, v := range ls {
logs.Debug(v)
logs.Debug(v.Host, v.Spend)
}
logs.Debug("总数量:", len(ls))
}

20
example/GetTrade/main.go Normal file
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.GetTrade("sz000001", 0, 20)
logs.PanicErr(err)
for _, v := range resp.List {
logs.Debug(v)
}
logs.Debug("总数:", resp.Count)
})
}

View File

@@ -1,6 +1,7 @@
package main
import (
"context"
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/extend"
@@ -13,7 +14,7 @@ func main() {
m, err := tdx.NewManage(nil)
logs.PanicErr(err)
err = pt.Pull(m, 2025, "sz000001")
err = pt.PullYear(context.Background(), m, 2025, "sz000001")
logs.Err(err)
}

View File

@@ -43,7 +43,7 @@ func (this *PullKlineMysql) Name() string {
}
func (this *PullKlineMysql) Run(ctx context.Context, m *tdx.Manage) error {
limit := chans.NewWaitLimit(uint(this.Config.Limit))
limit := chans.NewWaitLimit(this.Config.Limit)
//1. 获取所有股票代码
codes := this.Config.Codes

View File

@@ -105,7 +105,7 @@ func (this *PullKline) DayKlines(code string) (Klines, error) {
}
func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
limit := chans.NewWaitLimit(uint(this.Config.Limit))
limit := chans.NewWaitLimit(this.Config.Limit)
//1. 获取所有股票代码
codes := this.Config.Codes

View File

@@ -1,6 +1,7 @@
package extend
import (
"context"
"github.com/injoyai/conv"
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
@@ -19,7 +20,21 @@ type PullTrade struct {
Dir string
}
func (this *PullTrade) Pull(m *tdx.Manage, year int, code string) (err error) {
func (this *PullTrade) Pull(ctx context.Context, m *tdx.Manage, code string) error {
for i := 2000; i <= time.Now().Year(); i++ {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err := this.PullYear(ctx, m, i, code); err != nil {
return err
}
}
return nil
}
func (this *PullTrade) PullYear(ctx context.Context, m *tdx.Manage, year int, code string) (err error) {
tss := protocol.Trades{}
kss1 := protocol.Klines(nil)
@@ -29,9 +44,17 @@ func (this *PullTrade) Pull(m *tdx.Manage, year int, code string) (err error) {
kss60 := protocol.Klines(nil)
m.Workday.RangeYear(year, func(t time.Time) bool {
select {
case <-ctx.Done():
err = ctx.Err()
return false
default:
}
date := t.Format("20060102")
var resp *protocol.HistoryTradeResp
var resp *protocol.TradeResp
err = m.Do(func(c *tdx.Client) error {
resp, err = c.GetHistoryTradeAll(date, code)
return err
@@ -59,17 +82,16 @@ func (this *PullTrade) Pull(m *tdx.Manage, year int, code string) (err error) {
return true
})
_ = kss5
_ = kss15
_ = kss30
_ = kss60
if err != nil {
return
}
filename := filepath.Join(this.Dir, conv.String(year), "分时成交", code+".csv")
filename1 := filepath.Join(this.Dir, conv.String(year), "1分钟", code+".csv")
filename5 := filepath.Join(this.Dir, conv.String(year), "5分钟", code+".csv")
filename15 := filepath.Join(this.Dir, conv.String(year), "15分钟", code+".csv")
filename30 := filepath.Join(this.Dir, conv.String(year), "30分钟", code+".csv")
filename60 := filepath.Join(this.Dir, conv.String(year), "60分钟", code+".csv")
filename := filepath.Join(this.Dir, "分时成交", code+"-"+conv.String(year)+".csv")
filename1 := filepath.Join(this.Dir, "1分钟", code+"-"+conv.String(year)+".csv")
filename5 := filepath.Join(this.Dir, "5分钟", code+"-"+conv.String(year)+".csv")
filename15 := filepath.Join(this.Dir, "15分钟", code+"-"+conv.String(year)+".csv")
filename30 := filepath.Join(this.Dir, "30分钟", code+"-"+conv.String(year)+".csv")
filename60 := filepath.Join(this.Dir, "60分钟", code+"-"+conv.String(year)+".csv")
name := m.Codes.GetName(code)
err = TradeToCsv(filename, tss)

View File

@@ -114,15 +114,11 @@ func GetTHSDayKline(code string, _type uint8) ([]*Kline, error) {
}
total := conv.Int(m["total"])
sortYears := conv.Interfaces(m["sortYear"])
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 {
@@ -133,31 +129,43 @@ func GetTHSDayKline(code string, _type uint8) ([]*Kline, error) {
return nil, fmt.Errorf("total=%d prices=%d dates=%d volumns=%d", total, len(prices), len(dates), len(volumes))
}
ls := []*Kline(nil)
mYear := make(map[int][]string)
index := 0
for i, v := range sortYears {
if ls := conv.Ints(v); len(ls) == 2 {
year := conv.Int(ls[0])
length := conv.Int(ls[1])
if i == len(sortYears)-1 {
mYear[year] = dates[index:]
break
}
mYear[year] = dates[index : index+length]
index += length
}
}
year := t.Year()
lastDate := ""
for i := 0; i < total; i++ {
//当日前变小时(12xx变01xx),说明过了1年,除非该股票停牌了1年多则数据错误
if dates[i] < lastDate {
year++
ls := []*Kline(nil)
i := 0
nowYear := time.Now().Year()
for year := 1990; year <= nowYear; year++ {
for _, d := range mYear[year] {
x, err := time.Parse("0102", d)
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,
})
i++
}
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

12
go.mod
View File

@@ -5,10 +5,10 @@ go 1.20
require (
github.com/glebarez/go-sqlite v1.22.0
github.com/go-sql-driver/mysql v1.7.0
github.com/injoyai/base v1.2.7
github.com/injoyai/conv v1.2.2
github.com/injoyai/ios v0.0.7
github.com/injoyai/logs v1.0.9
github.com/injoyai/base v1.2.15
github.com/injoyai/conv v1.2.5
github.com/injoyai/ios v1.2.2
github.com/injoyai/logs v1.0.12
github.com/robfig/cron/v3 v3.0.1
golang.org/x/text v0.16.0
xorm.io/core v0.7.3
@@ -17,7 +17,7 @@ require (
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/goccy/go-json v0.8.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.5.0 // indirect
@@ -31,7 +31,7 @@ require (
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/libc v1.37.6 // indirect

33
go.sum
View File

@@ -6,9 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ=
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
@@ -27,14 +26,14 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/injoyai/base v1.2.7 h1:uYZoUIPGidkTNMfQfObsyHulu5VvNCOzuCh1DGn+Hz0=
github.com/injoyai/base v1.2.7/go.mod h1:BsXiJ6/hXpswWxI4zLdKz+pW2Cwo+qhdfyaWDfR/vwg=
github.com/injoyai/conv v1.2.2 h1:nxFD3zCYq/ZvVE6xAExBR+agi6gB+vc9O0si67VAsPk=
github.com/injoyai/conv v1.2.2/go.mod h1:s05l3fQJQ4mT4VX+KIdbvCWQB0YzZHprmUfUu2uxd1k=
github.com/injoyai/ios v0.0.7 h1:7k/brTmpnoqE6ajodyilkr2EJmJmcvSkpUD+LptgUVU=
github.com/injoyai/ios v0.0.7/go.mod h1:9HemWSJTmhyJCnr+kH+lRfmvrEdABi1rkCBVsbqV5X8=
github.com/injoyai/logs v1.0.9 h1:Wq7rCVIQKcPx+z+lzKQb2qyDK4TML/cgmaSZN9tx33c=
github.com/injoyai/logs v1.0.9/go.mod h1:CLchJCGhb39Obyrci816R+KMtbxZhgPs0FuikhyixK4=
github.com/injoyai/base v1.2.15 h1:K/ysPqZl7vgNUAz/jpG1IdDpzdSMWvUfoJL+1gPdM9g=
github.com/injoyai/base v1.2.15/go.mod h1:NfCQjml3z2pCvQ3J3YcOXtecqXD0xVPKjo4YTsMLhr8=
github.com/injoyai/conv v1.2.5 h1:G4OCyF0NTZul5W1u9IgXDOhW4/zmIigdPKXFHQGmv1M=
github.com/injoyai/conv v1.2.5/go.mod h1:s05l3fQJQ4mT4VX+KIdbvCWQB0YzZHprmUfUu2uxd1k=
github.com/injoyai/ios v1.2.2 h1:fAPWBL6t22DiE2ZEpBgf5bzyVQTcm2ZhLMkM+JFPhZA=
github.com/injoyai/ios v1.2.2/go.mod h1:DJVJGQFQvqF80CeJVabFOm6AKilqc/m8MFvz39Uy5ow=
github.com/injoyai/logs v1.0.12 h1:f7syIGZMTg9ZzhJhdd3tzaPdxkMhdKsncGaxljqIiYE=
github.com/injoyai/logs v1.0.12/go.mod h1:+dKEL6GvaFqqVRatqUBiCicJbZnAgtj7hVs824Src4s=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
@@ -42,11 +41,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
@@ -92,13 +88,10 @@ golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
@@ -113,8 +106,8 @@ gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -1,10 +1,12 @@
package tdx
import (
"github.com/injoyai/base/types"
"github.com/injoyai/logs"
"net"
"strings"
"sync"
"time"
)
var (
@@ -75,12 +77,12 @@ var (
}
)
// FastHosts 通过tcp(ping不可用)的方式筛选可用的地址,并排序(有点误差)
func FastHosts(hosts ...string) []string {
// FastHosts 通过tcp(ping不可用)连接速度的方式筛选排序可用的地址
func FastHosts(hosts ...string) []DialResult {
wg := sync.WaitGroup{}
wg.Add(len(hosts))
mu := sync.Mutex{}
ls := []string(nil)
ls := types.List[DialResult](nil)
for _, host := range hosts {
go func(host string) {
defer wg.Done()
@@ -88,17 +90,30 @@ func FastHosts(hosts ...string) []string {
if !strings.Contains(addr, ":") {
addr += ":7709"
}
now := time.Now()
c, err := net.Dial("tcp", addr)
if err != nil {
logs.Err(err)
return
}
defer c.Close()
spend := time.Since(now)
c.Close()
mu.Lock()
ls = append(ls, host)
ls = append(ls, DialResult{
Host: host,
Spend: spend,
})
mu.Unlock()
}(host)
}
wg.Wait()
return ls
return ls.Sort(func(a, b DialResult) bool {
return a.Spend < b.Spend
})
}
// DialResult 连接结果
type DialResult struct {
Host string
Spend time.Duration
}

View File

@@ -25,13 +25,21 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
cfg.Dial = DialDefault
}
//代码
codesClient, err := cfg.Dial(op...)
//通用客户端
commonClient, err := cfg.Dial(op...)
if err != nil {
return nil, err
}
codesClient.Wait.SetTimeout(time.Second * 5)
codes, err := NewCodes(codesClient, cfg.CodesFilename)
commonClient.Wait.SetTimeout(time.Second * 5)
//代码管理
codes, err := NewCodes(commonClient, cfg.CodesFilename)
if err != nil {
return nil, err
}
//工作日管理
workday, err := NewWorkday(commonClient, cfg.WorkdayFileName)
if err != nil {
return nil, err
}
@@ -44,17 +52,6 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
return nil, err
}
//工作日
workdayClient, err := cfg.Dial(op...)
if err != nil {
return nil, err
}
workdayClient.Wait.SetTimeout(time.Second * 5)
workday, err := NewWorkday(workdayClient, cfg.WorkdayFileName)
if err != nil {
return nil, err
}
return &Manage{
Pool: p,
Config: cfg,
@@ -72,6 +69,20 @@ type Manage struct {
Cron *cron.Cron
}
// RangeStocks 遍历所有股票
func (this *Manage) RangeStocks(f func(code string)) {
for _, v := range this.Codes.GetStocks() {
f(v)
}
}
// RangeETFs 遍历所有ETF
func (this *Manage) RangeETFs(f func(code string)) {
for _, v := range this.Codes.GetETFs() {
f(v)
}
}
// AddWorkdayTask 添加工作日任务
func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) {
this.Cron.AddFunc(spec, func() {

View File

@@ -39,6 +39,19 @@ type Frame struct {
Data []byte //数据
}
/*
Bytes
0c00000000011c001c002d0500003030303030310900010000000a0000000000000000000000
Prefix: 0c
MsgID: 0208d301
Control: 01
Length: 1c00
Length: 1c00
Type: 2d05
000030303030303104000100a401a40100000000000000000000
*/
func (this *Frame) Bytes() types.Bytes {
length := uint16(len(this.Data) + 2)
data := make([]byte, 12+len(this.Data))

View File

@@ -6,11 +6,8 @@ import (
"time"
)
// HistoryTradeResp 历史分时交易比实时少了单量
type HistoryTradeResp struct {
Count uint16
List Trades
}
// HistoryTradeResp 兼容之前的版本
type HistoryTradeResp = TradeResp
type historyTrade struct{}
@@ -31,7 +28,7 @@ func (historyTrade) Frame(date, code string, start, count uint16) (*Frame, error
}, nil
}
func (historyTrade) Decode(bs []byte, c TradeCache) (*HistoryTradeResp, error) {
func (historyTrade) Decode(bs []byte, c TradeCache) (*TradeResp, error) {
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
@@ -41,7 +38,7 @@ func (historyTrade) Decode(bs []byte, c TradeCache) (*HistoryTradeResp, error) {
return nil, err
}
resp := &HistoryTradeResp{
resp := &TradeResp{
Count: Uint16(bs[:2]),
}

View File

@@ -86,6 +86,27 @@ func (this *Kline) RiseRate() float64 {
type kline struct{}
/*
Frame
Prefix: 0c
MsgID: 0208d301
Control: 01
Length: 1c00
Length: 1c00
Type: 2d05
Data: 000030303030303104000100a401a40100000000000000000000
Data:
Exchange: 00
Unknown: 00
Code: 303030303031
Type: 04
Unknown: 00
Unknown: 0100
Start: a401
Count: a401
Append: 00000000000000000000
*/
func (kline) Frame(Type uint8, code string, start, count uint16) (*Frame, error) {
if count > 800 {
return nil, errors.New("单次数量不能超过800")
@@ -116,7 +137,6 @@ func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) {
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
resp := &KlineResp{
Count: Uint16(bs[:2]),
}
@@ -161,7 +181,7 @@ func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) {
k.Volume = int64(getVolume(Uint32(bs[:4])))
bs = bs[4:]
switch c.Type {
case TypeKlineMinute, TypeKline5Minute, TypeKlineMinute2, TypeKline15Minute, TypeKline30Minute, TypeKlineHour, TypeKlineDay2:
case TypeKlineMinute, TypeKline5Minute, TypeKlineMinute2, TypeKline15Minute, TypeKline30Minute, TypeKline60Minute, TypeKlineDay2:
k.Volume /= 100
}
k.Amount = Price(getVolume(Uint32(bs[:4])) * 1000) //从元转为厘,并去除多余的小数

View File

@@ -50,6 +50,7 @@ const (
TypeKline5Minute uint8 = 0 // 5分钟K 线
TypeKline15Minute uint8 = 1 // 15分钟K 线
TypeKline30Minute uint8 = 2 // 30分钟K 线
TypeKline60Minute uint8 = 3 // 60分钟K 线
TypeKlineHour uint8 = 3 // 1小时K 线
TypeKlineDay2 uint8 = 4 // 日K 线, 发现和Day的区别是这个要除以100,其他未知
TypeKlineWeek uint8 = 5 // 周K 线

View File

@@ -102,7 +102,7 @@ func GetHourMinute(bs [2]byte) string {
func GetTime(bs [4]byte, Type uint8) time.Time {
switch Type {
case TypeKlineMinute, TypeKlineMinute2, TypeKline5Minute, TypeKline15Minute, TypeKline30Minute, TypeKlineHour:
case TypeKlineMinute, TypeKlineMinute2, TypeKline5Minute, TypeKline15Minute, TypeKline30Minute, TypeKline60Minute:
yearMonthDay := Uint16(bs[:2])
hourMinute := Uint16(bs[2:4])