Compare commits

..

61 Commits

Author SHA1 Message Date
injoyai
f381d72ec4 优化Workday.Range 2025-10-30 10:45:26 +08:00
injoyai
5fa572f298 把集合竞价合并到931里面 2025-10-28 08:41:04 +08:00
injoyai
61b2e737b3 把集合竞价从931剥离出来到930 2025-10-27 09:09:19 +08:00
injoyai
5654065954 把集合竞价从931剥离出来到930 2025-10-27 08:49:22 +08:00
injoyai
d7dd7fe0bf 优化extend.GetBjCodes,直接输出代码 2025-10-22 14:25:30 +08:00
injoyai
9fb1a3b651 增加SetTimeout 设置超时时间 2025-10-21 09:57:03 +08:00
injoyai
f8a24c0cf1 优化GetHistoryTradeBefore,增加失败重试,共尝试3次,都失败则返回错误 2025-10-21 09:39:16 +08:00
injoyai
ad0abbe6ba 优化GetHistoryTradeFull,需要传入Workday,减少非工作日的查询操作 2025-10-20 16:00:09 +08:00
injoyai
0b35006323 优化GetHistoryTradeFull,需要传入Workday,减少非工作日的查询操作 2025-10-20 15:37:04 +08:00
injoyai
3b823e2e54 优化GetHistoryTradeFull,需要传入Workday,减少非工作日的查询操作 2025-10-20 15:35:20 +08:00
injoyai
5c8091ac26 修复深圳指数历史分时成交价格小10倍的问题 2025-10-20 09:49:01 +08:00
钱纯净
d7b6963bd6 修复workday没有要更新时会报错的问题 "no element on slice when insert" 2025-10-19 22:46:06 +08:00
injoyai
456a0af9a5 增加Manage对mysql的支持 2025-10-16 14:00:31 +08:00
injoyai
84404bcb2c 增加Manage对mysql的支持 2025-10-16 11:00:58 +08:00
injoyai
716e35122f 增加Manage对mysql的支持 2025-10-16 10:54:44 +08:00
injoyai
c4866a2f2e 增加Manage对mysql的支持 2025-10-16 10:47:24 +08:00
injoyai
fa98199dae 增加Codes和Workday对mysql的支持 2025-10-16 10:33:04 +08:00
injoyai
29882ea5c0 修复IsSZStock的判断 2025-10-14 13:07:59 +08:00
injoyai
37eb34beaa 优化 2025-10-14 09:23:45 +08:00
钱纯净
7bf4839310 修改GetHistoryTradeAll为GetHistoryTradeDay,增加GetHistoryTradeFull,获取上市至今的分时成交 2025-10-13 20:04:47 +08:00
钱纯净
cbf56d936d 优化Klines的Kline和Merge 2025-10-13 19:53:19 +08:00
injoyai
80ecdec737 增加extend.ListenCodesHTTP,因为北交所代码是网页爬虫,爬被封ip,感觉好像用处也不大 2025-10-13 15:05:21 +08:00
injoyai
ecf0365879 优化protocol.Trades,能直接生成完整的分时k线,和实际误差很小 2025-10-13 15:01:44 +08:00
injoyai
f1da1182ce 优化protocol.Trades,能直接生成完整的分时k线,和实际误差很小 2025-10-13 15:01:27 +08:00
injoyai
8fb069b855 简单测试北交所数据 2025-09-30 11:44:12 +08:00
injoyai
110eaddc4d 兼容北交所代码到GetCodeAll,方便原先的逻辑不用改动 2025-09-30 11:43:37 +08:00
injoyai
aec2cf1518 优化NewWorkday 2025-09-30 11:42:33 +08:00
injoyai
a596139d3e 移动GetBjCodes至tdx包 2025-09-30 11:42:10 +08:00
injoyai
578617e458 修复北交所(历史)分时成交小10倍的问题 2025-09-30 11:41:13 +08:00
钱纯净
fab9e92fcd 43开头也是北京交易所的 2025-09-27 22:32:22 +08:00
钱纯净
47084b1112 增加同花顺复权数据计算复权因子 2025-09-27 17:39:11 +08:00
钱纯净
2566ef5cec 增加获取北京交易所股票列表,通达信好像没有北交所股票列表 2025-09-27 17:38:43 +08:00
钱纯净
86404db551 Merge remote-tracking branch 'origin/master' 2025-09-21 15:28:48 +08:00
钱纯净
3f3438fca8 增加北京交易所数据 2025-09-21 15:28:32 +08:00
injoyai
1656b41f02 go1.25版本有点不兼容老版本,增加对go1.25版本的支持 2025-08-29 09:43:00 +08:00
injoyai
8090cc7216 Merge remote-tracking branch 'origin/master'
# Conflicts:
#	go.mod
#	go.sum
2025-08-29 09:41:42 +08:00
injoyai
a3169c67da go1.25版本有点不兼容老版本,增加对go1.25版本的支持 2025-08-29 09:41:01 +08:00
钱纯净
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
钱纯净
1187e1dcdf 优化PullTrade 2025-06-09 22:26:00 +08:00
钱纯净
2adcc9a322 修改名称 2025-06-09 22:23:40 +08:00
injoyai
b035cc7fbf 统一分时成交的结构名称为Trade,和K线Kline一样简洁 2025-06-09 16:14:02 +08:00
injoyai
e58130b3c3 统一分时成交的结构名称为Trade,和K线Kline一样简洁 2025-06-09 16:13:34 +08:00
钱纯净
d840e0d33d Merge remote-tracking branch 'origin/master' 2025-06-09 00:19:43 +08:00
钱纯净
78843ed6b7 增加PullTrade,用于拉取分时成交,然后计算成K线 2025-06-09 00:19:30 +08:00
钱纯净
04378dadf0 增加HistoryMinuteTrades和Klines 2025-06-09 00:18:51 +08:00
钱纯净
e74aeba7b2 增加RangeYear和Range 2025-06-09 00:18:11 +08:00
injoyai
bea9762c50 增加extend.GetTHSDayKlineFull获取前复权和后复权数据,并补充成交金额数据 2025-06-04 14:03:59 +08:00
41 changed files with 1733 additions and 541 deletions

