Compare commits

...

21 Commits

Author SHA1 Message Date
钱纯净
3cf7148b2c 增加几个指数相关的方法,方便使用 2025-03-15 15:42:50 +08:00
钱纯净
0edd96d75b 检测服务器IP的有效性 2025-03-15 15:18:05 +08:00
钱纯净
4035024c3f 修改价格的单位为厘(0.001元),以适配基金的数据 2025-03-15 15:15:05 +08:00
钱纯净
fa7f5e7068 修改示例名称 2025-03-15 15:12:20 +08:00
钱纯净
c357ca5995 增加历史分时图的示例 2025-03-15 15:11:48 +08:00
钱纯净
d35d936f8f 优化指数数据的获取方式,从K线中拆出,方便解析 2025-03-15 15:11:20 +08:00
钱纯净
66781b07c2 优化指数数据的获取方式,从K线中拆出,方便解析 2025-03-15 15:10:36 +08:00
钱纯净
26d37ba0b8 增加历史分时的枚举 2025-03-15 15:09:24 +08:00
钱纯净
db2cefc434 增加历史分时的枚举 2025-03-15 15:09:14 +08:00
钱纯净
3babf67cde 修改代码信息的字段PreClose,为LastClose,表示最新价(昨日收盘价,指数和基金有效,个股无效) 2025-03-15 15:08:45 +08:00
钱纯净
2e6b0e5e2e 增加(历史)分时图的时间字段"15:00" 2025-03-15 15:06:49 +08:00
钱纯净
db7f1a760f 增加历史分时图 2025-03-15 15:05:37 +08:00
钱纯净
1d552a0fc7 增加存储字段,小数点,倍数,和昨收价(对指数有效,个股无效) 2025-03-15 15:05:10 +08:00
钱纯净
4d8d9f1ad9 增加函数DialHostsRandom,随机连接设置的ip池 2025-03-12 22:18:34 +08:00
钱纯净
78a0bb9702 有个字段好像匹配上了基金的昨收净值? 2025-03-12 21:26:12 +08:00
钱纯净
c0850a0057 测试传参方式"sz000001"->"000001",只适用于个股 2025-03-12 20:47:07 +08:00
钱纯净
1f0958b53e 兼容代码传参方式,个股可以忽略交易所标识,例sz000001,可以传000001 2025-03-12 20:41:26 +08:00
钱纯净
344b2f467d 增加NewRandomDial,用于随机连接,防止连接池对一个IP建立大量连接 2025-03-05 23:14:59 +08:00
钱纯净
81c45e76d0 修复Codes,更新数据的时候,少了交易所的条件 2025-03-05 22:54:23 +08:00
钱纯净
a8c248bf9d 修复Codes,更新数据的时候,少了交易所的条件 2025-03-05 22:50:45 +08:00
钱纯净
3e57f54978 增加全局变量ExchangeEstablish(上海交易所成立时间) 2025-03-03 22:35:43 +08:00
20 changed files with 322 additions and 124 deletions

View File

