mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
Compare commits
152 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86404db551 | ||
|
|
3f3438fca8 | ||
|
|
1656b41f02 | ||
|
|
8090cc7216 | ||
|
|
a3169c67da | ||
|
|
0a1a6194af | ||
|
|
8b3ff3af01 | ||
|
|
c4c0d1dfa4 | ||
|
|
73b80857cc | ||
|
|
e7e8c6a46a | ||
|
|
2e17db7faf | ||
|
|
accda98f3b | ||
|
|
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 | ||
|
|
e76043dc29 | ||
|
|
ff7fc6aba0 | ||
|
|
0c27cc4276 | ||
|
|
80d6d6dfc5 | ||
|
|
a27461740a | ||
|
|
7a9d59f8f1 | ||
|
|
cba35308f6 | ||
|
|
424e259f2b | ||
|
|
3ea8d50cd7 | ||
|
|
09116ee8e8 | ||
|
|
31fcc0b3a3 | ||
|
|
296b7197a1 | ||
|
|
b4f748306a | ||
|
|
0cfd21f8fb | ||
|
|
1c9fd75876 | ||
|
|
e4de354a66 | ||
|
|
6062da9cb2 | ||
|
|
0414cbebc3 | ||
|
|
b221dbc3e2 | ||
|
|
51d8d06fbd | ||
|
|
9df96f0707 | ||
|
|
53cc5fd743 | ||
|
|
3e4c7a16ff | ||
|
|
4114bc72dd | ||
|
|
cd5cf290f1 | ||
|
|
3cf7148b2c | ||
|
|
0edd96d75b | ||
|
|
4035024c3f | ||
|
|
fa7f5e7068 | ||
|
|
c357ca5995 | ||
|
|
d35d936f8f | ||
|
|
66781b07c2 | ||
|
|
26d37ba0b8 | ||
|
|
db2cefc434 | ||
|
|
3babf67cde | ||
|
|
2e6b0e5e2e | ||
|
|
db7f1a760f | ||
|
|
1d552a0fc7 | ||
|
|
4d8d9f1ad9 | ||
|
|
78a0bb9702 | ||
|
|
c0850a0057 | ||
|
|
1f0958b53e | ||
|
|
344b2f467d | ||
|
|
81c45e76d0 | ||
|
|
a8c248bf9d | ||
|
|
3e57f54978 | ||
|
|
50c997f26f | ||
|
|
0740ff7ca0 | ||
|
|
81f2ffcbea | ||
|
|
8a63c2ff2e | ||
|
|
71035fb210 | ||
|
|
f479d99c99 | ||
|
|
ddb36b5aa7 | ||
|
|
ddc4b801bf | ||
|
|
e22d2776e7 | ||
|
|
f46b97e1e1 | ||
|
|
c7c3e42b61 | ||
|
|
2f46a3fb4b | ||
|
|
cee5270dfc | ||
|
|
c6f68ee031 | ||
|
|
bbc2013cd6 | ||
|
|
2976d84bde | ||
|
|
e9f3d0ac62 | ||
|
|
eb243b4ac7 | ||
|
|
859ea71e8d | ||
|
|
6e920c7330 | ||
|
|
821ff1a6e6 | ||
|
|
ef42ac32b2 | ||
|
|
f6811aca01 | ||
|
|
38494234cd | ||
|
|
60a8c65c93 | ||
|
|
058e87b9be | ||
|
|
59a9517a11 | ||
|
|
8e7135eabb | ||
|
|
e5bbb38aee | ||
|
|
f9998f1c2c | ||
|
|
e0b431649e | ||
|
|
50c3925e53 | ||
|
|
e443bb9134 | ||
|
|
97b1b1a0e5 | ||
|
|
784c349ee4 | ||
|
|
626678540d | ||
|
|
5e233dfb7d | ||
|
|
1c2f1c9191 | ||
|
|
beb97e16fa | ||
|
|
50694f6c44 | ||
|
|
c30866e110 | ||
|
|
1863c20be5 | ||
|
|
8a3b2ae7d4 | ||
|
|
306bc545f3 | ||
|
|
dc7e31f83e | ||
|
|
87fb6ea741 |
162
README.md
162
README.md
@@ -1,31 +1,9 @@
|
||||
### 拉取通达信的数据
|
||||
|
||||
1. 参考 https://github.com/bensema/gotdx
|
||||
2. 参考 https://github.com/mootdx/mootdx
|
||||
|
||||
### 开发进度
|
||||
|
||||
* 基本信息(5档报价)
|
||||

|
||||
* 股票列表
|
||||

|
||||
* 分时成交
|
||||

|
||||
* K线
|
||||

|
||||
|
||||
### 数据校对
|
||||
|
||||
* 日K线校对
|
||||

|
||||

|
||||
|
||||
* 所有K线已校验
|
||||
|
||||
* 校对分时成交
|
||||

