Compare commits

..

39 Commits

Author SHA1 Message Date
injoyai
b86e7dcacf 优化首次拉取会报错的问题(未创建目录) 2025-04-18 13:33:59 +08:00
injoyai
af505eb55e 优化首次拉取会报错的问题 2025-04-18 13:29:15 +08:00
钱纯净
630cbb8939 Merge remote-tracking branch 'origin/master' 2025-04-17 23:17:28 +08:00
钱纯净
0b89aadd7f 优化 2025-04-17 23:17:09 +08:00
injoyai
7c8b4989f6 增加pull-kline的mysql版本,待测试 2025-04-16 14:22:10 +08:00
injoyai
b00f1b65d5 默认按股票处理,不用配置DefaultCodes,基金才需要 2025-04-16 10:44:06 +08:00
钱纯净
78e2ead79c 修复分时成交金额不准的问题 2025-04-09 23:18:00 +08:00
钱纯净
f69dd66ecb 测试分时成交数据不准的问题 2025-04-09 23:17:30 +08:00
钱纯净
4ce4adbfea 查询盘口信息得初始化DefaultCodes 2025-04-09 23:16:53 +08:00
injoyai
20de683bca Merge remote-tracking branch 'origin/master' 2025-04-09 17:02:47 +08:00
injoyai
49c6deb9c4 测试分时部分股票值不对的问题 2025-04-09 17:02:33 +08:00
钱纯净
5c0bc2a772 优化 2025-04-01 18:50:00 +08:00
钱纯净
0f75b402bc 优化PullKline 2025-03-26 19:21:31 +08:00
钱纯净
ce6718831c Merge remote-tracking branch 'origin/master' 2025-03-25 20:39:28 +08:00
钱纯净
031d9f6509 调整获取指数示例 2025-03-25 20:39:13 +08:00
injoyai
5e4115d045 修改DefaultCodes,需要手动赋值 2025-03-24 17:01:24 +08:00
injoyai
1cae60c65e 修改NewRangeDial等待间隔为2秒 2025-03-24 16:40:37 +08:00
injoyai
e76043dc29 可以自定义连接方式 2025-03-24 16:33:52 +08:00
injoyai
ff7fc6aba0 增加RangDial等待时间 2025-03-24 16:29:03 +08:00
injoyai
0c27cc4276 默认开启日志,日志等级为Info 2025-03-24 16:28:30 +08:00
injoyai
80d6d6dfc5 优化 2025-03-20 13:37:34 +08:00
injoyai
a27461740a Merge remote-tracking branch 'origin/master' 2025-03-20 13:19:05 +08:00
injoyai
7a9d59f8f1 优化 2025-03-20 13:18:49 +08:00
钱纯净
cba35308f6 优化GetQuote方法 2025-03-19 22:47:46 +08:00
钱纯净
424e259f2b 测试 2025-03-19 22:20:29 +08:00
injoyai
3ea8d50cd7 整理代码 2025-03-18 16:50:26 +08:00
injoyai
09116ee8e8 整理代码 2025-03-18 16:43:39 +08:00
injoyai
31fcc0b3a3 整理代码 2025-03-18 16:43:20 +08:00
injoyai
296b7197a1 整理代码 2025-03-18 16:43:14 +08:00
injoyai
b4f748306a 优化NewRangeDial 2025-03-18 15:02:36 +08:00
injoyai
0cfd21f8fb 测试拓展NewPullKline 2025-03-18 14:59:38 +08:00
injoyai
1c9fd75876 默认使用DialHostsRange函数进行连接 2025-03-18 14:56:15 +08:00
injoyai
e4de354a66 测试DialHostsRange 2025-03-18 14:55:44 +08:00
injoyai
6062da9cb2 测试拓展pullkline 2025-03-18 14:55:15 +08:00
injoyai
0414cbebc3 测试拓展pullkline 2025-03-18 14:55:01 +08:00
injoyai
b221dbc3e2 Manage简单兼容DefaultCodes 2025-03-18 14:48:02 +08:00
injoyai
51d8d06fbd 增加NewRangeDial函数,更方便使用 2025-03-18 14:47:15 +08:00
injoyai
9df96f0707 增加DialHostsRange函数,更方便使用 2025-03-18 14:46:55 +08:00
injoyai
53cc5fd743 细节优化 2025-03-18 13:43:17 +08:00
19 changed files with 776 additions and 104 deletions

103
client.go
View File

