Compare commits

..

8 Commits

Author SHA1 Message Date
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
9 changed files with 113 additions and 44 deletions

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

@@ -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,6 +44,14 @@ 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
@@ -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)

6
go.mod
View File

@@ -5,9 +5,9 @@ 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/base v1.2.8
github.com/injoyai/conv v1.2.5
github.com/injoyai/ios v0.0.10
github.com/injoyai/logs v1.0.9
github.com/robfig/cron/v3 v3.0.1
golang.org/x/text v0.16.0

12
go.sum
View File

@@ -27,12 +27,12 @@ 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/base v1.2.8 h1:voEPqxE5U+wI1RvcQStVbjUZJDLENbl1SsoQQWMn4s0=
github.com/injoyai/base v1.2.8/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 v0.0.10 h1:Zd37Rwp90PYV5eFhirR0LJ+ni/aYLSCAYgHltk/ZzJA=
github.com/injoyai/ios v0.0.10/go.mod h1:zLTmvQmIbdTf7zZxymOVxbxvvSeUMpcs4SCVPDT9BOc=
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/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=

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() {