@@ -58,45 +58,45 @@ func main() {
#### 服务器地址(端口7709)
| IP | 测试时间 | 所属地区 | 运营商 |
|-----------------|-------------|------|-----|
| 124.71.187.122 | 2024-11-30 | 上海 | 华为 |
| 122.51.120.217 | 2024-11-30 | 上海 | 腾讯 |
| 111.229.247.189 | 2024-11-30 | 上海 | 腾讯 |
| 124.70.176.52 | 2024-11-30 | 上海 | 华为 |
| 123.60.186.45 | 2024-11-30 | 上海 | 华为 |
| 122.51.232.182 | 2024-11-30 | 上海 | 腾讯 |
| 118.25.98.114 | 2024-11-30 | 上海 | 腾讯 |
| 124.70.199.56 | 2024-11-30 | 上海 | 华为 |
| 121.36.225.169 | 2024-11-30 | 上海 | 华为 |
| 123.60.70.228 | 2024-11-30 | 上海 | 华为 |
| 123.60.73.44 | 2024-11-30 | 上海 | 华为 |
| 124.70.133.119 | 2024-11-30 | 上海 | 华为 |
| 124.71.187.72 | 2024-11-30 | 上海 | 华为 |
| 123.60.84.66 | 2024-11-30 | 上海 | 华为 |
| | | | |
| 121.36.54.217 | 2024-11-30 | 北京 | 华为 |
| 121.36.81.195 | 2024-11-30 | 北京 | 华为 |
| 123.249.15.60 | 2024-11-30 | 北京 | 华为 |
| 124.70.75.113 | 2024-11-30 | 北京 | 华为 |
| 120.46.186.223 | 2024-11-30 | 北京 | 华为 |
| 124.70.22.210 | 2024-11-30 | 北京 | 华为 |
| 139.9.133.247 | 2024-11-30 | 北京 | 华为 |
| | | | |
| 124.71.85.110 | 2024-11-30 | 广州 | 华为 |
| 139.9.51.18 | 2024-11-304 | 广州 | 华为 |
| 139.159.239.163 | 2024-11-30 | 广州 | 华为 |
| 124.71.9.153 | 2024-11-30 | 广州 | 华为 |
| 116.205.163.254 | 2024-11-30 | 广州 | 华为 |
| 116.205.171.132 | 2024-11-30 | 广州 | 华为 |
| 116.205.183.150 | 2024-11-30 | 广州 | 华为 |
| 111.230.186.52 | 2024-11-30 | 广州 | 腾讯 |
| 110.41.4.4 | 2024-11-30 | 广州 | 华为 |
| 110.41.2.72 | 2024-11-30 | 广州 | 华为 |
| 110.41.154.219 | 2024-11-30 | 广州 | 华为 |
| 110.41.147.114 | 2024-11-30 | 广州 | 华为 |
| | | | |
| 119.97.185.59 | 2024-11-30 | 武汉 | 电信 |
| IP | 测试时间 | 所属地区 | 运营商 |
|-----------------|------------|------|-----|
| 124.71.187.122 | 2025-03-15 | 上海 | 华为 |
| 122.51.120.217 | 2025-03-15 | 上海 | 腾讯 |
| 111.229.247.189 | 2025-03-15 | 上海 | 腾讯 |
| 124.70.176.52 | 2025-03-15 | 上海 | 华为 |
| 123.60.186.45 | 2025-03-15 | 上海 | 华为 |
| 122.51.232.182 | 2025-03-15 | 上海 | 腾讯 |
| 118.25.98.114 | 2025-03-15 | 上海 | 腾讯 |
| 124.70.199.56 | 2025-03-15 | 上海 | 华为 |
| 121.36.225.169 | 2025-03-15 | 上海 | 华为 |
| 123.60.70.228 | 2025-03-15 | 上海 | 华为 |
| 123.60.73.44 | 2025-03-15 | 上海 | 华为 |
| 124.70.133.119 | 2025-03-15 | 上海 | 华为 |
| 124.71.187.72 | 2025-03-15 | 上海 | 华为 |
| 123.60.84.66 | 2025-03-15 | 上海 | 华为 |
| | | | |
| 121.36.54.217 | 2025-03-15 | 北京 | 华为 |
| 121.36.81.195 | 2025-03-15 | 北京 | 华为 |
| 123.249.15.60 | 2025-03-15 | 北京 | 华为 |
| 124.70.75.113 | 2025-03-15 | 北京 | 华为 |
| 120.46.186.223 | 2025-03-15 | 北京 | 华为 |
| 124.70.22.210 | 2025-03-15 | 北京 | 华为 |
| 139.9.133.247 | 2025-03-15 | 北京 | 华为 |
| | | | |
| 124.71.85.110 | 2025-03-15 | 广州 | 华为 |
| 139.9.51.18 | 2025-03-15 | 广州 | 华为 |
| 139.159.239.163 | 2025-03-15 | 广州 | 华为 |
| 124.71.9.153 | 2025-03-15 | 广州 | 华为 |
| 116.205.163.254 | 2025-03-15 | 广州 | 华为 |
| 116.205.171.132 | 2025-03-15 | 广州 | 华为 |
| 116.205.183.150 | 2025-03-15 | 广州 | 华为 |
| 111.230.186.52 | 2025-03-15 | 广州 | 腾讯 |
| 110.41.4.4 | 2025-03-15 | 广州 | 华为 |
| 110.41.2.72 | 2025-03-15 | 广州 | 华为 |
| 110.41.154.219 | 2025-03-15 | 广州 | 华为 |
| 110.41.147.114 | 2025-03-15 | 广州 | 华为 |
| | | | |
| 119.97.185.59 | 2025-03-15 | 武汉 | 电信 |

View File

@@ -48,6 +48,11 @@ 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...)
}
// DialWith 与服务器建立连接
func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) {
@@ -128,6 +133,9 @@ 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))
@@ -218,8 +226,10 @@ func (this *Client) GetQuote(codes ...string) (protocol.QuotesResp, error) {
return result.(protocol.QuotesResp), 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
@@ -231,8 +241,22 @@ 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
}
// GetMinuteTrade 获取分时交易详情,服务器最多返回1800条,count-start<=1800
func (this *Client) GetMinuteTrade(code string, start, count uint16) (*protocol.MinuteTradeResp, error) {
code = protocol.AddPrefix(code)
f, err := protocol.MMinuteTrade.Frame(code, start, count)
if err != nil {
return nil, err
@@ -267,6 +291,7 @@ func (this *Client) GetMinuteTradeAll(code string) (*protocol.MinuteTradeResp, e
// 只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0
// 历史数据sz000001在20241116只能查到21111112,13年差几天,3141天,或者其他规则
func (this *Client) GetHistoryMinuteTrade(date, code string, start, count uint16) (*protocol.HistoryMinuteTradeResp, error) {
code = protocol.AddPrefix(code)
f, err := protocol.MHistoryMinuteTrade.Frame(date, code, start, count)
if err != nil {
return nil, err
@@ -297,13 +322,68 @@ 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, protocol.KlineCache{Type: Type, Code: code})
result, err := this.SendFrame(f, protocol.KlineCache{Type: Type, Kind: protocol.KindIndex})
if err != nil {
return nil, err
}
return result.(*protocol.KlineResp), nil
}
// 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.GetKline(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
}
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)
}
// 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
}

