mirror of
https://github.com/injoyai/tdx.git
synced 2025-11-26 21:25:35 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
47084b1112 | ||
|
|
2566ef5cec |
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))
|
||||
}
|
||||
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,
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
102
extend/codes_bj.go
Normal file
102
extend/codes_bj.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package extend
|
||||
|
||||
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"` //成交额,元
|
||||
}
|
||||
@@ -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"` //后复权因子
|
||||
}
|
||||
Reference in New Issue
Block a user