209
client.go
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 {
@@ -157,10 +159,10 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) {
resp, err = protocol.MHistoryMinute.Decode(f.Data)
case protocol.TypeMinuteTrade:
resp, err = protocol.MMinuteTrade.Decode(f.Data, conv.String(val))
resp, err = protocol.MTrade.Decode(f.Data, val.(protocol.TradeCache))
case protocol.TypeHistoryMinuteTrade:
resp, err = protocol.MHistoryMinuteTrade.Decode(f.Data, conv.String(val))
resp, err = protocol.MHistoryTrade.Decode(f.Data, val.(protocol.TradeCache))
case protocol.TypeKline:
resp, err = protocol.MKline.Decode(f.Data, val.(protocol.KlineCache))
@@ -179,6 +181,11 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) {
}
// SetTimeout 设置超时时间
func (this *Client) SetTimeout(t time.Duration) {
this.Wait.SetTimeout(t)
}
// SendFrame 发送数据,并等待响应
func (this *Client) SendFrame(f *protocol.Frame, cache ...any) (any, error) {
f.MsgID = atomic.AddUint32(&this.msgID, 1)
@@ -218,6 +225,26 @@ func (this *Client) GetCode(exchange protocol.Exchange, start uint16) (*protocol
// GetCodeAll 通过多次请求的方式获取全部证券代码
func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp, error) {
resp := &protocol.CodeResp{}
//通达信没有北交所代码列表,通过爬虫的方式从北交所官网获取,放在这里是为了方便业务逻辑
//不放在extend包时防止循环引用
//todo 这是临时方案,等通达信有北交所代码列表时再改
if exchange == protocol.ExchangeBJ {
codes, err := GetBjCodes()
if err != nil {
return nil, err
}
resp.Count = uint16(len(codes))
for _, v := range codes {
resp.List = append(resp.List, &protocol.Code{
Code: v.Code,
Name: v.Name,
LastPrice: v.Last,
})
}
return resp, nil
}
size := uint16(1000)
for start := uint16(0); ; start += size {
r, err := this.GetCode(exchange, start)
@@ -233,6 +260,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, protocol.ExchangeBJ} {
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 {
@@ -316,23 +377,34 @@ func (this *Client) GetHistoryMinute(date, code string) (*protocol.MinuteResp, e
return result.(*protocol.MinuteResp), nil
}
func (this *Client) GetTrade(code string, start, count uint16) (*protocol.TradeResp, error) {
return this.GetMinuteTrade(code, start, count)
}
// GetMinuteTrade 获取分时交易详情,服务器最多返回1800条,count-start<=1800
func (this *Client) GetMinuteTrade(code string, start, count uint16) (*protocol.MinuteTradeResp, error) {
func (this *Client) GetMinuteTrade(code string, start, count uint16) (*protocol.TradeResp, error) {
code = protocol.AddPrefix(code)
f, err := protocol.MMinuteTrade.Frame(code, start, count)
f, err := protocol.MTrade.Frame(code, start, count)
if err != nil {
return nil, err
}
result, err := this.SendFrame(f, code)
result, err := this.SendFrame(f, protocol.TradeCache{
Date: time.Now().Format("20060102"),
Code: code,
})
if err != nil {
return nil, err
}
return result.(*protocol.MinuteTradeResp), nil
return result.(*protocol.TradeResp), nil
}
func (this *Client) GetTradeAll(code string) (*protocol.TradeResp, error) {
return this.GetMinuteTradeAll(code)
}
// GetMinuteTradeAll 获取分时全部交易详情,todo 只做参考 因为交易实时在进行,然后又是分页读取的,所以会出现读取间隔内产生的交易会丢失
func (this *Client) GetMinuteTradeAll(code string) (*protocol.MinuteTradeResp, error) {
resp := &protocol.MinuteTradeResp{}
func (this *Client) GetMinuteTradeAll(code string) (*protocol.TradeResp, error) {
resp := &protocol.TradeResp{}
size := uint16(1800)
for start := uint16(0); ; start += size {
r, err := this.GetMinuteTrade(code, start, size)
@@ -349,26 +421,71 @@ func (this *Client) GetMinuteTradeAll(code string) (*protocol.MinuteTradeResp, e
return resp, nil
}
// GetHistoryMinuteTrade 获取历史分时交易
// 只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16) (*protocol.HistoryMinuteTradeResp, error) {
code = protocol.AddPrefix(code)
f, err := protocol.MHistoryMinuteTrade.Frame(date, code, start, count)
if err != nil {
return nil, err
}
result, err := this.SendFrame(f, code)
if err != nil {
return nil, err
}
return result.(*protocol.HistoryMinuteTradeResp), nil
func (this *Client) GetHistoryTrade(date, code string, start, count uint16) (*protocol.TradeResp, error) {
return this.GetHistoryMinuteTrade(date, code, start, count)
}
// GetHistoryMinuteTradeAll 获取历史分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.HistoryMinuteTradeResp, error) {
resp := &protocol.HistoryMinuteTradeResp{}
// GetHistoryMinuteTrade 获取历史分时交易
// 只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0
// 历史数据只能查到20000609
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 {
return nil, err
}
result, err := this.SendFrame(f, protocol.TradeCache{
Date: date,
Code: code,
})
if err != nil {
return nil, err
}
return result.(*protocol.TradeResp), nil
}
// GetHistoryTradeFull 获取上市至今的分时成交
func (this *Client) GetHistoryTradeFull(code string, w *Workday) (protocol.Trades, error) {
return this.GetHistoryTradeBefore(code, w, time.Now())
}
// GetHistoryTradeBefore 获取上市至今的分时成交
func (this *Client) GetHistoryTradeBefore(code string, w *Workday, before time.Time) (protocol.Trades, error) {
ls := protocol.Trades(nil)
resp, err := this.GetKlineMonthAll(code)
if err != nil {
return nil, err
}
if len(resp.List) == 0 {
return nil, nil
}
start := time.Date(resp.List[0].Time.Year(), resp.List[0].Time.Month(), 1, 0, 0, 0, 0, resp.List[0].Time.Location())
var res *protocol.TradeResp
w.Range(start, before, func(t time.Time) bool {
for i := 0; i < 3; i++ {
res, err = this.GetHistoryTradeDay(t.Format("20060102"), code)
if err == nil {
break
}
}
if err != nil {
return false
}
ls = append(ls, res.List...)
return true
})
return ls, err
}
// GetHistoryTradeDay 获取历史某天分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
func (this *Client) GetHistoryTradeDay(date, code string) (*protocol.TradeResp, error) {
return this.GetHistoryMinuteTradeDay(date, code)
}
// GetHistoryMinuteTradeDay 获取历史某天分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
// 历史数据只能查到20000609
func (this *Client) GetHistoryMinuteTradeDay(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)
@@ -581,18 +698,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线数据

102
client_bj_code.go Normal file
View File

@@ -0,0 +1,102 @@
package tdx
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/injoyai/conv"
"io"
"net/http"
"strings"
"time"
)
const (
// UrlBjCodes 最后跟的是时间戳(ms),但是随便什么时间戳都能请求成功
UrlBjCodes = "https://www.bse.cn/nqhqController/nqhq_en.do?callback=jQuery3710848510589806625_%d"
)
func GetBjCodes() ([]*BjCode, error) {
list := []*BjCode(nil)
//这个200预防下bug,除非北京上市公司有4000个
for page := 0; page < 200; page++ {
ls, done, err := getBjCodes(page)
if err != nil {
return nil, err
}
list = append(list, ls...)
if done {
break
}
<-time.After(time.Millisecond * 100)
}
return list, nil
}
func getBjCodes(page int) (_ []*BjCode, last bool, err error) {
url := fmt.Sprintf(UrlBjCodes, time.Now().UnixMilli())
bodyStr := "page=" + conv.String(page) + "&type_en=%5B%22B%22%5D&sortfield=hqcjsl&sorttype=desc&xxfcbj_en=%5B2%5D&zqdm="
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(bodyStr))
if err != nil {
return nil, false, err
}
req.Header.Set("X-Requested-With", "XMLHttpRequest")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.39 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, false, err
}
defer resp.Body.Close()
bs, err := io.ReadAll(resp.Body)
if err != nil {
return nil, false, err
}
//处理数据
i := bytes.IndexByte(bs, '(')
if len(bs) < 1 || len(bs) <= i {
return nil, false, errors.New("未知错误: " + string(bs))
}
bs = bs[i+1 : len(bs)-1]
ls := []*BjCodes(nil)
err = json.Unmarshal(bs, &ls)
if err != nil {
return nil, false, err
}
if len(ls) == 0 {
return nil, false, errors.New("未知错误: " + string(bs))
}
return ls[0].Data, ls[0].LastPage, nil
}
type BjCodes struct {
Data []*BjCode `json:"content"`
TotalNumber int `json:"totalElements"`
TotalPage int `json:"totalPages"`
LastPage bool `json:"lastPage"`
}
type BjCode struct {
Date string `json:"hqjsrq"` //日期
Code string `json:"hqzqdm"` //代码
Name string `json:"hqzqjc"` //名称
LastClose float64 `json:"hqzrsp"` //前一天收盘价
Open float64 `json:"hqjrkp"` //开盘价
High float64 `json:"hqzgcj"` //最高价
Low float64 `json:"hqzdcj"` //最低价
Last float64 `json:"hqzjcj"` //最新价/收盘价
Volume int `json:"hqcjsl"` //成交量,股
Amount float64 `json:"hqcjje"` //成交额,元
}

131
codes.go
View File

@@ -23,10 +23,22 @@ func DialCodes(filename string, op ...client.Option) (*Codes, error) {
if err != nil {
return nil, err
}
return NewCodes(c, filename)
return NewCodesSqlite(c, filename)
}
func NewCodes(c *Client, filenames ...string) (*Codes, error) {
func NewCodesMysql(c *Client, dsn string) (*Codes, error) {
//连接数据库
db, err := xorm.NewEngine("mysql", dsn)
if err != nil {
return nil, err
}
db.SetMapper(core.SameMapper{})
return NewCodes(c, db)
}
func NewCodesSqlite(c *Client, filenames ...string) (*Codes, error) {
//如果没有指定文件名,则使用默认
defaultFilename := filepath.Join(DefaultDatabaseDir, "codes.db")
@@ -44,6 +56,12 @@ func NewCodes(c *Client, filenames ...string) (*Codes, error) {
}
db.SetMapper(core.SameMapper{})
db.DB().SetMaxOpenConns(1)
return NewCodes(c, db)
}
func NewCodes(c *Client, db *xorm.Engine) (*Codes, error) {
if err := db.Sync2(new(CodeModel)); err != nil {
return nil, err
}
@@ -53,10 +71,11 @@ func NewCodes(c *Client, filenames ...string) (*Codes, error) {
update := new(UpdateModel)
{ //查询或者插入一条数据
has, err := db.Get(update)
has, err := db.Where("`Key`=?", "codes").Get(update)
if err != nil {
return nil, err
} else if !has {
update.Key = "codes"
if _, err := db.Insert(update); err != nil {
return nil, err
}
@@ -70,9 +89,10 @@ 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 {
err := cc.Update()
if err == nil {
return
}
logs.Err(err)
@@ -155,52 +175,8 @@ func (this *Codes) Get(code string) *CodeModel {
return this.Map[code]
}
// 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
return protocol.AddPrefix(code)
}
// Update 更新数据,从服务器或者数据库
@@ -219,7 +195,7 @@ func (this *Codes) Update(byDB ...bool) error {
this.list = codes
this.exchanges = exchanges
//更新时间
_, err = this.db.Update(&UpdateModel{Time: time.Now().Unix()})
_, err = this.db.Where("`Key`=?", "codes").Update(&UpdateModel{Time: time.Now().Unix()})
return err
}
@@ -249,7 +225,7 @@ func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
//3. 从服务器获取所有股票代码
insert := []*CodeModel(nil)
update := []*CodeModel(nil)
for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ, protocol.ExchangeBJ} {
resp, err := this.Client.GetCodeAll(exchange)
if err != nil {
return nil, err
@@ -282,29 +258,52 @@ func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
}
}
//4. 插入或者更新数据库
err := NewSessionFunc(this.db, func(session *xorm.Session) error {
for _, v := range insert {
if _, err := session.Insert(v); err != nil {
return err
switch this.db.Dialect().URI().DBType {
case "mysql":
// 1⃣ 清空
if _, err := this.db.Exec("TRUNCATE TABLE codes"); err != nil {
return nil, err
}
data := append(insert, update...)
// 2⃣ 直接批量插入
batchSize := 3000 // 8000(2m16s) 5000(43s) 3000(11s) 1000(59s)
for i := 0; i < len(data); i += batchSize {
end := i + batchSize
if end > len(data) {
end = len(data)
}
slice := conv.Array(data[i:end])
if _, err := this.db.Insert(slice); err != nil {
return nil, err
}
}
for _, v := range update {
if _, err := session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Cols("Name,LastPrice").Update(v); err != nil {
return err
case "sqlite3":
//4. 插入或者更新数据库
err := NewSessionFunc(this.db, func(session *xorm.Session) error {
for _, v := range insert {
if _, err := session.Insert(v); err != nil {
return err
}
}
for _, v := range update {
if _, err := session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Cols("Name,LastPrice").Update(v); err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
}
return nil
})
if err != nil {
return nil, err
}
return list, nil
}
type UpdateModel struct {
Key string
Time int64 //更新时间
}
@@ -334,7 +333,7 @@ func (this *CodeModel) FullCode() string {
func (this *CodeModel) Price(p protocol.Price) protocol.Price {
return protocol.Price(float64(p) * math.Pow10(int(2-this.Decimal)))
return p * protocol.Price(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 {

20
example/CodesHTTP/main.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx/extend"
"time"
)
func main() {
go extend.ListenCodesHTTP(10033)
<-time.After(time.Second * 3)
c := extend.DialCodesHTTP("http://localhost:10033")
stocks, err := c.GetStocks()
logs.PanicErr(err)
for _, v := range stocks {
println(v)
}
}

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

View File

@@ -0,0 +1,18 @@
package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx/extend"
)
func main() {
ls, err := extend.GetBjCodes()
if err != nil {
logs.Err(err)
return
}
for _, v := range ls {
logs.Debug(v)
}
logs.Debug("总数量:", len(ls))
}

View File

@@ -8,7 +8,7 @@ import (
func main() {
common.Test(func(c *tdx.Client) {
resp, err := c.GetHistoryMinuteTrade("20241025", "sz000001", 0, 20)
resp, err := c.GetHistoryMinuteTrade("20250929", "bj838971", 0, 20)
logs.PanicErr(err)
for _, v := range resp.List {

View File

@@ -8,7 +8,7 @@ import (
func main() {
common.Test(func(c *tdx.Client) {
resp, err := c.GetHistoryMinuteTradeAll("20241025", "sz000001")
resp, err := c.GetHistoryMinuteTradeDay("20251010", "sh000001")
logs.PanicErr(err)
for _, v := range resp.List {

View File

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

View File

@@ -0,0 +1,32 @@
package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/extend"
"time"
)
func main() {
c, err := tdx.DialDefault()
logs.PanicErr(err)
ks, fs, err := extend.GetTHSDayKlineFactorFull("000001", c)
logs.PanicErr(err)
m := map[int64]*extend.THSFactor{}
for _, v := range fs {
m[v.Date] = v
}
for _, v := range ks[0] {
logs.Debugf("%s 不复权:%.2f 前复权:%.2f 后复权:%.2f \n",
time.Unix(v.Date, 0).Format(time.DateOnly),
v.Close.Float64(),
v.Close.Float64()*m[v.Date].QFactor,
v.Close.Float64()*m[v.Date].HFactor,
)
}
}

View File

@@ -10,7 +10,7 @@ func main() {
c, err := tdx.Dial("124.71.187.122:7709", tdx.WithDebug())
logs.PanicErr(err)
tdx.DefaultCodes, err = tdx.NewCodes(c, "./codes.db")
tdx.DefaultCodes, err = tdx.NewCodesSqlite(c, "./codes.db")
logs.PanicErr(err)
_ = c

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

@@ -0,0 +1,17 @@
package main
import (
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
)
func main() {
_, err := tdx.NewManageMysql(&tdx.ManageConfig{
Number: 2,
CodesFilename: "root:root@tcp(192.168.1.105:3306)/stock?charset=utf8mb4&parseTime=True&loc=Local",
WorkdayFileName: "root:root@tcp(192.168.1.105:3306)/stock?charset=utf8mb4&parseTime=True&loc=Local",
Dial: nil,
})
logs.PanicErr(err)
logs.Debug("done")
}

20
example/PullTrade/main.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import (
"context"
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/extend"
)
func main() {
pt := extend.NewPullTrade("./data/trade")
m, err := tdx.NewManage(nil)
logs.PanicErr(err)
err = pt.PullYear(context.Background(), m, 2025, "sz000001")
logs.Err(err)
}

View File

@@ -0,0 +1,22 @@
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.GetHistoryTradeDay("20251010", "sz000001")
logs.PanicErr(err)
ks := resp.List.Klines()
for _, v := range ks {
logs.Debug(v)
}
})
}

View File

@@ -9,11 +9,12 @@ import (
func main() {
common.Test(func(c *tdx.Client) {
_, err := tdx.NewWorkday(c, "./workday.db")
_, err := tdx.NewWorkdaySqlite(c) //"./workday.db"
logs.PanicErr(err)
_, err = tdx.NewCodes(c, "./codes.db")
_, err = tdx.NewCodesSqlite(c) //"./codes.db"
logs.PanicErr(err)
c.Close()
})
}

17
extend/codes-bj.go Normal file
View File

@@ -0,0 +1,17 @@
package extend
import (
"github.com/injoyai/tdx"
)
func GetBjCodes() ([]string, error) {
cs, err := tdx.GetBjCodes()
if err != nil {
return nil, err
}
ls := []string(nil)
for _, v := range cs {
ls = append(ls, "bj"+v.Code)
}
return ls, nil
}

66
extend/codes-server.go Normal file
View File

@@ -0,0 +1,66 @@
package extend
import (
"encoding/json"
"fmt"
"github.com/injoyai/conv"
"github.com/injoyai/tdx"
"io"
"net/http"
"path/filepath"
)
func ListenCodesHTTP(port int, filename ...string) error {
code, err := tdx.DialCodes(conv.Default(filepath.Join(tdx.DefaultDatabaseDir, "codes.db"), filename...))
if err != nil {
return nil
}
return http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/stocks":
ls := code.GetStocks()
w.WriteHeader(http.StatusOK)
w.Write(conv.Bytes(ls))
case "/etfs":
ls := code.GetETFs()
w.WriteHeader(http.StatusOK)
w.Write(conv.Bytes(ls))
default:
http.NotFound(w, r)
}
}))
}
func DialCodesHTTP(address string) *CodesHTTP {
return &CodesHTTP{address: address}
}
type CodesHTTP struct {
address string
}
func (this *CodesHTTP) getList(path string) ([]string, error) {
resp, err := http.DefaultClient.Get(this.address + path)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http code:%d", resp.StatusCode)
}
bs, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
ls := []string(nil)
err = json.Unmarshal(bs, &ls)
return ls, err
}
func (this *CodesHTTP) GetStocks() ([]string, error) {
return this.getList("/stocks")
}
func (this *CodesHTTP) GetETFs() ([]string, error) {
return this.getList("/etfs")
}

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

168
extend/pull-trade.go Normal file
View File

@@ -0,0 +1,168 @@
package extend
import (
"context"
"github.com/injoyai/conv"
"github.com/injoyai/logs"
"github.com/injoyai/tdx"
"github.com/injoyai/tdx/protocol"
"path/filepath"
"time"
)
func NewPullTrade(dir string) *PullTrade {
return &PullTrade{
Dir: dir,
}
}
type PullTrade struct {
Dir string
}
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)
kss5 := protocol.Klines(nil)
kss15 := protocol.Klines(nil)
kss30 := protocol.Klines(nil)
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.TradeResp
err = m.Do(func(c *tdx.Client) error {
resp, err = c.GetHistoryTradeDay(date, code)
return err
})
if err != nil {
logs.Err(err)
return false
}
tss = append(tss, resp.List...)
//转成分时K线
ks := resp.List.Klines()
kss1 = append(kss1, ks...)
kss5 = append(kss5, ks.Merge(5)...)
kss15 = append(kss5, ks.Merge(15)...)
kss30 = append(kss5, ks.Merge(30)...)
kss60 = append(kss5, ks.Merge(60)...)
return true
})
if err != nil {
return
}
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)
if err != nil {
return err
}
err = KlinesToCsv(filename1, code, name, kss1)
if err != nil {
return err
}
err = KlinesToCsv(filename5, code, name, kss5)
if err != nil {
return err
}
err = KlinesToCsv(filename15, code, name, kss15)
if err != nil {
return err
}
err = KlinesToCsv(filename30, code, name, kss30)
if err != nil {
return err
}
err = KlinesToCsv(filename60, code, name, kss60)
if err != nil {
return err
}
return nil
}
func KlinesToCsv(filename string, code, name string, ks protocol.Klines) error {
data := [][]any{{"日期", "时间", "代码", "名称", "开盘", "最高", "最低", "收盘", "总手", "金额"}}
for _, v := range ks {
data = append(data, []any{
v.Time.Format("20060102"),
v.Time.Format("15:04"),
code,
name,
v.Open.Float64(),
v.High.Float64(),
v.Low.Float64(),
v.Close.Float64(),
v.Volume,
v.Amount.Float64(),
})
}
buf, err := toCsv(data)
if err != nil {
return err
}
return newFile(filename, buf)
}
func TradeToCsv(filename string, ts protocol.Trades) error {
data := [][]any{{"日期", "时间", "价格", "成交量(手)", "成交额", "方向(0买,1卖)"}}
for _, v := range ts {
data = append(data, []any{
v.Time.Format(time.DateOnly),
v.Time.Format("15:04"),
v.Price.Float64(),
v.Volume,
v.Amount().Float64(),
v.Status,
})
}
buf, err := toCsv(data)
if err != nil {
return err
}
return newFile(filename, buf)
}

198
extend/spider-ths.go Normal file
View File

@@ -0,0 +1,198 @@
package extend
import (
"bytes"
"encoding/json"
"fmt"
"github.com/injoyai/conv"
"github.com/injoyai/tdx"
"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_BFQ uint8 = 0 //不复权
THS_QFQ uint8 = 1 //前复权
THS_HFQ uint8 = 2 //后复权
)
// GetTHSDayKlineFactorFull 增加计算复权因子
func GetTHSDayKlineFactorFull(code string, c *tdx.Client) ([3][]*Kline, []*THSFactor, error) {
ks, err := GetTHSDayKlineFull(code, c)
if err != nil {
return [3][]*Kline{}, nil, err
}
mQPrice := make(map[int64]float64)
for _, v := range ks[1] {
mQPrice[v.Date] = v.Close.Float64()
}
mHPrice := make(map[int64]float64)
for _, v := range ks[2] {
mHPrice[v.Date] = v.Close.Float64()
}
fs := make([]*THSFactor, 0, len(ks[0]))
for _, v := range ks[0] {
fs = append(fs, &THSFactor{
Date: v.Date,
QFactor: mQPrice[v.Date] / v.Close.Float64(),
HFactor: mHPrice[v.Date] / v.Close.Float64(),
})
}
return ks, fs, nil
}
/*
GetTHSDayKlineFull
获取[不复权,前复权,后复权]数据,并补充成交金额数据
前复权,和通达信对的上,和东方财富对不上
后复权,和通达信,东方财富都对不上
*/
func GetTHSDayKlineFull(code string, c *tdx.Client) ([3][]*Kline, error) {
resp, err := c.GetKlineDayAll(code)
if err != nil {
return [3][]*Kline{}, err
}
mAmount := make(map[int64]protocol.Price)
bfq := []*Kline(nil)
for _, v := range resp.List {
mAmount[v.Time.Unix()] = v.Amount
bfq = append(bfq, &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,
})
}
//前复权
qfq, err := GetTHSDayKline(code, THS_QFQ)
if err != nil {
return [3][]*Kline{}, err
}
for i := range qfq {
qfq[i].Amount = mAmount[qfq[i].Date]
}
//后复权
hfq, err := GetTHSDayKline(code, THS_HFQ)
if err != nil {
return [3][]*Kline{}, err
}
for i := range hfq {
hfq[i].Amount = mAmount[hfq[i].Date]
}
return [3][]*Kline{bfq, qfq, hfq}, nil
}
/*
GetTHSDayKline
前复权,和通达信对的上,和东方财富对不上
后复权,和通达信,东方财富都对不上
*/
func GetTHSDayKline(code string, _type uint8) ([]*Kline, error) {
if _type != THS_BFQ && _type != THS_QFQ && _type != THS_HFQ {
return nil, fmt.Errorf("数据类型错误,例如:不复权0或前复权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"])
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"]), ",")
//好像到了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))
}
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
}
}
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++
}
}
return ls, nil
}

View File

@@ -1,118 +0,0 @@
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
}

12
extend/ths-factor.go Normal file
View File

@@ -0,0 +1,12 @@
package extend
//const (
// // UrlTHSFactor https://d.10jqka.com.cn/v6/line/hs_000001/01/2016.js
// UrlTHSFactor = "https://d.10jqka.com.cn/v6/line/hs_%s/0%d/%d.js"
//)
type THSFactor struct {
Date int64 `json:"date"` //时间
QFactor float64 `json:"q_factor"` //前复权因子
HFactor float64 `json:"h_factor"` //后复权因子
}

58
extend/util.go Normal file
View File

@@ -0,0 +1,58 @@
package extend
import (
"bytes"
"encoding/csv"
"github.com/injoyai/conv"
"io"
"os"
"path/filepath"
)
func toCsv(data [][]interface{}) (*bytes.Buffer, error) {
buf := bytes.NewBuffer(nil)
buf.WriteString("\xEF\xBB\xBF")
w := csv.NewWriter(buf)
for _, rows := range data {
if err := w.Write(conv.Strings(rows)); err != nil {
return nil, err
}
}
w.Flush()
return buf, nil
}
// newFile 新建文件,会覆盖
func newFile(filename string, v ...interface{}) error {
if len(v) == 0 {
return os.MkdirAll(filename, 0777)
}
dir, name := filepath.Split(filename)
if len(dir) > 0 {
if err := os.MkdirAll(dir, 0777); err != nil {
return err
}
}
if len(name) == 0 {
return nil
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
for _, k := range v {
switch r := k.(type) {
case nil:
case io.Reader:
if _, err = io.Copy(f, r); err != nil {
return err
}
default:
if _, err = f.Write(conv.Bytes(r)); err != nil {
return err
}
}
}
return 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.17
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

35
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,16 @@ 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/base v1.2.17 h1:+qYeCSeEMWgmTla+LBC0Ozan9ysS4mV0ne5nfMt9opU=
github.com/injoyai/base v1.2.17/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 +43,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 +90,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 +108,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

@@ -1,6 +1,7 @@
package tdx
import (
"errors"
"github.com/injoyai/ios/client"
"github.com/robfig/cron/v3"
"time"
@@ -10,6 +11,57 @@ const (
DefaultDatabaseDir = "./data/database"
)
func NewManageMysql(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
//初始化配置
if cfg == nil {
cfg = &ManageConfig{}
}
if cfg.CodesFilename == "" {
return nil, errors.New("未配置Codes的数据库")
}
if cfg.WorkdayFileName == "" {
return nil, errors.New("未配置Workday的数据库")
}
if cfg.Dial == nil {
cfg.Dial = DialDefault
}
//通用客户端
commonClient, err := cfg.Dial(op...)
if err != nil {
return nil, err
}
commonClient.Wait.SetTimeout(time.Second * 5)
//代码管理
codes, err := NewCodesMysql(commonClient, cfg.CodesFilename)
if err != nil {
return nil, err
}
//工作日管理
workday, err := NewWorkdayMysql(commonClient, cfg.WorkdayFileName)
if err != nil {
return nil, err
}
//连接池
p, err := NewPool(func() (*Client, error) {
return cfg.Dial(op...)
}, cfg.Number)
if err != nil {
return nil, err
}
return &Manage{
Pool: p,
Config: cfg,
Codes: codes,
Workday: workday,
Cron: cron.New(cron.WithSeconds()),
}, nil
}
func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
//初始化配置
if cfg == nil {
@@ -25,13 +77,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 := NewCodesSqlite(commonClient, cfg.CodesFilename)
if err != nil {
return nil, err
}
//工作日管理
workday, err := NewWorkdaySqlite(commonClient, cfg.WorkdayFileName)
if err != nil {
return nil, err
}
@@ -44,17 +104,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 +121,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

@@ -5,16 +5,16 @@ import (
)
var (
MConnect = connect{}
MHeart = heart{}
MCount = count{}
MQuote = quote{}
MCode = code{}
MMinute = minute{}
MHistoryMinute = historyMinute{}
MMinuteTrade = minuteTrade{}
MHistoryMinuteTrade = historyMinuteTrade{}
MKline = kline{}
MConnect = connect{}
MHeart = heart{}
MCount = count{}
MQuote = quote{}
MCode = code{}
MMinute = minute{}
MHistoryMinute = historyMinute{}
MTrade = trade{}
MHistoryTrade = historyTrade{}
MKline = kline{}
)
type ConnectResp struct {

View File

@@ -1,94 +0,0 @@
package protocol
import (
"errors"
"fmt"
"github.com/injoyai/conv"
)
// HistoryMinuteTradeResp 历史分时交易比实时少了单量
type HistoryMinuteTradeResp struct {
Count uint16
List []*HistoryMinuteTrade
}
type HistoryMinuteTrade struct {
Time string //时间
Price Price //价格
Volume int //成交量
Status int //0是买1是卖2无效汇总出现中途也可能出现2,例20241115(sz000001)的14:56
}
func (this *HistoryMinuteTrade) String() string {
return fmt.Sprintf("%s \t%s \t%-6s \t%-6d(手) \t%-4s", this.Time, this.Price, this.Amount(), this.Volume, this.StatusString())
}
// Amount 成交额
func (this *HistoryMinuteTrade) Amount() Price {
return this.Price * Price(this.Volume*100)
}
func (this *HistoryMinuteTrade) StatusString() string {
switch this.Status {
case 0:
return "买入"
case 1:
return "卖出"
default:
return ""
}
}
type historyMinuteTrade struct{}
func (historyMinuteTrade) Frame(date, code string, start, count uint16) (*Frame, error) {
exchange, number, err := DecodeCode(code)
if err != nil {
return nil, err
}
dataBs := Bytes(conv.Uint32(date)) //req.Time.Format("20060102"))
dataBs = append(dataBs, exchange.Uint8(), 0x0)
dataBs = append(dataBs, []byte(number)...)
dataBs = append(dataBs, Bytes(start)...)
dataBs = append(dataBs, Bytes(count)...)
return &Frame{
Control: Control01,
Type: TypeHistoryMinuteTrade,
Data: dataBs,
}, nil
}
func (historyMinuteTrade) Decode(bs []byte, code string) (*HistoryMinuteTradeResp, error) {
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
_, number, err := DecodeCode(code)
if err != nil {
return nil, err
}
resp := &HistoryMinuteTradeResp{
Count: Uint16(bs[:2]),
}
//第2-6字节不知道是啥
bs = bs[2+4:]
lastPrice := Price(0)
for i := uint16(0); i < resp.Count; i++ {
mt := &HistoryMinuteTrade{
Time: GetHourMinute([2]byte(bs[:2])),
}
var sub Price
bs, sub = GetPrice(bs[2:])
lastPrice += sub * 10 //把分转成厘
mt.Price = lastPrice / basePrice(number)
bs, mt.Volume = CutInt(bs)
bs, mt.Status = CutInt(bs)
bs, _ = CutInt(bs) //这个得到的是0不知道是啥
resp.List = append(resp.List, mt)
}
return resp, nil
}

View File

@@ -0,0 +1,67 @@
package protocol
import (
"errors"
"github.com/injoyai/conv"
"time"
)
// HistoryTradeResp 兼容之前的版本
type HistoryTradeResp = TradeResp
type historyTrade struct{}
func (historyTrade) Frame(date, code string, start, count uint16) (*Frame, error) {
exchange, number, err := DecodeCode(code)
if err != nil {
return nil, err
}
dataBs := Bytes(conv.Uint32(date)) //req.Time.Format("20060102"))
dataBs = append(dataBs, exchange.Uint8(), 0x0)
dataBs = append(dataBs, []byte(number)...)
dataBs = append(dataBs, Bytes(start)...)
dataBs = append(dataBs, Bytes(count)...)
return &Frame{
Control: Control01,
Type: TypeHistoryMinuteTrade,
Data: dataBs,
}, nil
}
func (historyTrade) Decode(bs []byte, c TradeCache) (*TradeResp, error) {
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
_, number, err := DecodeCode(c.Code)
if err != nil {
return nil, err
}
resp := &TradeResp{
Count: Uint16(bs[:2]),
}
//第2-6字节不知道是啥
bs = bs[2+4:]
lastPrice := Price(0)
for i := uint16(0); i < resp.Count; i++ {
timeStr := GetHourMinute([2]byte(bs[:2]))
t, err := time.Parse("2006010215:04", c.Date+timeStr)
if err != nil {
return nil, err
}
mt := &Trade{Time: t}
var sub Price
bs, sub = GetPrice(bs[2:])
lastPrice += sub * 10 //把分转成厘
mt.Price = lastPrice / basePrice(number)
bs, mt.Volume = CutInt(bs)
bs, mt.Status = CutInt(bs)
bs, _ = CutInt(bs) //这个得到的是0不知道是啥
resp.List = append(resp.List, mt)
}
return resp, nil
}

View File

@@ -7,7 +7,7 @@ import (
func Test_stockHistoryMinuteTrade_Frame(t *testing.T) {
// 预期 0c 02000000 00 1200 1200 b50f 84da3401 0000 30303030303100006400
// 0c000000000112001200b50f84da3401000030303030303100006400
f, err := MHistoryMinuteTrade.Frame("20241028", "sz000001", 0, 100)
f, err := MHistoryTrade.Frame("20241028", "sz000001", 0, 100)
if err != nil {
t.Error(err)
return

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/injoyai/base/types"
"github.com/injoyai/conv"
"sort"
"time"
)
@@ -85,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")
@@ -115,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]),
}
@@ -160,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) //从元转为厘,并去除多余的小数
@@ -209,3 +230,132 @@ func FixKlineTime(ks []*Kline) []*Kline {
}
return ks
}
type Klines []*Kline
// LastPrice 获取最后一个K线的收盘价
func (this Klines) LastPrice() Price {
if len(this) == 0 {
return 0
}
return this[len(this)-1].Close
}
func (this Klines) Len() int {
return len(this)
}
func (this Klines) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
func (this Klines) Less(i, j int) bool {
return this[i].Time.Before(this[j].Time)
}
func (this Klines) Sort() {
sort.Sort(this)
}
func (this Klines) Kline(t time.Time, last Price) *Kline {
k := &Kline{
Time: t,
Open: last,
High: last,
Low: last,
Close: last,
Volume: 0,
Amount: 0,
}
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
default:
if k.Open == 0 {
k.Open = v.Open
}
k.High = conv.Select(k.High < v.High, v.High, k.High)
k.Low = conv.Select(k.Low > v.Low, v.Low, k.Low)
}
k.Close = v.Close
k.Volume += v.Volume
k.Amount += v.Amount
}
return k
}
// Merge 合并成其他类型的K线
func (this Klines) Merge(n int) Klines {
if n <= 1 {
return this
}
ks := Klines(nil)
ls := Klines(nil)
for i := 0; ; i++ {
if len(this) <= i*n {
break
}
if len(this) < (i+1)*n {
ls = this[i*n:]
} else {
ls = this[i*n : (i+1)*n]
}
if len(ls) == 0 {
break
}
last := ls[len(ls)-1]
k := ls.Kline(last.Time, ls[0].Open)
ks = append(ks, k)
}
return ks
}
//// 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.Time = v.Time
// }
// 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线,1分钟转成5,15,30分钟等
//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
//}

View File

@@ -1,116 +0,0 @@
package protocol
import (
"errors"
"fmt"
)
type MinuteTradeResp struct {
Count uint16
List []*MinuteTrade
}
// MinuteTrade 分时成交todo 时间没有到秒,客户端上也没有,东方客户端能显示秒
type MinuteTrade struct {
Time string //时间
Price Price //价格
Volume int //成交量
Number int //单数,历史数据该字段无效
Status int //0是买1是卖2无效汇总出现
}
func (this *MinuteTrade) String() string {
return fmt.Sprintf("%s \t%-6s \t%-6s \t%-6d(手) \t%-4d(单) \t%-4s",
this.Time, this.Price, this.Amount(), this.Volume, this.Number, this.StatusString())
}
// Amount 成交额
func (this *MinuteTrade) Amount() Price {
return this.Price * Price(this.Volume) * 100
}
func (this *MinuteTrade) StatusString() string {
switch this.Status {
case 0:
return "买入"
case 1:
return "卖出"
default:
return ""
}
}
// AvgVolume 平均每单成交量
func (this *MinuteTrade) AvgVolume() float64 {
return float64(this.Volume) / float64(this.Number)
}
// AvgPrice 平均每单成交金额
func (this *MinuteTrade) AvgPrice() Price {
return Price(this.AvgVolume() * float64(this.Price) * 100)
}
// IsBuy 是否是买单
func (this *MinuteTrade) IsBuy() bool {
return this.Status == 0
}
// IsSell 是否是卖单
func (this *MinuteTrade) IsSell() bool {
return this.Status == 1
}
type minuteTrade struct{}
func (minuteTrade) Frame(code string, start, count uint16) (*Frame, error) {
exchange, number, err := DecodeCode(code)
if err != nil {
return nil, err
}
codeBs := []byte(number)
codeBs = append(codeBs, Bytes(start)...)
codeBs = append(codeBs, Bytes(count)...)
return &Frame{
Control: Control01,
Type: TypeMinuteTrade,
Data: append([]byte{exchange.Uint8(), 0x0}, codeBs...),
}, nil
}
func (minuteTrade) Decode(bs []byte, code string) (*MinuteTradeResp, error) {
var err error
_, code, err = DecodeCode(code)
if err != nil {
return nil, err
}
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
resp := &MinuteTradeResp{
Count: Uint16(bs[:2]),
}
bs = bs[2:]
lastPrice := Price(0)
for i := uint16(0); i < resp.Count; i++ {
mt := &MinuteTrade{
Time: GetHourMinute([2]byte(bs[:2])),
}
var sub Price
bs, sub = GetPrice(bs[2:])
lastPrice += sub * 10 //把分转换成厘
mt.Price = lastPrice / basePrice(code)
bs, mt.Volume = CutInt(bs)
bs, mt.Number = CutInt(bs)
bs, mt.Status = CutInt(bs)
bs, _ = CutInt(bs) //这个得到的是0不知道是啥
resp.List = append(resp.List, mt)
}
return resp, nil
}

232
protocol/model_trade.go Normal file
View File

@@ -0,0 +1,232 @@
package protocol
import (
"errors"
"fmt"
"github.com/injoyai/base/types"
"github.com/injoyai/conv"
"time"
)
type TradeResp struct {
Count uint16
List Trades
}
// Trade 分时成交todo 时间没有到秒,客户端上也没有,东方客户端能显示秒
type Trade struct {
Time time.Time //时间, 09:30
Price Price //价格
Volume int //成交量,手
Status int //0是买1是卖2中性/汇总 中途也可能出现2,例20241115(sz000001)的14:56
Number int //单数,历史数据该字段无效
}
func (this *Trade) String() string {
return fmt.Sprintf("%s \t%-6s \t%-6s \t%-6d(手) \t%-4d(单) \t%-4s",
this.Time, this.Price, this.Amount(), this.Volume, this.Number, this.StatusString())
}
// Amount 成交额
func (this *Trade) Amount() Price {
return this.Price * Price(this.Volume*100)
}
func (this *Trade) StatusString() string {
switch this.Status {
case 0:
return "买入"
case 1:
return "卖出"
default:
return ""
}
}
// AvgVolume 平均每单成交量
func (this *Trade) AvgVolume() float64 {
return float64(this.Volume) / float64(this.Number)
}
// AvgPrice 平均每单成交金额
func (this *Trade) AvgPrice() Price {
return Price(this.AvgVolume() * float64(this.Price) * 100)
}
// IsBuy 是否是买单
func (this *Trade) IsBuy() bool {
return this.Status == 0
}
// IsSell 是否是卖单
func (this *Trade) IsSell() bool {
return this.Status == 1
}
type trade struct{}
func (trade) Frame(code string, start, count uint16) (*Frame, error) {
exchange, number, err := DecodeCode(code)
if err != nil {
return nil, err
}
codeBs := []byte(number)
codeBs = append(codeBs, Bytes(start)...)
codeBs = append(codeBs, Bytes(count)...)
return &Frame{
Control: Control01,
Type: TypeMinuteTrade,
Data: append([]byte{exchange.Uint8(), 0x0}, codeBs...),
}, nil
}
func (trade) Decode(bs []byte, c TradeCache) (*TradeResp, error) {
_, code, err := DecodeCode(c.Code)
if err != nil {
return nil, err
}
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
resp := &TradeResp{
Count: Uint16(bs[:2]),
}
bs = bs[2:]
lastPrice := Price(0)
for i := uint16(0); i < resp.Count; i++ {
timeStr := GetHourMinute([2]byte(bs[:2]))
t, err := time.Parse("2006010215:04", c.Date+timeStr)
if err != nil {
return nil, err
}
mt := &Trade{Time: t}
var sub Price
bs, sub = GetPrice(bs[2:])
lastPrice += sub * 10 //把分转换成厘
mt.Price = lastPrice / basePrice(code)
bs, mt.Volume = CutInt(bs)
bs, mt.Number = CutInt(bs)
bs, mt.Status = CutInt(bs)
bs, _ = CutInt(bs) //这个得到的是0不知道是啥
resp.List = append(resp.List, mt)
}
return resp, nil
}
type Trades []*Trade
// Klines 合并分时成交成k线
func (this Trades) Klines() Klines {
//按天分割
m := make(types.SortMap[int64, Trades])
for _, v := range this {
//获取当天零点的时间戳
unix := time.Date(v.Time.Year(), v.Time.Month(), v.Time.Day(), 0, 0, 0, 0, v.Time.Location()).Unix()
m[unix] = append(m[unix], v)
}
//按天排序
mKline := types.SortMap[int64, Klines]{}
for date, v := range m {
//生成一分钟k线
t := time.Unix(date, 0)
mKline[date] = v.klinesForDay(t)
}
//按时间排序
lss := mKline.Sort()
ls := Klines{}
for _, v := range lss {
ls = append(ls, v...)
}
return ls
}
// Kline 合并分时成交成1个k线,注意分时成交时间保持一致
func (this Trades) Kline(t time.Time, last Price) *Kline {
k := &Kline{
Time: t,
Last: last,
Open: last,
High: last,
Low: last,
Close: last,
}
first := 0
for _, v := range this {
if v.Price <= 0 {
continue
}
switch first {
case 0:
k.Open = v.Price
k.High = v.Price
k.Low = v.Price
k.Close = v.Price
default:
k.High = conv.Select(k.High < v.Price, v.Price, k.High)
k.Low = conv.Select(k.Low > v.Price, v.Price, k.Low)
}
k.Close = v.Price
k.Volume += int64(v.Volume)
k.Amount += v.Price * Price(v.Volume) * 100
first++
}
return k
}
// kline1 生成一分钟k线,一天
func (this Trades) klinesForDay(date time.Time) Klines {
_930 := 570 //9:30 的分钟
_1130 := 690 //11:30 的分钟
_1300 := 780 //13:00 的分钟
_1500 := 900 //15:00 的分钟
keys := []int(nil)
//早上
m := map[int]Trades{}
for i := 1; i <= 120; i++ {
keys = append(keys, _930+i)
m[_930+i] = []*Trade{}
}
//下午
for i := 1; i <= 120; i++ {
keys = append(keys, _1300+i)
m[_1300+i] = []*Trade{}
}
//获取开盘价,有可能前几分钟没有数据,先遍历一遍
var open Price
for _, v := range this {
if v.Price > 0 {
open = v.Price
break
}
}
//分组,按
for _, v := range this {
ms := minutes(v.Time)
t := conv.Select(ms < _930, _930, ms)
t++
t = conv.Select(t > _1130 && t <= _1300, _1130, t)
t = conv.Select(t > _1500, _1500, t)
m[t] = append(m[t], v)
}
//合并
ls := []*Kline(nil)
for _, v := range keys {
k := m[v].Kline(time.Date(date.Year(), date.Month(), date.Day(), v/60, v%60, 0, 0, date.Location()), open)
open = k.Close
ls = append(ls, k)
}
return ls
}
type TradeCache struct {
Date string //日期
Code string //计算倍数
}

View File

@@ -20,8 +20,8 @@ func (this Exchange) String() string {
return "sz"
case ExchangeSH:
return "sh"
//case ExchangeBJ:
//return "bj"
case ExchangeBJ:
return "bj"
default:
return "unknown"
}
@@ -33,8 +33,8 @@ func (this Exchange) Name() string {
return "上海"
case ExchangeSZ:
return "深圳"
//case ExchangeBJ:
//return "北京"
case ExchangeBJ:
return "北京"
default:
return "未知"
}
@@ -43,13 +43,14 @@ func (this Exchange) Name() string {
const (
ExchangeSZ Exchange = iota //深圳交易所
ExchangeSH //上海交易所
//ExchangeBJ //北京交易所
ExchangeBJ //北京交易所
)
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

@@ -58,6 +58,8 @@ func DecodeCode(code string) (Exchange, string, error) {
return ExchangeSH, code[2:], nil
case ExchangeSZ.String():
return ExchangeSZ, code[2:], nil
case ExchangeBJ.String():
return ExchangeBJ, code[2:], nil
default:
return 0, "", fmt.Errorf("股票代码错误,例如:SZ000001")
}
@@ -102,7 +104,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])
@@ -125,14 +127,18 @@ func GetTime(bs [4]byte, Type uint8) time.Time {
}
func basePrice(code string) Price {
if len(code) == 0 {
if len(code) < 2 {
return 1
}
switch code[:1] {
case "8":
return 1
}
switch code[:2] {
case "60", "30", "68", "00":
case "60", "30", "68", "00", "92", "43", "39":
return 1
default:
return 10
return 1
}
}
@@ -237,20 +243,34 @@ func getVolume2(val uint32) float64 {
// IsStock 是否是股票,示例sz000001
func IsStock(code string) bool {
if len(code) != 8 {
return false
}
code = strings.ToLower(code)
switch {
case code[0:2] == ExchangeSH.String() &&
(code[2:3] == "6"):
return true
return IsSZStock(code) || IsSHStock(code) || IsBJStock(code)
case code[0:2] == ExchangeSZ.String() &&
(code[2:3] == "0" || code[2:4] == "30"):
return true
}
return false
//if len(code) != 8 {
// return false
//}
//code = strings.ToLower(code)
//switch {
//case code[0:2] == ExchangeSH.String() &&
// (code[2:3] == "6"):
// return true
//
//case code[0:2] == ExchangeSZ.String() &&
// (code[2:3] == "0" || code[2:4] == "30"):
// return true
//}
//return false
}
func IsSZStock(code string) bool {
return len(code) == 8 && strings.ToLower(code[0:2]) == ExchangeSZ.String() && (code[2:3] == "0" || code[2:4] == "30")
}
func IsSHStock(code string) bool {
return len(code) == 8 && strings.ToLower(code[0:2]) == ExchangeSH.String() && code[2:3] == "6"
}
func IsBJStock(code string) bool {
return len(code) == 8 && strings.ToLower(code[0:2]) == ExchangeBJ.String() && (code[2:4] == "92" || code[2:4] == "43" || code[2:3] == "8")
}
// IsETF 是否是基金,示例sz159558
@@ -271,17 +291,33 @@ func IsETF(code string) bool {
return false
}
// AddPrefix 添加股票代码前缀,针对股票生效,例如000001,会增加前缀sz000001(平安银行),而不是sh000001(上证指数)
// AddPrefix 添加股票/基金代码前缀,针对股票/基金生效,例如000001,会增加前缀sz000001(平安银行),而不是sh000001(上证指数)
func AddPrefix(code string) string {
if len(code) == 6 {
switch {
case code[:1] == "6":
//上海股票
code = ExchangeSH.String() + code
case code[:1] == "0":
//深圳股票
code = ExchangeSZ.String() + code
case code[:2] == "30":
//深圳股票
code = ExchangeSZ.String() + code
case code[:3] == "510" || code[:3] == "511" || code[:3] == "512" || code[:3] == "513" || code[:3] == "515":
//上海基金
code = ExchangeSH.String() + code
case code[:3] == "159":
//深圳基金
code = ExchangeSZ.String() + code
case code[:1] == "8" || code[:2] == "92" || code[:2] == "43":
//北京股票
code = ExchangeBJ.String() + code
}
}
return code
}
func minutes(t time.Time) int {
return t.Hour()*60 + t.Minute()
}

View File

@@ -3,8 +3,11 @@ package tdx
import (
"errors"
_ "github.com/glebarez/go-sqlite"
_ "github.com/go-sql-driver/mysql"
"github.com/injoyai/base/maps"
"github.com/injoyai/conv"
"github.com/injoyai/logs"
"github.com/injoyai/tdx/protocol"
"github.com/robfig/cron/v3"
"os"
"path/filepath"
@@ -13,7 +16,22 @@ import (
"xorm.io/xorm"
)
func NewWorkday(c *Client, filename string) (*Workday, error) {
func NewWorkdayMysql(c *Client, dsn string) (*Workday, error) {
//连接数据库
db, err := xorm.NewEngine("mysql", dsn)
if err != nil {
return nil, err
}
db.SetMapper(core.SameMapper{})
return NewWorkday(c, db)
}
func NewWorkdaySqlite(c *Client, filenames ...string) (*Workday, error) {
defaultFilename := filepath.Join(DefaultDatabaseDir, "workday.db")
filename := conv.Default(defaultFilename, filenames...)
//如果文件夹不存在就创建
dir, _ := filepath.Split(filename)
@@ -26,6 +44,11 @@ func NewWorkday(c *Client, filename string) (*Workday, error) {
}
db.SetMapper(core.SameMapper{})
db.DB().SetMaxOpenConns(1)
return NewWorkday(c, db)
}
func NewWorkday(c *Client, db *xorm.Engine) (*Workday, error) {
if err := db.Sync2(new(WorkdayModel)); err != nil {
return nil, err
}
@@ -35,12 +58,12 @@ func NewWorkday(c *Client, filename string) (*Workday, error) {
db: db,
cache: maps.NewBit(),
}
//设置定时器,每天早上9点更新数据,8点多获取不到今天的数据
task := cron.New(cron.WithSeconds())
task.AddFunc("0 0 9 * * *", func() {
for i := 0; i < 3; i++ {
if err := w.Update(); err == nil {
err := w.Update()
if err == nil {
return
}
logs.Err(err)
@@ -48,7 +71,6 @@ func NewWorkday(c *Client, filename string) (*Workday, error) {
}
})
task.Start()
return w, w.Update()
}
@@ -82,27 +104,30 @@ func (this *Workday) Update() error {
}
now := time.Now()
if lastWorkday == nil || lastWorkday.Unix < IntegerDay(now).Unix() {
if lastWorkday.Unix < IntegerDay(now).Unix() {
resp, err := this.Client.GetIndexDayAll("sh000001")
if err != nil {
logs.Err(err)
return err
}
return NewSessionFunc(this.db, func(session *xorm.Session) error {
for _, v := range resp.List {
if unix := v.Time.Unix(); unix > lastWorkday.Unix {
_, err = session.Insert(&WorkdayModel{Unix: unix, Date: v.Time.Format("20060102"), Is: true})
if err != nil {
return err
}
this.cache.Set(uint64(unix), true)
}
inserts := []any(nil)
for _, v := range resp.List {
if unix := v.Time.Unix(); unix > lastWorkday.Unix {
inserts = append(inserts, &WorkdayModel{Unix: unix, Date: v.Time.Format("20060102")})
this.cache.Set(uint64(unix), true)
}
}
if len(inserts) == 0 {
return nil
})
}
_, err = this.db.Insert(inserts)
return err
}
return nil
}
@@ -116,6 +141,29 @@ func (this *Workday) TodayIs() bool {
return this.Is(time.Now())
}
// RangeYear 遍历一年的所有工作日
func (this *Workday) RangeYear(year int, f func(t time.Time) bool) {
this.Range(
time.Date(year, 1, 1, 0, 0, 0, 0, time.Local),
time.Date(year, 12, 31, 0, 0, 0, 0, time.Local),
f,
)
}
// Range 遍历指定范围的工作日,推荐start带上时间15:00,这样当天小于15点不会触发
func (this *Workday) Range(start, end time.Time, f func(t time.Time) bool) {
start = conv.Select(start.Before(protocol.ExchangeEstablish), protocol.ExchangeEstablish, start)
//now := IntegerDay(time.Now())
//end = conv.Select(end.After(now), now, end).Add(1)
for ; start.Before(end); start = start.Add(time.Hour * 24) {
if this.Is(start) {
if !f(start) {
return
}
}
}
}
// RangeDesc 倒序遍历工作日,从今天-1990年12月19日(上海交易所成立时间)
func (this *Workday) RangeDesc(f func(t time.Time) bool) {
t := IntegerDay(time.Now())
@@ -133,7 +181,6 @@ type WorkdayModel struct {
ID int64 `json:"id"` //主键
Unix int64 `json:"unix"` //时间戳
Date string `json:"date"` //日期
Is bool `json:"is"` //是否是工作日
}
func (this *WorkdayModel) TableName() string {