View File

@@ -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)
})
}

View File

@@ -120,16 +120,22 @@ func (this *Codes) Code(byDatabase bool) ([]*CodeModel, error) {
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(),
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(),
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)
@@ -145,7 +151,7 @@ func (this *Codes) Code(byDatabase bool) ([]*CodeModel, error) {
}
}
for _, v := range update {
if _, err := session.Where("Code=?", v.Code).Cols("Name").Update(v); err != nil {
if _, err := session.Where("Exchange=? and Code=? ", v.Exchange, v.Code).Cols("Name").Update(v); err != nil {
return err
}
}
@@ -160,12 +166,15 @@ func (this *Codes) Code(byDatabase bool) ([]*CodeModel, error) {
}
type CodeModel struct {
ID int64 `json:"id"` //主键
Name string `json:"name"` //名称,有时候名称会变,例STxxx
Code string `json:"code" xorm:"index"` //代码
Exchange string `json:"exchange" xorm:"index"` //交易所
EditDate int64 `json:"editDate" xorm:"updated"` //修改时间
InDate int64 `json:"inDate" xorm:"created"` //创建时间
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 (c *CodeModel) TableName() string {

17
dial.go
View File

@@ -3,8 +3,10 @@ package tdx
import (
"context"
"github.com/injoyai/ios"
"math/rand"
"net"
"strings"
"time"
)
func NewHostDial(hosts []string) ios.DialFunc {
@@ -26,3 +28,18 @@ func NewHostDial(hosts []string) ios.DialFunc {
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
}
}

View 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)
})
}

View File

