mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
Compare commits
48 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1783643f47 | ||
|
|
b9f0951b15 | ||
|
|
2e4ecd034c | ||
|
|
9269bca388 | ||
|
|
fc1f25c6c9 | ||
|
|
705f6e4e3a | ||
|
|
12079f1ee2 | ||
|
|
34701c4197 | ||
|
|
1187e1dcdf | ||
|
|
2adcc9a322 | ||
|
|
b035cc7fbf | ||
|
|
e58130b3c3 | ||
|
|
d840e0d33d | ||
|
|
78843ed6b7 | ||
|
|
04378dadf0 | ||
|
|
e74aeba7b2 | ||
|
|
bea9762c50 | ||
|
|
6750edb3ec | ||
|
|
2efd7e089e | ||
|
|
6d849a8756 | ||
|
|
bc45a72d8d | ||
|
|
c5f62c15b3 | ||
|
|
ecaad0e85f | ||
|
|
9ddaafbf48 | ||
|
|
84b0ec6f6c | ||
|
|
73d068002c | ||
|
|
abe39fb2ae | ||
|
|
3ebd6e3fb6 | ||
|
|
1afa0c7c6d | ||
|
|
9bad0908b7 | ||
|
|
360d0e861b | ||
|
|
b86e7dcacf | ||
|
|
af505eb55e | ||
|
|
630cbb8939 | ||
|
|
0b89aadd7f | ||
|
|
7c8b4989f6 | ||
|
|
b00f1b65d5 | ||
|
|
78e2ead79c | ||
|
|
f69dd66ecb | ||
|
|
4ce4adbfea | ||
|
|
20de683bca | ||
|
|
49c6deb9c4 | ||
|
|
5c0bc2a772 | ||
|
|
0f75b402bc | ||
|
|
ce6718831c | ||
|
|
031d9f6509 | ||
|
|
5e4115d045 | ||
|
|
1cae60c65e |
68
README.md
68
README.md
@@ -60,43 +60,43 @@ func main() {
|
|||||||
|
|
||||||
| IP | 测试时间 | 所属地区 | 运营商 |
|
| IP | 测试时间 | 所属地区 | 运营商 |
|
||||||
|-----------------|------------|------|-----|
|
|-----------------|------------|------|-----|
|
||||||
| 124.71.187.122 | 2025-03-15 | 上海 | 华为 |
|
| 124.71.187.122 | 2025-05-21 | 上海 | 华为 |
|
||||||
| 122.51.120.217 | 2025-03-15 | 上海 | 腾讯 |
|
| 122.51.120.217 | 2025-05-21 | 上海 | 腾讯 |
|
||||||
| 111.229.247.189 | 2025-03-15 | 上海 | 腾讯 |
|
| 111.229.247.189 | 2025-05-21 | 上海 | 腾讯 |
|
||||||
| 124.70.176.52 | 2025-03-15 | 上海 | 华为 |
|
| 124.70.176.52 | 2025-05-21 | 上海 | 华为 |
|
||||||
| 123.60.186.45 | 2025-03-15 | 上海 | 华为 |
|
| 123.60.186.45 | 2025-05-21 | 上海 | 华为 |
|
||||||
| 122.51.232.182 | 2025-03-15 | 上海 | 腾讯 |
|
| 122.51.232.182 | 2025-05-21 | 上海 | 腾讯 |
|
||||||
| 118.25.98.114 | 2025-03-15 | 上海 | 腾讯 |
|
| 118.25.98.114 | 2025-05-21 | 上海 | 腾讯 |
|
||||||
| 124.70.199.56 | 2025-03-15 | 上海 | 华为 |
|
| 124.70.199.56 | 2025-05-21 | 上海 | 华为 |
|
||||||
| 121.36.225.169 | 2025-03-15 | 上海 | 华为 |
|
| 121.36.225.169 | 2025-05-21 | 上海 | 华为 |
|
||||||
| 123.60.70.228 | 2025-03-15 | 上海 | 华为 |
|
| 123.60.70.228 | 2025-05-21 | 上海 | 华为 |
|
||||||
| 123.60.73.44 | 2025-03-15 | 上海 | 华为 |
|
| 123.60.73.44 | 2025-05-21 | 上海 | 华为 |
|
||||||
| 124.70.133.119 | 2025-03-15 | 上海 | 华为 |
|
| 124.70.133.119 | 2025-05-21 | 上海 | 华为 |
|
||||||
| 124.71.187.72 | 2025-03-15 | 上海 | 华为 |
|
| 124.71.187.72 | 2025-05-21 | 上海 | 华为 |
|
||||||
| 123.60.84.66 | 2025-03-15 | 上海 | 华为 |
|
| 123.60.84.66 | 2025-05-21 | 上海 | 华为 |
|
||||||
| | | | |
|
| | | | |
|
||||||
| 121.36.54.217 | 2025-03-15 | 北京 | 华为 |
|
| 121.36.54.217 | 2025-05-21 | 北京 | 华为 |
|
||||||
| 121.36.81.195 | 2025-03-15 | 北京 | 华为 |
|
| 121.36.81.195 | 2025-05-21 | 北京 | 华为 |
|
||||||
| 123.249.15.60 | 2025-03-15 | 北京 | 华为 |
|
| 123.249.15.60 | 2025-05-21 | 北京 | 华为 |
|
||||||
| 124.70.75.113 | 2025-03-15 | 北京 | 华为 |
|
| 124.70.75.113 | 2025-05-21 | 北京 | 华为 |
|
||||||
| 120.46.186.223 | 2025-03-15 | 北京 | 华为 |
|
| 120.46.186.223 | 2025-05-21 | 北京 | 华为 |
|
||||||
| 124.70.22.210 | 2025-03-15 | 北京 | 华为 |
|
| 124.70.22.210 | 2025-05-21 | 北京 | 华为 |
|
||||||
| 139.9.133.247 | 2025-03-15 | 北京 | 华为 |
|
| 139.9.133.247 | 2025-05-21 | 北京 | 华为 |
|
||||||
| | | | |
|
| | | | |
|
||||||
| 124.71.85.110 | 2025-03-15 | 广州 | 华为 |
|
| 124.71.85.110 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 139.9.51.18 | 2025-03-15 | 广州 | 华为 |
|
| 139.9.51.18 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 139.159.239.163 | 2025-03-15 | 广州 | 华为 |
|
| 139.159.239.163 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 124.71.9.153 | 2025-03-15 | 广州 | 华为 |
|
| 124.71.9.153 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 116.205.163.254 | 2025-03-15 | 广州 | 华为 |
|
| 116.205.163.254 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 116.205.171.132 | 2025-03-15 | 广州 | 华为 |
|
| 116.205.171.132 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 116.205.183.150 | 2025-03-15 | 广州 | 华为 |
|
| 116.205.183.150 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 111.230.186.52 | 2025-03-15 | 广州 | 腾讯 |
|
| 111.230.186.52 | 2025-05-21 | 广州 | 腾讯 |
|
||||||
| 110.41.4.4 | 2025-03-15 | 广州 | 华为 |
|
| 110.41.4.4 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 110.41.2.72 | 2025-03-15 | 广州 | 华为 |
|
| 110.41.2.72 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 110.41.154.219 | 2025-03-15 | 广州 | 华为 |
|
| 110.41.154.219 | 2025-05-21 | 广州 | 华为 |
|
||||||
| 110.41.147.114 | 2025-03-15 | 广州 | 华为 |
|
| 110.41.147.114 | 2025-05-21 | 广州 | 华为 |
|
||||||
| | | | |
|
| | | | |
|
||||||
| 119.97.185.59 | 2025-03-15 | 武汉 | 电信 |
|
| 119.97.185.59 | 2025-05-21 | 武汉 | 电信 |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
113
client.go
113
client.go
@@ -4,7 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/injoyai/base/maps"
|
"github.com/injoyai/base/maps"
|
||||||
"github.com/injoyai/base/maps/wait/v2"
|
"github.com/injoyai/base/maps/wait"
|
||||||
"github.com/injoyai/conv"
|
"github.com/injoyai/conv"
|
||||||
"github.com/injoyai/ios"
|
"github.com/injoyai/ios"
|
||||||
"github.com/injoyai/ios/client"
|
"github.com/injoyai/ios/client"
|
||||||
@@ -105,16 +105,6 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
|
|||||||
|
|
||||||
go cli.Client.Run()
|
go cli.Client.Run()
|
||||||
|
|
||||||
/*
|
|
||||||
部分接口需要通过代码信息计算得出
|
|
||||||
*/
|
|
||||||
codesOnce.Do(func() {
|
|
||||||
//初始化代码管理
|
|
||||||
if DefaultCodes == nil {
|
|
||||||
DefaultCodes, err = NewCodes(cli, "./codes.db")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return cli, err
|
return cli, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,10 +157,10 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) {
|
|||||||
resp, err = protocol.MHistoryMinute.Decode(f.Data)
|
resp, err = protocol.MHistoryMinute.Decode(f.Data)
|
||||||
|
|
||||||
case protocol.TypeMinuteTrade:
|
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:
|
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:
|
case protocol.TypeKline:
|
||||||
resp, err = protocol.MKline.Decode(f.Data, val.(protocol.KlineCache))
|
resp, err = protocol.MKline.Decode(f.Data, val.(protocol.KlineCache))
|
||||||
@@ -245,12 +235,18 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp,
|
|||||||
|
|
||||||
// GetQuote 获取盘口五档报价
|
// GetQuote 获取盘口五档报价
|
||||||
func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
||||||
if DefaultCodes == nil {
|
|
||||||
return nil, errors.New("DefaultCodes未初始化")
|
|
||||||
}
|
|
||||||
for i := range codes {
|
for i := range codes {
|
||||||
codes[i] = DefaultCodes.AddExchange(codes[i])
|
//如果是股票代码,则加上前缀
|
||||||
|
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...)
|
f, err := protocol.MQuote.Frame(codes...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -266,26 +262,25 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
|||||||
if len(quotes) != len(codes) {
|
if len(quotes) != len(codes) {
|
||||||
return nil, fmt.Errorf("预期%d个,实际%d个", len(codes), len(quotes))
|
return nil, fmt.Errorf("预期%d个,实际%d个", len(codes), len(quotes))
|
||||||
}
|
}
|
||||||
if DefaultCodes == nil {
|
|
||||||
return nil, errors.New("DefaultCodes未初始化")
|
|
||||||
}
|
|
||||||
for i, code := range codes {
|
for i, code := range codes {
|
||||||
m := DefaultCodes.Get(code)
|
if !protocol.IsStock(code) {
|
||||||
if m == nil {
|
m := DefaultCodes.Get(code)
|
||||||
return nil, fmt.Errorf("未查询到代码[%s]相关信息", 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].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)
|
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),
|
quotes[i].K = protocol.K{
|
||||||
Open: m.Price(quotes[i].K.Open),
|
Last: m.Price(quotes[i].K.Last),
|
||||||
High: m.Price(quotes[i].K.High),
|
Open: m.Price(quotes[i].K.Open),
|
||||||
Low: m.Price(quotes[i].K.Low),
|
High: m.Price(quotes[i].K.High),
|
||||||
Close: m.Price(quotes[i].K.Close),
|
Low: m.Price(quotes[i].K.Low),
|
||||||
|
Close: m.Price(quotes[i].K.Close),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -321,23 +316,34 @@ func (this *Client) GetHistoryMinute(date, code string) (*protocol.MinuteResp, e
|
|||||||
return result.(*protocol.MinuteResp), nil
|
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
|
// 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)
|
code = protocol.AddPrefix(code)
|
||||||
f, err := protocol.MMinuteTrade.Frame(code, start, count)
|
f, err := protocol.MTrade.Frame(code, start, count)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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 只做参考 因为交易实时在进行,然后又是分页读取的,所以会出现读取间隔内产生的交易会丢失
|
// GetMinuteTradeAll 获取分时全部交易详情,todo 只做参考 因为交易实时在进行,然后又是分页读取的,所以会出现读取间隔内产生的交易会丢失
|
||||||
func (this *Client) GetMinuteTradeAll(code string) (*protocol.MinuteTradeResp, error) {
|
func (this *Client) GetMinuteTradeAll(code string) (*protocol.TradeResp, error) {
|
||||||
resp := &protocol.MinuteTradeResp{}
|
resp := &protocol.TradeResp{}
|
||||||
size := uint16(1800)
|
size := uint16(1800)
|
||||||
for start := uint16(0); ; start += size {
|
for start := uint16(0); ; start += size {
|
||||||
r, err := this.GetMinuteTrade(code, start, size)
|
r, err := this.GetMinuteTrade(code, start, size)
|
||||||
@@ -354,26 +360,37 @@ func (this *Client) GetMinuteTradeAll(code string) (*protocol.MinuteTradeResp, e
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Client) GetHistoryTrade(date, code string, start, count uint16) (*protocol.HistoryTradeResp, error) {
|
||||||
|
return this.GetHistoryMinuteTrade(date, code, start, count)
|
||||||
|
}
|
||||||
|
|
||||||
// GetHistoryMinuteTrade 获取历史分时交易
|
// GetHistoryMinuteTrade 获取历史分时交易
|
||||||
// 只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0
|
// 只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0
|
||||||
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
|
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
|
||||||
func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16) (*protocol.HistoryMinuteTradeResp, error) {
|
func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16) (*protocol.HistoryTradeResp, error) {
|
||||||
code = protocol.AddPrefix(code)
|
code = protocol.AddPrefix(code)
|
||||||
f, err := protocol.MHistoryMinuteTrade.Frame(date, code, start, count)
|
f, err := protocol.MHistoryTrade.Frame(date, code, start, count)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
result, err := this.SendFrame(f, code)
|
result, err := this.SendFrame(f, protocol.TradeCache{
|
||||||
|
Date: date,
|
||||||
|
Code: code,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return result.(*protocol.HistoryMinuteTradeResp), nil
|
return result.(*protocol.HistoryTradeResp), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Client) GetHistoryTradeAll(date, code string) (*protocol.HistoryTradeResp, error) {
|
||||||
|
return this.GetHistoryMinuteTradeAll(date, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHistoryMinuteTradeAll 获取历史分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
|
// GetHistoryMinuteTradeAll 获取历史分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
|
||||||
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
|
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
|
||||||
func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.HistoryMinuteTradeResp, error) {
|
func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.HistoryTradeResp, error) {
|
||||||
resp := &protocol.HistoryMinuteTradeResp{}
|
resp := &protocol.HistoryTradeResp{}
|
||||||
size := uint16(2000)
|
size := uint16(2000)
|
||||||
for start := uint16(0); ; start += size {
|
for start := uint16(0); ; start += size {
|
||||||
r, err := this.GetHistoryMinuteTrade(date, code, start, size)
|
r, err := this.GetHistoryMinuteTrade(date, code, start, size)
|
||||||
|
|||||||
52
codes.go
52
codes.go
@@ -1,26 +1,37 @@
|
|||||||
package tdx
|
package tdx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/injoyai/conv"
|
"github.com/injoyai/conv"
|
||||||
|
"github.com/injoyai/ios/client"
|
||||||
"github.com/injoyai/logs"
|
"github.com/injoyai/logs"
|
||||||
"github.com/injoyai/tdx/protocol"
|
"github.com/injoyai/tdx/protocol"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
"xorm.io/core"
|
"xorm.io/core"
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 增加单例,部分数据需要通过Codes里面的信息计算
|
// DefaultCodes 增加单例,部分数据需要通过Codes里面的信息计算
|
||||||
var (
|
var DefaultCodes *Codes
|
||||||
DefaultCodes *Codes
|
|
||||||
codesOnce sync.Once
|
|
||||||
)
|
|
||||||
|
|
||||||
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) {
|
||||||
|
|
||||||
|
//如果没有指定文件名,则使用默认
|
||||||
|
defaultFilename := filepath.Join(DefaultDatabaseDir, "codes.db")
|
||||||
|
filename := conv.Default(defaultFilename, filenames...)
|
||||||
|
filename = conv.Select(filename == "", defaultFilename, filename)
|
||||||
|
|
||||||
//如果文件夹不存在就创建
|
//如果文件夹不存在就创建
|
||||||
dir, _ := filepath.Split(filename)
|
dir, _ := filepath.Split(filename)
|
||||||
@@ -59,7 +70,7 @@ func NewCodes(c *Client, filename string) (*Codes, error) {
|
|||||||
|
|
||||||
{ //设置定时器,每天早上9点更新数据
|
{ //设置定时器,每天早上9点更新数据
|
||||||
task := cron.New(cron.WithSeconds())
|
task := cron.New(cron.WithSeconds())
|
||||||
task.AddFunc("0 0 9 * * *", func() {
|
task.AddFunc("10 0 9 * * *", func() {
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
if err := cc.Update(); err == nil {
|
if err := cc.Update(); err == nil {
|
||||||
return
|
return
|
||||||
@@ -110,7 +121,7 @@ func (this *Codes) GetName(code string) string {
|
|||||||
|
|
||||||
// GetStocks 获取股票代码,sh6xxx sz0xx sz30xx
|
// GetStocks 获取股票代码,sh6xxx sz0xx sz30xx
|
||||||
func (this *Codes) GetStocks(limits ...int) []string {
|
func (this *Codes) GetStocks(limits ...int) []string {
|
||||||
limit := conv.DefaultInt(-1, limits...)
|
limit := conv.Default(-1, limits...)
|
||||||
ls := []string(nil)
|
ls := []string(nil)
|
||||||
for _, m := range this.list {
|
for _, m := range this.list {
|
||||||
code := m.FullCode()
|
code := m.FullCode()
|
||||||
@@ -124,6 +135,22 @@ func (this *Codes) GetStocks(limits ...int) []string {
|
|||||||
return ls
|
return ls
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetETFs 获取基金代码,sz159xxx,sh510xxx,sh511xxx
|
||||||
|
func (this *Codes) GetETFs(limits ...int) []string {
|
||||||
|
limit := conv.Default(-1, limits...)
|
||||||
|
ls := []string(nil)
|
||||||
|
for _, m := range this.list {
|
||||||
|
code := m.FullCode()
|
||||||
|
if protocol.IsETF(code) {
|
||||||
|
ls = append(ls, code)
|
||||||
|
}
|
||||||
|
if limit > 0 && len(ls) >= limit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ls
|
||||||
|
}
|
||||||
|
|
||||||
func (this *Codes) Get(code string) *CodeModel {
|
func (this *Codes) Get(code string) *CodeModel {
|
||||||
return this.Map[code]
|
return this.Map[code]
|
||||||
}
|
}
|
||||||
@@ -199,6 +226,10 @@ func (this *Codes) Update(byDB ...bool) error {
|
|||||||
// GetCodes 更新股票并返回结果
|
// GetCodes 更新股票并返回结果
|
||||||
func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
|
func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
|
||||||
|
|
||||||
|
if this.Client == nil {
|
||||||
|
return nil, errors.New("client is nil")
|
||||||
|
}
|
||||||
|
|
||||||
//2. 查询数据库所有股票
|
//2. 查询数据库所有股票
|
||||||
list := []*CodeModel(nil)
|
list := []*CodeModel(nil)
|
||||||
if err := this.db.Find(&list); err != nil {
|
if err := this.db.Find(&list); err != nil {
|
||||||
@@ -302,7 +333,8 @@ func (this *CodeModel) FullCode() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *CodeModel) Price(p protocol.Price) protocol.Price {
|
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 {
|
func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error {
|
||||||
|
|||||||
4
dial.go
4
dial.go
@@ -73,8 +73,8 @@ func NewRangeDial(hosts []string) ios.DialFunc {
|
|||||||
}
|
}
|
||||||
if i < len(hosts)-1 {
|
if i < len(hosts)-1 {
|
||||||
//最后一个错误返回出去
|
//最后一个错误返回出去
|
||||||
logs.Err(err)
|
logs.Err(err, "等待2秒后尝试下一个服务地址...")
|
||||||
<-time.After(time.Second * 3)
|
<-time.After(time.Second * 2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
46
example/CheckGetHistoryErr/main.go
Normal file
46
example/CheckGetHistoryErr/main.go
Normal 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
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
14
example/FastHosts/main.go
Normal file
14
example/FastHosts/main.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/injoyai/logs"
|
||||||
|
"github.com/injoyai/tdx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ls := tdx.FastHosts(tdx.Hosts...)
|
||||||
|
for _, v := range ls {
|
||||||
|
logs.Debug(v.Host, v.Spend)
|
||||||
|
}
|
||||||
|
logs.Debug("总数量:", len(ls))
|
||||||
|
}
|
||||||
@@ -3,20 +3,18 @@ package main
|
|||||||
import (
|
import (
|
||||||
"github.com/injoyai/logs"
|
"github.com/injoyai/logs"
|
||||||
"github.com/injoyai/tdx"
|
"github.com/injoyai/tdx"
|
||||||
|
"github.com/injoyai/tdx/example/common"
|
||||||
"github.com/injoyai/tdx/protocol"
|
"github.com/injoyai/tdx/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
c, err := tdx.Dial("124.71.187.122:7709")
|
common.Test(func(c *tdx.Client) {
|
||||||
logs.PanicErr(err)
|
resp, err := c.GetCode(protocol.ExchangeSH, 369)
|
||||||
|
logs.PanicErr(err)
|
||||||
|
|
||||||
resp, err := c.GetCode(protocol.ExchangeSH, 369)
|
for i, v := range resp.List {
|
||||||
logs.PanicErr(err)
|
logs.Debug(i, v, v.LastPrice)
|
||||||
|
}
|
||||||
for i, v := range resp.List {
|
logs.Debug("总数:", resp.Count)
|
||||||
logs.Debug(i, v)
|
})
|
||||||
}
|
|
||||||
logs.Debug("总数:", resp.Count)
|
|
||||||
|
|
||||||
select {}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
common.Test(func(c *tdx.Client) {
|
common.Test(func(c *tdx.Client) {
|
||||||
resp, err := c.GetHistoryMinuteTrade("20241025", "sz000001", 0, 20)
|
resp, err := c.GetHistoryMinuteTrade("20250609", "sz000001", 0, 20)
|
||||||
logs.PanicErr(err)
|
logs.PanicErr(err)
|
||||||
|
|
||||||
for _, v := range resp.List {
|
for _, v := range resp.List {
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
common.Test(func(c *tdx.Client) {
|
common.Test(func(c *tdx.Client) {
|
||||||
resp, err := c.GetKlineDay("sh000001", 0, 10)
|
resp, err := c.GetIndexDay("sh000001", 0, 10)
|
||||||
logs.PanicErr(err)
|
logs.PanicErr(err)
|
||||||
|
|
||||||
for _, v := range resp.List {
|
for _, v := range resp.List {
|
||||||
|
|||||||
20
example/GetKline30MinuteAll/main.go
Normal file
20
example/GetKline30MinuteAll/main.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/injoyai/logs"
|
||||||
|
"github.com/injoyai/tdx"
|
||||||
|
"github.com/injoyai/tdx/example/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
common.Test(func(c *tdx.Client) {
|
||||||
|
resp, err := c.GetKline30Minute("sz000001", 0, 20)
|
||||||
|
logs.PanicErr(err)
|
||||||
|
|
||||||
|
for _, v := range resp.List {
|
||||||
|
logs.Debug(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
logs.Debug("总数:", resp.Count)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -10,6 +10,9 @@ func main() {
|
|||||||
c, err := tdx.Dial("124.71.187.122:7709", tdx.WithDebug())
|
c, err := tdx.Dial("124.71.187.122:7709", tdx.WithDebug())
|
||||||
logs.PanicErr(err)
|
logs.PanicErr(err)
|
||||||
|
|
||||||
|
tdx.DefaultCodes, err = tdx.NewCodes(c, "./codes.db")
|
||||||
|
logs.PanicErr(err)
|
||||||
|
|
||||||
_ = c
|
_ = c
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
20
example/GetTrade/main.go
Normal file
20
example/GetTrade/main.go
Normal 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
36
example/Income/main.go
Normal file
36
example/Income/main.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/injoyai/logs"
|
||||||
|
"github.com/injoyai/tdx/extend"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
code := "sz000001"
|
||||||
|
|
||||||
|
pull := extend.NewPullKline(extend.PullKlineConfig{
|
||||||
|
Codes: []string{code},
|
||||||
|
Tables: []string{extend.Day},
|
||||||
|
})
|
||||||
|
|
||||||
|
//m, err := tdx.NewManage(nil)
|
||||||
|
//logs.PanicErr(err)
|
||||||
|
|
||||||
|
//err = pull.Run(context.Background(), m)
|
||||||
|
//logs.PanicErr(err)
|
||||||
|
|
||||||
|
ks, err := pull.DayKlines(code)
|
||||||
|
logs.PanicErr(err)
|
||||||
|
|
||||||
|
t := time.Now().AddDate(0, -1, -9)
|
||||||
|
logs.Debug(t.Format(time.DateOnly))
|
||||||
|
ls := extend.DoIncomes(ks, t, 5, 10, 20)
|
||||||
|
|
||||||
|
logs.Debug(len(ls))
|
||||||
|
|
||||||
|
for _, v := range ls {
|
||||||
|
logs.Info(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/injoyai/tdx"
|
"github.com/injoyai/tdx"
|
||||||
"github.com/injoyai/tdx/extend"
|
"github.com/injoyai/tdx/extend"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -13,12 +14,13 @@ func main() {
|
|||||||
m, err := tdx.NewManage(nil)
|
m, err := tdx.NewManage(nil)
|
||||||
logs.PanicErr(err)
|
logs.PanicErr(err)
|
||||||
|
|
||||||
err = extend.NewPullKline(
|
err = extend.NewPullKline(extend.PullKlineConfig{
|
||||||
[]string{"sz000001"},
|
Codes: []string{"sz000001"},
|
||||||
[]string{extend.Year},
|
Tables: []string{extend.Year},
|
||||||
filepath.Join(tdx.DefaultDatabaseDir, "kline"),
|
Dir: filepath.Join(tdx.DefaultDatabaseDir, "kline"),
|
||||||
1,
|
Limit: 1,
|
||||||
).Run(context.Background(), m)
|
StartAt: time.Time{},
|
||||||
|
}).Run(context.Background(), m)
|
||||||
logs.PanicErr(err)
|
logs.PanicErr(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
20
example/PullTrade/main.go
Normal file
20
example/PullTrade/main.go
Normal 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)
|
||||||
|
|
||||||
|
}
|
||||||
65
extend/income.go
Normal file
65
extend/income.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package extend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/injoyai/tdx/protocol"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DoIncomes(ks Klines, startAt time.Time, days ...int) Incomes {
|
||||||
|
year, month, day := startAt.Date()
|
||||||
|
start := time.Date(year, month, day, 15, 0, 0, 0, startAt.Location()).Unix()
|
||||||
|
for i, v := range ks {
|
||||||
|
if v.Date >= start {
|
||||||
|
ks = ks[i:]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ls := Incomes{}
|
||||||
|
|
||||||
|
for _, v := range days {
|
||||||
|
if v < len(ks) {
|
||||||
|
x := ks[v]
|
||||||
|
ls = append(ls, &Income{
|
||||||
|
Offset: v,
|
||||||
|
Time: time.Unix(x.Date, 0),
|
||||||
|
Source: protocol.K{
|
||||||
|
Open: ks[0].Open,
|
||||||
|
High: ks[0].High,
|
||||||
|
Low: ks[0].Low,
|
||||||
|
Close: ks[0].Close,
|
||||||
|
},
|
||||||
|
Current: protocol.K{
|
||||||
|
Open: x.Open,
|
||||||
|
High: x.High,
|
||||||
|
Low: x.Low,
|
||||||
|
Close: x.Close,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ls
|
||||||
|
}
|
||||||
|
|
||||||
|
type Incomes []*Income
|
||||||
|
|
||||||
|
type Income struct {
|
||||||
|
Offset int //偏移量
|
||||||
|
Time time.Time //时间
|
||||||
|
Source protocol.K //源
|
||||||
|
Current protocol.K //当前
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Income) String() string {
|
||||||
|
return fmt.Sprintf("偏移: %d, 时间: %s, 涨幅: %.1f%%", this.Offset, this.Time.Format(time.DateOnly), this.RiseRate()*100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Income) Rise() protocol.Price {
|
||||||
|
return this.Current.Close - this.Source.Close
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Income) RiseRate() float64 {
|
||||||
|
return this.Rise().Float64() / this.Source.Close.Float64()
|
||||||
|
}
|
||||||
148
extend/pull-kline-mysql.go
Normal file
148
extend/pull-kline-mysql.go
Normal 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
|
||||||
|
}
|
||||||
@@ -7,8 +7,10 @@ import (
|
|||||||
"github.com/injoyai/logs"
|
"github.com/injoyai/logs"
|
||||||
"github.com/injoyai/tdx"
|
"github.com/injoyai/tdx"
|
||||||
"github.com/injoyai/tdx/protocol"
|
"github.com/injoyai/tdx/protocol"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
"time"
|
||||||
"xorm.io/core"
|
"xorm.io/core"
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
)
|
)
|
||||||
@@ -24,53 +26,89 @@ const (
|
|||||||
Month = "month"
|
Month = "month"
|
||||||
Quarter = "quarter"
|
Quarter = "quarter"
|
||||||
Year = "year"
|
Year = "year"
|
||||||
|
|
||||||
|
tableMinute = "MinuteKline"
|
||||||
|
table5Minute = "Minute5Kline"
|
||||||
|
table15Minute = "Minute15Kline"
|
||||||
|
table30Minute = "Minute30Kline"
|
||||||
|
tableHour = "HourKline"
|
||||||
|
tableDay = "DayKline"
|
||||||
|
tableWeek = "WeekKline"
|
||||||
|
tableMonth = "MonthKline"
|
||||||
|
tableQuarter = "QuarterKline"
|
||||||
|
tableYear = "YearKline"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
AllKlineType = []string{Minute, Minute5, Minute15, Minute30, Hour, Day, Week, Month, Quarter, Year}
|
AllKlineType = []string{Minute, Minute5, Minute15, Minute30, Hour, Day, Week, Month, Quarter, Year}
|
||||||
KlineTableMap = map[string]*KlineTable{
|
KlineTableMap = map[string]*KlineTable{
|
||||||
Minute: NewKlineTable("MinuteKline", func(c *tdx.Client) KlineHandler { return c.GetKlineMinuteUntil }),
|
Minute: NewKlineTable(tableMinute, func(c *tdx.Client) KlineHandler { return c.GetKlineMinuteUntil }),
|
||||||
Minute5: NewKlineTable("Minute5Kline", func(c *tdx.Client) KlineHandler { return c.GetKline5MinuteUntil }),
|
Minute5: NewKlineTable(table5Minute, func(c *tdx.Client) KlineHandler { return c.GetKline5MinuteUntil }),
|
||||||
Minute15: NewKlineTable("Minute15Kline", func(c *tdx.Client) KlineHandler { return c.GetKline15MinuteUntil }),
|
Minute15: NewKlineTable(table15Minute, func(c *tdx.Client) KlineHandler { return c.GetKline15MinuteUntil }),
|
||||||
Minute30: NewKlineTable("Minute30Kline", func(c *tdx.Client) KlineHandler { return c.GetKline30MinuteUntil }),
|
Minute30: NewKlineTable(table30Minute, func(c *tdx.Client) KlineHandler { return c.GetKline30MinuteUntil }),
|
||||||
Hour: NewKlineTable("HourKline", func(c *tdx.Client) KlineHandler { return c.GetKlineHourUntil }),
|
Hour: NewKlineTable(tableHour, func(c *tdx.Client) KlineHandler { return c.GetKlineHourUntil }),
|
||||||
Day: NewKlineTable("DayKline", func(c *tdx.Client) KlineHandler { return c.GetKlineDayUntil }),
|
Day: NewKlineTable(tableDay, func(c *tdx.Client) KlineHandler { return c.GetKlineDayUntil }),
|
||||||
Week: NewKlineTable("WeekKline", func(c *tdx.Client) KlineHandler { return c.GetKlineWeekUntil }),
|
Week: NewKlineTable(tableWeek, func(c *tdx.Client) KlineHandler { return c.GetKlineWeekUntil }),
|
||||||
Month: NewKlineTable("MonthKline", func(c *tdx.Client) KlineHandler { return c.GetKlineMonthUntil }),
|
Month: NewKlineTable(tableMonth, func(c *tdx.Client) KlineHandler { return c.GetKlineMonthUntil }),
|
||||||
Quarter: NewKlineTable("QuarterKline", func(c *tdx.Client) KlineHandler { return c.GetKlineQuarterUntil }),
|
Quarter: NewKlineTable(tableQuarter, func(c *tdx.Client) KlineHandler { return c.GetKlineQuarterUntil }),
|
||||||
Year: NewKlineTable("YearKline", func(c *tdx.Client) KlineHandler { return c.GetKlineYearUntil }),
|
Year: NewKlineTable(tableYear, func(c *tdx.Client) KlineHandler { return c.GetKlineYearUntil }),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPullKline(codes, tables []string, dir string, limit int) *PullKline {
|
type PullKlineConfig struct {
|
||||||
|
Codes []string //操作代码
|
||||||
|
Tables []string //数据类型
|
||||||
|
Dir string //数据位置
|
||||||
|
Limit int //协程数量
|
||||||
|
StartAt time.Time //数据开始时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPullKline(cfg PullKlineConfig) *PullKline {
|
||||||
_tables := []*KlineTable(nil)
|
_tables := []*KlineTable(nil)
|
||||||
for _, v := range tables {
|
for _, v := range cfg.Tables {
|
||||||
_tables = append(_tables, KlineTableMap[v])
|
_tables = append(_tables, KlineTableMap[v])
|
||||||
}
|
}
|
||||||
|
if cfg.Limit <= 0 {
|
||||||
|
cfg.Limit = 1
|
||||||
|
}
|
||||||
|
if len(cfg.Dir) == 0 {
|
||||||
|
cfg.Dir = filepath.Join(tdx.DefaultDatabaseDir, "kline")
|
||||||
|
}
|
||||||
return &PullKline{
|
return &PullKline{
|
||||||
tables: _tables,
|
tables: _tables,
|
||||||
dir: dir,
|
Config: cfg,
|
||||||
Codes: codes,
|
|
||||||
limit: limit,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type PullKline struct {
|
type PullKline struct {
|
||||||
tables []*KlineTable
|
tables []*KlineTable
|
||||||
dir string //数据目录
|
Config PullKlineConfig
|
||||||
Codes []string //指定的代码
|
|
||||||
limit int //并发数量
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *PullKline) Name() string {
|
func (this *PullKline) Name() string {
|
||||||
return "拉取k线数据"
|
return "拉取k线数据"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *PullKline) DayKlines(code string) (Klines, error) {
|
||||||
|
//连接数据库
|
||||||
|
db, err := xorm.NewEngine("sqlite", filepath.Join(this.Config.Dir, code+".db"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
db.SetMapper(core.SameMapper{})
|
||||||
|
db.DB().SetMaxOpenConns(1)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
data := Klines{}
|
||||||
|
err = db.Table(tableDay).Asc("date").Find(&data)
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
|
||||||
func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
|
func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
|
||||||
limit := chans.NewWaitLimit(uint(this.limit))
|
limit := chans.NewWaitLimit(uint(this.Config.Limit))
|
||||||
|
|
||||||
//1. 获取所有股票代码
|
//1. 获取所有股票代码
|
||||||
codes := this.Codes
|
codes := this.Config.Codes
|
||||||
if len(codes) == 0 {
|
if len(codes) == 0 {
|
||||||
codes = m.Codes.GetStocks()
|
codes = m.Codes.GetStocks()
|
||||||
}
|
}
|
||||||
@@ -86,12 +124,15 @@ func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
|
|||||||
go func(code string) {
|
go func(code string) {
|
||||||
defer limit.Done()
|
defer limit.Done()
|
||||||
|
|
||||||
|
_ = os.MkdirAll(this.Config.Dir, 0777)
|
||||||
|
|
||||||
//连接数据库
|
//连接数据库
|
||||||
db, err := xorm.NewEngine("sqlite", filepath.Join(this.dir, code+".db"))
|
db, err := xorm.NewEngine("sqlite", filepath.Join(this.Config.Dir, code+".db"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Err(err)
|
logs.Err(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer db.Close()
|
||||||
db.SetMapper(core.SameMapper{})
|
db.SetMapper(core.SameMapper{})
|
||||||
db.DB().SetMaxOpenConns(1)
|
db.DB().SetMaxOpenConns(1)
|
||||||
|
|
||||||
@@ -106,7 +147,7 @@ func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Sync2(table)
|
logs.PrintErr(db.Sync2(table))
|
||||||
|
|
||||||
//2. 获取最后一条数据
|
//2. 获取最后一条数据
|
||||||
last := new(Kline)
|
last := new(Kline)
|
||||||
@@ -157,7 +198,7 @@ func (this *PullKline) pull(code string, lastDate int64, f func(code string, f f
|
|||||||
}
|
}
|
||||||
|
|
||||||
resp, err := f(code, func(k *protocol.Kline) bool {
|
resp, err := f(code, func(k *protocol.Kline) bool {
|
||||||
return k.Time.Unix() <= lastDate
|
return k.Time.Unix() <= lastDate || k.Time.Unix() <= this.Config.StartAt.Unix()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -181,7 +222,7 @@ func (this *PullKline) pull(code string, lastDate int64, f func(code string, f f
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Kline struct {
|
type Kline struct {
|
||||||
Code string `json:"code" xorm:"-"` //代码
|
Code string `json:"code"` //代码
|
||||||
Date int64 `json:"date"` //时间节点 2006-01-02 15:00
|
Date int64 `json:"date"` //时间节点 2006-01-02 15:00
|
||||||
Open protocol.Price `json:"open"` //开盘价
|
Open protocol.Price `json:"open"` //开盘价
|
||||||
High protocol.Price `json:"high"` //最高价
|
High protocol.Price `json:"high"` //最高价
|
||||||
|
|||||||
172
extend/pull-trade.go
Normal file
172
extend/pull-trade.go
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
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.HistoryTradeResp
|
||||||
|
err = m.Do(func(c *tdx.Client) error {
|
||||||
|
resp, err = c.GetHistoryTradeAll(date, code)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logs.Err(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
tss = append(tss, resp.List...)
|
||||||
|
|
||||||
|
//转成分时K线
|
||||||
|
ks, err := resp.List.Klines1()
|
||||||
|
if err != nil {
|
||||||
|
logs.Err(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
164
extend/spider-ths.go
Normal file
164
extend/spider-ths.go
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
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_QFQ uint8 = 1 //前复权
|
||||||
|
THS_HFQ uint8 = 2 //后复权
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
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_QFQ && _type != THS_HFQ {
|
||||||
|
return nil, fmt.Errorf("数据类型错误,例如:前复权1或后复权2")
|
||||||
|
}
|
||||||
|
|
||||||
|
code = protocol.AddPrefix(code)
|
||||||
|
if len(code) != 8 {
|
||||||
|
return nil, fmt.Errorf("股票代码错误,例如:SZ000001或000001")
|
||||||
|
}
|
||||||
|
|
||||||
|
u := fmt.Sprintf(UrlTHSDayKline, code[2:], _type)
|
||||||
|
req, err := http.NewRequest(http.MethodGet, u, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
|
||||||
|
'Chrome/90.0.4430.212 Safari/537.36',
|
||||||
|
'Referer': 'http://stockpage.10jqka.com.cn/',
|
||||||
|
'DNT': '1',
|
||||||
|
*/
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.54")
|
||||||
|
req.Header.Set("Referer", "http://stockpage.10jqka.com.cn/")
|
||||||
|
req.Header.Set("DNT", "1")
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
bs, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
n := bytes.IndexByte(bs, '(')
|
||||||
|
bs = bs[n+1 : len(bs)-1]
|
||||||
|
|
||||||
|
m := map[string]any{}
|
||||||
|
err = json.Unmarshal(bs, &m)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
total := conv.Int(m["total"])
|
||||||
|
priceFactor := conv.Float64(m["priceFactor"])
|
||||||
|
prices := strings.Split(conv.String(m["price"]), ",")
|
||||||
|
dates := strings.Split(conv.String(m["dates"]), ",")
|
||||||
|
volumes := strings.Split(conv.String(m["volumn"]), ",")
|
||||||
|
start := conv.String(m["start"])
|
||||||
|
t, err := time.Parse("20060102", start)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//好像到了22点,总数量会比实际多1
|
||||||
|
if total == len(dates)+1 && total == len(volumes)+1 {
|
||||||
|
total -= 1
|
||||||
|
}
|
||||||
|
//判断数量是否对应
|
||||||
|
if total*4 != len(prices) || total != len(dates) || total != len(volumes) {
|
||||||
|
return nil, fmt.Errorf("total=%d prices=%d dates=%d volumns=%d", total, len(prices), len(dates), len(volumes))
|
||||||
|
}
|
||||||
|
|
||||||
|
ls := []*Kline(nil)
|
||||||
|
|
||||||
|
year := t.Year()
|
||||||
|
lastDate := ""
|
||||||
|
for i := 0; i < total; i++ {
|
||||||
|
//当日前变小时(12xx变01xx),说明过了1年,除非该股票停牌了1年多则数据错误
|
||||||
|
if dates[i] < lastDate {
|
||||||
|
year++
|
||||||
|
}
|
||||||
|
lastDate = dates[i]
|
||||||
|
x, err := time.Parse("0102", dates[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
x = time.Date(year, x.Month(), x.Day(), 15, 0, 0, 0, time.Local)
|
||||||
|
low := protocol.Price(conv.Float64(prices[i*4+0]) * 1000 / priceFactor)
|
||||||
|
ls = append(ls, &Kline{
|
||||||
|
Code: protocol.AddPrefix(code),
|
||||||
|
Date: x.Unix(),
|
||||||
|
Open: protocol.Price(conv.Float64(prices[i*4+1])*1000/priceFactor) + low,
|
||||||
|
High: protocol.Price(conv.Float64(prices[i*4+2])*1000/priceFactor) + low,
|
||||||
|
Low: low,
|
||||||
|
Close: protocol.Price(conv.Float64(prices[i*4+3])*1000/priceFactor) + low,
|
||||||
|
Volume: (conv.Int64(volumes[i]) + 50) / 100,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return ls, nil
|
||||||
|
}
|
||||||
16
extend/spider-ths_test.go
Normal file
16
extend/spider-ths_test.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package extend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewSpiderTHS(t *testing.T) {
|
||||||
|
ls, err := GetTHSDayKline("sz000001", THS_HFQ)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, v := range ls {
|
||||||
|
t.Log(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
58
extend/util.go
Normal file
58
extend/util.go
Normal 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
|
||||||
|
}
|
||||||
10
go.mod
10
go.mod
@@ -4,9 +4,10 @@ go 1.20
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/glebarez/go-sqlite v1.22.0
|
github.com/glebarez/go-sqlite v1.22.0
|
||||||
github.com/injoyai/base v1.0.18
|
github.com/go-sql-driver/mysql v1.7.0
|
||||||
github.com/injoyai/conv v1.1.10
|
github.com/injoyai/base v1.2.8
|
||||||
github.com/injoyai/ios v0.0.4
|
github.com/injoyai/conv v1.2.5
|
||||||
|
github.com/injoyai/ios v0.0.10
|
||||||
github.com/injoyai/logs v1.0.9
|
github.com/injoyai/logs v1.0.9
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
golang.org/x/text v0.16.0
|
golang.org/x/text v0.16.0
|
||||||
@@ -21,14 +22,13 @@ require (
|
|||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/google/uuid v1.5.0 // indirect
|
github.com/google/uuid v1.5.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/kr/text v0.2.0 // indirect
|
github.com/kr/pretty v0.3.1 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
|
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
github.com/satori/go.uuid v1.2.0 // indirect
|
|
||||||
github.com/stretchr/testify v1.9.0 // indirect
|
github.com/stretchr/testify v1.9.0 // indirect
|
||||||
github.com/syndtr/goleveldb v1.0.0 // indirect
|
github.com/syndtr/goleveldb v1.0.0 // indirect
|
||||||
golang.org/x/sys v0.22.0 // indirect
|
golang.org/x/sys v0.22.0 // indirect
|
||||||
|
|||||||
34
go.sum
34
go.sum
@@ -14,36 +14,32 @@ 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/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.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 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 h1:4/Wjm0JIJaTDm8K1KcGrLHJoa8EsJ13YWeX+6Kfq6uI=
|
||||||
github.com/goccy/go-json v0.8.1/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
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=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
|
||||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||||
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
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/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 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
github.com/injoyai/base v1.0.18 h1:AMB1NhPe9acvMp04sThE8Ji/SRS1/MHsx19NmSoD8LA=
|
github.com/injoyai/base v1.2.8 h1:voEPqxE5U+wI1RvcQStVbjUZJDLENbl1SsoQQWMn4s0=
|
||||||
github.com/injoyai/base v1.0.18/go.mod h1:2JSv8ndJ/FmKyazfhDBewzylEbVfJV0EE/r6kOvnzwU=
|
github.com/injoyai/base v1.2.8/go.mod h1:NfCQjml3z2pCvQ3J3YcOXtecqXD0xVPKjo4YTsMLhr8=
|
||||||
github.com/injoyai/conv v1.1.5/go.mod h1:PYoJcbqaz4eyQUovzErRFCuDSPGB4L07AvdmFsTrTew=
|
github.com/injoyai/conv v1.2.5 h1:G4OCyF0NTZul5W1u9IgXDOhW4/zmIigdPKXFHQGmv1M=
|
||||||
github.com/injoyai/conv v1.1.10 h1:LefmOA4SKTEw6gGHXZ6UT8n2K+OzCttYSW8lVmvgQd8=
|
github.com/injoyai/conv v1.2.5/go.mod h1:s05l3fQJQ4mT4VX+KIdbvCWQB0YzZHprmUfUu2uxd1k=
|
||||||
github.com/injoyai/conv v1.1.10/go.mod h1:PYoJcbqaz4eyQUovzErRFCuDSPGB4L07AvdmFsTrTew=
|
github.com/injoyai/ios v0.0.10 h1:Zd37Rwp90PYV5eFhirR0LJ+ni/aYLSCAYgHltk/ZzJA=
|
||||||
github.com/injoyai/ios v0.0.4 h1:yEZ6wN5uCSjAJB4qwpT6R77aMtjkblZo4giKwu9/s7Y=
|
github.com/injoyai/ios v0.0.10/go.mod h1:zLTmvQmIbdTf7zZxymOVxbxvvSeUMpcs4SCVPDT9BOc=
|
||||||
github.com/injoyai/ios v0.0.4/go.mod h1:heABkaIUwoRRe424otl6mKgdU6LYt5gadav/V4gaojA=
|
|
||||||
github.com/injoyai/logs v1.0.9 h1:Wq7rCVIQKcPx+z+lzKQb2qyDK4TML/cgmaSZN9tx33c=
|
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/logs v1.0.9/go.mod h1:CLchJCGhb39Obyrci816R+KMtbxZhgPs0FuikhyixK4=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
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/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
|
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
|
||||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
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.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||||
@@ -68,14 +64,15 @@ github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
|
|||||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||||
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
|
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
|
||||||
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
||||||
|
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
@@ -88,7 +85,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
|
|||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
|
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
|
||||||
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
||||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
|
||||||
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
|
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
|
||||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
@@ -104,18 +100,12 @@ 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 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
||||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.22.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.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
|
||||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
|
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
|
||||||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
|
||||||
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||||
|
|||||||
50
hosts.go
50
hosts.go
@@ -1,5 +1,14 @@
|
|||||||
package tdx
|
package tdx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/injoyai/base/types"
|
||||||
|
"github.com/injoyai/logs"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
||||||
// Hosts 所有服务器地址(2024-11-30测试通过)
|
// Hosts 所有服务器地址(2024-11-30测试通过)
|
||||||
@@ -67,3 +76,44 @@ var (
|
|||||||
"119.97.185.59", //电信
|
"119.97.185.59", //电信
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FastHosts 通过tcp(ping不可用)连接速度的方式筛选排序可用的地址
|
||||||
|
func FastHosts(hosts ...string) []DialResult {
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(len(hosts))
|
||||||
|
mu := sync.Mutex{}
|
||||||
|
ls := types.List[DialResult](nil)
|
||||||
|
for _, host := range hosts {
|
||||||
|
go func(host string) {
|
||||||
|
defer wg.Done()
|
||||||
|
addr := host
|
||||||
|
if !strings.Contains(addr, ":") {
|
||||||
|
addr += ":7709"
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
c, err := net.Dial("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
logs.Err(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
spend := time.Since(now)
|
||||||
|
c.Close()
|
||||||
|
mu.Lock()
|
||||||
|
ls = append(ls, DialResult{
|
||||||
|
Host: host,
|
||||||
|
Spend: spend,
|
||||||
|
})
|
||||||
|
mu.Unlock()
|
||||||
|
}(host)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
return ls.Sort(func(a, b DialResult) bool {
|
||||||
|
return a.Spend < b.Spend
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialResult 连接结果
|
||||||
|
type DialResult struct {
|
||||||
|
Host string
|
||||||
|
Spend time.Duration
|
||||||
|
}
|
||||||
|
|||||||
60
manage.go
60
manage.go
@@ -3,7 +3,6 @@ package tdx
|
|||||||
import (
|
import (
|
||||||
"github.com/injoyai/ios/client"
|
"github.com/injoyai/ios/client"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"path/filepath"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,28 +15,34 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
|||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
cfg = &ManageConfig{}
|
cfg = &ManageConfig{}
|
||||||
}
|
}
|
||||||
if cfg.CodesDir == "" {
|
if cfg.CodesFilename == "" {
|
||||||
cfg.CodesDir = DefaultDatabaseDir
|
cfg.CodesFilename = DefaultDatabaseDir + "/codes.db"
|
||||||
}
|
}
|
||||||
if cfg.WorkdayDir == "" {
|
if cfg.WorkdayFileName == "" {
|
||||||
cfg.WorkdayDir = DefaultDatabaseDir
|
cfg.WorkdayFileName = DefaultDatabaseDir + "/workday.db"
|
||||||
}
|
}
|
||||||
if cfg.Dial == nil {
|
if cfg.Dial == nil {
|
||||||
cfg.Dial = DialDefault
|
cfg.Dial = DialDefault
|
||||||
}
|
}
|
||||||
|
|
||||||
//代码
|
//通用客户端
|
||||||
DefaultCodes = &Codes{}
|
commonClient, err := cfg.Dial(op...)
|
||||||
codesClient, err := cfg.Dial(op...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
codesClient.Wait.SetTimeout(time.Second * 5)
|
commonClient.Wait.SetTimeout(time.Second * 5)
|
||||||
codes, err := NewCodes(codesClient, filepath.Join(cfg.CodesDir, "codes.db"))
|
|
||||||
|
//代码管理
|
||||||
|
codes, err := NewCodes(commonClient, cfg.CodesFilename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//工作日管理
|
||||||
|
workday, err := NewWorkday(commonClient, cfg.WorkdayFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
DefaultCodes = codes
|
|
||||||
|
|
||||||
//连接池
|
//连接池
|
||||||
p, err := NewPool(func() (*Client, error) {
|
p, err := NewPool(func() (*Client, error) {
|
||||||
@@ -47,17 +52,6 @@ func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//工作日
|
|
||||||
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"))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Manage{
|
return &Manage{
|
||||||
Pool: p,
|
Pool: p,
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
@@ -75,6 +69,20 @@ type Manage struct {
|
|||||||
Cron *cron.Cron
|
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 添加工作日任务
|
// AddWorkdayTask 添加工作日任务
|
||||||
func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) {
|
func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) {
|
||||||
this.Cron.AddFunc(spec, func() {
|
this.Cron.AddFunc(spec, func() {
|
||||||
@@ -85,8 +93,8 @@ func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ManageConfig struct {
|
type ManageConfig struct {
|
||||||
Number int //客户端数量
|
Number int //客户端数量
|
||||||
CodesDir string //代码数据库位置
|
CodesFilename string //代码数据库位置
|
||||||
WorkdayDir string //工作日数据库位置
|
WorkdayFileName string //工作日数据库位置
|
||||||
Dial func(op ...client.Option) (cli *Client, err error) //默认连接方式
|
Dial func(op ...client.Option) (cli *Client, err error) //默认连接方式
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"compress/zlib"
|
"compress/zlib"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/injoyai/base/bytes"
|
"github.com/injoyai/base/types"
|
||||||
"github.com/injoyai/base/g"
|
|
||||||
"github.com/injoyai/conv"
|
"github.com/injoyai/conv"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
@@ -19,7 +19,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Message interface {
|
type Message interface {
|
||||||
Bytes() g.Bytes
|
Bytes() types.Bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -39,7 +39,7 @@ type Frame struct {
|
|||||||
Data []byte //数据
|
Data []byte //数据
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Frame) Bytes() g.Bytes {
|
func (this *Frame) Bytes() types.Bytes {
|
||||||
length := uint16(len(this.Data) + 2)
|
length := uint16(len(this.Data) + 2)
|
||||||
data := make([]byte, 12+len(this.Data))
|
data := make([]byte, 12+len(this.Data))
|
||||||
data[0] = Prefix
|
data[0] = Prefix
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func (code) Decode(bs []byte) (*CodeResp, error) {
|
|||||||
Multiple: Uint16(bs[6:8]),
|
Multiple: Uint16(bs[6:8]),
|
||||||
Name: string(UTF8ToGBK(bs[8:16])),
|
Name: string(UTF8ToGBK(bs[8:16])),
|
||||||
Decimal: int8(bs[20]),
|
Decimal: int8(bs[20]),
|
||||||
LastPrice: getVolume(Uint32(bs[21:25])),
|
LastPrice: getVolume2(Uint32(bs[21:25])),
|
||||||
}
|
}
|
||||||
//logs.Debug(bs[25:29]) //26和28字节 好像是枚举(基本是44,45和34,35)
|
//logs.Debug(bs[25:29]) //26和28字节 好像是枚举(基本是44,45和34,35)
|
||||||
bs = bs[29:]
|
bs = bs[29:]
|
||||||
|
|||||||
@@ -5,16 +5,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
MConnect = connect{}
|
MConnect = connect{}
|
||||||
MHeart = heart{}
|
MHeart = heart{}
|
||||||
MCount = count{}
|
MCount = count{}
|
||||||
MQuote = quote{}
|
MQuote = quote{}
|
||||||
MCode = code{}
|
MCode = code{}
|
||||||
MMinute = minute{}
|
MMinute = minute{}
|
||||||
MHistoryMinute = historyMinute{}
|
MHistoryMinute = historyMinute{}
|
||||||
MMinuteTrade = minuteTrade{}
|
MTrade = trade{}
|
||||||
MHistoryMinuteTrade = historyMinuteTrade{}
|
MHistoryTrade = historyTrade{}
|
||||||
MKline = kline{}
|
MKline = kline{}
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConnectResp struct {
|
type ConnectResp struct {
|
||||||
|
|||||||
@@ -33,10 +33,10 @@ func (this historyMinute) Decode(bs []byte) (*MinuteResp, error) {
|
|||||||
Count: Uint16(bs[:2]),
|
Count: Uint16(bs[:2]),
|
||||||
}
|
}
|
||||||
|
|
||||||
multiple := Price(1)
|
multiple := Price(1) * 10
|
||||||
if bs[5] > 0x40 {
|
//if bs[5] > 0x40 {
|
||||||
multiple = 10
|
//multiple = 10
|
||||||
}
|
//}
|
||||||
|
|
||||||
//2-4字节是啥?
|
//2-4字节是啥?
|
||||||
bs = bs[6:]
|
bs = bs[6:]
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
70
protocol/model_history_trade.go
Normal file
70
protocol/model_history_trade.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package protocol
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/injoyai/conv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HistoryTradeResp 历史分时交易比实时少了单量
|
||||||
|
type HistoryTradeResp struct {
|
||||||
|
Count uint16
|
||||||
|
List Trades
|
||||||
|
}
|
||||||
|
|
||||||
|
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) (*HistoryTradeResp, error) {
|
||||||
|
if len(bs) < 2 {
|
||||||
|
return nil, errors.New("数据长度不足")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, number, err := DecodeCode(c.Code)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := &HistoryTradeResp{
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
func Test_stockHistoryMinuteTrade_Frame(t *testing.T) {
|
func Test_stockHistoryMinuteTrade_Frame(t *testing.T) {
|
||||||
// 预期 0c 02000000 00 1200 1200 b50f 84da3401 0000 30303030303100006400
|
// 预期 0c 02000000 00 1200 1200 b50f 84da3401 0000 30303030303100006400
|
||||||
// 0c000000000112001200b50f84da3401000030303030303100006400
|
// 0c000000000112001200b50f84da3401000030303030303100006400
|
||||||
f, err := MHistoryMinuteTrade.Frame("20241028", "sz000001", 0, 100)
|
f, err := MHistoryTrade.Frame("20241028", "sz000001", 0, 100)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
@@ -3,8 +3,9 @@ package protocol
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/injoyai/base/g"
|
"github.com/injoyai/base/types"
|
||||||
"github.com/injoyai/conv"
|
"github.com/injoyai/conv"
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ type KlineReq struct {
|
|||||||
Count uint16
|
Count uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *KlineReq) Bytes(Type uint8) (g.Bytes, error) {
|
func (this *KlineReq) Bytes(Type uint8) (types.Bytes, error) {
|
||||||
if this.Count > 800 {
|
if this.Count > 800 {
|
||||||
return nil, errors.New("单次数量不能超过800")
|
return nil, errors.New("单次数量不能超过800")
|
||||||
}
|
}
|
||||||
@@ -177,7 +178,7 @@ func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) {
|
|||||||
|
|
||||||
resp.List = append(resp.List, k)
|
resp.List = append(resp.List, k)
|
||||||
}
|
}
|
||||||
|
resp.List = FixKlineTime(resp.List)
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,3 +186,90 @@ type KlineCache struct {
|
|||||||
Type uint8 //1分钟,5分钟,日线等
|
Type uint8 //1分钟,5分钟,日线等
|
||||||
Kind string //指数,个股等
|
Kind string //指数,个股等
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FixKlineTime 修复盘内下午(13~15点)拉取数据的时候,11.30的时间变成13.00
|
||||||
|
func FixKlineTime(ks []*Kline) []*Kline {
|
||||||
|
if len(ks) == 0 {
|
||||||
|
return ks
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
//只有当天下午13~15点之间才会出现的时间问题
|
||||||
|
node1 := time.Date(now.Year(), now.Month(), now.Day(), 13, 0, 0, 0, now.Location())
|
||||||
|
node2 := time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, now.Location())
|
||||||
|
if ks[len(ks)-1].Time.Unix() < node1.Unix() || ks[len(ks)-1].Time.Unix() > node2.Unix() {
|
||||||
|
return ks
|
||||||
|
}
|
||||||
|
ls := ks
|
||||||
|
if len(ls) >= 120 {
|
||||||
|
ls = ls[len(ls)-120:]
|
||||||
|
}
|
||||||
|
for i, v := range ls {
|
||||||
|
if v.Time.Unix() == node1.Unix() {
|
||||||
|
ls[i].Time = time.Date(now.Year(), now.Month(), now.Day(), 11, 30, 0, 0, now.Location())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ks
|
||||||
|
}
|
||||||
|
|
||||||
|
type Klines []*Kline
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,7 +19,10 @@ func Test_stockKline_Decode(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp, err := MKline.Decode(bs, 9)
|
resp, err := MKline.Decode(bs, KlineCache{
|
||||||
|
Type: 9,
|
||||||
|
Kind: "",
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -47,7 +47,7 @@ type Quote struct {
|
|||||||
func (this *Quote) String() string {
|
func (this *Quote) String() string {
|
||||||
return fmt.Sprintf(`%s%s
|
return fmt.Sprintf(`%s%s
|
||||||
%s
|
%s
|
||||||
总量:%s, 现量:%s, 总金额:%s, 内盘:%s, 外盘:%s
|
总手:%s, 现量:%s, 总金额:%s, 内盘:%s, 外盘:%s
|
||||||
%s%s
|
%s%s
|
||||||
`,
|
`,
|
||||||
this.Exchange.String(), this.Code, this.K,
|
this.Exchange.String(), this.Code, this.K,
|
||||||
@@ -142,9 +142,9 @@ func (this quote) Decode(bs []byte) QuotesResp {
|
|||||||
sellLevel := PriceLevel{}
|
sellLevel := PriceLevel{}
|
||||||
|
|
||||||
bs, p = GetPrice(bs)
|
bs, p = GetPrice(bs)
|
||||||
buyLevel.Price = p + sec.K.Close
|
buyLevel.Price = p*10 + sec.K.Close
|
||||||
bs, p = GetPrice(bs)
|
bs, p = GetPrice(bs)
|
||||||
sellLevel.Price = p + sec.K.Close
|
sellLevel.Price = p*10 + sec.K.Close
|
||||||
|
|
||||||
bs, buyLevel.Number = CutInt(bs)
|
bs, buyLevel.Number = CutInt(bs)
|
||||||
bs, sellLevel.Number = CutInt(bs)
|
bs, sellLevel.Number = CutInt(bs)
|
||||||
|
|||||||
175
protocol/model_trade.go
Normal file
175
protocol/model_trade.go
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
package protocol
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"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
|
||||||
|
|
||||||
|
func (this Trades) Kline() (k *Kline, err error) {
|
||||||
|
k = &Kline{}
|
||||||
|
for i, v := range this {
|
||||||
|
switch i {
|
||||||
|
case 0:
|
||||||
|
k.Time = v.Time
|
||||||
|
k.Open = v.Price
|
||||||
|
k.High = v.Price
|
||||||
|
k.Low = v.Price
|
||||||
|
k.Close = v.Price
|
||||||
|
case len(this) - 1:
|
||||||
|
k.Close = v.Price
|
||||||
|
}
|
||||||
|
k.High = conv.Select(v.Price > k.High, v.Price, k.High)
|
||||||
|
k.Low = conv.Select(v.Price < k.Low, v.Price, k.Low)
|
||||||
|
k.Volume += int64(v.Volume)
|
||||||
|
k.Amount += v.Amount()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Klines1 1分K线
|
||||||
|
func (this Trades) Klines1() (Klines, error) {
|
||||||
|
m := make(map[int64]Trades)
|
||||||
|
for _, v := range this {
|
||||||
|
//小于9点30的数据归类到9点30
|
||||||
|
if v.Time.Hour() == 9 && v.Time.Minute() < 30 {
|
||||||
|
v.Time = time.Date(v.Time.Year(), v.Time.Month(), v.Time.Day(), 9, 30, 0, 0, v.Time.Location())
|
||||||
|
}
|
||||||
|
//15:00之前和11:30之前+1
|
||||||
|
if (v.Time.Hour() >= 13 && v.Time.Hour() < 15) || (v.Time.Hour() == 11 && v.Time.Minute() < 30) || v.Time.Hour() < 11 {
|
||||||
|
v.Time = v.Time.Add(time.Minute)
|
||||||
|
}
|
||||||
|
m[v.Time.Unix()] = append(m[v.Time.Unix()], v)
|
||||||
|
}
|
||||||
|
|
||||||
|
ls := Klines(nil)
|
||||||
|
for _, v := range m {
|
||||||
|
k, err := v.Kline()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ls = append(ls, k)
|
||||||
|
}
|
||||||
|
ls.Sort()
|
||||||
|
return ls, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type TradeCache struct {
|
||||||
|
Date string //日期
|
||||||
|
Code string //计算倍数
|
||||||
|
}
|
||||||
@@ -78,6 +78,13 @@ func DecodeK(bs []byte) ([]byte, K) {
|
|||||||
bs, k.Low = GetPrice(bs)
|
bs, k.Low = GetPrice(bs)
|
||||||
k.Low += k.Close
|
k.Low += k.Close
|
||||||
|
|
||||||
|
//默认按股票展示
|
||||||
|
k.Last *= 10
|
||||||
|
k.Open *= 10
|
||||||
|
k.Close *= 10
|
||||||
|
k.High *= 10
|
||||||
|
k.Low *= 10
|
||||||
|
|
||||||
return bs, k
|
return bs, k
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package protocol
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
bytes2 "github.com/injoyai/base/bytes"
|
|
||||||
"github.com/injoyai/conv"
|
"github.com/injoyai/conv"
|
||||||
"golang.org/x/text/encoding/simplifiedchinese"
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
"golang.org/x/text/transform"
|
"golang.org/x/text/transform"
|
||||||
@@ -15,12 +14,12 @@ import (
|
|||||||
|
|
||||||
// String 字节先转小端,再转字符
|
// String 字节先转小端,再转字符
|
||||||
func String(bs []byte) string {
|
func String(bs []byte) string {
|
||||||
return string(bytes2.Reverse(bs))
|
return string(Reverse(bs))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bytes 任意类型转小端字节
|
// Bytes 任意类型转小端字节
|
||||||
func Bytes(n any) []byte {
|
func Bytes(n any) []byte {
|
||||||
return bytes2.Reverse(conv.Bytes(n))
|
return Reverse(conv.Bytes(n))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reverse 字节倒序
|
// Reverse 字节倒序
|
||||||
@@ -34,12 +33,12 @@ func Reverse(bs []byte) []byte {
|
|||||||
|
|
||||||
// Uint32 字节通过小端方式转为uint32
|
// Uint32 字节通过小端方式转为uint32
|
||||||
func Uint32(bs []byte) uint32 {
|
func Uint32(bs []byte) uint32 {
|
||||||
return conv.Uint32(bytes2.Reverse(bs))
|
return conv.Uint32(Reverse(bs))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint16 字节通过小端方式转为uint16
|
// Uint16 字节通过小端方式转为uint16
|
||||||
func Uint16(bs []byte) uint16 {
|
func Uint16(bs []byte) uint16 {
|
||||||
return conv.Uint16(bytes2.Reverse(bs))
|
return conv.Uint16(Reverse(bs))
|
||||||
}
|
}
|
||||||
|
|
||||||
func UTF8ToGBK(text []byte) []byte {
|
func UTF8ToGBK(text []byte) []byte {
|
||||||
@@ -196,6 +195,46 @@ func getVolume(val uint32) (volume float64) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getVolume2(val uint32) float64 {
|
||||||
|
ivol := int32(val)
|
||||||
|
logpoint := ivol >> 24 // 提取最高字节(原8*3移位)
|
||||||
|
hleax := (ivol >> 16) & 0xff // 提取次高字节
|
||||||
|
lheax := (ivol >> 8) & 0xff // 提取第三字节
|
||||||
|
lleax := ivol & 0xff // 提取最低字节
|
||||||
|
|
||||||
|
dwEcx := logpoint*2 - 0x7f // 基础指数计算
|
||||||
|
dbl_xmm6 := math.Exp2(float64(dwEcx)) // 核心指数计算仅一次
|
||||||
|
|
||||||
|
// 计算dbl_xmm4
|
||||||
|
var dbl_xmm4 float64
|
||||||
|
if hleax > 0x80 {
|
||||||
|
// 高位分支:合并指数计算
|
||||||
|
dbl_xmm4 = dbl_xmm6 * (64.0 + float64(hleax&0x7f)) / 64.0
|
||||||
|
} else {
|
||||||
|
// 低位分支:复用核心指数
|
||||||
|
dbl_xmm4 = dbl_xmm6 * float64(hleax) / 128.0
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算缩放因子
|
||||||
|
scale := 1.0
|
||||||
|
if (hleax & 0x80) != 0 {
|
||||||
|
scale = 2.0
|
||||||
|
}
|
||||||
|
|
||||||
|
// 预计算常量的倒数,优化除法
|
||||||
|
const (
|
||||||
|
inv32768 = 1.0 / 32768.0 // 2^15
|
||||||
|
inv8388608 = 1.0 / 8388608.0 // 2^23
|
||||||
|
)
|
||||||
|
|
||||||
|
// 计算低位分量
|
||||||
|
dbl_xmm3 := dbl_xmm6 * float64(lheax) * inv32768 * scale
|
||||||
|
dbl_xmm1 := dbl_xmm6 * float64(lleax) * inv8388608 * scale
|
||||||
|
|
||||||
|
// 合计最终结果
|
||||||
|
return dbl_xmm6 + dbl_xmm4 + dbl_xmm3 + dbl_xmm1
|
||||||
|
}
|
||||||
|
|
||||||
// IsStock 是否是股票,示例sz000001
|
// IsStock 是否是股票,示例sz000001
|
||||||
func IsStock(code string) bool {
|
func IsStock(code string) bool {
|
||||||
if len(code) != 8 {
|
if len(code) != 8 {
|
||||||
@@ -214,15 +253,42 @@ func IsStock(code string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddPrefix 添加股票代码前缀,针对股票生效,例如000001,会增加前缀sz000001(平安银行),而不是sh000001(上证指数)
|
// IsETF 是否是基金,示例sz159558
|
||||||
|
func IsETF(code string) bool {
|
||||||
|
if len(code) != 8 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
code = strings.ToLower(code)
|
||||||
|
switch {
|
||||||
|
case code[0:2] == ExchangeSH.String() &&
|
||||||
|
(code[2:5] == "510" || code[2:5] == "511" || code[2:5] == "512" || code[2:5] == "513" || code[2:5] == "515"):
|
||||||
|
return true
|
||||||
|
|
||||||
|
case code[0:2] == ExchangeSZ.String() &&
|
||||||
|
(code[2:5] == "159"):
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddPrefix 添加股票/基金代码前缀,针对股票/基金生效,例如000001,会增加前缀sz000001(平安银行),而不是sh000001(上证指数)
|
||||||
func AddPrefix(code string) string {
|
func AddPrefix(code string) string {
|
||||||
if len(code) == 6 {
|
if len(code) == 6 {
|
||||||
switch {
|
switch {
|
||||||
case code[:1] == "6":
|
case code[:1] == "6":
|
||||||
|
//上海股票
|
||||||
code = ExchangeSH.String() + code
|
code = ExchangeSH.String() + code
|
||||||
case code[:1] == "0":
|
case code[:1] == "0":
|
||||||
|
//深圳股票
|
||||||
code = ExchangeSZ.String() + code
|
code = ExchangeSZ.String() + code
|
||||||
case code[:2] == "30":
|
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
|
code = ExchangeSZ.String() + code
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,5 +20,6 @@ func TestUTF8ToGBK(t *testing.T) {
|
|||||||
|
|
||||||
func Test_getVolume(t *testing.T) {
|
func Test_getVolume(t *testing.T) {
|
||||||
t.Log(getVolume(1237966432))
|
t.Log(getVolume(1237966432))
|
||||||
t.Log(getVolume(1237966432))
|
t.Log(getVolume2(1237966432))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
31
workday.go
31
workday.go
@@ -1,9 +1,12 @@
|
|||||||
package tdx
|
package tdx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
_ "github.com/glebarez/go-sqlite"
|
_ "github.com/glebarez/go-sqlite"
|
||||||
"github.com/injoyai/base/maps"
|
"github.com/injoyai/base/maps"
|
||||||
|
"github.com/injoyai/conv"
|
||||||
"github.com/injoyai/logs"
|
"github.com/injoyai/logs"
|
||||||
|
"github.com/injoyai/tdx/protocol"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -59,6 +62,11 @@ type Workday struct {
|
|||||||
|
|
||||||
// Update 更新
|
// Update 更新
|
||||||
func (this *Workday) Update() error {
|
func (this *Workday) Update() error {
|
||||||
|
|
||||||
|
if this.Client == nil {
|
||||||
|
return errors.New("client is nil")
|
||||||
|
}
|
||||||
|
|
||||||
//获取沪市指数的日K线,用作历史是否节假日的判断依据
|
//获取沪市指数的日K线,用作历史是否节假日的判断依据
|
||||||
//判断日K线是否拉取过
|
//判断日K线是否拉取过
|
||||||
|
|
||||||
@@ -110,6 +118,29 @@ func (this *Workday) TodayIs() bool {
|
|||||||
return this.Is(time.Now())
|
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 遍历指定范围的工作日
|
||||||
|
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日(上海交易所成立时间)
|
// RangeDesc 倒序遍历工作日,从今天-1990年12月19日(上海交易所成立时间)
|
||||||
func (this *Workday) RangeDesc(f func(t time.Time) bool) {
|
func (this *Workday) RangeDesc(f func(t time.Time) bool) {
|
||||||
t := IntegerDay(time.Now())
|
t := IntegerDay(time.Now())
|
||||||
|
|||||||
Reference in New Issue
Block a user