mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8fb069b855 | ||
|
|
110eaddc4d | ||
|
|
aec2cf1518 | ||
|
|
a596139d3e | ||
|
|
578617e458 | ||
|
|
fab9e92fcd | ||
|
|
47084b1112 | ||
|
|
2566ef5cec | ||
|
|
86404db551 | ||
|
|
3f3438fca8 | ||
|
|
1656b41f02 | ||
|
|
8090cc7216 | ||
|
|
a3169c67da | ||
|
|
0a1a6194af | ||
|
|
8b3ff3af01 |
26
client.go
26
client.go
@@ -87,17 +87,19 @@ func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
|
||||
c.SetOption(op...) //自定义选项
|
||||
c.Event.OnReadFrom = protocol.ReadFrom //分包
|
||||
c.Event.OnDealMessage = cli.handlerDealMessage //解析数据并处理
|
||||
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 {
|
||||
return nil, err
|
||||
@@ -218,6 +220,26 @@ func (this *Client) GetCode(exchange protocol.Exchange, start uint16) (*protocol
|
||||
// GetCodeAll 通过多次请求的方式获取全部证券代码
|
||||
func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp, error) {
|
||||
resp := &protocol.CodeResp{}
|
||||
|
||||
//通达信没有北交所代码列表,通过爬虫的方式从北交所官网获取,放在这里是为了方便业务逻辑
|
||||
//不放在extend包时防止循环引用
|
||||
//todo 这是临时方案,等通达信有北交所代码列表时再改
|
||||
if exchange == protocol.ExchangeBJ {
|
||||
codes, err := GetBjCodes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Count = uint16(len(codes))
|
||||
for _, v := range codes {
|
||||
resp.List = append(resp.List, &protocol.Code{
|
||||
Code: v.Code,
|
||||
Name: v.Name,
|
||||
LastPrice: v.Last,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
size := uint16(1000)
|
||||
for start := uint16(0); ; start += size {
|
||||
r, err := this.GetCode(exchange, start)
|
||||
@@ -236,7 +258,7 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp,
|
||||
// GetStockAll 获取所有股票代码
|
||||
func (this *Client) GetStockAll() ([]string, error) {
|
||||
ls := []string(nil)
|
||||
for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
|
||||
for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ, protocol.ExchangeBJ} {
|
||||
resp, err := this.GetCodeAll(ex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
102
client_bj_code.go
Normal file
102
client_bj_code.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package tdx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/injoyai/conv"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// UrlBjCodes 最后跟的是时间戳(ms),但是随便什么时间戳都能请求成功
|
||||
UrlBjCodes = "https://www.bse.cn/nqhqController/nqhq_en.do?callback=jQuery3710848510589806625_%d"
|
||||
)
|
||||
|
||||
func GetBjCodes() ([]*BjCode, error) {
|
||||
list := []*BjCode(nil)
|
||||
//这个200预防下bug,除非北京上市公司有4000个
|
||||
for page := 0; page < 200; page++ {
|
||||
ls, done, err := getBjCodes(page)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list = append(list, ls...)
|
||||
if done {
|
||||
break
|
||||
}
|
||||
<-time.After(time.Millisecond * 100)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func getBjCodes(page int) (_ []*BjCode, last bool, err error) {
|
||||
|
||||
url := fmt.Sprintf(UrlBjCodes, time.Now().UnixMilli())
|
||||
|
||||
bodyStr := "page=" + conv.String(page) + "&type_en=%5B%22B%22%5D&sortfield=hqcjsl&sorttype=desc&xxfcbj_en=%5B2%5D&zqdm="
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(bodyStr))
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.39 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
bs, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
//处理数据
|
||||
i := bytes.IndexByte(bs, '(')
|
||||
if len(bs) < 1 || len(bs) <= i {
|
||||
return nil, false, errors.New("未知错误: " + string(bs))
|
||||
}
|
||||
|
||||
bs = bs[i+1 : len(bs)-1]
|
||||
|
||||
ls := []*BjCodes(nil)
|
||||
err = json.Unmarshal(bs, &ls)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if len(ls) == 0 {
|
||||
return nil, false, errors.New("未知错误: " + string(bs))
|
||||
}
|
||||
|
||||
return ls[0].Data, ls[0].LastPage, nil
|
||||
}
|
||||
|
||||
type BjCodes struct {
|
||||
Data []*BjCode `json:"content"`
|
||||
TotalNumber int `json:"totalElements"`
|
||||
TotalPage int `json:"totalPages"`
|
||||
LastPage bool `json:"lastPage"`
|
||||
}
|
||||
|
||||
type BjCode struct {
|
||||
Date string `json:"hqjsrq"` //日期
|
||||
Code string `json:"hqzqdm"` //代码
|
||||
Name string `json:"hqzqjc"` //名称
|
||||
LastClose float64 `json:"hqzrsp"` //前一天收盘价
|
||||
Open float64 `json:"hqjrkp"` //开盘价
|
||||
High float64 `json:"hqzgcj"` //最高价
|
||||
Low float64 `json:"hqzdcj"` //最低价
|
||||
Last float64 `json:"hqzjcj"` //最新价/收盘价
|
||||
Volume int `json:"hqcjsl"` //成交量,股
|
||||
Amount float64 `json:"hqcjje"` //成交额,元
|
||||
}
|
||||
93
codes.go
93
codes.go
@@ -155,52 +155,53 @@ 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
|
||||
}
|
||||
}
|
||||
//// GetExchange 获取股票交易所,这里的参数不需要带前缀
|
||||
//func (this *Codes) GetExchange(code string) protocol.Exchange {
|
||||
// if len(code) == 6 {
|
||||
// switch {
|
||||
// case code[:1] == "6":
|
||||
// return protocol.ExchangeSH
|
||||
// case code[:1] == "0":
|
||||
// return protocol.ExchangeSZ
|
||||
// case code[:2] == "30":
|
||||
// return protocol.ExchangeSZ
|
||||
// }
|
||||
// }
|
||||
// var exchange string
|
||||
// exchanges := this.exchanges[code]
|
||||
// if len(exchanges) >= 1 {
|
||||
// exchange = exchanges[0]
|
||||
// }
|
||||
// if len(code) == 8 {
|
||||
// exchange = code[0:2]
|
||||
// }
|
||||
// switch exchange {
|
||||
// case protocol.ExchangeSH.String():
|
||||
// return protocol.ExchangeSH
|
||||
// case protocol.ExchangeSZ.String():
|
||||
// return protocol.ExchangeSZ
|
||||
// default:
|
||||
// return protocol.ExchangeSH
|
||||
// }
|
||||
//}
|
||||
|
||||
func (this *Codes) AddExchange(code string) string {
|
||||
if exchanges := this.exchanges[code]; len(exchanges) == 1 {
|
||||
return exchanges[0] + code
|
||||
}
|
||||
if len(code) == 6 {
|
||||
switch {
|
||||
case code[:1] == "6":
|
||||
return protocol.ExchangeSH.String() + code
|
||||
case code[:1] == "0":
|
||||
return protocol.ExchangeSZ.String() + code
|
||||
case code[:2] == "30":
|
||||
return protocol.ExchangeSZ.String() + code
|
||||
}
|
||||
return this.GetExchange(code).String() + code
|
||||
}
|
||||
return code
|
||||
return protocol.AddPrefix(code)
|
||||
//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 更新数据,从服务器或者数据库
|
||||
@@ -249,7 +250,7 @@ func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) {
|
||||
//3. 从服务器获取所有股票代码
|
||||
insert := []*CodeModel(nil)
|
||||
update := []*CodeModel(nil)
|
||||
for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} {
|
||||
for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ, protocol.ExchangeBJ} {
|
||||
resp, err := this.Client.GetCodeAll(exchange)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -334,7 +335,7 @@ func (this *CodeModel) FullCode() string {
|
||||
|
||||
func (this *CodeModel) Price(p protocol.Price) protocol.Price {
|
||||
return protocol.Price(float64(p) * math.Pow10(int(2-this.Decimal)))
|
||||
return p * protocol.Price(math.Pow10(int(2-this.Decimal)))
|
||||
//return p * protocol.Price(math.Pow10(int(2-this.Decimal)))
|
||||
}
|
||||
|
||||
func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error {
|
||||
|
||||
18
example/GetBjCodes/main.go
Normal file
18
example/GetBjCodes/main.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx/extend"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ls, err := extend.GetBjCodes()
|
||||
if err != nil {
|
||||
logs.Err(err)
|
||||
return
|
||||
}
|
||||
for _, v := range ls {
|
||||
logs.Debug(v)
|
||||
}
|
||||
logs.Debug("总数量:", len(ls))
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func main() {
|
||||
common.Test(func(c *tdx.Client) {
|
||||
resp, err := c.GetHistoryMinuteTrade("20250609", "sz000001", 0, 20)
|
||||
resp, err := c.GetHistoryMinuteTrade("20250929", "bj838971", 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.GetKlineDay("000001", 0, 10)
|
||||
resp, err := c.GetKlineDay("838971", 0, 20)
|
||||
logs.PanicErr(err)
|
||||
|
||||
for _, v := range resp.List {
|
||||
|
||||
32
example/GetKlineDayFactor/main.go
Normal file
32
example/GetKlineDayFactor/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/injoyai/logs"
|
||||
"github.com/injoyai/tdx"
|
||||
"github.com/injoyai/tdx/extend"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
c, err := tdx.DialDefault()
|
||||
logs.PanicErr(err)
|
||||
|
||||
ks, fs, err := extend.GetTHSDayKlineFactorFull("000001", c)
|
||||
logs.PanicErr(err)
|
||||
|
||||
m := map[int64]*extend.THSFactor{}
|
||||
for _, v := range fs {
|
||||
m[v.Date] = v
|
||||
}
|
||||
|
||||
for _, v := range ks[0] {
|
||||
logs.Debugf("%s 不复权:%.2f 前复权:%.2f 后复权:%.2f \n",
|
||||
time.Unix(v.Date, 0).Format(time.DateOnly),
|
||||
v.Close.Float64(),
|
||||
v.Close.Float64()*m[v.Date].QFactor,
|
||||
v.Close.Float64()*m[v.Date].HFactor,
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,11 +9,12 @@ import (
|
||||
func main() {
|
||||
common.Test(func(c *tdx.Client) {
|
||||
|
||||
_, err := tdx.NewWorkday(c, "./workday.db")
|
||||
_, err := tdx.NewWorkday(c) //"./workday.db"
|
||||
logs.PanicErr(err)
|
||||
|
||||
_, err = tdx.NewCodes(c, "./codes.db")
|
||||
_, err = tdx.NewCodes(c) //"./codes.db"
|
||||
logs.PanicErr(err)
|
||||
|
||||
c.Close()
|
||||
})
|
||||
}
|
||||
|
||||
9
extend/codes_bj.go
Normal file
9
extend/codes_bj.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package extend
|
||||
|
||||
import (
|
||||
"github.com/injoyai/tdx"
|
||||
)
|
||||
|
||||
func GetBjCodes() ([]*tdx.BjCode, error) {
|
||||
return tdx.GetBjCodes()
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func (this *PullKlineMysql) Name() string {
|
||||
}
|
||||
|
||||
func (this *PullKlineMysql) Run(ctx context.Context, m *tdx.Manage) error {
|
||||
limit := chans.NewWaitLimit(uint(this.Config.Limit))
|
||||
limit := chans.NewWaitLimit(this.Config.Limit)
|
||||
|
||||
//1. 获取所有股票代码
|
||||
codes := this.Config.Codes
|
||||
|
||||
@@ -105,7 +105,7 @@ func (this *PullKline) DayKlines(code string) (Klines, error) {
|
||||
}
|
||||
|
||||
func (this *PullKline) Run(ctx context.Context, m *tdx.Manage) error {
|
||||
limit := chans.NewWaitLimit(uint(this.Config.Limit))
|
||||
limit := chans.NewWaitLimit(this.Config.Limit)
|
||||
|
||||
//1. 获取所有股票代码
|
||||
codes := this.Config.Codes
|
||||
|
||||
@@ -15,10 +15,36 @@ import (
|
||||
|
||||
const (
|
||||
UrlTHSDayKline = "http://d.10jqka.com.cn/v6/line/hs_%s/0%d/all.js"
|
||||
THS_BFQ uint8 = 0 //不复权
|
||||
THS_QFQ uint8 = 1 //前复权
|
||||
THS_HFQ uint8 = 2 //后复权
|
||||
)
|
||||
|
||||
// GetTHSDayKlineFactorFull 增加计算复权因子
|
||||
func GetTHSDayKlineFactorFull(code string, c *tdx.Client) ([3][]*Kline, []*THSFactor, error) {
|
||||
ks, err := GetTHSDayKlineFull(code, c)
|
||||
if err != nil {
|
||||
return [3][]*Kline{}, nil, err
|
||||
}
|
||||
mQPrice := make(map[int64]float64)
|
||||
for _, v := range ks[1] {
|
||||
mQPrice[v.Date] = v.Close.Float64()
|
||||
}
|
||||
mHPrice := make(map[int64]float64)
|
||||
for _, v := range ks[2] {
|
||||
mHPrice[v.Date] = v.Close.Float64()
|
||||
}
|
||||
fs := make([]*THSFactor, 0, len(ks[0]))
|
||||
for _, v := range ks[0] {
|
||||
fs = append(fs, &THSFactor{
|
||||
Date: v.Date,
|
||||
QFactor: mQPrice[v.Date] / v.Close.Float64(),
|
||||
HFactor: mHPrice[v.Date] / v.Close.Float64(),
|
||||
})
|
||||
}
|
||||
return ks, fs, nil
|
||||
}
|
||||
|
||||
/*
|
||||
GetTHSDayKlineFull
|
||||
获取[不复权,前复权,后复权]数据,并补充成交金额数据
|
||||
@@ -70,8 +96,8 @@ GetTHSDayKline
|
||||
后复权,和通达信,东方财富都对不上
|
||||
*/
|
||||
func GetTHSDayKline(code string, _type uint8) ([]*Kline, error) {
|
||||
if _type != THS_QFQ && _type != THS_HFQ {
|
||||
return nil, fmt.Errorf("数据类型错误,例如:前复权1或后复权2")
|
||||
if _type != THS_BFQ && _type != THS_QFQ && _type != THS_HFQ {
|
||||
return nil, fmt.Errorf("数据类型错误,例如:不复权0或前复权1或后复权2")
|
||||
}
|
||||
|
||||
code = protocol.AddPrefix(code)
|
||||
|
||||
12
extend/ths-factor.go
Normal file
12
extend/ths-factor.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package extend
|
||||
|
||||
//const (
|
||||
// // UrlTHSFactor https://d.10jqka.com.cn/v6/line/hs_000001/01/2016.js
|
||||
// UrlTHSFactor = "https://d.10jqka.com.cn/v6/line/hs_%s/0%d/%d.js"
|
||||
//)
|
||||
|
||||
type THSFactor struct {
|
||||
Date int64 `json:"date"` //时间
|
||||
QFactor float64 `json:"q_factor"` //前复权因子
|
||||
HFactor float64 `json:"h_factor"` //后复权因子
|
||||
}
|
||||
10
go.mod
10
go.mod
@@ -5,10 +5,10 @@ go 1.20
|
||||
require (
|
||||
github.com/glebarez/go-sqlite v1.22.0
|
||||
github.com/go-sql-driver/mysql v1.7.0
|
||||
github.com/injoyai/base v1.2.8
|
||||
github.com/injoyai/base v1.2.17
|
||||
github.com/injoyai/conv v1.2.5
|
||||
github.com/injoyai/ios v0.0.10
|
||||
github.com/injoyai/logs v1.0.9
|
||||
github.com/injoyai/ios v1.2.2
|
||||
github.com/injoyai/logs v1.0.12
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
golang.org/x/text v0.16.0
|
||||
xorm.io/core v0.7.3
|
||||
@@ -17,7 +17,7 @@ require (
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/fatih/color v1.14.1 // indirect
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
github.com/goccy/go-json v0.8.1 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/uuid v1.5.0 // indirect
|
||||
@@ -31,7 +31,7 @@ require (
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/stretchr/testify v1.9.0 // indirect
|
||||
github.com/syndtr/goleveldb v1.0.0 // indirect
|
||||
golang.org/x/sys v0.22.0 // indirect
|
||||
golang.org/x/sys v0.25.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
modernc.org/libc v1.37.6 // indirect
|
||||
|
||||
31
go.sum
31
go.sum
@@ -6,9 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
|
||||
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
|
||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ=
|
||||
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
|
||||
@@ -27,14 +26,16 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/injoyai/base v1.2.8 h1:voEPqxE5U+wI1RvcQStVbjUZJDLENbl1SsoQQWMn4s0=
|
||||
github.com/injoyai/base v1.2.8/go.mod h1:NfCQjml3z2pCvQ3J3YcOXtecqXD0xVPKjo4YTsMLhr8=
|
||||
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 v0.0.10 h1:Zd37Rwp90PYV5eFhirR0LJ+ni/aYLSCAYgHltk/ZzJA=
|
||||
github.com/injoyai/ios v0.0.10/go.mod h1:zLTmvQmIbdTf7zZxymOVxbxvvSeUMpcs4SCVPDT9BOc=
|
||||
github.com/injoyai/logs v1.0.9 h1:Wq7rCVIQKcPx+z+lzKQb2qyDK4TML/cgmaSZN9tx33c=
|
||||
github.com/injoyai/logs v1.0.9/go.mod h1:CLchJCGhb39Obyrci816R+KMtbxZhgPs0FuikhyixK4=
|
||||
github.com/injoyai/ios v1.2.2 h1:fAPWBL6t22DiE2ZEpBgf5bzyVQTcm2ZhLMkM+JFPhZA=
|
||||
github.com/injoyai/ios v1.2.2/go.mod h1:DJVJGQFQvqF80CeJVabFOm6AKilqc/m8MFvz39Uy5ow=
|
||||
github.com/injoyai/logs v1.0.12 h1:f7syIGZMTg9ZzhJhdd3tzaPdxkMhdKsncGaxljqIiYE=
|
||||
github.com/injoyai/logs v1.0.12/go.mod h1:+dKEL6GvaFqqVRatqUBiCicJbZnAgtj7hVs824Src4s=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
|
||||
@@ -42,11 +43,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
@@ -92,13 +90,10 @@ golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||
@@ -113,8 +108,8 @@ gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -39,6 +39,19 @@ type Frame struct {
|
||||
Data []byte //数据
|
||||
}
|
||||
|
||||
/*
|
||||
Bytes
|
||||
|
||||
0c00000000011c001c002d0500003030303030310900010000000a0000000000000000000000
|
||||
|
||||
Prefix: 0c
|
||||
MsgID: 0208d301
|
||||
Control: 01
|
||||
Length: 1c00
|
||||
Length: 1c00
|
||||
Type: 2d05
|
||||
000030303030303104000100a401a40100000000000000000000
|
||||
*/
|
||||
func (this *Frame) Bytes() types.Bytes {
|
||||
length := uint16(len(this.Data) + 2)
|
||||
data := make([]byte, 12+len(this.Data))
|
||||
|
||||
@@ -86,6 +86,27 @@ func (this *Kline) RiseRate() float64 {
|
||||
|
||||
type kline struct{}
|
||||
|
||||
/*
|
||||
Frame
|
||||
Prefix: 0c
|
||||
MsgID: 0208d301
|
||||
Control: 01
|
||||
Length: 1c00
|
||||
Length: 1c00
|
||||
Type: 2d05
|
||||
Data: 000030303030303104000100a401a40100000000000000000000
|
||||
|
||||
Data:
|
||||
Exchange: 00
|
||||
Unknown: 00
|
||||
Code: 303030303031
|
||||
Type: 04
|
||||
Unknown: 00
|
||||
Unknown: 0100
|
||||
Start: a401
|
||||
Count: a401
|
||||
Append: 00000000000000000000
|
||||
*/
|
||||
func (kline) Frame(Type uint8, code string, start, count uint16) (*Frame, error) {
|
||||
if count > 800 {
|
||||
return nil, errors.New("单次数量不能超过800")
|
||||
@@ -116,7 +137,6 @@ func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) {
|
||||
if len(bs) < 2 {
|
||||
return nil, errors.New("数据长度不足")
|
||||
}
|
||||
|
||||
resp := &KlineResp{
|
||||
Count: Uint16(bs[:2]),
|
||||
}
|
||||
|
||||
@@ -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,7 +43,7 @@ func (this Exchange) Name() string {
|
||||
const (
|
||||
ExchangeSZ Exchange = iota //深圳交易所
|
||||
ExchangeSH //上海交易所
|
||||
//ExchangeBJ //北京交易所
|
||||
ExchangeBJ //北京交易所
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
@@ -125,11 +127,15 @@ func GetTime(bs [4]byte, Type uint8) time.Time {
|
||||
}
|
||||
|
||||
func basePrice(code string) Price {
|
||||
if len(code) == 0 {
|
||||
if len(code) < 2 {
|
||||
return 1
|
||||
}
|
||||
switch code[:1] {
|
||||
case "8":
|
||||
return 1
|
||||
}
|
||||
switch code[:2] {
|
||||
case "60", "30", "68", "00":
|
||||
case "60", "30", "68", "00", "92", "43":
|
||||
return 1
|
||||
default:
|
||||
return 10
|
||||
@@ -237,20 +243,34 @@ func getVolume2(val uint32) float64 {
|
||||
|
||||
// IsStock 是否是股票,示例sz000001
|
||||
func IsStock(code string) bool {
|
||||
if len(code) != 8 {
|
||||
return false
|
||||
}
|
||||
code = strings.ToLower(code)
|
||||
switch {
|
||||
case code[0:2] == ExchangeSH.String() &&
|
||||
(code[2:3] == "6"):
|
||||
return true
|
||||
return IsSZStock(code) || IsSHStock(code) || IsBJStock(code)
|
||||
|
||||
case code[0:2] == ExchangeSZ.String() &&
|
||||
(code[2:3] == "0" || code[2:4] == "30"):
|
||||
return true
|
||||
}
|
||||
return false
|
||||
//if len(code) != 8 {
|
||||
// return false
|
||||
//}
|
||||
//code = strings.ToLower(code)
|
||||
//switch {
|
||||
//case code[0:2] == ExchangeSH.String() &&
|
||||
// (code[2:3] == "6"):
|
||||
// return true
|
||||
//
|
||||
//case code[0:2] == ExchangeSZ.String() &&
|
||||
// (code[2:3] == "0" || code[2:4] == "30"):
|
||||
// return true
|
||||
//}
|
||||
//return false
|
||||
}
|
||||
|
||||
func IsSZStock(code string) bool {
|
||||
return len(code) == 8 && strings.ToLower(code[0:2]) == ExchangeSZ.String() && code[2:3] == "0"
|
||||
}
|
||||
|
||||
func IsSHStock(code string) bool {
|
||||
return len(code) == 8 && strings.ToLower(code[0:2]) == ExchangeSH.String() && code[2:3] == "6"
|
||||
}
|
||||
|
||||
func IsBJStock(code string) bool {
|
||||
return len(code) == 8 && strings.ToLower(code[0:2]) == ExchangeBJ.String() && (code[2:4] == "92" || code[2:4] == "43" || code[2:3] == "8")
|
||||
}
|
||||
|
||||
// IsETF 是否是基金,示例sz159558
|
||||
@@ -290,6 +310,9 @@ func AddPrefix(code string) string {
|
||||
case code[:3] == "159":
|
||||
//深圳基金
|
||||
code = ExchangeSZ.String() + code
|
||||
case code[:1] == "8" || code[:2] == "92" || code[:2] == "43":
|
||||
//北京股票
|
||||
code = ExchangeBJ.String() + code
|
||||
}
|
||||
}
|
||||
return code
|
||||
|
||||
@@ -15,7 +15,10 @@ import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func NewWorkday(c *Client, filename string) (*Workday, error) {
|
||||
func NewWorkday(c *Client, filenames ...string) (*Workday, error) {
|
||||
|
||||
defaultFilename := filepath.Join(DefaultDatabaseDir, "workday.db")
|
||||
filename := conv.Default(defaultFilename, filenames...)
|
||||
|
||||
//如果文件夹不存在就创建
|
||||
dir, _ := filepath.Split(filename)
|
||||
|
||||
Reference in New Issue
Block a user