@@ -8,7 +8,7 @@ import (
func main() {
common.Test(func(c *tdx.Client) {
resp, err := c.GetKlineDay("sh000001", 0, 10)
resp, err := c.GetKlineDay("000001", 0, 10)
logs.PanicErr(err)
for _, v := range resp.List {

View File

@@ -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 {

View File

@@ -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 (

View File

@@ -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: getVolume(Uint32(bs[21:25])),
}
//logs.Debug(bs[25:29]) //26和28字节 好像是枚举(基本是44,45和34,35)
bs = bs[29:]

View File

@@ -11,6 +11,7 @@ var (
MQuote = quote{}
MCode = code{}
MMinute = minute{}
MHistoryMinute = historyMinute{}
MMinuteTrade = minuteTrade{}
MHistoryMinuteTrade = historyMinuteTrade{}
MKline = kline{}

View 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)
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
}

View File

@@ -25,7 +25,7 @@ func (this *HistoryMinuteTrade) String() string {
// Amount 成交额
func (this *HistoryMinuteTrade) Amount() Price {
return this.Price * Price(this.Volume) * 100
return this.Price * Price(this.Volume*100)
}
func (this *HistoryMinuteTrade) StatusString() string {
@@ -82,7 +82,7 @@ func (historyMinuteTrade) Decode(bs []byte, code string) (*HistoryMinuteTradeRes
}
var sub Price
bs, sub = GetPrice(bs[2:])
lastPrice += sub
lastPrice += sub * 10 //把分转成厘
mt.Price = lastPrice / basePrice(number)
bs, mt.Volume = CutInt(bs)
bs, mt.Status = CutInt(bs)

View File

@@ -51,9 +51,9 @@ type Kline struct {
}
func (this *Kline) String() string {
return fmt.Sprintf("%s 昨收盘:%s 开盘价:%s 最高价:%s 最低价:%s 收盘价:%s 涨跌:%s 涨跌幅:%0.2f 成交量:%s 成交额:%s 涨跌数: %d/%d",
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,
@@ -133,11 +133,11 @@ func (kline) Decode(bs []byte, c KlineCache) (*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
/*
@@ -160,11 +160,12 @@ func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) {
case TypeKlineMinute, TypeKline5Minute, TypeKlineMinute2, TypeKline15Minute, TypeKline30Minute, TypeKlineHour, TypeKlineDay2:
k.Volume /= 100
}
k.Amount = Price(getVolume(Uint32(bs[:4])) * 100) //从元转为,并去除多余的小数
k.Amount = Price(getVolume(Uint32(bs[:4])) * 1000) //从元转为,并去除多余的小数
bs = bs[4:]
//指数和股票的差别,指数多解析4字节,并处理成交量*100
if !IsStock(c.Code) {
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]})
@@ -178,6 +179,6 @@ func (kline) Decode(bs []byte, c KlineCache) (*KlineResp, error) {
}
type KlineCache struct {
Type uint8
Code string
Type uint8 //1分钟,5分钟,日线等
Kind string //指数,个股等
}

View File

@@ -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,
})

View File

@@ -15,7 +15,7 @@ type MinuteTrade struct {
Time string //时间
Price Price //价格
Volume int //成交量
Number int //单数,历史数据字段无效
Number int //单数,历史数据字段无效
Status int //0是买1是卖2无效汇总出现
}
@@ -103,7 +103,7 @@ func (minuteTrade) Decode(bs []byte, code string) (*MinuteTradeResp, error) {
}
var sub Price
bs, sub = GetPrice(bs[2:])
lastPrice += sub
lastPrice += sub * 10 //把分转换成厘
mt.Price = lastPrice / basePrice(code)
bs, mt.Volume = CutInt(bs)
bs, mt.Number = CutInt(bs)

View File

@@ -30,7 +30,7 @@ type Quote struct {
OuterDisc int // 外盘(东财的盘口-外盘)(和东财对不上)
ReversedBytes2 int // 保留,未知
ReversedBytes3 int // 保留,未知
ReversedBytes3 int // 保留,未知,基金的昨收净值?
BuyLevel PriceLevels // 5档买盘(买1-5)
SellLevel PriceLevels // 5档卖盘(卖1-5)

View File

@@ -60,3 +60,8 @@ const (
TypeKlineQuarter uint8 = 10 // 季K 线
TypeKlineYear uint8 = 11 // 年K 线
)
const (
KindIndex = "index"
KindStock = "stock"
)

View File

@@ -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 {

View File

@@ -50,6 +50,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")
}
@@ -212,3 +213,18 @@ func IsStock(code string) bool {
}
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
}
}
return code
}