|
||||
### 说明
|
||||
|
||||
* 参考golang库 [`https://github.com/bensema/gotdx`](https://github.com/bensema/gotdx)
|
||||
* 参考python库 [`https://github.com/mootdx/mootdx`](https://github.com/mootdx/mootdx)
|
||||
|
||||
* 数据入库示例(开发中) [`https://github.com/injoyai/stock`](https://github.com/injoyai/stock)
|
||||
|
||||
### 如何使用
|
||||
|
||||
@@ -56,79 +34,69 @@ func main() {
|
||||
|
||||
```
|
||||
|
||||
#### IP地址
|
||||
### 开发进度(一期完成)
|
||||
|
||||
| IP | 测试时间 |
|
||||
|-----------------|------------|
|
||||
| 124.71.187.122 | 2024-10-30 |
|
||||
| 122.51.120.217 | 2024-10-30 |
|
||||
| 111.229.247.189 | 2024-11-04 |
|
||||
| 124.70.199.56 | 2024-11-04 |
|
||||
| 124.70.176.52 | 2024-11-04 |
|
||||
| 124.70.133.119 | 2024-11-04 |
|
||||
| 123.60.84.66 | 2024-11-04 |
|
||||
| 123.60.73.44 | 2024-11-04 |
|
||||
| 123.60.70.228 | 2024-11-04 |
|
||||
| 123.60.186.45 | 2024-11-04 |
|
||||
| 123.40.164.122 | 2024-11-04 |
|
||||
| 123.249.15.60 | 2024-11-04 |
|
||||
| 122.51.232.182 | 2024-11-04 |
|
||||
| 121.36.81.195 | 2024-11-04 |
|
||||
| 121.36.54.217 | 2024-11-04 |
|
||||
| 121.36.225.169 | 2024-11-04 |
|
||||
| 120.46.186.223 | 2024-11-04 |
|
||||
| 119.97.185.59 | 2024-11-04 |
|
||||
| 118.25.98.114 | 2024-11-04 |
|
||||
| 116.205.183.150 | 2024-11-04 |
|
||||
| 116.205.171.132 | 2024-11-04 |
|
||||
| 116.205.163.254 | 2024-11-04 |
|
||||
| 111.230.186.52 | 2024-11-04 |
|
||||
| 110.41.4.4 | 2024-11-04 |
|
||||
| 110.41.2.72 | 2024-11-04 |
|
||||
| 110.41.154.219 | 2024-11-04 |
|
||||
| 110.41.147.114 | 2024-11-04 |
|
||||
* 基本信息(5档报价)
|
||||

|
||||
* 股票列表
|
||||

|
||||
* 分时成交
|
||||

|
||||
* K线
|
||||

|
||||
|
||||
* 从(https://github.com/mootdx/mootdx)复制,未校验
|
||||
```
|
||||
('深圳双线主站1', '110.41.147.114', 7709),
|
||||
('深圳双线主站2', '8.129.13.54', 7709),
|
||||
('深圳双线主站3', '120.24.149.49', 7709),
|
||||
('深圳双线主站4', '47.113.94.204', 7709),
|
||||
('深圳双线主站5', '8.129.174.169', 7709),
|
||||
('深圳双线主站6', '110.41.154.219', 7709),
|
||||
('上海双线主站1', '124.70.176.52', 7709),
|
||||
('上海双线主站2', '47.100.236.28', 7709),
|
||||
('上海双线主站3', '101.133.214.242', 7709),
|
||||
('上海双线主站4', '47.116.21.80', 7709),
|
||||
('上海双线主站5', '47.116.105.28', 7709),
|
||||
('上海双线主站6', '124.70.199.56', 7709),
|
||||
('北京双线主站1', '121.36.54.217', 7709),
|
||||
('北京双线主站2', '121.36.81.195', 7709),
|
||||
('北京双线主站3', '123.249.15.60', 7709),
|
||||
('广州双线主站1', '124.71.85.110', 7709),
|
||||
('广州双线主站2', '139.9.51.18', 7709),
|
||||
('广州双线主站3', '139.159.239.163', 7709),
|
||||
('上海双线主站7', '106.14.201.131', 7709),
|
||||
('上海双线主站8', '106.14.190.242', 7709),
|
||||
('上海双线主站9', '121.36.225.169', 7709),
|
||||
('上海双线主站10', '123.60.70.228', 7709),
|
||||
('上海双线主站11', '123.60.73.44', 7709),
|
||||
('上海双线主站12', '124.70.133.119', 7709),
|
||||
('上海双线主站13', '124.71.187.72', 7709),
|
||||
('上海双线主站14', '124.71.187.122', 7709),
|
||||
('武汉电信主站1', '119.97.185.59', 7709),
|
||||
('深圳双线主站7', '47.107.64.168', 7709),
|
||||
('北京双线主站4', '124.70.75.113', 7709),
|
||||
('广州双线主站4', '124.71.9.153', 7709),
|
||||
('上海双线主站15', '123.60.84.66', 7709),
|
||||
('深圳双线主站8', '47.107.228.47', 7719),
|
||||
('北京双线主站5', '120.46.186.223', 7709),
|
||||
('北京双线主站6', '124.70.22.210', 7709),
|
||||
('北京双线主站7', '139.9.133.247', 7709),
|
||||
('广州双线主站5', '116.205.163.254', 7709),
|
||||
('广州双线主站6', '116.205.171.132', 7709),
|
||||
('广州双线主站7', '116.205.183.150', 7709)
|
||||
```
|
||||
### 数据校对
|
||||
|
||||
* 日K线校对
|
||||

|
||||

|
||||
|
||||
* 所有K线已校验
|
||||
|
||||
* 校对分时成交
|
||||

|
||||
|
||||
#### 服务器地址(端口7709)
|
||||
|
||||
| IP | 测试时间 | 所属地区 | 运营商 |
|
||||
|-----------------|------------|------|-----|
|
||||
| 124.71.187.122 | 2025-05-21 | 上海 | 华为 |
|
||||
| 122.51.120.217 | 2025-05-21 | 上海 | 腾讯 |
|
||||
| 111.229.247.189 | 2025-05-21 | 上海 | 腾讯 |
|
||||
| 124.70.176.52 | 2025-05-21 | 上海 | 华为 |
|
||||
| 123.60.186.45 | 2025-05-21 | 上海 | 华为 |
|
||||
| 122.51.232.182 | 2025-05-21 | 上海 | 腾讯 |
|
||||
| 118.25.98.114 | 2025-05-21 | 上海 | 腾讯 |
|
||||
| 124.70.199.56 | 2025-05-21 | 上海 | 华为 |
|
||||
| 121.36.225.169 | 2025-05-21 | 上海 | 华为 |
|
||||
| 123.60.70.228 | 2025-05-21 | 上海 | 华为 |
|
||||
| 123.60.73.44 | 2025-05-21 | 上海 | 华为 |
|
||||
| 124.70.133.119 | 2025-05-21 | 上海 | 华为 |
|
||||
| 124.71.187.72 | 2025-05-21 | 上海 | 华为 |
|
||||
| 123.60.84.66 | 2025-05-21 | 上海 | 华为 |
|
||||
| | | | |
|
||||
| 121.36.54.217 | 2025-05-21 | 北京 | 华为 |
|
||||
| 121.36.81.195 | 2025-05-21 | 北京 | 华为 |
|
||||
| 123.249.15.60 | 2025-05-21 | 北京 | 华为 |
|
||||
| 124.70.75.113 | 2025-05-21 | 北京 | 华为 |
|
||||
| 120.46.186.223 | 2025-05-21 | 北京 | 华为 |
|
||||
| 124.70.22.210 | 2025-05-21 | 北京 | 华为 |
|
||||
| 139.9.133.247 | 2025-05-21 | 北京 | 华为 |
|
||||
| | | | |
|
||||
| 124.71.85.110 | 2025-05-21 | 广州 | 华为 |
|
||||
| 139.9.51.18 | 2025-05-21 | 广州 | 华为 |
|
||||
| 139.159.239.163 | 2025-05-21 | 广州 | 华为 |
|
||||
| 124.71.9.153 | 2025-05-21 | 广州 | 华为 |
|
||||
| 116.205.163.254 | 2025-05-21 | 广州 | 华为 |
|
||||
| 116.205.171.132 | 2025-05-21 | 广州 | 华为 |
|
||||
| 116.205.183.150 | 2025-05-21 | 广州 | 华为 |
|
||||
| 111.230.186.52 | 2025-05-21 | 广州 | 腾讯 |
|
||||
| 110.41.4.4 | 2025-05-21 | 广州 | 华为 |
|
||||
| 110.41.2.72 | 2025-05-21 | 广州 | 华为 |
|
||||
| 110.41.154.219 | 2025-05-21 | 广州 | 华为 |
|
||||
| 110.41.147.114 | 2025-05-21 | 广州 | 华为 |
|
||||
| | | | |
|
||||
| 119.97.185.59 | 2025-05-21 | 武汉 | 电信 |
|
||||
|
||||
|
||||
|
||||
|
||||
405
client.go
405
client.go
@@ -1,21 +1,31 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"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/ios"
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/injoyai/ios/module/tcp"
|
||||
"github.com/injoyai/ios/module/common"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
LevelNone = common.LevelNone
|
||||
LevelDebug = common.LevelDebug
|
||||
LevelWrite = common.LevelWrite
|
||||
LevelRead = common.LevelRead
|
||||
LevelInfo = common.LevelInfo
|
||||
LevelError = common.LevelError
|
||||
LevelAll = common.LevelAll
|
||||
)
|
||||
|
||||
// WithDebug 是否打印通讯数据
|
||||
func WithDebug(b ...bool) client.Option {
|
||||
return func(c *client.Client) {
|
||||
@@ -23,6 +33,12 @@ func WithDebug(b ...bool) client.Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithLevel(level int) client.Option {
|
||||
return func(c *client.Client) {
|
||||
c.Logger.SetLevel(level)
|
||||
}
|
||||
}
|
||||
|
||||
// WithRedial 断线重连
|
||||
func WithRedial(b ...bool) client.Option {
|
||||
return func(c *client.Client) {
|
||||
@@ -30,12 +46,30 @@ func WithRedial(b ...bool) client.Option {
|
||||
}
|
||||
}
|
||||
|
||||
// DialDefault 默认连接方式
|
||||
func DialDefault(op ...client.Option) (cli *Client, err error) {
|
||||
op = append([]client.Option{WithRedial()}, op...)
|
||||
return DialHostsRange(Hosts, op...)
|
||||
}
|
||||
|
||||
// Dial 与服务器建立连接
|
||||
func Dial(addr string, op ...client.Option) (cli *Client, err error) {
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr += ":7709"
|
||||
}
|
||||
return DialWith(tcp.NewDial(addr), op...)
|
||||
return DialWith(NewTCPDial(addr), op...)
|
||||
}
|
||||
|
||||
// DialHosts 与服务器建立连接,多个服务器轮询,开启重试生效
|
||||
func DialHosts(hosts []string, op ...client.Option) (cli *Client, err error) {
|
||||
return DialWith(NewHostDial(hosts), op...)
|
||||
}
|
||||
|
||||
// DialHostsRandom 与服务器建立连接,多个服务器随机连接
|
||||
func DialHostsRandom(hosts []string, op ...client.Option) (cli *Client, err error) {
|
||||
return DialWith(NewRandomDial(hosts), op...)
|
||||
}
|
||||
|
||||
// DialHostsRange 遍历设置的服务地址进行连接,成功则结束遍历
|
||||
func DialHostsRange(hosts []string, op ...client.Option) (cli *Client, err error) {
|
||||
return DialWith(NewRangeDial(hosts), op...)
|
||||
}
|
||||
|
||||
// DialWith 与服务器建立连接
|
||||
@@ -47,21 +81,24 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
|
||||
}
|
||||
|
||||
cli.Client, err = client.Dial(dial, func(c *client.Client) {
|
||||
c.Logger.Debug(false) //关闭日志打印
|
||||
c.Logger.Debug(true) //关闭日志打印
|
||||
c.Logger.SetLevel(LevelInfo) //设置日志级别
|
||||
c.Logger.WithHEX() //以HEX显示
|
||||
c.SetOption(op...) //自定义选项
|
||||
c.Event.OnReadFrom = protocol.ReadFrom //分包
|
||||
c.Event.OnDealMessage = cli.handlerDealMessage //解析数据并处理
|
||||
//无数据超时时间是60秒,30秒发送一个心跳包
|
||||
c.GoTimerWriter(30*time.Second, func(w ios.MoreWriter) error {
|
||||
bs := protocol.MHeart.Frame().Bytes()
|
||||
_, err := w.Write(bs)
|
||||
return err
|
||||
})
|
||||
|
||||
f := protocol.MConnect.Frame()
|
||||
if _, err = c.Write(f.Bytes()); err != nil {
|
||||
c.Close()
|
||||
c.Event.OnConnected = func(c *client.Client) error {
|
||||
//无数据超时时间是60秒,30秒发送一个心跳包
|
||||
c.GoTimerWriter(30*time.Second, func(w ios.MoreWriter) error {
|
||||
bs := protocol.MHeart.Frame().Bytes()
|
||||
_, err := w.Write(bs)
|
||||
return err
|
||||
})
|
||||
f := protocol.MConnect.Frame()
|
||||
if _, err = c.Write(f.Bytes()); err != nil {
|
||||
c.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
@@ -70,7 +107,7 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
|
||||
|
||||
go cli.Client.Run()
|
||||
|
||||
return cli, nil
|
||||
return cli, err
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
@@ -118,14 +155,17 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) {
|
||||
case protocol.TypeMinute:
|
||||
resp, err = protocol.MMinute.Decode(f.Data)
|
||||
|
||||
case protocol.TypeHistoryMinute:
|
||||
resp, err = protocol.MHistoryMinute.Decode(f.Data)
|
||||
|
||||
case protocol.TypeMinuteTrade:
|
||||
resp, err = protocol.MMinuteTrade.Decode(f.Data, conv.String(val)) //todo
|
||||
resp, err = protocol.MTrade.Decode(f.Data, val.(protocol.TradeCache))
|
||||
|
||||
case protocol.TypeHistoryMinuteTrade:
|
||||
resp, err = protocol.MHistoryMinuteTrade.Decode(f.Data, conv.String(val))
|
||||
resp, err = protocol.MHistoryTrade.Decode(f.Data, val.(protocol.TradeCache))
|
||||
|
||||
case protocol.TypeKline:
|
||||
resp, err = protocol.MKline.Decode(f.Data, conv.Uint8(val))
|
||||
resp, err = protocol.MKline.Decode(f.Data, val.(protocol.KlineCache))
|
||||
|
||||
default:
|
||||
err = fmt.Errorf("通讯类型未解析:0x%X", f.Type)
|
||||
@@ -150,7 +190,7 @@ func (this *Client) SendFrame(f *protocol.Frame, cache ...any) (any, error) {
|
||||
if _, err := this.Client.Write(f.Bytes()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Wait.Wait(conv.String(this.msgID))
|
||||
return this.Wait.Wait(conv.String(f.MsgID))
|
||||
}
|
||||
|
||||
// GetCount 获取市场内的股票数量
|
||||
@@ -195,8 +235,54 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp,
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// GetStockAll 获取所有股票代码
|
||||
func (this *Client) GetStockAll() ([]string, error) {
|
||||
ls := []string(nil)
|
||||
for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
|
||||
resp, err := this.GetCodeAll(ex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range resp.List {
|
||||
if protocol.IsStock(v.Code) {
|
||||
ls = append(ls, v.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ls, nil
|
||||
}
|
||||
|
||||
// GetETFAll 获取所有ETF代码
|
||||
func (this *Client) GetETFAll() ([]string, error) {
|
||||
ls := []string(nil)
|
||||
for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
|
||||
resp, err := this.GetCodeAll(ex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range resp.List {
|
||||
if protocol.IsETF(v.Code) {
|
||||
ls = append(ls, v.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ls, nil
|
||||
}
|
||||
|
||||
// GetQuote 获取盘口五档报价
|
||||
func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
||||
for i := range codes {
|
||||
//如果是股票代码,则加上前缀
|
||||
codes[i] = protocol.AddPrefix(codes[i])
|
||||
if !protocol.IsStock(codes[i]) {
|
||||
if DefaultCodes == nil {
|
||||
return nil, errors.New("DefaultCodes未初始化")
|
||||
}
|
||||
//不是股票代码的话,根据codes的信息加上前缀
|
||||
codes[i] = DefaultCodes.AddExchange(codes[i])
|
||||
}
|
||||
}
|
||||
|
||||
f, err := protocol.MQuote.Frame(codes...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -205,11 +291,43 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(protocol.QuotesResp), nil
|
||||
quotes := result.(protocol.QuotesResp)
|
||||
|
||||
{ //todo 临时处理下先,后续优化,感觉有问题
|
||||
//判断长度和预期是否一致
|
||||
if len(quotes) != len(codes) {
|
||||
return nil, fmt.Errorf("预期%d个,实际%d个", len(codes), len(quotes))
|
||||
}
|
||||
for i, code := range codes {
|
||||
if !protocol.IsStock(code) {
|
||||
m := DefaultCodes.Get(code)
|
||||
if m == nil {
|
||||
return nil, fmt.Errorf("未查询到代码[%s]相关信息", code)
|
||||
}
|
||||
for ii, v := range quotes[i].SellLevel {
|
||||
quotes[i].SellLevel[ii].Price = m.Price(v.Price)
|
||||
}
|
||||
for ii, v := range quotes[i].BuyLevel {
|
||||
quotes[i].BuyLevel[ii].Price = m.Price(v.Price)
|
||||
}
|
||||
quotes[i].K = protocol.K{
|
||||
Last: m.Price(quotes[i].K.Last),
|
||||
Open: m.Price(quotes[i].K.Open),
|
||||
High: m.Price(quotes[i].K.High),
|
||||
Low: m.Price(quotes[i].K.Low),
|
||||
Close: m.Price(quotes[i].K.Close),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return quotes, nil
|
||||
}
|
||||
|
||||
// GetMinute 获取分时数据,todo 解析好像不对
|
||||
// GetMinute 获取分时数据,todo 解析好像不对,先用历史数据
|
||||
func (this *Client) GetMinute(code string) (*protocol.MinuteResp, error) {
|
||||
return this.GetHistoryMinute(time.Now().Format("20060102"), code)
|
||||
|
||||
f, err := protocol.MMinute.Frame(code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -221,22 +339,47 @@ func (this *Client) GetMinute(code string) (*protocol.MinuteResp, error) {
|
||||
return result.(*protocol.MinuteResp), nil
|
||||
}
|
||||
|
||||
// GetHistoryMinute 获取历史分时数据
|
||||
func (this *Client) GetHistoryMinute(date, code string) (*protocol.MinuteResp, error) {
|
||||
f, err := protocol.MHistoryMinute.Frame(date, code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err := this.SendFrame(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*protocol.MinuteResp), nil
|
||||
}
|
||||
|
||||
func (this *Client) GetTrade(code string, start, count uint16) (*protocol.TradeResp, error) {
|
||||
return this.GetMinuteTrade(code, start, count)
|
||||
}
|
||||
|
||||
// GetMinuteTrade 获取分时交易详情,服务器最多返回1800条,count-start<=1800
|
||||
func (this *Client) GetMinuteTrade(code string, start, count uint16) (*protocol.MinuteTradeResp, error) {
|
||||
f, err := protocol.MMinuteTrade.Frame(code, start, count)
|
||||
func (this *Client) GetMinuteTrade(code string, start, count uint16) (*protocol.TradeResp, error) {
|
||||
code = protocol.AddPrefix(code)
|
||||
f, err := protocol.MTrade.Frame(code, start, count)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err := this.SendFrame(f, code)
|
||||
result, err := this.SendFrame(f, protocol.TradeCache{
|
||||
Date: time.Now().Format("20060102"),
|
||||
Code: code,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*protocol.MinuteTradeResp), nil
|
||||
return result.(*protocol.TradeResp), nil
|
||||
}
|
||||
|
||||
func (this *Client) GetTradeAll(code string) (*protocol.TradeResp, error) {
|
||||
return this.GetMinuteTradeAll(code)
|
||||
}
|
||||
|
||||
// GetMinuteTradeAll 获取分时全部交易详情,todo 只做参考 因为交易实时在进行,然后又是分页读取的,所以会出现读取间隔内产生的交易会丢失
|
||||
func (this *Client) GetMinuteTradeAll(code string) (*protocol.MinuteTradeResp, error) {
|
||||
resp := &protocol.MinuteTradeResp{}
|
||||
func (this *Client) GetMinuteTradeAll(code string) (*protocol.TradeResp, error) {
|
||||
resp := &protocol.TradeResp{}
|
||||
size := uint16(1800)
|
||||
for start := uint16(0); ; start += size {
|
||||
r, err := this.GetMinuteTrade(code, start, size)
|
||||
@@ -253,22 +396,37 @@ func (this *Client) GetMinuteTradeAll(code string) (*protocol.MinuteTradeResp, e
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// GetHistoryMinuteTrade 获取历史分时交易,,只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0
|
||||
func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16) (*protocol.HistoryMinuteTradeResp, error) {
|
||||
f, err := protocol.MHistoryMinuteTrade.Frame(date, code, start, count)
|
||||
func (this *Client) GetHistoryTrade(date, code string, start, count uint16) (*protocol.TradeResp, error) {
|
||||
return this.GetHistoryMinuteTrade(date, code, start, count)
|
||||
}
|
||||
|
||||
// GetHistoryMinuteTrade 获取历史分时交易
|
||||
// 只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0
|
||||
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
|
||||
func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16) (*protocol.TradeResp, error) {
|
||||
code = protocol.AddPrefix(code)
|
||||
f, err := protocol.MHistoryTrade.Frame(date, code, start, count)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err := this.SendFrame(f, code)
|
||||
result, err := this.SendFrame(f, protocol.TradeCache{
|
||||
Date: date,
|
||||
Code: code,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*protocol.HistoryMinuteTradeResp), nil
|
||||
return result.(*protocol.TradeResp), nil
|
||||
}
|
||||
|
||||
func (this *Client) GetHistoryTradeAll(date, code string) (*protocol.TradeResp, error) {
|
||||
return this.GetHistoryMinuteTradeAll(date, code)
|
||||
}
|
||||
|
||||
// GetHistoryMinuteTradeAll 获取历史分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
|
||||
func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.HistoryMinuteTradeResp, error) {
|
||||
resp := &protocol.HistoryMinuteTradeResp{}
|
||||
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
|
||||
func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.TradeResp, error) {
|
||||
resp := &protocol.TradeResp{}
|
||||
size := uint16(2000)
|
||||
for start := uint16(0); ; start += size {
|
||||
r, err := this.GetHistoryMinuteTrade(date, code, start, size)
|
||||
@@ -284,21 +442,112 @@ func (this *Client) GetHistoryMinuteTradeAll(date, code string) (*protocol.Histo
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// GetKline 获取k线数据,推荐收盘之后获取,否则会获取到当天的数据
|
||||
func (this *Client) GetKline(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
/*
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
// GetIndex 获取指数,接口是和k线一样的,但是解析不知道怎么区分(解析方式不一致),所以加一个方法
|
||||
func (this *Client) GetIndex(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
code = protocol.AddPrefix(code)
|
||||
f, err := protocol.MKline.Frame(Type, code, start, count)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err := this.SendFrame(f, Type)
|
||||
result, err := this.SendFrame(f, protocol.KlineCache{Type: Type, Kind: protocol.KindIndex})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*protocol.KlineResp), nil
|
||||
}
|
||||
|
||||
// GetKlineAll 获取全部k线数据
|
||||
func (this *Client) GetKlineAll(Type uint8, code string) (*protocol.KlineResp, error) {
|
||||
// GetIndexUntil 获取指数k线数据,通过多次请求来拼接,直到满足func返回true
|
||||
func (this *Client) GetIndexUntil(Type uint8, code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
resp := &protocol.KlineResp{}
|
||||
size := uint16(800)
|
||||
var last *protocol.Kline
|
||||
for start := uint16(0); ; start += size {
|
||||
r, err := this.GetIndex(Type, code, start, size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if last != nil && len(r.List) > 0 {
|
||||
last.Last = r.List[len(r.List)-1].Close
|
||||
}
|
||||
if len(r.List) > 0 {
|
||||
last = r.List[0]
|
||||
}
|
||||
for i := len(r.List) - 1; i >= 0; i-- {
|
||||
if f(r.List[i]) {
|
||||
resp.Count += r.Count - uint16(i)
|
||||
resp.List = append(r.List[i:], resp.List...)
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
resp.Count += r.Count
|
||||
resp.List = append(r.List, resp.List...)
|
||||
if r.Count < size {
|
||||
break
|
||||
}
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// GetIndexAll 获取全部k线数据
|
||||
func (this *Client) GetIndexAll(Type uint8, code string) (*protocol.KlineResp, error) {
|
||||
return this.GetIndexUntil(Type, code, func(k *protocol.Kline) bool { return false })
|
||||
}
|
||||
|
||||
func (this *Client) GetIndexDay(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetIndex(protocol.TypeKlineDay, code, start, count)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndexDayUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetIndexUntil(protocol.TypeKlineDay, code, f)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndexDayAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetIndexAll(protocol.TypeKlineDay, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndexWeekAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetIndexAll(protocol.TypeKlineWeek, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndexMonthAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetIndexAll(protocol.TypeKlineMonth, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndexQuarterAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetIndexAll(protocol.TypeKlineQuarter, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetIndexYearAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetIndexAll(protocol.TypeKlineYear, code)
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
|
||||
*/
|
||||
|
||||
// GetKline 获取k线数据,推荐收盘之后获取,否则会获取到当天的数据
|
||||
func (this *Client) GetKline(Type uint8, code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
code = protocol.AddPrefix(code)
|
||||
f, err := protocol.MKline.Frame(Type, code, start, count)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err := this.SendFrame(f, protocol.KlineCache{Type: Type, Kind: protocol.KindStock})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*protocol.KlineResp), nil
|
||||
}
|
||||
|
||||
// GetKlineUntil 获取k线数据,通过多次请求来拼接,直到满足func返回true
|
||||
func (this *Client) GetKlineUntil(Type uint8, code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
resp := &protocol.KlineResp{}
|
||||
size := uint16(800)
|
||||
var last *protocol.Kline
|
||||
@@ -313,6 +562,13 @@ func (this *Client) GetKlineAll(Type uint8, code string) (*protocol.KlineResp, e
|
||||
if len(r.List) > 0 {
|
||||
last = r.List[0]
|
||||
}
|
||||
for i := len(r.List) - 1; i >= 0; i-- {
|
||||
if f(r.List[i]) {
|
||||
resp.Count += r.Count - uint16(i)
|
||||
resp.List = append(r.List[i:], resp.List...)
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
resp.Count += r.Count
|
||||
resp.List = append(r.List, resp.List...)
|
||||
if r.Count < size {
|
||||
@@ -322,6 +578,11 @@ func (this *Client) GetKlineAll(Type uint8, code string) (*protocol.KlineResp, e
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// GetKlineAll 获取全部k线数据
|
||||
func (this *Client) GetKlineAll(Type uint8, code string) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(Type, code, func(k *protocol.Kline) bool { return false })
|
||||
}
|
||||
|
||||
// GetKlineMinute 获取一分钟k线数据,每次最多800条,最多只能获取24000条数据
|
||||
func (this *Client) GetKlineMinute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKlineMinute, code, start, count)
|
||||
@@ -332,6 +593,10 @@ func (this *Client) GetKlineMinuteAll(code string) (*protocol.KlineResp, error)
|
||||
return this.GetKlineAll(protocol.TypeKlineMinute, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKlineMinuteUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKlineMinute, code, f)
|
||||
}
|
||||
|
||||
// GetKline5Minute 获取五分钟k线数据
|
||||
func (this *Client) GetKline5Minute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKline5Minute, code, start, count)
|
||||
@@ -342,6 +607,10 @@ func (this *Client) GetKline5MinuteAll(code string) (*protocol.KlineResp, error)
|
||||
return this.GetKlineAll(protocol.TypeKline5Minute, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKline5MinuteUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKline5Minute, code, f)
|
||||
}
|
||||
|
||||
// GetKline15Minute 获取十五分钟k线数据
|
||||
func (this *Client) GetKline15Minute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKline15Minute, code, start, count)
|
||||
@@ -352,6 +621,10 @@ func (this *Client) GetKline15MinuteAll(code string) (*protocol.KlineResp, error
|
||||
return this.GetKlineAll(protocol.TypeKline15Minute, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKline15MinuteUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKline15Minute, code, f)
|
||||
}
|
||||
|
||||
// GetKline30Minute 获取三十分钟k线数据
|
||||
func (this *Client) GetKline30Minute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKline30Minute, code, start, count)
|
||||
@@ -362,14 +635,36 @@ func (this *Client) GetKline30MinuteAll(code string) (*protocol.KlineResp, error
|
||||
return this.GetKlineAll(protocol.TypeKline30Minute, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKline30MinuteUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKline30Minute, code, f)
|
||||
}
|
||||
|
||||
// GetKline60Minute 获取60分钟k线数据
|
||||
func (this *Client) GetKline60Minute(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKline60Minute, code, start, count)
|
||||
}
|
||||
|
||||
// GetKlineHour 获取小时k线数据
|
||||
func (this *Client) GetKlineHour(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKlineHour, code, start, count)
|
||||
return this.GetKline(protocol.TypeKline60Minute, code, start, count)
|
||||
}
|
||||
|
||||
// GetKline60MinuteAll 获取60分钟k线全部数据
|
||||
func (this *Client) GetKline60MinuteAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineAll(protocol.TypeKline60Minute, code)
|
||||
}
|
||||
|
||||
// GetKlineHourAll 获取小时k线全部数据
|
||||
func (this *Client) GetKlineHourAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineAll(protocol.TypeKlineHour, code)
|
||||
return this.GetKlineAll(protocol.TypeKline60Minute, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKline60MinuteUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKline60Minute, code, f)
|
||||
}
|
||||
|
||||
func (this *Client) GetKlineHourUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKline60Minute, code, f)
|
||||
}
|
||||
|
||||
// GetKlineDay 获取日k线数据
|
||||
@@ -382,6 +677,10 @@ func (this *Client) GetKlineDayAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineAll(protocol.TypeKlineDay, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKlineDayUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKlineDay, code, f)
|
||||
}
|
||||
|
||||
// GetKlineWeek 获取周k线数据
|
||||
func (this *Client) GetKlineWeek(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKlineWeek, code, start, count)
|
||||
@@ -392,6 +691,10 @@ func (this *Client) GetKlineWeekAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineAll(protocol.TypeKlineWeek, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKlineWeekUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKlineWeek, code, f)
|
||||
}
|
||||
|
||||
// GetKlineMonth 获取月k线数据
|
||||
func (this *Client) GetKlineMonth(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKlineMonth, code, start, count)
|
||||
@@ -402,6 +705,10 @@ func (this *Client) GetKlineMonthAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineAll(protocol.TypeKlineMonth, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKlineMonthUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKlineMonth, code, f)
|
||||
}
|
||||
|
||||
// GetKlineQuarter 获取季k线数据
|
||||
func (this *Client) GetKlineQuarter(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKlineQuarter, code, start, count)
|
||||
@@ -412,6 +719,10 @@ func (this *Client) GetKlineQuarterAll(code string) (*protocol.KlineResp, error)
|
||||
return this.GetKlineAll(protocol.TypeKlineQuarter, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKlineQuarterUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKlineQuarter, code, f)
|
||||
}
|
||||
|
||||
// GetKlineYear 获取年k线数据
|
||||
func (this *Client) GetKlineYear(code string, start, count uint16) (*protocol.KlineResp, error) {
|
||||
return this.GetKline(protocol.TypeKlineYear, code, start, count)
|
||||
@@ -421,3 +732,7 @@ func (this *Client) GetKlineYear(code string, start, count uint16) (*protocol.Kl
|
||||
func (this *Client) GetKlineYearAll(code string) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineAll(protocol.TypeKlineYear, code)
|
||||
}
|
||||
|
||||
func (this *Client) GetKlineYearUntil(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error) {
|
||||
return this.GetKlineUntil(protocol.TypeKlineYear, code, f)
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
c *Client
|
||||
do func(f func(c *Client))
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
c, err = Dial("124.71.187.122:7709")
|
||||
logs.PanicErr(err)
|
||||
do = func(f func(c *Client)) {
|
||||
f(c)
|
||||
<-c.Done()
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_GetStockHistoryMinuteTrade(t *testing.T) {
|
||||
do(func(c *Client) {
|
||||
resp, err := c.GetHistoryMinuteTrade("20241028", "sz000001", 0, 100)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
for _, v := range resp.List {
|
||||
t.Log(v)
|
||||
}
|
||||
t.Log("总数:", resp.Count)
|
||||
})
|
||||
|
||||
}
|
||||
356
codes.go
Normal file
356
codes.go
Normal file
@@ -0,0 +1,356 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/injoyai/conv"
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"xorm.io/core"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
// DefaultCodes 增加单例,部分数据需要通过Codes里面的信息计算
|
||||
var DefaultCodes *Codes
|
||||
|
||||
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)
|
||||
_ = os.MkdirAll(dir, 0777)
|
||||
|
||||
//连接数据库
|
||||
db, err := xorm.NewEngine("sqlite", filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMapper(core.SameMapper{})
|
||||
db.DB().SetMaxOpenConns(1)
|
||||
if err := db.Sync2(new(CodeModel)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := db.Sync2(new(UpdateModel)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
update := new(UpdateModel)
|
||||
{ //查询或者插入一条数据
|
||||
has, err := db.Get(update)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
if _, err := db.Insert(update); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cc := &Codes{
|
||||
Client: c,
|
||||
db: db,
|
||||
}
|
||||
|
||||
{ //设置定时器,每天早上9点更新数据
|
||||
task := cron.New(cron.WithSeconds())
|
||||
task.AddFunc("10 0 9 * * *", func() {
|
||||
for i := 0; i < 3; i++ {
|
||||
if err := cc.Update(); err == nil {
|
||||
return
|
||||
}
|
||||
logs.Err(err)
|
||||
<-time.After(time.Minute * 5)
|
||||
}
|
||||
})
|
||||
task.Start()
|
||||
}
|
||||
|
||||
{ //判断是否更新过,更新过则不更新
|
||||
now := time.Now()
|
||||
node := time.Date(now.Year(), now.Month(), now.Day(), 9, 0, 0, 0, time.Local)
|
||||
updateTime := time.Unix(update.Time, 0)
|
||||
if now.Sub(node) > 0 {
|
||||
//当前时间在9点之后,且更新时间在9点之前,需要更新
|
||||
if updateTime.Sub(node) < 0 {
|
||||
return cc, cc.Update()
|
||||
}
|
||||
} else {
|
||||
//当前时间在9点之前,且更新时间在上个节点之前
|
||||
if updateTime.Sub(node.Add(time.Hour*24)) < 0 {
|
||||
return cc, cc.Update()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//从缓存中加载
|
||||
return cc, cc.Update(true)
|
||||
}
|
||||
|
||||
type Codes struct {
|
||||
*Client //客户端
|
||||
db *xorm.Engine //数据库实例
|
||||
Map map[string]*CodeModel //股票缓存
|
||||
list []*CodeModel //列表方式缓存
|
||||
exchanges map[string][]string //交易所缓存
|
||||
}
|
||||
|
||||
// GetName 获取股票名称
|
||||
func (this *Codes) GetName(code string) string {
|
||||
if v, ok := this.Map[code]; ok {
|
||||
return v.Name
|
||||
}
|
||||
return "未知"
|
||||
}
|
||||
|
||||
// GetStocks 获取股票代码,sh6xxx sz0xx sz30xx
|
||||
func (this *Codes) GetStocks(limits ...int) []string {
|
||||
limit := conv.Default(-1, limits...)
|
||||
ls := []string(nil)
|
||||
for _, m := range this.list {
|
||||
code := m.FullCode()
|
||||
if protocol.IsStock(code) {
|
||||
ls = append(ls, code)
|
||||
}
|
||||
if limit > 0 && len(ls) >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
return ls
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return this.Map[code]
|
||||
}
|
||||
|
||||
// GetExchange 获取股票交易所,这里的参数不需要带前缀
|
||||
func (this *Codes) GetExchange(code string) protocol.Exchange {
|
||||
if len(code) == 6 {
|
||||
switch {
|
||||
case code[:1] == "6":
|
||||
return protocol.ExchangeSH
|
||||
case code[:1] == "0":
|
||||
return protocol.ExchangeSZ
|
||||
case code[:2] == "30":
|
||||
return protocol.ExchangeSZ
|
||||
}
|
||||
}
|
||||
var exchange string
|
||||
exchanges := this.exchanges[code]
|
||||
if len(exchanges) >= 1 {
|
||||
exchange = exchanges[0]
|
||||
}
|
||||
if len(code) == 8 {
|
||||
exchange = code[0:2]
|
||||
}
|
||||
switch exchange {
|
||||
case protocol.ExchangeSH.String():
|
||||
return protocol.ExchangeSH
|
||||
case protocol.ExchangeSZ.String():
|
||||
return protocol.ExchangeSZ
|
||||
default:
|
||||
return protocol.ExchangeSH
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Codes) AddExchange(code string) string {
|
||||
if exchanges := this.exchanges[code]; len(exchanges) == 1 {
|
||||
return exchanges[0] + code
|
||||
}
|
||||
if len(code) == 6 {
|
||||
switch {
|
||||
case code[:1] == "6":
|
||||
return protocol.ExchangeSH.String() + code
|
||||
case code[:1] == "0":
|
||||
return protocol.ExchangeSZ.String() + code
|
||||
case code[:2] == "30":
|
||||
return protocol.ExchangeSZ.String() + code
|
||||
}
|
||||
return this.GetExchange(code).String() + code
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
// Update 更新数据,从服务器或者数据库
|
||||
func (this *Codes) Update(byDB ...bool) error {
|
||||
codes, err := this.GetCodes(len(byDB) > 0 && byDB[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
codeMap := make(map[string]*CodeModel)
|
||||
exchanges := make(map[string][]string)
|
||||
for _, code := range codes {
|
||||
codeMap[code.Exchange+code.Code] = code
|
||||
exchanges[code.Code] = append(exchanges[code.Code], code.Exchange)
|
||||
}
|
||||
this.Map = codeMap
|
||||
this.list = codes
|
||||
this.exchanges = exchanges
|
||||
//更新时间
|
||||
_, err = this.db.Update(&UpdateModel{Time: time.Now().Unix()})
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCodes 更新股票并返回结果
|
||||
func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
|
||||
|
||||
if this.Client == nil {
|
||||
return nil, errors.New("client is nil")
|
||||
}
|
||||
|
||||
//2. 查询数据库所有股票
|
||||
list := []*CodeModel(nil)
|
||||
if err := this.db.Find(&list); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//如果是从缓存读取,则返回结果
|
||||
if byDatabase {
|
||||
return list, nil
|
||||
}
|
||||
|
||||
mCode := make(map[string]*CodeModel, len(list))
|
||||
for _, v := range list {
|
||||
mCode[v.Code] = v
|
||||
}
|
||||
|
||||
//3. 从服务器获取所有股票代码
|
||||
insert := []*CodeModel(nil)
|
||||
update := []*CodeModel(nil)
|
||||
for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
|
||||
resp, err := this.Client.GetCodeAll(exchange)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range resp.List {
|
||||
if _, ok := mCode[v.Code]; ok {
|
||||
if mCode[v.Code].Name != v.Name {
|
||||
mCode[v.Code].Name = v.Name
|
||||
update = append(update, &CodeModel{
|
||||
Name: v.Name,
|
||||
Code: v.Code,
|
||||
Exchange: exchange.String(),
|
||||
Multiple: v.Multiple,
|
||||
Decimal: v.Decimal,
|
||||
LastPrice: v.LastPrice,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
code := &CodeModel{
|
||||
Name: v.Name,
|
||||
Code: v.Code,
|
||||
Exchange: exchange.String(),
|
||||
Multiple: v.Multiple,
|
||||
Decimal: v.Decimal,
|
||||
LastPrice: v.LastPrice,
|
||||
}
|
||||
insert = append(insert, code)
|
||||
list = append(list, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//4. 插入或者更新数据库
|
||||
err := NewSessionFunc(this.db, func(session *xorm.Session) error {
|
||||
for _, v := range insert {
|
||||
if _, err := session.Insert(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, v := range update {
|
||||
if _, err := session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Cols("Name,LastPrice").Update(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
|
||||
}
|
||||
|
||||
type UpdateModel struct {
|
||||
Time int64 //更新时间
|
||||
}
|
||||
|
||||
func (*UpdateModel) TableName() string {
|
||||
return "update"
|
||||
}
|
||||
|
||||
type CodeModel struct {
|
||||
ID int64 `json:"id"` //主键
|
||||
Name string `json:"name"` //名称,有时候名称会变,例STxxx
|
||||
Code string `json:"code" xorm:"index"` //代码
|
||||
Exchange string `json:"exchange" xorm:"index"` //交易所
|
||||
Multiple uint16 `json:"multiple"` //倍数
|
||||
Decimal int8 `json:"decimal"` //小数位
|
||||
LastPrice float64 `json:"lastPrice"` //昨收价格
|
||||
EditDate int64 `json:"editDate" xorm:"updated"` //修改时间
|
||||
InDate int64 `json:"inDate" xorm:"created"` //创建时间
|
||||
}
|
||||
|
||||
func (*CodeModel) TableName() string {
|
||||
return "codes"
|
||||
}
|
||||
|
||||
func (this *CodeModel) FullCode() string {
|
||||
return this.Exchange + this.Code
|
||||
}
|
||||
|
||||
func (this *CodeModel) Price(p protocol.Price) protocol.Price {
|
||||
return 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 {
|
||||
session := db.NewSession()
|
||||
defer session.Close()
|
||||
if err := session.Begin(); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if err := fn(session); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
if err := session.Commit(); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
33
const.go
33
const.go
@@ -1,33 +0,0 @@
|
||||
package tdx
|
||||
|
||||
var (
|
||||
Hosts = []string{
|
||||
"124.71.187.122",
|
||||
"122.51.120.217",
|
||||
"111.229.247.189",
|
||||
"124.70.199.56",
|
||||
"124.70.176.52",
|
||||
"124.70.133.119",
|
||||
"123.60.84.66",
|
||||
"123.60.73.44",
|
||||
"123.60.70.228",
|
||||
"123.60.186.45",
|
||||
"123.40.164.122",
|
||||
"123.249.15.60",
|
||||
"122.51.232.182",
|
||||
"121.36.81.195",
|
||||
"121.36.54.217",
|
||||
"121.36.225.169",
|
||||
"120.46.186.223",
|
||||
"119.97.185.59",
|
||||
"118.25.98.114",
|
||||
"116.205.183.150",
|
||||
"116.205.171.132",
|
||||
"116.205.163.254",
|
||||
"111.230.186.52",
|
||||
"110.41.4.4",
|
||||
"110.41.2.72",
|
||||
"110.41.154.219",
|
||||
"110.41.147.114",
|
||||
}
|
||||
)
|
||||
59
dial.go
59
dial.go
@@ -3,12 +3,22 @@ package tdx
|
||||
import (
|
||||
"context"
|
||||
"github.com/injoyai/ios"
|
||||
"github.com/injoyai/ios/module/tcp"
|
||||
"github.com/injoyai/logs"
|
||||
"math/rand"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewHostDial(hosts []string, timeout time.Duration) ios.DialFunc {
|
||||
func NewTCPDial(addr string) ios.DialFunc {
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr += ":7709"
|
||||
}
|
||||
return tcp.NewDial(addr)
|
||||
}
|
||||
|
||||
func NewHostDial(hosts []string) ios.DialFunc {
|
||||
if len(hosts) == 0 {
|
||||
hosts = Hosts
|
||||
}
|
||||
@@ -23,7 +33,50 @@ func NewHostDial(hosts []string, timeout time.Duration) ios.DialFunc {
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr += ":7709"
|
||||
}
|
||||
c, err := net.DialTimeout("tcp", addr, timeout)
|
||||
return c, hosts[index], err
|
||||
c, err := net.Dial("tcp", addr)
|
||||
return c, addr, err
|
||||
}
|
||||
}
|
||||
|
||||
func NewRandomDial(hosts []string) ios.DialFunc {
|
||||
if len(hosts) == 0 {
|
||||
hosts = Hosts
|
||||
}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
return func(ctx context.Context) (ios.ReadWriteCloser, string, error) {
|
||||
addr := hosts[r.Intn(len(hosts))]
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr += ":7709"
|
||||
}
|
||||
c, err := net.Dial("tcp", addr)
|
||||
return c, addr, err
|
||||
}
|
||||
}
|
||||
|
||||
func NewRangeDial(hosts []string) ios.DialFunc {
|
||||
if len(hosts) == 0 {
|
||||
hosts = Hosts
|
||||
}
|
||||
return func(ctx context.Context) (c ios.ReadWriteCloser, _ string, err error) {
|
||||
for i, addr := range hosts {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, "", ctx.Err()
|
||||
default:
|
||||
}
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr += ":7709"
|
||||
}
|
||||
c, err = net.Dial("tcp", addr)
|
||||
if err == nil {
|
||||
return c, addr, nil
|
||||
}
|
||||
if i < len(hosts)-1 {
|
||||
//最后一个错误返回出去
|
||||
logs.Err(err, "等待2秒后尝试下一个服务地址...")
|
||||
<-time.After(time.Second * 2)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
22
example/CheckHosts/main.go
Normal file
22
example/CheckHosts/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
for _, v := range tdx.Hosts {
|
||||
|
||||
c, err := tdx.Dial(v)
|
||||
if err != nil {
|
||||
logs.Errf("[%s:7709] %v", v, err)
|
||||
continue
|
||||
}
|
||||
c.Close()
|
||||
logs.Debugf("[%s:7709] 连接成功...\n", v)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
11
example/DialHostsRange/main.go
Normal file
11
example/DialHostsRange/main.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_, err := tdx.DialHostsRange([]string{"1", "2", "127.0.0.1"})
|
||||
logs.PrintErr(err)
|
||||
}
|
||||
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 (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
"github.com/injoyai/tdx/example/common"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c, err := tdx.Dial("124.71.187.122:7709")
|
||||
logs.PanicErr(err)
|
||||
common.Test(func(c *tdx.Client) {
|
||||
resp, err := c.GetCode(protocol.ExchangeSH, 369)
|
||||
logs.PanicErr(err)
|
||||
|
||||
resp, err := c.GetCode(protocol.ExchangeSH, 369)
|
||||
logs.PanicErr(err)
|
||||
|
||||
for i, v := range resp.List {
|
||||
logs.Debug(i, v)
|
||||
}
|
||||
logs.Debug("总数:", resp.Count)
|
||||
|
||||
select {}
|
||||
for i, v := range resp.List {
|
||||
logs.Debug(i, v, v.LastPrice)
|
||||
}
|
||||
logs.Debug("总数:", resp.Count)
|
||||
})
|
||||
}
|
||||
|
||||
22
example/GetHistoryMinute/main.go
Normal file
22
example/GetHistoryMinute/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
"github.com/injoyai/tdx/example/common"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
common.Test(func(c *tdx.Client) {
|
||||
resp, err := c.GetHistoryMinute("20250314", "sz000001")
|
||||
logs.PanicErr(err)
|
||||
|
||||
for _, v := range resp.List {
|
||||
logs.Debug(v)
|
||||
}
|
||||
|
||||
logs.Debug(resp.Count)
|
||||
|
||||
})
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func main() {
|
||||
common.Test(func(c *tdx.Client) {
|
||||
resp, err := c.GetHistoryMinuteTrade("20241025", "sz000001", 0, 20)
|
||||
resp, err := c.GetHistoryMinuteTrade("20250609", "sz000001", 0, 20)
|
||||
logs.PanicErr(err)
|
||||
|
||||
for _, v := range resp.List {
|
||||
20
example/GetIndex/main.go
Normal file
20
example/GetIndex/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.GetIndexDay("sh000001", 0, 10)
|
||||
logs.PanicErr(err)
|
||||
|
||||
for _, v := range resp.List {
|
||||
logs.Debug(v)
|
||||
}
|
||||
|
||||
logs.Debug("总数:", resp.Count)
|
||||
})
|
||||
}
|
||||
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)
|
||||
})
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func main() {
|
||||
common.Test(func(c *tdx.Client) {
|
||||
resp, err := c.GetKlineDay("sz000001", 0, 800)
|
||||
resp, err := c.GetKlineDay("838971", 0, 20)
|
||||
logs.PanicErr(err)
|
||||
|
||||
for _, v := range resp.List {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func main() {
|
||||
common.Test(func(c *tdx.Client) {
|
||||
resp, err := c.GetKlineDayAll("sz000001")
|
||||
resp, err := c.GetKlineDayAll("sz159558")
|
||||
logs.PanicErr(err)
|
||||
|
||||
for _, v := range resp.List {
|
||||
23
example/GetKlineUntil/main.go
Normal file
23
example/GetKlineUntil/main.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
"github.com/injoyai/tdx/example/common"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
common.Test(func(c *tdx.Client) {
|
||||
old := time.Now().Add(-time.Hour * 24 * 17)
|
||||
resp, err := c.GetKlineUntil(protocol.TypeKlineDay, "sz000001", func(k *protocol.Kline) bool {
|
||||
return k.Time.Sub(old) < 0
|
||||
})
|
||||
logs.PanicErr(err)
|
||||
for _, v := range resp.List {
|
||||
logs.Debug(v)
|
||||
}
|
||||
logs.Debug(resp.Count)
|
||||
})
|
||||
}
|
||||
20
example/GetKlineYear/main.go
Normal file
20
example/GetKlineYear/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.GetKlineYear("sz000001", 0, 800)
|
||||
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())
|
||||
logs.PanicErr(err)
|
||||
|
||||
tdx.DefaultCodes, err = tdx.NewCodes(c, "./codes.db")
|
||||
logs.PanicErr(err)
|
||||
|
||||
_ = c
|
||||
|
||||
/*
|
||||
@@ -21,7 +24,7 @@ func main() {
|
||||
b1cb74001c00000000000d005100bd00789c6378c1cecb252ace6066c5b4898987b9050ed1f90cc5b74c18a5bc18c1b43490fecff09c81819191f13fc3c9f3bb169f5e7dfefeb5ef57f7199a305009308208e5b32bb6bcbf70148712002d7f1e13
|
||||
b1cb74000c02000000003e05ac00ac000102020000303030303031601294121a1c2d4eadabcf0ed412aae5fc01afb0024561124fbcc08301afa47900b2e3174100bf68871a4201b741b6144302bb09af334403972e96354504ac09b619560e00000000f8ff601201363030303038b60fba04060607429788a70efa04ada37ab2531c12974d91e7449dbc354184b6010001844bad324102b5679ea1014203a65abd8d0143048a6ba4dd01440587e101b3d2029613000000000000b60f
|
||||
*/
|
||||
resp, err := c.GetQuote("sz000001", "sh600008")
|
||||
resp, err := c.GetQuote("000001", "600000", "159558", "010504")
|
||||
logs.PanicErr(err)
|
||||
|
||||
for _, v := range resp {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
26
example/PullKline/main.go
Normal file
26
example/PullKline/main.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
"github.com/injoyai/tdx/extend"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
m, err := tdx.NewManage(nil)
|
||||
logs.PanicErr(err)
|
||||
|
||||
err = extend.NewPullKline(extend.PullKlineConfig{
|
||||
Codes: []string{"sz000001"},
|
||||
Tables: []string{extend.Year},
|
||||
Dir: filepath.Join(tdx.DefaultDatabaseDir, "kline"),
|
||||
Limit: 1,
|
||||
StartAt: time.Time{},
|
||||
}).Run(context.Background(), m)
|
||||
logs.PanicErr(err)
|
||||
|
||||
}
|
||||
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)
|
||||
|
||||
}
|
||||
19
example/WorkdayAndCodes/main.go
Normal file
19
example/WorkdayAndCodes/main.go
Normal file
@@ -0,0 +1,19 @@
|
||||
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) {
|
||||
|
||||
_, err := tdx.NewWorkday(c, "./workday.db")
|
||||
logs.PanicErr(err)
|
||||
|
||||
_, err = tdx.NewCodes(c, "./codes.db")
|
||||
logs.PanicErr(err)
|
||||
|
||||
})
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
func Test(f func(c *tdx.Client)) {
|
||||
|
||||
//重连方式1,优点,同一个客户端指针
|
||||
c, err := tdx.DialWith(tdx.NewHostDial(tdx.Hosts, 0), tdx.WithDebug())
|
||||
c, err := tdx.DialWith(tdx.NewHostDial(tdx.Hosts), tdx.WithDebug())
|
||||
logs.PanicErr(err)
|
||||
f(c)
|
||||
<-c.Done()
|
||||
|
||||
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(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
|
||||
}
|
||||
309
extend/pull-kline.go
Normal file
309
extend/pull-kline.go
Normal file
@@ -0,0 +1,309 @@
|
||||
package extend
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "github.com/glebarez/go-sqlite"
|
||||
"github.com/injoyai/base/chans"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"time"
|
||||
"xorm.io/core"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
const (
|
||||
Minute = "minute"
|
||||
Minute5 = "5minute"
|
||||
Minute15 = "15minute"
|
||||
Minute30 = "30minute"
|
||||
Hour = "hour"
|
||||
Day = "day"
|
||||
Week = "week"
|
||||
Month = "month"
|
||||
Quarter = "quarter"
|
||||
Year = "year"
|
||||
|
||||
tableMinute = "MinuteKline"
|
||||
table5Minute = "Minute5Kline"
|
||||
table15Minute = "Minute15Kline"
|
||||
table30Minute = "Minute30Kline"
|
||||
tableHour = "HourKline"
|
||||
tableDay = "DayKline"
|
||||
tableWeek = "WeekKline"
|
||||
tableMonth = "MonthKline"
|
||||
tableQuarter = "QuarterKline"
|
||||
tableYear = "YearKline"
|
||||
)
|
||||
|
||||
var (
|
||||
AllKlineType = []string{Minute, Minute5, Minute15, Minute30, Hour, Day, Week, Month, Quarter, Year}
|
||||
KlineTableMap = map[string]*KlineTable{
|
||||
Minute: NewKlineTable(tableMinute, func(c *tdx.Client) KlineHandler { return c.GetKlineMinuteUntil }),
|
||||
Minute5: NewKlineTable(table5Minute, func(c *tdx.Client) KlineHandler { return c.GetKline5MinuteUntil }),
|
||||
Minute15: NewKlineTable(table15Minute, func(c *tdx.Client) KlineHandler { return c.GetKline15MinuteUntil }),
|
||||
Minute30: NewKlineTable(table30Minute, func(c *tdx.Client) KlineHandler { return c.GetKline30MinuteUntil }),
|
||||
Hour: NewKlineTable(tableHour, func(c *tdx.Client) KlineHandler { return c.GetKlineHourUntil }),
|
||||
Day: NewKlineTable(tableDay, func(c *tdx.Client) KlineHandler { return c.GetKlineDayUntil }),
|
||||
Week: NewKlineTable(tableWeek, func(c *tdx.Client) KlineHandler { return c.GetKlineWeekUntil }),
|
||||
Month: NewKlineTable(tableMonth, func(c *tdx.Client) KlineHandler { return c.GetKlineMonthUntil }),
|
||||
Quarter: NewKlineTable(tableQuarter, func(c *tdx.Client) KlineHandler { return c.GetKlineQuarterUntil }),
|
||||
Year: NewKlineTable(tableYear, func(c *tdx.Client) KlineHandler { return c.GetKlineYearUntil }),
|
||||
}
|
||||
)
|
||||
|
||||
type PullKlineConfig struct {
|
||||
Codes []string //操作代码
|
||||
Tables []string //数据类型
|
||||
Dir string //数据位置
|
||||
Limit int //协程数量
|
||||
StartAt time.Time //数据开始时间
|
||||
}
|
||||
|
||||
func NewPullKline(cfg PullKlineConfig) *PullKline {
|
||||
_tables := []*KlineTable(nil)
|
||||
for _, v := range cfg.Tables {
|
||||
_tables = append(_tables, KlineTableMap[v])
|
||||
}
|
||||
if cfg.Limit <= 0 {
|
||||
cfg.Limit = 1
|
||||
}
|
||||
if len(cfg.Dir) == 0 {
|
||||
cfg.Dir = filepath.Join(tdx.DefaultDatabaseDir, "kline")
|
||||
}
|
||||
return &PullKline{
|
||||
tables: _tables,
|
||||
Config: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
type PullKline struct {
|
||||
tables []*KlineTable
|
||||
Config PullKlineConfig
|
||||
}
|
||||
|
||||
func (this *PullKline) Name() string {
|
||||
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 {
|
||||
limit := chans.NewWaitLimit(this.Config.Limit)
|
||||
|
||||
//1. 获取所有股票代码
|
||||
codes := this.Config.Codes
|
||||
if len(codes) == 0 {
|
||||
codes = m.Codes.GetStocks()
|
||||
}
|
||||
|
||||
for _, v := range codes {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
limit.Add()
|
||||
go func(code string) {
|
||||
defer limit.Done()
|
||||
|
||||
_ = os.MkdirAll(this.Config.Dir, 0777)
|
||||
|
||||
//连接数据库
|
||||
db, err := xorm.NewEngine("sqlite", filepath.Join(this.Config.Dir, code+".db"))
|
||||
if err != nil {
|
||||
logs.Err(err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetMapper(core.SameMapper{})
|
||||
db.DB().SetMaxOpenConns(1)
|
||||
|
||||
for _, table := range this.tables {
|
||||
if table == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
logs.PrintErr(db.Sync2(table))
|
||||
|
||||
//2. 获取最后一条数据
|
||||
last := new(Kline)
|
||||
if _, err = db.Table(table).Desc("Date").Get(last); err != nil {
|
||||
logs.Err(err)
|
||||
return
|
||||
}
|
||||
|
||||
//3. 从服务器获取数据
|
||||
insert := Klines{}
|
||||
err = m.Do(func(c *tdx.Client) error {
|
||||
insert, err = this.pull(code, last.Date, table.Handler(c))
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
logs.Err(err)
|
||||
return
|
||||
}
|
||||
|
||||
//4. 插入数据库
|
||||
err = tdx.NewSessionFunc(db, func(session *xorm.Session) error {
|
||||
for i, v := range insert {
|
||||
if i == 0 {
|
||||
if _, err := session.Table(table).Where("Date >= ?", v.Date).Delete(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := session.Table(table).Insert(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
logs.PrintErr(err)
|
||||
|
||||
}
|
||||
|
||||
}(v)
|
||||
}
|
||||
limit.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *PullKline) pull(code string, lastDate int64, f func(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error)) (Klines, error) {
|
||||
|
||||
if lastDate == 0 {
|
||||
lastDate = protocol.ExchangeEstablish.Unix()
|
||||
}
|
||||
|
||||
resp, err := f(code, func(k *protocol.Kline) bool {
|
||||
return k.Time.Unix() <= lastDate || k.Time.Unix() <= this.Config.StartAt.Unix()
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ks := Klines{}
|
||||
for _, v := range resp.List {
|
||||
ks = append(ks, &Kline{
|
||||
Code: code,
|
||||
Date: v.Time.Unix(),
|
||||
Open: v.Open,
|
||||
High: v.High,
|
||||
Low: v.Low,
|
||||
Close: v.Close,
|
||||
Volume: v.Volume,
|
||||
Amount: v.Amount,
|
||||
})
|
||||
}
|
||||
|
||||
return ks, nil
|
||||
}
|
||||
|
||||
type Kline struct {
|
||||
Code string `json:"code"` //代码
|
||||
Date int64 `json:"date"` //时间节点 2006-01-02 15:00
|
||||
Open protocol.Price `json:"open"` //开盘价
|
||||
High protocol.Price `json:"high"` //最高价
|
||||
Low protocol.Price `json:"low"` //最低价
|
||||
Close protocol.Price `json:"close"` //收盘价
|
||||
Volume int64 `json:"volume"` //成交量
|
||||
Amount protocol.Price `json:"amount"` //成交额
|
||||
InDate int64 `json:"inDate" xorm:"created"` //创建时间
|
||||
}
|
||||
|
||||
type Klines []*Kline
|
||||
|
||||
func (this Klines) Less(i, j int) bool { return this[i].Code > this[j].Code }
|
||||
|
||||
func (this Klines) Swap(i, j int) { this[i], this[j] = this[j], this[i] }
|
||||
|
||||
func (this Klines) Len() int { return len(this) }
|
||||
|
||||
func (this Klines) Sort() { sort.Sort(this) }
|
||||
|
||||
// Kline 计算多个K线,成一个K线
|
||||
func (this Klines) Kline() *Kline {
|
||||
if this == nil {
|
||||
return new(Kline)
|
||||
}
|
||||
k := new(Kline)
|
||||
for i, v := range this {
|
||||
switch i {
|
||||
case 0:
|
||||
k.Open = v.Open
|
||||
k.High = v.High
|
||||
k.Low = v.Low
|
||||
k.Close = v.Close
|
||||
case len(this) - 1:
|
||||
k.Close = v.Close
|
||||
k.Date = v.Date
|
||||
}
|
||||
if v.High > k.High {
|
||||
k.High = v.High
|
||||
}
|
||||
if v.Low < k.Low {
|
||||
k.Low = v.Low
|
||||
}
|
||||
k.Volume += v.Volume
|
||||
k.Amount += v.Amount
|
||||
}
|
||||
|
||||
return k
|
||||
}
|
||||
|
||||
// Merge 合并K线
|
||||
func (this Klines) Merge(n int) Klines {
|
||||
if this == nil {
|
||||
return nil
|
||||
}
|
||||
ks := []*Kline(nil)
|
||||
for i := 0; i < len(this); i += n {
|
||||
if i+n > len(this) {
|
||||
ks = append(ks, this[i:].Kline())
|
||||
} else {
|
||||
ks = append(ks, this[i:i+n].Kline())
|
||||
}
|
||||
}
|
||||
return ks
|
||||
}
|
||||
|
||||
type KlineHandler func(code string, f func(k *protocol.Kline) bool) (*protocol.KlineResp, error)
|
||||
|
||||
func NewKlineTable(tableName string, handler func(c *tdx.Client) KlineHandler) *KlineTable {
|
||||
return &KlineTable{
|
||||
tableName: tableName,
|
||||
Handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
type KlineTable struct {
|
||||
Kline `xorm:"extends"`
|
||||
tableName string
|
||||
Handler func(c *tdx.Client) KlineHandler `xorm:"-"`
|
||||
}
|
||||
|
||||
func (this *KlineTable) TableName() string {
|
||||
return this.tableName
|
||||
}
|
||||
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.TradeResp
|
||||
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)
|
||||
}
|
||||
172
extend/spider-ths.go
Normal file
172
extend/spider-ths.go
Normal file
@@ -0,0 +1,172 @@
|
||||
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"])
|
||||
sortYears := conv.Interfaces(m["sortYear"])
|
||||
priceFactor := conv.Float64(m["priceFactor"])
|
||||
prices := strings.Split(conv.String(m["price"]), ",")
|
||||
dates := strings.Split(conv.String(m["dates"]), ",")
|
||||
volumes := strings.Split(conv.String(m["volumn"]), ",")
|
||||
|
||||
//好像到了22点,总数量会比实际多1
|
||||
if total == len(dates)+1 && total == len(volumes)+1 {
|
||||
total -= 1
|
||||
}
|
||||
//判断数量是否对应
|
||||
if total*4 != len(prices) || total != len(dates) || total != len(volumes) {
|
||||
return nil, fmt.Errorf("total=%d prices=%d dates=%d volumns=%d", total, len(prices), len(dates), len(volumes))
|
||||
}
|
||||
|
||||
mYear := make(map[int][]string)
|
||||
index := 0
|
||||
for i, v := range sortYears {
|
||||
if ls := conv.Ints(v); len(ls) == 2 {
|
||||
year := conv.Int(ls[0])
|
||||
length := conv.Int(ls[1])
|
||||
if i == len(sortYears)-1 {
|
||||
mYear[year] = dates[index:]
|
||||
break
|
||||
}
|
||||
mYear[year] = dates[index : index+length]
|
||||
index += length
|
||||
}
|
||||
}
|
||||
|
||||
ls := []*Kline(nil)
|
||||
i := 0
|
||||
nowYear := time.Now().Year()
|
||||
for year := 1990; year <= nowYear; year++ {
|
||||
for _, d := range mYear[year] {
|
||||
x, err := time.Parse("0102", d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x = time.Date(year, x.Month(), x.Day(), 15, 0, 0, 0, time.Local)
|
||||
low := protocol.Price(conv.Float64(prices[i*4+0]) * 1000 / priceFactor)
|
||||
ls = append(ls, &Kline{
|
||||
Code: protocol.AddPrefix(code),
|
||||
Date: x.Unix(),
|
||||
Open: protocol.Price(conv.Float64(prices[i*4+1])*1000/priceFactor) + low,
|
||||
High: protocol.Price(conv.Float64(prices[i*4+2])*1000/priceFactor) + low,
|
||||
Low: low,
|
||||
Close: protocol.Price(conv.Float64(prices[i*4+3])*1000/priceFactor) + low,
|
||||
Volume: (conv.Int64(volumes[i]) + 50) / 100,
|
||||
})
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return ls, nil
|
||||
}
|
||||
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
|
||||
}
|
||||
33
go.mod
33
go.mod
@@ -3,25 +3,40 @@ module github.com/injoyai/tdx
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/injoyai/base v1.0.18
|
||||
github.com/injoyai/conv v1.1.10
|
||||
github.com/injoyai/ios v0.0.4
|
||||
github.com/injoyai/logs v1.0.9
|
||||
github.com/glebarez/go-sqlite v1.22.0
|
||||
github.com/go-sql-driver/mysql v1.7.0
|
||||
github.com/injoyai/base v1.2.17
|
||||
github.com/injoyai/conv v1.2.5
|
||||
github.com/injoyai/ios v1.2.2
|
||||
github.com/injoyai/logs v1.0.12
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
golang.org/x/text v0.16.0
|
||||
xorm.io/core v0.7.3
|
||||
xorm.io/xorm v1.3.9
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/fatih/color v1.14.1 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
github.com/goccy/go-json v0.8.1 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/uuid v1.5.0 // indirect
|
||||
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-isatty v0.0.17 // 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/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
|
||||
github.com/satori/go.uuid v1.2.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/stretchr/testify v1.9.0 // indirect
|
||||
golang.org/x/sys v0.22.0 // indirect
|
||||
github.com/syndtr/goleveldb v1.0.0 // indirect
|
||||
golang.org/x/sys v0.25.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
modernc.org/libc v1.37.6 // indirect
|
||||
modernc.org/mathutil v1.6.0 // indirect
|
||||
modernc.org/memory v1.7.2 // indirect
|
||||
modernc.org/sqlite v1.28.0 // indirect
|
||||
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
|
||||
)
|
||||
|
||||
132
go.sum
132
go.sum
@@ -1,77 +1,135 @@
|
||||
gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s=
|
||||
gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
|
||||
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ=
|
||||
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
|
||||
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
|
||||
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/goccy/go-json v0.8.1 h1:4/Wjm0JIJaTDm8K1KcGrLHJoa8EsJ13YWeX+6Kfq6uI=
|
||||
github.com/goccy/go-json v0.8.1/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
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/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/injoyai/base v1.0.18 h1:AMB1NhPe9acvMp04sThE8Ji/SRS1/MHsx19NmSoD8LA=
|
||||
github.com/injoyai/base v1.0.18/go.mod h1:2JSv8ndJ/FmKyazfhDBewzylEbVfJV0EE/r6kOvnzwU=
|
||||
github.com/injoyai/conv v1.1.5/go.mod h1:PYoJcbqaz4eyQUovzErRFCuDSPGB4L07AvdmFsTrTew=
|
||||
github.com/injoyai/conv v1.1.10 h1:LefmOA4SKTEw6gGHXZ6UT8n2K+OzCttYSW8lVmvgQd8=
|
||||
github.com/injoyai/conv v1.1.10/go.mod h1:PYoJcbqaz4eyQUovzErRFCuDSPGB4L07AvdmFsTrTew=
|
||||
github.com/injoyai/ios v0.0.4 h1:yEZ6wN5uCSjAJB4qwpT6R77aMtjkblZo4giKwu9/s7Y=
|
||||
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/go.mod h1:CLchJCGhb39Obyrci816R+KMtbxZhgPs0FuikhyixK4=
|
||||
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/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/injoyai/base v1.2.15 h1:K/ysPqZl7vgNUAz/jpG1IdDpzdSMWvUfoJL+1gPdM9g=
|
||||
github.com/injoyai/base v1.2.15/go.mod h1:NfCQjml3z2pCvQ3J3YcOXtecqXD0xVPKjo4YTsMLhr8=
|
||||
github.com/injoyai/base v1.2.17 h1:+qYeCSeEMWgmTla+LBC0Ozan9ysS4mV0ne5nfMt9opU=
|
||||
github.com/injoyai/base v1.2.17/go.mod h1:NfCQjml3z2pCvQ3J3YcOXtecqXD0xVPKjo4YTsMLhr8=
|
||||
github.com/injoyai/conv v1.2.5 h1:G4OCyF0NTZul5W1u9IgXDOhW4/zmIigdPKXFHQGmv1M=
|
||||
github.com/injoyai/conv v1.2.5/go.mod h1:s05l3fQJQ4mT4VX+KIdbvCWQB0YzZHprmUfUu2uxd1k=
|
||||
github.com/injoyai/ios v1.2.2 h1:fAPWBL6t22DiE2ZEpBgf5bzyVQTcm2ZhLMkM+JFPhZA=
|
||||
github.com/injoyai/ios v1.2.2/go.mod h1:DJVJGQFQvqF80CeJVabFOm6AKilqc/m8MFvz39Uy5ow=
|
||||
github.com/injoyai/logs v1.0.12 h1:f7syIGZMTg9ZzhJhdd3tzaPdxkMhdKsncGaxljqIiYE=
|
||||
github.com/injoyai/logs v1.0.12/go.mod h1:+dKEL6GvaFqqVRatqUBiCicJbZnAgtj7hVs824Src4s=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
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/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
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/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
|
||||
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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||
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/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/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
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.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
|
||||
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
||||
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-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
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=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
|
||||
google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
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/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||
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/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
lukechampine.com/uint128 v1.3.0 h1:cDdUVfRwDUDovz610ABgFD17nXD4/uDgVHl2sC3+sbo=
|
||||
modernc.org/cc/v3 v3.41.0 h1:QoR1Sn3YWlmA1T4vLaKZfawdVtSiGx8H+cEojbC7v1Q=
|
||||
modernc.org/ccgo/v3 v3.16.15 h1:KbDR3ZAVU+wiLyMESPtbtE/Add4elztFyfsWoNTgxS0=
|
||||
modernc.org/libc v1.37.6 h1:orZH3c5wmhIQFTXF+Nt+eeauyd+ZIt2BX6ARe+kD+aw=
|
||||
modernc.org/libc v1.37.6/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
|
||||
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
||||
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
|
||||
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
|
||||
modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
|
||||
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
|
||||
modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ=
|
||||
modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0=
|
||||
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 h1:bvLlAPW1ZMTWA32LuZMBEGHAUOcATZjzHcotf3SWweM=
|
||||
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
|
||||
xorm.io/core v0.7.3 h1:W8ws1PlrnkS1CZU1YWaYLMQcQilwAmQXU0BJDJon+H0=
|
||||
xorm.io/core v0.7.3/go.mod h1:jJfd0UAEzZ4t87nbQYtVjmqpIODugN6PD2D9E+dJvdM=
|
||||
xorm.io/xorm v1.3.9 h1:TUovzS0ko+IQ1XnNLfs5dqK1cJl1H5uHpWbWqAQ04nU=
|
||||
xorm.io/xorm v1.3.9/go.mod h1:LsCCffeeYp63ssk0pKumP6l96WZcHix7ChpurcLNuMw=
|
||||
|
||||
119
hosts.go
Normal file
119
hosts.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"github.com/injoyai/base/types"
|
||||
"github.com/injoyai/logs"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
// Hosts 所有服务器地址(2024-11-30测试通过)
|
||||
Hosts = func() []string {
|
||||
lenSH := len(SHHosts)
|
||||
lenBJ := len(BJHosts)
|
||||
lenGZ := len(GZHosts)
|
||||
lenWH := len(WHHosts)
|
||||
|
||||
ls := make([]string, lenSH+lenBJ+lenGZ+lenWH)
|
||||
copy(ls[:lenSH], SHHosts)
|
||||
copy(ls[lenSH:lenSH+lenBJ], BJHosts)
|
||||
copy(ls[lenSH+lenBJ:lenSH+lenBJ+lenGZ], GZHosts)
|
||||
copy(ls[lenSH+lenBJ+lenGZ:lenSH+lenBJ+lenGZ+lenWH], WHHosts)
|
||||
return ls
|
||||
}()
|
||||
|
||||
// SHHosts 上海服务器地址
|
||||
SHHosts = []string{
|
||||
"124.71.187.122", //华为
|
||||
"122.51.120.217", //腾讯
|
||||
"111.229.247.189", //腾讯
|
||||
"124.70.176.52", //华为
|
||||
"123.60.186.45", //华为
|
||||
"122.51.232.182", //腾讯
|
||||
"118.25.98.114", //腾讯
|
||||
"124.70.199.56", //华为
|
||||
"121.36.225.169", //华为
|
||||
"123.60.70.228", //华为
|
||||
"123.60.73.44", //华为
|
||||
"124.70.133.119", //华为
|
||||
"124.71.187.72", //华为
|
||||
"123.60.84.66", //华为
|
||||
}
|
||||
|
||||
// BJHosts 北京服务器地址
|
||||
BJHosts = []string{
|
||||
"121.36.54.217", //华为
|
||||
"121.36.81.195", //华为
|
||||
"123.249.15.60", //华为
|
||||
"124.70.75.113", //华为
|
||||
"120.46.186.223", //华为
|
||||
"124.70.22.210", //华为
|
||||
"139.9.133.247", //华为
|
||||
}
|
||||
|
||||
// GZHosts 广州服务器地址,客户端上可能显示深圳
|
||||
GZHosts = []string{
|
||||
"124.71.85.110", //华为
|
||||
"139.9.51.18", //华为
|
||||
"139.159.239.163", //华为
|
||||
"124.71.9.153", //华为
|
||||
"116.205.163.254", //华为
|
||||
"116.205.171.132", //华为
|
||||
"116.205.183.150", //华为
|
||||
"111.230.186.52", //腾讯
|
||||
"110.41.4.4", //华为
|
||||
"110.41.2.72", //华为
|
||||
"110.41.154.219", //华为
|
||||
"110.41.147.114", //华为,这个客户端显示深圳线路1,IP查询是广州的
|
||||
}
|
||||
|
||||
// WHHosts 武汉服务器地址
|
||||
WHHosts = []string{
|
||||
"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
|
||||
}
|
||||
100
manage.go
Normal file
100
manage.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"github.com/injoyai/ios/client"
|
||||
"github.com/robfig/cron/v3"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultDatabaseDir = "./data/database"
|
||||
)
|
||||
|
||||
func NewManage(cfg *ManageConfig, op ...client.Option) (*Manage, error) {
|
||||
//初始化配置
|
||||
if cfg == nil {
|
||||
cfg = &ManageConfig{}
|
||||
}
|
||||
if cfg.CodesFilename == "" {
|
||||
cfg.CodesFilename = DefaultDatabaseDir + "/codes.db"
|
||||
}
|
||||
if cfg.WorkdayFileName == "" {
|
||||
cfg.WorkdayFileName = DefaultDatabaseDir + "/workday.db"
|
||||
}
|
||||
if cfg.Dial == nil {
|
||||
cfg.Dial = DialDefault
|
||||
}
|
||||
|
||||
//通用客户端
|
||||
commonClient, err := cfg.Dial(op...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commonClient.Wait.SetTimeout(time.Second * 5)
|
||||
|
||||
//代码管理
|
||||
codes, err := NewCodes(commonClient, cfg.CodesFilename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//工作日管理
|
||||
workday, err := NewWorkday(commonClient, cfg.WorkdayFileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//连接池
|
||||
p, err := NewPool(func() (*Client, error) {
|
||||
return cfg.Dial(op...)
|
||||
}, cfg.Number)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Manage{
|
||||
Pool: p,
|
||||
Config: cfg,
|
||||
Codes: codes,
|
||||
Workday: workday,
|
||||
Cron: cron.New(cron.WithSeconds()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Manage struct {
|
||||
*Pool
|
||||
Config *ManageConfig
|
||||
Codes *Codes
|
||||
Workday *Workday
|
||||
Cron *cron.Cron
|
||||
}
|
||||
|
||||
// RangeStocks 遍历所有股票
|
||||
func (this *Manage) RangeStocks(f func(code string)) {
|
||||
for _, v := range this.Codes.GetStocks() {
|
||||
f(v)
|
||||
}
|
||||
}
|
||||
|
||||
// RangeETFs 遍历所有ETF
|
||||
func (this *Manage) RangeETFs(f func(code string)) {
|
||||
for _, v := range this.Codes.GetETFs() {
|
||||
f(v)
|
||||
}
|
||||
}
|
||||
|
||||
// AddWorkdayTask 添加工作日任务
|
||||
func (this *Manage) AddWorkdayTask(spec string, f func(m *Manage)) {
|
||||
this.Cron.AddFunc(spec, func() {
|
||||
if this.Workday.TodayIs() {
|
||||
f(this)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type ManageConfig struct {
|
||||
Number int //客户端数量
|
||||
CodesFilename string //代码数据库位置
|
||||
WorkdayFileName string //工作日数据库位置
|
||||
Dial func(op ...client.Option) (cli *Client, err error) //默认连接方式
|
||||
}
|
||||
76
pool.go
Normal file
76
pool.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/injoyai/base/safe"
|
||||
)
|
||||
|
||||
// NewPool 简易版本的连接池
|
||||
func NewPool(dial func() (*Client, error), number int) (*Pool, error) {
|
||||
if number <= 0 {
|
||||
number = 1
|
||||
}
|
||||
ch := make(chan *Client, number)
|
||||
p := &Pool{
|
||||
ch: ch,
|
||||
Closer: safe.NewCloser().SetCloseFunc(func(err error) error {
|
||||
close(ch)
|
||||
return nil
|
||||
}),
|
||||
}
|
||||
for i := 0; i < number; i++ {
|
||||
c, err := dial()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.ch <- c
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
type Pool struct {
|
||||
ch chan *Client
|
||||
*safe.Closer
|
||||
}
|
||||
|
||||
func (this *Pool) Get() (*Client, error) {
|
||||
select {
|
||||
case <-this.Done():
|
||||
return nil, this.Err()
|
||||
case c, ok := <-this.ch:
|
||||
if !ok {
|
||||
return nil, errors.New("已关闭")
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Pool) Put(c *Client) {
|
||||
select {
|
||||
case <-this.Done():
|
||||
c.Close()
|
||||
return
|
||||
case this.ch <- c:
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Pool) Do(fn func(c *Client) error) error {
|
||||
c, err := this.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer this.Put(c)
|
||||
return fn(c)
|
||||
}
|
||||
|
||||
func (this *Pool) Go(fn func(c *Client)) error {
|
||||
c, err := this.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func(c *Client) {
|
||||
defer this.Put(c)
|
||||
fn(c)
|
||||
}(c)
|
||||
return nil
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package protocol
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
TypeConnect = 0x000D //建立连接
|
||||
TypeHeart = 0x0004 //心跳
|
||||
@@ -8,10 +10,16 @@ const (
|
||||
TypeQuote = 0x053E //行情信息
|
||||
TypeMinute = 0x051D //分时数据
|
||||
TypeMinuteTrade = 0x0FC5 //分时交易
|
||||
TypeHistoryMinute = 0x0FB4 //历史分时数据
|
||||
TypeHistoryMinuteTrade = 0x0FB5 //历史分时交易
|
||||
TypeKline = 0x052D //K线图
|
||||
)
|
||||
|
||||
var (
|
||||
// ExchangeEstablish 交易所成立时间
|
||||
ExchangeEstablish = time.Date(1990, 12, 19, 0, 0, 0, 0, time.Local)
|
||||
)
|
||||
|
||||
/*
|
||||
从其他地方复制
|
||||
const (
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/injoyai/base/bytes"
|
||||
"github.com/injoyai/base/g"
|
||||
"github.com/injoyai/base/types"
|
||||
"github.com/injoyai/conv"
|
||||
"io"
|
||||
)
|
||||
@@ -19,7 +19,7 @@ const (
|
||||
)
|
||||
|
||||
type Message interface {
|
||||
Bytes() g.Bytes
|
||||
Bytes() types.Bytes
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -39,7 +39,20 @@ type Frame struct {
|
||||
Data []byte //数据
|
||||
}
|
||||
|
||||
func (this *Frame) Bytes() g.Bytes {
|
||||
/*
|
||||
Bytes
|
||||
|
||||
0c00000000011c001c002d0500003030303030310900010000000a0000000000000000000000
|
||||
|
||||
Prefix: 0c
|
||||
MsgID: 0208d301
|
||||
Control: 01
|
||||
Length: 1c00
|
||||
Length: 1c00
|
||||
Type: 2d05
|
||||
000030303030303104000100a401a40100000000000000000000
|
||||
*/
|
||||
func (this *Frame) Bytes() types.Bytes {
|
||||
length := uint16(len(this.Data) + 2)
|
||||
data := make([]byte, 12+len(this.Data))
|
||||
data[0] = Prefix
|
||||
|
||||
@@ -11,11 +11,11 @@ type CodeResp struct {
|
||||
}
|
||||
|
||||
type Code struct {
|
||||
Name string //股票名称
|
||||
Code string //股票代码
|
||||
Multiple uint16 //倍数,基本是0x64=100
|
||||
Decimal int8 //小数点,基本是2
|
||||
PreClose float64 //未知
|
||||
Name string //股票名称
|
||||
Code string //股票代码
|
||||
Multiple uint16 //倍数,基本是0x64=100
|
||||
Decimal int8 //小数点,基本是2
|
||||
LastPrice float64 //昨收价格,单位元,对个股无效,对指数有效,对其他未知
|
||||
}
|
||||
|
||||
func (this *Code) String() string {
|
||||
@@ -45,11 +45,11 @@ func (code) Decode(bs []byte) (*CodeResp, error) {
|
||||
|
||||
for i := uint16(0); i < resp.Count; i++ {
|
||||
sec := &Code{
|
||||
Code: string(bs[:6]),
|
||||
Multiple: Uint16(bs[6:8]),
|
||||
Name: string(UTF8ToGBK(bs[8:16])),
|
||||
Decimal: int8(bs[20]),
|
||||
PreClose: getVolume(Uint32(bs[21:25])),
|
||||
Code: string(bs[:6]),
|
||||
Multiple: Uint16(bs[6:8]),
|
||||
Name: string(UTF8ToGBK(bs[8:16])),
|
||||
Decimal: int8(bs[20]),
|
||||
LastPrice: getVolume2(Uint32(bs[21:25])),
|
||||
}
|
||||
//logs.Debug(bs[25:29]) //26和28字节 好像是枚举(基本是44,45和34,35)
|
||||
bs = bs[29:]
|
||||
|
||||
@@ -5,15 +5,16 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
MConnect = connect{}
|
||||
MHeart = heart{}
|
||||
MCount = count{}
|
||||
MQuote = quote{}
|
||||
MCode = code{}
|
||||
MMinute = minute{}
|
||||
MMinuteTrade = minuteTrade{}
|
||||
MHistoryMinuteTrade = historyMinuteTrade{}
|
||||
MKline = kline{}
|
||||
MConnect = connect{}
|
||||
MHeart = heart{}
|
||||
MCount = count{}
|
||||
MQuote = quote{}
|
||||
MCode = code{}
|
||||
MMinute = minute{}
|
||||
MHistoryMinute = historyMinute{}
|
||||
MTrade = trade{}
|
||||
MHistoryTrade = historyTrade{}
|
||||
MKline = kline{}
|
||||
)
|
||||
|
||||
type ConnectResp struct {
|
||||
|
||||
64
protocol/model_history_minute.go
Normal file
64
protocol/model_history_minute.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/injoyai/conv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type historyMinute struct{}
|
||||
|
||||
func (this historyMinute) Frame(date, code string) (*Frame, error) {
|
||||
exchange, number, err := DecodeCode(code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dataBs := Bytes(conv.Uint32(date))
|
||||
dataBs = append(dataBs, exchange.Uint8())
|
||||
dataBs = append(dataBs, []byte(number)...)
|
||||
return &Frame{
|
||||
Control: Control01,
|
||||
Type: TypeHistoryMinute,
|
||||
Data: dataBs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (this historyMinute) Decode(bs []byte) (*MinuteResp, error) {
|
||||
|
||||
if len(bs) < 6 {
|
||||
return nil, errors.New("数据长度不足")
|
||||
}
|
||||
|
||||
resp := &MinuteResp{
|
||||
Count: Uint16(bs[:2]),
|
||||
}
|
||||
|
||||
multiple := Price(1) * 10
|
||||
//if bs[5] > 0x40 {
|
||||
//multiple = 10
|
||||
//}
|
||||
|
||||
//2-4字节是啥?
|
||||
bs = bs[6:]
|
||||
|
||||
lastPrice := Price(0)
|
||||
t := time.Date(0, 0, 0, 9, 30, 0, 0, time.Local)
|
||||
for i := uint16(0); i < resp.Count; i++ {
|
||||
var price Price
|
||||
bs, price = GetPrice(bs)
|
||||
bs, _ = GetPrice(bs) //这个是什么
|
||||
lastPrice += price
|
||||
var number int
|
||||
bs, number = CutInt(bs)
|
||||
|
||||
if i == 120 {
|
||||
t = t.Add(time.Minute * 90)
|
||||
}
|
||||
resp.List = append(resp.List, PriceNumber{
|
||||
Time: t.Add(time.Minute * time.Duration(i+1)).Format("15:04"),
|
||||
Price: lastPrice * multiple,
|
||||
Number: number,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
@@ -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无效(汇总出现)
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
}
|
||||
67
protocol/model_history_trade.go
Normal file
67
protocol/model_history_trade.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/injoyai/conv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HistoryTradeResp 兼容之前的版本
|
||||
type HistoryTradeResp = TradeResp
|
||||
|
||||
type historyTrade struct{}
|
||||
|
||||
func (historyTrade) Frame(date, code string, start, count uint16) (*Frame, error) {
|
||||
exchange, number, err := DecodeCode(code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dataBs := Bytes(conv.Uint32(date)) //req.Time.Format("20060102"))
|
||||
dataBs = append(dataBs, exchange.Uint8(), 0x0)
|
||||
dataBs = append(dataBs, []byte(number)...)
|
||||
dataBs = append(dataBs, Bytes(start)...)
|
||||
dataBs = append(dataBs, Bytes(count)...)
|
||||
return &Frame{
|
||||
Control: Control01,
|
||||
Type: TypeHistoryMinuteTrade,
|
||||
Data: dataBs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (historyTrade) Decode(bs []byte, c TradeCache) (*TradeResp, error) {
|
||||
if len(bs) < 2 {
|
||||
return nil, errors.New("数据长度不足")
|
||||
}
|
||||
|
||||
_, number, err := DecodeCode(c.Code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &TradeResp{
|
||||
Count: Uint16(bs[:2]),
|
||||
}
|
||||
|
||||
//第2-6字节不知道是啥
|
||||
bs = bs[2+4:]
|
||||
|
||||
lastPrice := Price(0)
|
||||
for i := uint16(0); i < resp.Count; i++ {
|
||||
timeStr := GetHourMinute([2]byte(bs[:2]))
|
||||
t, err := time.Parse("2006010215:04", c.Date+timeStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mt := &Trade{Time: t}
|
||||
var sub Price
|
||||
bs, sub = GetPrice(bs[2:])
|
||||
lastPrice += sub * 10 //把分转成厘
|
||||
mt.Price = lastPrice / basePrice(number)
|
||||
bs, mt.Volume = CutInt(bs)
|
||||
bs, mt.Status = CutInt(bs)
|
||||
bs, _ = CutInt(bs) //这个得到的是0,不知道是啥
|
||||
resp.List = append(resp.List, mt)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
func Test_stockHistoryMinuteTrade_Frame(t *testing.T) {
|
||||
// 预期 0c 02000000 00 1200 1200 b50f 84da3401 0000 30303030303100006400
|
||||
// 0c000000000112001200b50f84da3401000030303030303100006400
|
||||
f, err := MHistoryMinuteTrade.Frame("20241028", "sz000001", 0, 100)
|
||||
f, err := MHistoryTrade.Frame("20241028", "sz000001", 0, 100)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
@@ -3,7 +3,9 @@ package protocol
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/injoyai/base/g"
|
||||
"github.com/injoyai/base/types"
|
||||
"github.com/injoyai/conv"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -14,7 +16,7 @@ type KlineReq struct {
|
||||
Count uint16
|
||||
}
|
||||
|
||||
func (this *KlineReq) Bytes(Type uint8) (g.Bytes, error) {
|
||||
func (this *KlineReq) Bytes(Type uint8) (types.Bytes, error) {
|
||||
if this.Count > 800 {
|
||||
return nil, errors.New("单次数量不能超过800")
|
||||
}
|
||||
@@ -37,22 +39,25 @@ type KlineResp struct {
|
||||
}
|
||||
|
||||
type Kline struct {
|
||||
Last Price //昨日收盘价,这个是列表的上一条数据的收盘价,如果没有上条数据,那么这个值为0
|
||||
Open Price //开盘价
|
||||
High Price //最高价
|
||||
Low Price //最低价
|
||||
Close Price //收盘价,如果是当天,则是最新价/实时价
|
||||
Volume int64 //成交量
|
||||
Amount Price //成交额
|
||||
Time time.Time //时间
|
||||
Last Price //昨日收盘价,这个是列表的上一条数据的收盘价,如果没有上条数据,那么这个值为0
|
||||
Open Price //开盘价
|
||||
High Price //最高价
|
||||
Low Price //最低价
|
||||
Close Price //收盘价,如果是当天,则是最新价/实时价
|
||||
Volume int64 //成交量
|
||||
Amount Price //成交额
|
||||
Time time.Time //时间
|
||||
UpCount int //上涨数量,指数有效
|
||||
DownCount int //下跌数量,指数有效
|
||||
}
|
||||
|
||||
func (this *Kline) String() string {
|
||||
return fmt.Sprintf("%s 昨收盘:%s 开盘价:%s 最高价:%s 最低价:%s 收盘价:%s 涨跌:%s 涨跌幅:%0.2f 成交量:%s 成交额:%s",
|
||||
return fmt.Sprintf("%s 昨收盘:%.3f 开盘价:%.3f 最高价:%.3f 最低价:%.3f 收盘价:%.3f 涨跌:%s 涨跌幅:%0.2f 成交量:%s 成交额:%s 涨跌数: %d/%d",
|
||||
this.Time.Format("2006-01-02 15:04:05"),
|
||||
this.Last, this.Open, this.High, this.Low, this.Close,
|
||||
this.Last.Float64(), this.Open.Float64(), this.High.Float64(), this.Low.Float64(), this.Close.Float64(),
|
||||
this.RisePrice(), this.RiseRate(),
|
||||
Int64UnitString(this.Volume), FloatUnitString(this.Amount.Float64()),
|
||||
this.UpCount, this.DownCount,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -73,11 +78,35 @@ func (this *Kline) RisePrice() Price {
|
||||
|
||||
// RiseRate 涨跌比例/涨跌幅,第一个数据不准,仅做参考
|
||||
func (this *Kline) RiseRate() float64 {
|
||||
return float64(this.RisePrice()) / float64(this.Open) * 100
|
||||
if this.Last == 0 {
|
||||
return float64(this.Close-this.Open) / float64(this.Open) * 100
|
||||
}
|
||||
return float64(this.Close-this.Last) / float64(this.Last) * 100
|
||||
}
|
||||
|
||||
type kline struct{}
|
||||
|
||||
/*
|
||||
Frame
|
||||
Prefix: 0c
|
||||
MsgID: 0208d301
|
||||
Control: 01
|
||||
Length: 1c00
|
||||
Length: 1c00
|
||||
Type: 2d05
|
||||
Data: 000030303030303104000100a401a40100000000000000000000
|
||||
|
||||
Data:
|
||||
Exchange: 00
|
||||
Unknown: 00
|
||||
Code: 303030303031
|
||||
Type: 04
|
||||
Unknown: 00
|
||||
Unknown: 0100
|
||||
Start: a401
|
||||
Count: a401
|
||||
Append: 00000000000000000000
|
||||
*/
|
||||
func (kline) Frame(Type uint8, code string, start, count uint16) (*Frame, error) {
|
||||
if count > 800 {
|
||||
return nil, errors.New("单次数量不能超过800")
|
||||
@@ -103,22 +132,20 @@ func (kline) Frame(Type uint8, code string, start, count uint16) (*Frame, error)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) {
|
||||
func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) {
|
||||
|
||||
if len(bs) < 2 {
|
||||
return nil, errors.New("数据长度不足")
|
||||
}
|
||||
|
||||
resp := &KlineResp{
|
||||
Count: Uint16(bs[:2]),
|
||||
}
|
||||
|
||||
bs = bs[2:]
|
||||
|
||||
var last Price //上条数据(昨天)的收盘价
|
||||
for i := uint16(0); i < resp.Count; i++ {
|
||||
k := &Kline{
|
||||
Time: GetTime([4]byte(bs[:4]), Type),
|
||||
Time: GetTime([4]byte(bs[:4]), c.Type),
|
||||
}
|
||||
|
||||
var open Price
|
||||
@@ -130,11 +157,11 @@ func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) {
|
||||
var low Price
|
||||
bs, low = GetPrice(bs)
|
||||
|
||||
k.Last = last / 10
|
||||
k.Open = (open + last) / 10
|
||||
k.Close = (last + open + _close) / 10
|
||||
k.High = (open + last + high) / 10
|
||||
k.Low = (open + last + low) / 10
|
||||
k.Last = last
|
||||
k.Open = open + last
|
||||
k.Close = last + open + _close
|
||||
k.High = open + last + high
|
||||
k.Low = open + last + low
|
||||
last = last + open + _close
|
||||
|
||||
/*
|
||||
@@ -152,15 +179,117 @@ func (kline) Decode(bs []byte, Type uint8) (*KlineResp, error) {
|
||||
|
||||
*/
|
||||
k.Volume = int64(getVolume(Uint32(bs[:4])))
|
||||
switch Type {
|
||||
case TypeKlineMinute, TypeKline5Minute, TypeKlineMinute2, TypeKline15Minute, TypeKline30Minute, TypeKlineHour, TypeKlineDay2:
|
||||
bs = bs[4:]
|
||||
switch c.Type {
|
||||
case TypeKlineMinute, TypeKline5Minute, TypeKlineMinute2, TypeKline15Minute, TypeKline30Minute, TypeKline60Minute, TypeKlineDay2:
|
||||
k.Volume /= 100
|
||||
}
|
||||
k.Amount = Price(getVolume(Uint32(bs[4:8])) * 100) //从元转为分,并去除多余的小数
|
||||
k.Amount = Price(getVolume(Uint32(bs[:4])) * 1000) //从元转为厘,并去除多余的小数
|
||||
bs = bs[4:]
|
||||
|
||||
switch c.Kind {
|
||||
case KindIndex:
|
||||
//指数和股票的差别,指数多解析4字节,并处理成交量*100
|
||||
k.Volume *= 100
|
||||
k.UpCount = conv.Int([]byte{bs[1], bs[0]})
|
||||
k.DownCount = conv.Int([]byte{bs[3], bs[2]})
|
||||
bs = bs[4:]
|
||||
}
|
||||
|
||||
bs = bs[8:]
|
||||
resp.List = append(resp.List, k)
|
||||
}
|
||||
|
||||
resp.List = FixKlineTime(resp.List)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
type KlineCache struct {
|
||||
Type uint8 //1分钟,5分钟,日线等
|
||||
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)
|
||||
return
|
||||
}
|
||||
resp, err := MKline.Decode(bs, 9)
|
||||
resp, err := MKline.Decode(bs, KlineCache{
|
||||
Type: 9,
|
||||
Kind: "",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
|
||||
@@ -2,6 +2,8 @@ package protocol
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MinuteResp struct {
|
||||
@@ -10,10 +12,15 @@ type MinuteResp struct {
|
||||
}
|
||||
|
||||
type PriceNumber struct {
|
||||
Time string
|
||||
Price Price
|
||||
Number int
|
||||
}
|
||||
|
||||
func (this PriceNumber) String() string {
|
||||
return fmt.Sprintf("%s \t%-6s \t%-6d(手)", this.Time, this.Price, this.Number)
|
||||
}
|
||||
|
||||
type minute struct{}
|
||||
|
||||
func (this *minute) Frame(code string) (*Frame, error) {
|
||||
@@ -43,12 +50,17 @@ func (this *minute) Decode(bs []byte) (*MinuteResp, error) {
|
||||
bs = bs[6:]
|
||||
price := Price(0)
|
||||
|
||||
t := time.Date(0, 0, 0, 9, 0, 0, 0, time.Local)
|
||||
for i := uint16(0); i < resp.Count; i++ {
|
||||
bs, price = GetPrice(bs)
|
||||
bs, _ = CutInt(bs) //这个是什么
|
||||
var number int
|
||||
bs, number = CutInt(bs)
|
||||
if i == 120 {
|
||||
t = t.Add(time.Hour * 2)
|
||||
}
|
||||
resp.List = append(resp.List, PriceNumber{
|
||||
Time: t.Add(time.Minute * time.Duration(i)).Format("15:04"),
|
||||
Price: price,
|
||||
Number: number,
|
||||
})
|
||||
|
||||
@@ -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
|
||||
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
|
||||
}
|
||||
@@ -22,15 +22,15 @@ type Quote struct {
|
||||
K K //k线
|
||||
ServerTime string // 时间
|
||||
ReversedBytes0 int // 保留(时间 ServerTime)
|
||||
ReversedBytes1 int // 保留
|
||||
ReversedBytes1 int // 保留 这个等于 负的收盘价格?
|
||||
TotalHand int // 总手(东财的盘口-总手)
|
||||
Intuition int // 现量(东财的盘口-现量)
|
||||
Intuition int // 现量(东财的盘口-现量)现在成交量
|
||||
Amount float64 // 金额(东财的盘口-金额)
|
||||
InsideDish int // 内盘(东财的盘口-外盘)(和东财对不上)
|
||||
OuterDisc int // 外盘(东财的盘口-外盘)(和东财对不上)
|
||||
|
||||
ReversedBytes2 int // 保留,未知
|
||||
ReversedBytes3 int // 保留,未知
|
||||
ReversedBytes3 int // 保留,未知,基金的昨收净值?
|
||||
BuyLevel PriceLevels // 5档买盘(买1-5)
|
||||
SellLevel PriceLevels // 5档卖盘(卖1-5)
|
||||
|
||||
@@ -47,7 +47,7 @@ type Quote struct {
|
||||
func (this *Quote) String() string {
|
||||
return fmt.Sprintf(`%s%s
|
||||
%s
|
||||
总量:%s, 现量:%s, 总金额:%s, 内盘:%s, 外盘:%s
|
||||
总手:%s, 现量:%s, 总金额:%s, 内盘:%s, 外盘:%s
|
||||
%s%s
|
||||
`,
|
||||
this.Exchange.String(), this.Code, this.K,
|
||||
@@ -142,9 +142,9 @@ func (this quote) Decode(bs []byte) QuotesResp {
|
||||
sellLevel := PriceLevel{}
|
||||
|
||||
bs, p = GetPrice(bs)
|
||||
buyLevel.Price = p + sec.K.Close
|
||||
buyLevel.Price = p*10 + sec.K.Close
|
||||
bs, p = GetPrice(bs)
|
||||
sellLevel.Price = p + sec.K.Close
|
||||
sellLevel.Price = p*10 + sec.K.Close
|
||||
|
||||
bs, buyLevel.Number = CutInt(bs)
|
||||
bs, sellLevel.Number = CutInt(bs)
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package protocol
|
||||
|
||||
/*
|
||||
可能是带金额的分时成交,待解析
|
||||
b1cb7400
|
||||
1c
|
||||
68022e00 消息id
|
||||
00
|
||||
6305 //类型
|
||||
841f //长度
|
||||
024b //解压长度
|
||||
789c6d5c77605455d6bf334242b05066546c24a22028ea4c2633995123ef4d49828202768d8a2d620f76c132965857831db06c56505134b2d6585647446c415174c14aa2882888d86263ddef9cdf3933ef3ef7dbff363f4fbda7defb86c383019388c422b1a4091bfe5f20d8dcbcd0358533b3fcff2a3ee9cb78a4ba26417880ff146c5d7b849b5ff26c8385c7e2a00f029f3deb38d79829691b4f5433fd26c0d74d4c125e706d3c198911de07f8f4f09f8e310b6c3c12ab61bcafd28f734dfeb18c8dd7d632ff32a56f76cd7e57d45b78942c20bc1cb83179c72cbcc1c66391ea28e1fd60ff92ae471ce38cf0d99f88d4105ea1f63d4bfab5f8ec230984f707bd31db937eb7dafac552c938e19b827e52e75564db55367da42696227c33e04bbae612ff636cfd22a924dbbf39f3e743207ac7f1f93fcef45b803edc51e99ac64f733ef9c007006fecd9d6310357db782409ff0e54fa56d79c7eb2cf3fb1249fef20e04d6d8349b6ebf37f34c6f60f56fb3a9cee91b736fae207e71b52ff8d77f32f3de3c31329e68f080cf68e7c9ff877dbf64513313edf2df57cd713b699cfffb11a3ebfad8057b68e4f9b0d937cf6c9f96e0dbcb97904f9ef7aa1ff54e557f3f90e51ff7739e6a80b1b2c3c96407c6d033c16ab25fa469b3e16ad65fbb645022de97a9df01a1fff68b296f0ed101fa39a2e23fd0fcbd8f4b104d36f0fbcb2757fc277b271ca3f3ebf1dd47f0f3966c3fc9c8da7103f43413fa9f34ca29fe0dafc23c02b357faad325ff0a1e8fc5587e15f0cea121c287d8fa47a3d5acff8ee0dfd4b688b02a9b7f3415e1f31ba6f131c931ad17d8fe8bd30113be93f2dfc635d15d7dfe8d4699ffcee0df3b92e2bbea949c8f7f8afd3f1cf475fd77715a06ee3fd6a6af4eb0fc11c05ff9399c5e5a15b1f1481cf1bd0bf8af9bb8826cbfcf773eb5b57cfe238137f67c43f840d7475fcbfa8f3292df6b087fdf1f3f518eaf5d8dd48fcd88f6265f7c44903fbb213e66cfeaebe5afe2b549e63f5ae373986bba3fceface3fc6faedaef6b751fe0ef69d7f3ccefcf7d0f83a8b786feb8b9f449ceddf13f2e9ec1c53d8a6de77be51c623e0dfd8338de8dff3c567a486f947c1ff959fc7fd557f328fedaf56fffd3ec6e43b7cf4a914d78718f0e57376764bf1f999d6bf28db5f03f9bd23c3ae89687c2b9e44fd8d030f77bc36c64cd9a5dec263d108d32734be2e21de7fa66d3c11e7f8a9053eb5aecbcd2f7da3c1e61f8bb1fe49d68fcee743e72ff414c05cbf539a3fd4bf06a46dfa580dce772ff8776ecb2092ffa68f3e5acbfcf706ffe9e19dd2a65096b5e9aba36cdf3eea9feba97f3c9bf1f14fb17d75eaffc5c4bf9fcfbe5ae4dfbed06f49d73e143f5b59fe4b266b22ecff3190dfb6f076a25f91b6f138f4c791063b1ac6bb66e05d3efa4492e95d23f56520d33ab6fc14e20f2ca93ecf70cdcc7bedf3a3f2c27846e3fb90b4e9bec2b63f22f50f7fa2f96214319a66db1f89233f721a1fb73966f27c1f7da29afb3b4282f273a663d237d7fb70c44783da47fdfbda9b6cfda2b5b55c7fd092826d0b2bc8ff9ffafc1f87fd63e1bf255d33c911e7f9f4ab8e33be9fcaaf714bfdb1a83ffad7fe46eafb0ee952fe14f5437c8c03fdf239343fb4bfea8b8f6884e9c7839efc43e7b3d0e7df04f2e700cdff9b497ed35fe28ffd7f20f8c762c7ba66d51d3eff44d17f27809efbb3f964900f4f22ff276afeecc5f1edf34f0ae73709f43dd36ac8b6a44f7e22c6f973909eef3f1c1319ef8f0ff8ff60cddf2fc7987e4ff8edc3f91d02bcb127e89ad5b7fbf2bf36c1fe3914f9d7336d08e9b7b5cfffb11a3e9fc340df33ad8f63067dd568e33551a63f5ccf87fb7bc4579f92981f8f30321f1f4af66de1cfcf24d31fa9f68f23db4fb070ca3fccbf47a97fef27de17fbfc43094c7813f49b
|
||||
|
||||
*/
|
||||
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 //计算倍数
|
||||
}
|
||||
@@ -20,8 +20,8 @@ func (this Exchange) String() string {
|
||||
return "sz"
|
||||
case ExchangeSH:
|
||||
return "sh"
|
||||
//case ExchangeBJ:
|
||||
//return "bj"
|
||||
case ExchangeBJ:
|
||||
return "bj"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
@@ -33,8 +33,8 @@ func (this Exchange) Name() string {
|
||||
return "上海"
|
||||
case ExchangeSZ:
|
||||
return "深圳"
|
||||
//case ExchangeBJ:
|
||||
//return "北京"
|
||||
case ExchangeBJ:
|
||||
return "北京"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
@@ -43,13 +43,14 @@ func (this Exchange) Name() string {
|
||||
const (
|
||||
ExchangeSZ Exchange = iota //深圳交易所
|
||||
ExchangeSH //上海交易所
|
||||
//ExchangeBJ //北京交易所
|
||||
ExchangeBJ //北京交易所
|
||||
)
|
||||
|
||||
const (
|
||||
TypeKline5Minute uint8 = 0 // 5分钟K 线
|
||||
TypeKline15Minute uint8 = 1 // 15分钟K 线
|
||||
TypeKline30Minute uint8 = 2 // 30分钟K 线
|
||||
TypeKline60Minute uint8 = 3 // 60分钟K 线
|
||||
TypeKlineHour uint8 = 3 // 1小时K 线
|
||||
TypeKlineDay2 uint8 = 4 // 日K 线, 发现和Day的区别是这个要除以100,其他未知
|
||||
TypeKlineWeek uint8 = 5 // 周K 线
|
||||
@@ -60,3 +61,8 @@ const (
|
||||
TypeKlineQuarter uint8 = 10 // 季K 线
|
||||
TypeKlineYear uint8 = 11 // 年K 线
|
||||
)
|
||||
|
||||
const (
|
||||
KindIndex = "index"
|
||||
KindStock = "stock"
|
||||
)
|
||||
|
||||
@@ -4,11 +4,11 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Price 价格,单位分,分时成交的总金额可能会超出范围,后续改成int64
|
||||
// Price 价格,单位厘
|
||||
type Price int64
|
||||
|
||||
func (this Price) Float64() float64 {
|
||||
return float64(this) / 100
|
||||
return float64(this) / 1000
|
||||
}
|
||||
|
||||
func (this Price) Int64() int64 {
|
||||
@@ -17,7 +17,6 @@ func (this Price) Int64() int64 {
|
||||
|
||||
func (this Price) String() string {
|
||||
return fmt.Sprintf("%s元", FloatUnitString(this.Float64()))
|
||||
return fmt.Sprintf("%0.2f元", this.Float64())
|
||||
}
|
||||
|
||||
type PriceLevel struct {
|
||||
@@ -79,6 +78,13 @@ func DecodeK(bs []byte) ([]byte, K) {
|
||||
bs, k.Low = GetPrice(bs)
|
||||
k.Low += k.Close
|
||||
|
||||
//默认按股票展示
|
||||
k.Last *= 10
|
||||
k.Open *= 10
|
||||
k.Close *= 10
|
||||
k.High *= 10
|
||||
k.Low *= 10
|
||||
|
||||
return bs, k
|
||||
}
|
||||
|
||||
|
||||
131
protocol/unit.go
131
protocol/unit.go
@@ -3,7 +3,6 @@ package protocol
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
bytes2 "github.com/injoyai/base/bytes"
|
||||
"github.com/injoyai/conv"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"golang.org/x/text/transform"
|
||||
@@ -15,12 +14,12 @@ import (
|
||||
|
||||
// String 字节先转小端,再转字符
|
||||
func String(bs []byte) string {
|
||||
return string(bytes2.Reverse(bs))
|
||||
return string(Reverse(bs))
|
||||
}
|
||||
|
||||
// Bytes 任意类型转小端字节
|
||||
func Bytes(n any) []byte {
|
||||
return bytes2.Reverse(conv.Bytes(n))
|
||||
return Reverse(conv.Bytes(n))
|
||||
}
|
||||
|
||||
// Reverse 字节倒序
|
||||
@@ -34,12 +33,12 @@ func Reverse(bs []byte) []byte {
|
||||
|
||||
// Uint32 字节通过小端方式转为uint32
|
||||
func Uint32(bs []byte) uint32 {
|
||||
return conv.Uint32(bytes2.Reverse(bs))
|
||||
return conv.Uint32(Reverse(bs))
|
||||
}
|
||||
|
||||
// Uint16 字节通过小端方式转为uint16
|
||||
func Uint16(bs []byte) uint16 {
|
||||
return conv.Uint16(bytes2.Reverse(bs))
|
||||
return conv.Uint16(Reverse(bs))
|
||||
}
|
||||
|
||||
func UTF8ToGBK(text []byte) []byte {
|
||||
@@ -50,6 +49,7 @@ func UTF8ToGBK(text []byte) []byte {
|
||||
}
|
||||
|
||||
func DecodeCode(code string) (Exchange, string, error) {
|
||||
code = AddPrefix(code)
|
||||
if len(code) != 8 {
|
||||
return 0, "", fmt.Errorf("股票代码长度错误,例如:SZ000001")
|
||||
}
|
||||
@@ -58,6 +58,8 @@ func DecodeCode(code string) (Exchange, string, error) {
|
||||
return ExchangeSH, code[2:], nil
|
||||
case ExchangeSZ.String():
|
||||
return ExchangeSZ, code[2:], nil
|
||||
case ExchangeBJ.String():
|
||||
return ExchangeBJ, code[2:], nil
|
||||
default:
|
||||
return 0, "", fmt.Errorf("股票代码错误,例如:SZ000001")
|
||||
}
|
||||
@@ -102,7 +104,7 @@ func GetHourMinute(bs [2]byte) string {
|
||||
|
||||
func GetTime(bs [4]byte, Type uint8) time.Time {
|
||||
switch Type {
|
||||
case TypeKlineMinute, TypeKlineMinute2, TypeKline5Minute, TypeKline15Minute, TypeKline30Minute, TypeKlineHour:
|
||||
case TypeKlineMinute, TypeKlineMinute2, TypeKline5Minute, TypeKline15Minute, TypeKline30Minute, TypeKline60Minute:
|
||||
|
||||
yearMonthDay := Uint16(bs[:2])
|
||||
hourMinute := Uint16(bs[2:4])
|
||||
@@ -194,3 +196,120 @@ func getVolume(val uint32) (volume float64) {
|
||||
volume = dbl_xmm6 + dbl_xmm4 + dbl_xmm3 + dbl_xmm1
|
||||
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
|
||||
func IsStock(code string) bool {
|
||||
return IsSZStock(code) || IsSHStock(code) || IsBJStock(code)
|
||||
|
||||
//if len(code) != 8 {
|
||||
// return false
|
||||
//}
|
||||
//code = strings.ToLower(code)
|
||||
//switch {
|
||||
//case code[0:2] == ExchangeSH.String() &&
|
||||
// (code[2:3] == "6"):
|
||||
// return true
|
||||
//
|
||||
//case code[0:2] == ExchangeSZ.String() &&
|
||||
// (code[2:3] == "0" || code[2:4] == "30"):
|
||||
// return true
|
||||
//}
|
||||
//return false
|
||||
}
|
||||
|
||||
func IsSZStock(code string) bool {
|
||||
return len(code) == 8 && strings.ToLower(code[0:2]) == ExchangeSZ.String() && code[2:3] == "0"
|
||||
}
|
||||
|
||||
func IsSHStock(code string) bool {
|
||||
return len(code) == 8 && strings.ToLower(code[0:2]) == ExchangeSH.String() && code[2:3] == "6"
|
||||
}
|
||||
|
||||
func IsBJStock(code string) bool {
|
||||
return len(code) == 8 && strings.ToLower(code[0:2]) == ExchangeBJ.String() && (code[2:4] == "92" || code[2:3] == "8")
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if len(code) == 6 {
|
||||
switch {
|
||||
case code[:1] == "6":
|
||||
//上海股票
|
||||
code = ExchangeSH.String() + code
|
||||
case code[:1] == "0":
|
||||
//深圳股票
|
||||
code = ExchangeSZ.String() + code
|
||||
case code[:2] == "30":
|
||||
//深圳股票
|
||||
code = ExchangeSZ.String() + code
|
||||
case code[:3] == "510" || code[:3] == "511" || code[:3] == "512" || code[:3] == "513" || code[:3] == "515":
|
||||
//上海基金
|
||||
code = ExchangeSH.String() + code
|
||||
case code[:3] == "159":
|
||||
//深圳基金
|
||||
code = ExchangeSZ.String() + code
|
||||
case code[:1] == "8" || code[:2] == "92":
|
||||
//北京股票
|
||||
code = ExchangeBJ.String() + code
|
||||
}
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
@@ -20,5 +20,6 @@ func TestUTF8ToGBK(t *testing.T) {
|
||||
|
||||
func Test_getVolume(t *testing.T) {
|
||||
t.Log(getVolume(1237966432))
|
||||
t.Log(getVolume(1237966432))
|
||||
t.Log(getVolume2(1237966432))
|
||||
|
||||
}
|
||||
|
||||
171
workday.go
Normal file
171
workday.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
_ "github.com/glebarez/go-sqlite"
|
||||
"github.com/injoyai/base/maps"
|
||||
"github.com/injoyai/conv"
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/protocol"
|
||||
"github.com/robfig/cron/v3"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"xorm.io/core"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func NewWorkday(c *Client, filename string) (*Workday, error) {
|
||||
|
||||
//如果文件夹不存在就创建
|
||||
dir, _ := filepath.Split(filename)
|
||||
_ = os.MkdirAll(dir, 0777)
|
||||
|
||||
//连接数据库
|
||||
db, err := xorm.NewEngine("sqlite", filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMapper(core.SameMapper{})
|
||||
db.DB().SetMaxOpenConns(1)
|
||||
if err := db.Sync2(new(WorkdayModel)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
w := &Workday{
|
||||
Client: c,
|
||||
db: db,
|
||||
cache: maps.NewBit(),
|
||||
}
|
||||
|
||||
//设置定时器,每天早上9点更新数据,8点多获取不到今天的数据
|
||||
task := cron.New(cron.WithSeconds())
|
||||
task.AddFunc("0 0 9 * * *", func() {
|
||||
for i := 0; i < 3; i++ {
|
||||
if err := w.Update(); err == nil {
|
||||
return
|
||||
}
|
||||
logs.Err(err)
|
||||
<-time.After(time.Minute * 5)
|
||||
}
|
||||
})
|
||||
task.Start()
|
||||
|
||||
return w, w.Update()
|
||||
}
|
||||
|
||||
type Workday struct {
|
||||
*Client
|
||||
db *xorm.Engine
|
||||
cache maps.Bit
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
func (this *Workday) Update() error {
|
||||
|
||||
if this.Client == nil {
|
||||
return errors.New("client is nil")
|
||||
}
|
||||
|
||||
//获取沪市指数的日K线,用作历史是否节假日的判断依据
|
||||
//判断日K线是否拉取过
|
||||
|
||||
//获取全部工作日
|
||||
all := []*WorkdayModel(nil)
|
||||
if err := this.db.Find(&all); err != nil {
|
||||
return err
|
||||
}
|
||||
var lastWorkday = &WorkdayModel{}
|
||||
if len(all) > 0 {
|
||||
lastWorkday = all[len(all)-1]
|
||||
}
|
||||
for _, v := range all {
|
||||
this.cache.Set(uint64(v.Unix), true)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if lastWorkday == nil || lastWorkday.Unix < IntegerDay(now).Unix() {
|
||||
resp, err := this.Client.GetIndexDayAll("sh000001")
|
||||
if err != nil {
|
||||
logs.Err(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return NewSessionFunc(this.db, func(session *xorm.Session) error {
|
||||
for _, v := range resp.List {
|
||||
if unix := v.Time.Unix(); unix > lastWorkday.Unix {
|
||||
_, err = session.Insert(&WorkdayModel{Unix: unix, Date: v.Time.Format("20060102"), Is: true})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
this.cache.Set(uint64(unix), true)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Is 是否是工作日
|
||||
func (this *Workday) Is(t time.Time) bool {
|
||||
return this.cache.Get(uint64(IntegerDay(t).Add(time.Hour * 15).Unix()))
|
||||
}
|
||||
|
||||
// TodayIs 今天是否是工作日
|
||||
func (this *Workday) TodayIs() bool {
|
||||
return this.Is(time.Now())
|
||||
}
|
||||
|
||||
// RangeYear 遍历一年的所有工作日
|
||||
func (this *Workday) RangeYear(year int, f func(t time.Time) bool) {
|
||||
this.Range(
|
||||
time.Date(year, 1, 1, 0, 0, 0, 0, time.Local),
|
||||
time.Date(year, 12, 31, 0, 0, 0, 0, time.Local),
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// Range 遍历指定范围的工作日
|
||||
func (this *Workday) Range(start, end time.Time, f func(t time.Time) bool) {
|
||||
start = conv.Select(start.Before(protocol.ExchangeEstablish), protocol.ExchangeEstablish, start)
|
||||
now := IntegerDay(time.Now())
|
||||
end = conv.Select(end.After(now), now, end).Add(1)
|
||||
for ; start.Before(end); start = start.Add(time.Hour * 24) {
|
||||
if this.Is(start) {
|
||||
if !f(start) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RangeDesc 倒序遍历工作日,从今天-1990年12月19日(上海交易所成立时间)
|
||||
func (this *Workday) RangeDesc(f func(t time.Time) bool) {
|
||||
t := IntegerDay(time.Now())
|
||||
for ; t.After(time.Date(1990, 12, 18, 0, 0, 0, 0, time.Local)); t = t.Add(-time.Hour * 24) {
|
||||
if this.Is(t) {
|
||||
if !f(t) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WorkdayModel 工作日
|
||||
type WorkdayModel struct {
|
||||
ID int64 `json:"id"` //主键
|
||||
Unix int64 `json:"unix"` //时间戳
|
||||
Date string `json:"date"` //日期
|
||||
Is bool `json:"is"` //是否是工作日
|
||||
}
|
||||
|
||||
func (this *WorkdayModel) TableName() string {
|
||||
return "workday"
|
||||
}
|
||||
|
||||
func IntegerDay(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
Reference in New Issue
Block a user