6 Star 18 Fork 28

王布衣 / gotdx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
block_raw.go 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
package securities
import (
"gitee.com/quant1x/exchange"
"gitee.com/quant1x/exchange/cache"
"gitee.com/quant1x/gotdx"
"gitee.com/quant1x/gox/api"
"gitee.com/quant1x/gox/encoding/binary/struc"
"gitee.com/quant1x/gox/text/encoding"
"os"
"strings"
)
// 下载板块原始数据文件
func downloadBlockRawData(filename string) {
tdxApi := gotdx.GetTdxApi()
fn := cache.GetBlockPath() + "/" + filename
fileInfo, err := os.Stat(fn)
if err == nil || os.IsExist(err) {
toInit := exchange.CanInitialize(fileInfo.ModTime())
if !toInit {
return
}
}
resp, err := tdxApi.GetBlockInfo(filename)
if err == nil {
fn := cache.GetBlockPath() + "/" + filename
_ = api.CheckFilepath(fn, true)
fp, err := os.Create(fn)
if err == nil {
_, _ = fp.Write(resp.Data)
_ = fp.Close()
}
}
}
type __raw_block_info struct {
BlockName string `struc:"[9]byte,little"` // 板块名称
Num uint16 `struc:"uint16,little"` // 个股数量
BlockType uint16 `struc:"uint16,little"` // 板块类型
List [400]__block_stock `struct:"[400]__block_stock,little"` // 个股列表
}
type __block_stock struct {
Code string `struc:"[7]byte,little"` // 证券代码
}
type __raw_block_data struct {
//Header blockHeader `struc:"[386]byte,little"`
Unknown [384]byte `struc:"[384]byte,little"` // 头信息, 忽略
Count uint16 `struc:"uint16,little,sizeof=Data"` // 板块数量
Data []__raw_block_info `struc:"[2813]byte, little"` // 板块数据
}
func parseRawBlockData(blockFilename string) *__raw_block_data {
fn := cache.GetBlockPath() + "/" + blockFilename
_ = api.CheckFilepath(fn, true)
file, err := os.Open(fn)
if err != nil {
return nil
}
defer api.CloseQuietly(file)
var block __raw_block_data
err = struc.Unpack(file, &block)
if err != nil {
return nil
}
decoder := encoding.NewDecoder("GBK")
for i, v := range block.Data {
name := decoder.ConvertString(v.BlockName)
block.Data[i].BlockName = strings.ReplaceAll(name, string([]byte{0x00}), "")
for j, s := range v.List {
block.Data[i].List[j].Code = strings.ReplaceAll(s.Code, string([]byte{0x00}), "")
}
}
return &block
}
Go
1
https://gitee.com/quant1x/gotdx.git
git@gitee.com:quant1x/gotdx.git
quant1x
gotdx
gotdx
v1.22.6

搜索帮助