@@ -8,15 +8,24 @@ import (
"github.com/injoyai/conv"
"github.com/injoyai/ios"
"github.com/injoyai/ios/client"
"github.com/injoyai/ios/module/tcp"
"github.com/injoyai/ios/module/common"
"github.com/injoyai/logs"
"github.com/injoyai/tdx/protocol"
"runtime/debug"
"strings"
"sync/atomic"
"time"
)
const (
LevelNone = common.LevelNone
LevelDebug = common.LevelDebug
LevelWrite = common.LevelWrite
LevelRead = common.LevelRead
LevelInfo = common.LevelInfo
LevelError = common.LevelError
LevelAll = common.LevelAll
)
// WithDebug 是否打印通讯数据
func WithDebug(b ...bool) client.Option {
return func(c *client.Client) {
@@ -24,6 +33,12 @@ func WithDebug(b ...bool) client.Option {
}
}
func WithLevel(level int) client.Option {
return func(c *client.Client) {
c.Logger.SetLevel(level)
}
}
// WithRedial 断线重连
func WithRedial(b ...bool) client.Option {
return func(c *client.Client) {
@@ -31,17 +46,15 @@ func WithRedial(b ...bool) client.Option {
}
}
// Dial 与服务器建立连接
func Dial(addr string, op ...client.Option) (cli *Client, err error) {
if !strings.Contains(addr, ":") {
addr += ":7709"
}
return DialWith(tcp.NewDial(addr), op...)
}
// DialDefault 默认连接方式
func DialDefault(op ...client.Option) (cli *Client, err error) {
return DialWith(NewHostDial(Hosts), op...)
op = append([]client.Option{WithRedial()}, op...)
return DialHostsRange(Hosts, op...)
}
// Dial 与服务器建立连接
func Dial(addr string, op ...client.Option) (cli *Client, err error) {
return DialWith(NewTCPDial(addr), op...)
}
// DialHosts 与服务器建立连接,多个服务器轮询,开启重试生效
@@ -54,6 +67,11 @@ func DialHostsRandom(hosts []string, op ...client.Option) (cli *Client, err erro
return DialWith(NewRandomDial(hosts), op...)
}
// DialHostsRange 遍历设置的服务地址进行连接,成功则结束遍历
func DialHostsRange(hosts []string, op ...client.Option) (cli *Client, err error) {
return DialWith(NewRangeDial(hosts), op...)
}
// DialWith 与服务器建立连接
func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
@@ -63,7 +81,8 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
}
cli.Client, err = client.Dial(dial, func(c *client.Client) {
c.Logger.Debug(false) //关闭日志打印
c.Logger.Debug(true) //关闭日志打印
c.Logger.SetLevel(LevelInfo) //设置日志级别
c.Logger.WithHEX() //以HEX显示
c.SetOption(op...) //自定义选项
c.Event.OnReadFrom = protocol.ReadFrom //分包
@@ -86,16 +105,6 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
go cli.Client.Run()
/*
部分接口需要通过代码信息计算得出
*/
codesOnce.Do(func() {
//初始化代码管理
if DefaultCodes == nil {
DefaultCodes, err = NewCodes(cli, "./codes.db")
}
})
return cli, err
}
@@ -226,9 +235,18 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp,
// GetQuote 获取盘口五档报价
func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
if DefaultCodes == nil {
return nil, errors.New("DefaultCodes未初始化")
for i := range codes {
//如果是股票代码,则加上前缀
codes[i] = protocol.AddPrefix(codes[i])
if !protocol.IsStock(codes[i]) {
if DefaultCodes == nil {
return nil, errors.New("DefaultCodes未初始化")
}
//不是股票代码的话根据codes的信息加上前缀
codes[i] = DefaultCodes.AddExchange(codes[i])
}
}
f, err := protocol.MQuote.Frame(codes...)
if err != nil {
return nil, err
@@ -244,26 +262,25 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
if len(quotes) != len(codes) {
return nil, fmt.Errorf("预期%d个实际%d个", len(codes), len(quotes))
}
if DefaultCodes == nil {
return nil, errors.New("DefaultCodes未初始化")
}
for i, code := range codes {
m := DefaultCodes.Get(code)
if m == nil {
return nil, fmt.Errorf("未查询到代码[%s]相关信息", code)
}
for ii, v := range quotes[i].SellLevel {
quotes[i].SellLevel[ii].Price = m.Price(v.Price)
}
for ii, v := range quotes[i].BuyLevel {
quotes[i].BuyLevel[ii].Price = m.Price(v.Price)
}
quotes[i].K = protocol.K{
Last: m.Price(quotes[i].K.Last),
Open: m.Price(quotes[i].K.Open),
High: m.Price(quotes[i].K.High),
Low: m.Price(quotes[i].K.Low),
Close: m.Price(quotes[i].K.Close),
if !protocol.IsStock(code) {
m := DefaultCodes.Get(code)
if m == nil {
return nil, fmt.Errorf("未查询到代码[%s]相关信息", code)
}
for ii, v := range quotes[i].SellLevel {
quotes[i].SellLevel[ii].Price = m.Price(v.Price)
}
for ii, v := range quotes[i].BuyLevel {
quotes[i].BuyLevel[ii].Price = m.Price(v.Price)
}
quotes[i].K = protocol.K{
Last: m.Price(quotes[i].K.Last),
Open: m.Price(quotes[i].K.Open),
High: m.Price(quotes[i].K.High),
Low: m.Price(quotes[i].K.Low),
Close: m.Price(quotes[i].K.Close),
}
}
}
}

124
codes.go
View File

@@ -1,25 +1,34 @@
package tdx
import (
"errors"
"github.com/injoyai/conv"
"github.com/injoyai/ios/client"
"github.com/injoyai/logs"
"github.com/injoyai/tdx/protocol"
"github.com/robfig/cron/v3"
"math"
"os"
"path/filepath"
"sync"
"time"
"xorm.io/core"
"xorm.io/xorm"
)
// 增加单例,部分数据需要通过Codes里面的信息计算
var (
DefaultCodes *Codes
codesOnce sync.Once
)
// DefaultCodes 增加单例,部分数据需要通过Codes里面的信息计算
var DefaultCodes *Codes
func NewCodes(c *Client, filename string) (*Codes, error) {
func DialCodes(filename string, op ...client.Option) (*Codes, error) {
c, err := DialDefault(op...)
if err != nil {
return nil, err
}
return NewCodes(c, filename)
}
func NewCodes(c *Client, filenames ...string) (*Codes, error) {
filename := conv.DefaultString("./codes.db", filenames...)
//如果文件夹不存在就创建
dir, _ := filepath.Split(filename)
@@ -54,7 +63,6 @@ func NewCodes(c *Client, filename string) (*Codes, error) {
cc := &Codes{
Client: c,
db: db,
Codes: nil,
}
{ //设置定时器,每天早上9点更新数据
@@ -93,47 +101,104 @@ func NewCodes(c *Client, filename string) (*Codes, error) {
}
type Codes struct {
*Client //客户端
db *xorm.Engine //数据库实例
Codes map[string]*CodeModel //股票缓存
*Client //客户端
db *xorm.Engine //数据库实例
Map map[string]*CodeModel //股票缓存
list []*CodeModel //列表方式缓存
exchanges map[string][]string //交易所缓存
}
// GetName 获取股票名称
func (this *Codes) GetName(code string) string {
if v, ok := this.Codes[code]; ok {
if v, ok := this.Map[code]; ok {
return v.Name
}
return "未知"
}
// GetStocks 获取股票代码,sh6xxx sz0xx sz30xx
func (this *Codes) GetStocks() []string {
func (this *Codes) GetStocks(limits ...int) []string {
limit := conv.DefaultInt(-1, limits...)
ls := []string(nil)
for k, _ := range this.Codes {
if protocol.IsStock(k) {
ls = append(ls, k)
for _, m := range this.list {
code := m.FullCode()
if protocol.IsStock(code) {
ls = append(ls, code)
}
if limit > 0 && len(ls) >= limit {
break
}
}
return ls
}
func (this *Codes) Get(code string) *CodeModel {
if v, ok := this.Codes[code]; ok {
return v
}
return nil
return this.Map[code]
}
func (this *Codes) Update(byCache ...bool) error {
codes, err := this.GetCodes(len(byCache) > 0 && byCache[0])
// GetExchange 获取股票交易所,这里的参数不需要带前缀
func (this *Codes) GetExchange(code string) protocol.Exchange {
if len(code) == 6 {
switch {
case code[:1] == "6":
return protocol.ExchangeSH
case code[:1] == "0":
return protocol.ExchangeSZ
case code[:2] == "30":
return protocol.ExchangeSZ
}
}
var exchange string
exchanges := this.exchanges[code]
if len(exchanges) >= 1 {
exchange = exchanges[0]
}
if len(code) == 8 {
exchange = code[0:2]
}
switch exchange {
case protocol.ExchangeSH.String():
return protocol.ExchangeSH
case protocol.ExchangeSZ.String():
return protocol.ExchangeSZ
default:
return protocol.ExchangeSH
}
}
func (this *Codes) AddExchange(code string) string {
if exchanges := this.exchanges[code]; len(exchanges) == 1 {
return exchanges[0] + code
}
if len(code) == 6 {
switch {
case code[:1] == "6":
return protocol.ExchangeSH.String() + code
case code[:1] == "0":
return protocol.ExchangeSZ.String() + code
case code[:2] == "30":
return protocol.ExchangeSZ.String() + code
}
return this.GetExchange(code).String() + code
}
return code
}
// Update 更新数据,从服务器或者数据库
func (this *Codes) Update(byDB ...bool) error {
codes, err := this.GetCodes(len(byDB) > 0 && byDB[0])
if err != nil {
return err
}
codeMap := make(map[string]*CodeModel)
exchanges := make(map[string][]string)
for _, code := range codes {
codeMap[code.Exchange+code.Code] = code
exchanges[code.Code] = append(exchanges[code.Code], code.Exchange)
}
this.Codes = codeMap
this.Map = codeMap
this.list = codes
this.exchanges = exchanges
//更新时间
_, err = this.db.Update(&UpdateModel{Time: time.Now().Unix()})
return err
@@ -142,6 +207,10 @@ func (this *Codes) Update(byCache ...bool) error {
// GetCodes 更新股票并返回结果
func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
if this.Client == nil {
return nil, errors.New("client is nil")
}
//2. 查询数据库所有股票
list := []*CodeModel(nil)
if err := this.db.Find(&list); err != nil {
@@ -202,7 +271,7 @@ func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
}
}
for _, v := range update {
if _, err := session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Cols("Name").Update(v); err != nil {
if _, err := session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Cols("Name,LastPrice").Update(v); err != nil {
return err
}
}
@@ -240,8 +309,13 @@ func (*CodeModel) TableName() string {
return "codes"
}
func (this *CodeModel) FullCode() string {
return this.Exchange + this.Code
}
func (this *CodeModel) Price(p protocol.Price) protocol.Price {
return p * protocol.Price(math.Pow10(int(3-this.Decimal)))
return protocol.Price(float64(p) * math.Pow10(int(2-this.Decimal)))
return p * protocol.Price(math.Pow10(int(2-this.Decimal)))
}
func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error {

37
dial.go
View File

@@ -3,12 +3,21 @@ package tdx
import (
"context"
"github.com/injoyai/ios"
"github.com/injoyai/ios/module/tcp"
"github.com/injoyai/logs"
"math/rand"
"net"
"strings"
"time"
)
func NewTCPDial(addr string) ios.DialFunc {
if !strings.Contains(addr, ":") {
addr += ":7709"
}
return tcp.NewDial(addr)
}
func NewHostDial(hosts []string) ios.DialFunc {
if len(hosts) == 0 {
hosts = Hosts
@@ -43,3 +52,31 @@ func NewRandomDial(hosts []string) ios.DialFunc {
return c, addr, err
}
}
func NewRangeDial(hosts []string) ios.DialFunc {
if len(hosts) == 0 {
hosts = Hosts
}
return func(ctx context.Context) (c ios.ReadWriteCloser, _ string, err error) {
for i, addr := range hosts {
select {
case <-ctx.Done():
return nil, "", ctx.Err()
default:
}
if !strings.Contains(addr, ":") {
addr += ":7709"
}
c, err = net.Dial("tcp", addr)
if err == nil {
return c, addr, nil
}
if i < len(hosts)-1 {
//最后一个错误返回出去
logs.Err(err, "等待2秒后尝试下一个服务地址...")
<-time.After(time.Second * 2)
}
}
return
}
}

View File

@@ -0,0 +1,46 @@
package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"time"
)
func main() {
m, err := tdx.NewManage(nil)
logs.PanicErr(err)
codes := m.Codes.GetStocks()
//codes = []string{
// "sz000001",
// "sz000002",
//}
for _, code := range codes {
m.Do(func(c *tdx.Client) error {
resp, err := c.GetHistoryMinute(time.Now().Format("20060102"), code)
logs.PanicErr(err)
resp2, err := c.GetKlineDay(code, 0, 1)
logs.PanicErr(err)
if len(resp2.List) == 0 {
logs.Debug(code)
return nil
}
if len(resp.List) == 0 {
logs.Debug(code)
return nil
}
if resp2.List[0].Close != resp.List[len(resp.List)-1].Price {
logs.Debug(code)
}
return nil
})
}
}

View File

@@ -0,0 +1,11 @@
package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
)
func main() {
_, err := tdx.DialHostsRange([]string{"1", "2", "127.0.0.1"})
logs.PrintErr(err)
}

View File

@@ -8,7 +8,7 @@ import (
func main() {
common.Test(func(c *tdx.Client) {
resp, err := c.GetKlineDay("sh000001", 0, 10)
resp, err := c.GetIndexDay("sh000001", 0, 10)
logs.PanicErr(err)
for _, v := range resp.List {

View File

@@ -10,6 +10,9 @@ func main() {
c, err := tdx.Dial("124.71.187.122:7709", tdx.WithDebug())
logs.PanicErr(err)
tdx.DefaultCodes, err = tdx.NewCodes(c, "./codes.db")
logs.PanicErr(err)
_ = c
/*
@@ -21,7 +24,7 @@ func main() {
b1cb74001c00000000000d005100bd00789c6378c1cecb252ace6066c5b4898987b9050ed1f90cc5b74c18a5bc18c1b43490fecff09c81819191f13fc3c9f3bb169f5e7dfefeb5ef57f7199a305009308208e5b32bb6bcbf70148712002d7f1e13
b1cb74000c02000000003e05ac00ac000102020000303030303031601294121a1c2d4eadabcf0ed412aae5fc01afb0024561124fbcc08301afa47900b2e3174100bf68871a4201b741b6144302bb09af334403972e96354504ac09b619560e00000000f8ff601201363030303038b60fba04060607429788a70efa04ada37ab2531c12974d91e7449dbc354184b6010001844bad324102b5679ea1014203a65abd8d0143048a6ba4dd01440587e101b3d2029613000000000000b60f
*/
resp, err := c.GetQuote("sz000001", "sh600008")
resp, err := c.GetQuote("000001", "600000", "159558", "010504")
logs.PanicErr(err)
for _, v := range resp {

26
example/PullKline/main.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import (
"context"
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/extend"
"path/filepath"
"time"
)
func main() {
m, err := tdx.NewManage(nil)
logs.PanicErr(err)
err = extend.NewPullKline(extend.PullKlineConfig{
Codes: []string{"sz000001"},
Tables: []string{extend.Year},
Dir: filepath.Join(tdx.DefaultDatabaseDir, "kline"),
Limit: 1,
StartAt: time.Time{},
}).Run(context.Background(), m)
logs.PanicErr(err)
}

148
extend/pull-kline-mysql.go Normal file
View File

@@ -0,0 +1,148 @@
package extend
import (
"context"
_ "github.com/go-sql-driver/mysql"
"github.com/injoyai/base/chans"
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/protocol"
"xorm.io/core"
"xorm.io/xorm"
)
func NewPullKlineMysql(cfg PullKlineConfig) (*PullKlineMysql, error) {
db, err := xorm.NewEngine("mysql", cfg.Dir)
if err != nil {
return nil, err
}
db.SetMapper(core.SameMapper{})
_tables := []*KlineTable(nil)
for _, v := range cfg.Tables {
table := KlineTableMap[v]
if err = db.Sync2(table); err != nil {
return nil, err
}
_tables = append(_tables, table)
}
return &PullKlineMysql{
tables: _tables,
Config: cfg,
DB: db,
}, nil
}
type PullKlineMysql struct {
tables []*KlineTable
Config PullKlineConfig
DB *xorm.Engine
}
func (this *PullKlineMysql) Name() string {
return "拉取k线数据"
}
func (this *PullKlineMysql) Run(ctx context.Context, m *tdx.Manage) error {
limit := chans.NewWaitLimit(uint(this.Config.Limit))
//1. 获取所有股票代码
codes := this.Config.Codes
if len(codes) == 0 {
codes = m.Codes.GetStocks()
}
for _, v := range codes {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
limit.Add()
go func(code string) {
defer limit.Done()
for _, table := range this.tables {
if table == nil {
continue
}
select {
case <-ctx.Done():
return
default:
}
var err error
//2. 获取最后一条数据
last := new(Kline)
if _, err = this.DB.Table(table).Where("Code=?", code).Desc("Date").Get(last); err != nil {
logs.Err(err)
return
}
//3. 从服务器获取数据
insert := Klines{}
err = m.Do(func(c *tdx.Client) error {
insert, err = this.pull(code, last.Date, table.Handler(c))
return err
})
if err != nil {
logs.Err(err)
return
}
//4. 插入数据库
err = tdx.NewSessionFunc(this.DB, func(session *xorm.Session) error {
for i, v := range insert {
if i == 0 {
if _, err := session.Table(table).Where("Code=? and Date >= ?", code, v.Date).Delete(); err != nil {
return err
}
}
if _, err := session.Table(table).Insert(v); err != nil {
return err
}
}
return nil
})
logs.PrintErr(err)
}
}(v)
}
limit.Wait()
return nil
}
func (this *PullKlineMysql) pull(code string, lastDate int64, f func(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error)) (Klines, error) {
if lastDate == 0 {
lastDate = protocol.ExchangeEstablish.Unix()
}
resp, err := f(code, func(k *protocol.Kline) bool {
return k.Time.Unix() <= lastDate || k.Time.Unix() <= this.Config.StartAt.Unix()
})
if err != nil {
return nil, err
}
ks := Klines{}
for _, v := range resp.List {
ks = append(ks, &Kline{
Code: code,
Date: v.Time.Unix(),
Open: v.Open,
High: v.High,
Low: v.Low,
Close: v.Close,
Volume: v.Volume,
Amount: v.Amount,
})
}
return ks, nil
}

288
extend/pull-kline.go Normal file
View File

@@ -0,0 +1,288 @@
package extend
import (
"context"
_ "github.com/glebarez/go-sqlite"
"github.com/injoyai/base/chans"
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/protocol"
"os"
"path/filepath"
"sort"
"time"
"xorm.io/core"
"xorm.io/xorm"
)
const (
Minute = "minute"
Minute5 = "5minute"
Minute15 = "15minute"
Minute30 = "30minute"
Hour = "hour"
Day = "day"
Week = "week"
Month = "month"
Quarter = "quarter"
Year = "year"
)
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",
}
)
type PullKlineConfig struct {
Codes []string //操作代码
Tables []string //数据类型
Dir string //数据位置
Limit int //协程数量
StartAt time.Time //数据开始时间
}
func NewPullKline(cfg PullKlineConfig) *PullKline {
_tables := []*KlineTable(nil)
for _, v := range cfg.Tables {
_tables = append(_tables, KlineTableMap[v])
}
return &PullKline{
tables: _tables,
Config: cfg,
}
}
type PullKline struct {
tables []*KlineTable
Config PullKlineConfig
}
func (this *PullKline) Name() string {
return "拉取k线数据"
}
func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
limit := chans.NewWaitLimit(uint(this.Config.Limit))
//1. 获取所有股票代码
codes := this.Config.Codes
if len(codes) == 0 {
codes = m.Codes.GetStocks()
}
for _, v := range codes {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
limit.Add()
go func(code string) {
defer limit.Done()
_ = os.MkdirAll(this.Config.Dir, 0777)
//连接数据库
db, err := xorm.NewEngine("sqlite", filepath.Join(this.Config.Dir, code+".db"))
if err != nil {
logs.Err(err)
return
}
db.SetMapper(core.SameMapper{})
db.DB().SetMaxOpenConns(1)
for _, table := range this.tables {
if table == nil {
continue
}
select {
case <-ctx.Done():
return
default:
}
logs.PrintErr(db.Sync2(table))
//2. 获取最后一条数据
last := new(Kline)
if _, err = db.Table(table).Desc("Date").Get(last); err != nil {
logs.Err(err)
return
}
//3. 从服务器获取数据
insert := Klines{}
err = m.Do(func(c *tdx.Client) error {
insert, err = this.pull(code, last.Date, table.Handler(c))
return err
})
if err != nil {
logs.Err(err)
return
}
//4. 插入数据库
err = tdx.NewSessionFunc(db, func(session *xorm.Session) error {
for i, v := range insert {
if i == 0 {
if _, err := session.Table(table).Where("Date >= ?", v.Date).Delete(); err != nil {
return err
}
}
if _, err := session.Table(table).Insert(v); err != nil {
return err
}
}
return nil
})
logs.PrintErr(err)
}
}(v)
}
limit.Wait()
return nil
}
func (this *PullKline) pull(code string, lastDate int64, f func(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error)) (Klines, error) {
if lastDate == 0 {
lastDate = protocol.ExchangeEstablish.Unix()
}
resp, err := f(code, func(k *protocol.Kline) bool {
return k.Time.Unix() <= lastDate || k.Time.Unix() <= this.Config.StartAt.Unix()
})
if err != nil {
return nil, err
}
ks := Klines{}
for _, v := range resp.List {
ks = append(ks, &Kline{
Code: code,
Date: v.Time.Unix(),
Open: v.Open,
High: v.High,
Low: v.Low,
Close: v.Close,
Volume: v.Volume,
Amount: v.Amount,
})
}
return ks, nil
}
type Kline struct {
Code string `json:"code"` //代码
Date int64 `json:"date"` //时间节点 2006-01-02 15:00
Open protocol.Price `json:"open"` //开盘价
High protocol.Price `json:"high"` //最高价
Low protocol.Price `json:"low"` //最低价
Close protocol.Price `json:"close"` //收盘价
Volume int64 `json:"volume"` //成交量
Amount protocol.Price `json:"amount"` //成交额
InDate int64 `json:"inDate" xorm:"created"` //创建时间
}
type Klines []*Kline
func (this Klines) Less(i, j int) bool { return this[i].Code > this[j].Code }
func (this Klines) Swap(i, j int) { this[i], this[j] = this[j], this[i] }
func (this Klines) Len() int { return len(this) }
func (this Klines) Sort() { sort.Sort(this) }
// Kline 计算多个K线,成一个K线
func (this Klines) Kline() *Kline {
if this == nil {
return new(Kline)
}
k := new(Kline)
for i, v := range this {
switch i {
case 0:
k.Open = v.Open
k.High = v.High
k.Low = v.Low
k.Close = v.Close
case len(this) - 1:
k.Close = v.Close
k.Date = v.Date
}
if v.High > k.High {
k.High = v.High
}
if v.Low < k.Low {
k.Low = v.Low
}
k.Volume += v.Volume
k.Amount += v.Amount
}
return k
}
// Merge 合并K线
func (this Klines) Merge(n int) Klines {
if this == nil {
return nil
}
ks := []*Kline(nil)
for i := 0; i < len(this); i += n {
if i+n > len(this) {
ks = append(ks, this[i:].Kline())
} else {
ks = append(ks, this[i:i+n].Kline())
}
}
return ks
}
type KlineHandler func(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error)
func NewKlineTable(tableName string, handler func(c *tdx.Client) KlineHandler) *KlineTable {
return &KlineTable{
tableName: tableName,
Handler: handler,
}
}
type KlineTable struct {
Kline `xorm:"extends"`
tableName string
Handler func(c *tdx.Client) KlineHandler `xorm:"-"`
}
func (this *KlineTable) TableName() string {
return this.tableName
}

1
go.mod
View File

@@ -4,6 +4,7 @@ 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.0.18
github.com/injoyai/conv v1.1.10
github.com/injoyai/ios v0.0.4

1
go.sum
View File

@@ -14,6 +14,7 @@ github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.8.1 h1:4/Wjm0JIJaTDm8K1KcGrLHJoa8EsJ13YWeX+6Kfq6uI=
github.com/goccy/go-json v0.8.1/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=

View File

@@ -3,51 +3,54 @@ package tdx
import (
"github.com/injoyai/ios/client"
"github.com/robfig/cron/v3"
"path/filepath"
"time"
)
const (
DefaultDatabaseDir = "./data/database"
)
func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
//初始化配置
if cfg == nil {
cfg = &ManageConfig{}
}
if len(cfg.Hosts) == 0 {
cfg.Hosts = Hosts
if cfg.CodesFilename == "" {
cfg.CodesFilename = DefaultDatabaseDir + "/codes.db"
}
if cfg.CodesDir == "" {
cfg.CodesDir = "./data/database"
if cfg.WorkdayFileName == "" {
cfg.WorkdayFileName = DefaultDatabaseDir + "/workday.db"
}
if cfg.WorkdayDir == "" {
cfg.WorkdayDir = "./data/database"
if cfg.Dial == nil {
cfg.Dial = DialDefault
}
//代码
codesClient, err := cfg.Dial(op...)
if err != nil {
return nil, err
}
codesClient.Wait.SetTimeout(time.Second * 5)
codes, err := NewCodes(codesClient, cfg.CodesFilename)
if err != nil {
return nil, err
}
//连接池
p, err := NewPool(func() (*Client, error) {
return DialHosts(cfg.Hosts, op...)
return cfg.Dial(op...)
}, cfg.Number)
if err != nil {
return nil, err
}
//代码
codesClient, err := DialHosts(cfg.Hosts, op...)
if err != nil {
return nil, err
}
codesClient.Wait.SetTimeout(time.Second * 5)
codes, err := NewCodes(codesClient, filepath.Join(cfg.CodesDir, "codes.db"))
if err != nil {
return nil, err
}
//工作日
workdayClient, err := DialHosts(cfg.Hosts, op...)
workdayClient, err := cfg.Dial(op...)
if err != nil {
return nil, err
}
workdayClient.Wait.SetTimeout(time.Second * 5)
workday, err := NewWorkday(workdayClient, filepath.Join(cfg.WorkdayDir, "workday.db"))
workday, err := NewWorkday(workdayClient, cfg.WorkdayFileName)
if err != nil {
return nil, err
}
@@ -79,8 +82,8 @@ func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) {
}
type ManageConfig struct {
Hosts []string //服务端IP
Number int //客户端数量
CodesDir string //代码数据库位置
WorkdayDir string //工作日数据库位置
Number int //客户端数量
CodesFilename string //代码数据库位置
WorkdayFileName string //工作日数据库位置
Dial func(op ...client.Option) (cli *Client, err error) //默认连接方式
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/injoyai/base/safe"
)
// NewPool 简易版本的连接池
func NewPool(dial func() (*Client, error), number int) (*Pool, error) {
if number <= 0 {
number = 1

View File

@@ -33,10 +33,10 @@ func (this historyMinute) Decode(bs []byte) (*MinuteResp, error) {
Count: Uint16(bs[:2]),
}
multiple := Price(1)
if bs[5] > 0x40 {
multiple = 10
}
multiple := Price(1) * 10
//if bs[5] > 0x40 {
//multiple = 10
//}
//2-4字节是啥?
bs = bs[6:]

View File

@@ -77,7 +77,10 @@ func (this *Kline) RisePrice() Price {
// RiseRate 涨跌比例/涨跌幅,第一个数据不准,仅做参考
func (this *Kline) RiseRate() float64 {
return float64(this.RisePrice()) / float64(this.Open) * 100
if this.Last == 0 {
return float64(this.Close-this.Open) / float64(this.Open) * 100
}
return float64(this.Close-this.Last) / float64(this.Last) * 100
}
type kline struct{}

View File

@@ -47,7 +47,7 @@ type Quote struct {
func (this *Quote) String() string {
return fmt.Sprintf(`%s%s
%s
%s, 现量:%s, 总金额:%s, 内盘:%s, 外盘:%s
%s, 现量:%s, 总金额:%s, 内盘:%s, 外盘:%s
%s%s
`,
this.Exchange.String(), this.Code, this.K,
@@ -142,9 +142,9 @@ func (this quote) Decode(bs []byte) QuotesResp {
sellLevel := PriceLevel{}
bs, p = GetPrice(bs)
buyLevel.Price = p + sec.K.Close
buyLevel.Price = p*10 + sec.K.Close
bs, p = GetPrice(bs)
sellLevel.Price = p + sec.K.Close
sellLevel.Price = p*10 + sec.K.Close
bs, buyLevel.Number = CutInt(bs)
bs, sellLevel.Number = CutInt(bs)

View File

@@ -78,6 +78,13 @@ func DecodeK(bs []byte) ([]byte, K) {
bs, k.Low = GetPrice(bs)
k.Low += k.Close
//默认按股票展示
k.Last *= 10
k.Open *= 10
k.Close *= 10
k.High *= 10
k.Low *= 10
return bs, k
}

View File

@@ -1,6 +1,7 @@
package tdx
import (
"errors"
_ "github.com/glebarez/go-sqlite"
"github.com/injoyai/base/maps"
"github.com/injoyai/logs"
@@ -59,6 +60,11 @@ type Workday struct {
// Update 更新
func (this *Workday) Update() error {
if this.Client == nil {
return errors.New("client is nil")
}
//获取沪市指数的日K线,用作历史是否节假日的判断依据
//判断日K线是否拉取过
@@ -77,7 +83,7 @@ func (this *Workday) Update() error {
now := time.Now()
if lastWorkday == nil || lastWorkday.Unix < IntegerDay(now).Unix() {
resp, err := this.Client.GetKlineDayAll("sh000001")
resp, err := this.Client.GetIndexDayAll("sh000001")
if err != nil {
logs.Err(err)
return err