mirror of
https://github.com/bensema/gotdx.git
synced 2025-11-21 02:45:33 +08:00
readme
This commit is contained in:
52
README.md
52
README.md
@@ -1 +1,53 @@
|
||||
# gotdx
|
||||
|
||||
|
||||
## Example
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"gotdx"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var opt = &gotdx.Opt{
|
||||
Host: "119.147.212.81",
|
||||
Port: 7709,
|
||||
}
|
||||
api := gotdx.NewClient(opt)
|
||||
connectReply, err := api.Connect()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
log.Println(connectReply.Info)
|
||||
|
||||
reply, err := api.GetSecurityQuotes([]uint8{gotdx.MarketSh, gotdx.MarketSz}, []string{"000001", "600008"})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
for _, obj := range reply.List {
|
||||
log.Println(obj)
|
||||
}
|
||||
|
||||
_ = api.Disconnect()
|
||||
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
- Connect 连接券商行情服务器
|
||||
- Disconnect 断开服务器
|
||||
- GetSecurityCount 获取指定市场内的证券数目
|
||||
- GetSecurityQuotes 获取盘口五档报价
|
||||
- GetSecurityList 获取市场内指定范围内的所有证券代码
|
||||
- GetSecurityBars 获取股票K线
|
||||
- GetIndexBars 获取指数K线
|
||||
- GetMinuteTimeData 获取分时图数据
|
||||
- GetHistoryMinuteTimeData 获取历史分时图数据
|
||||
- GetTransactionData 获取分时成交
|
||||
- GetHistoryTransactionData 获取历史分时成交
|
||||
48
client.go
48
client.go
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"gotdx/proto"
|
||||
"io"
|
||||
"log"
|
||||
@@ -142,8 +143,18 @@ func (client *Client) GetSecurityCount(market uint16) (*proto.GetSecurityCountRe
|
||||
}
|
||||
|
||||
// GetSecurityQuotes 获取盘口五档报价
|
||||
func (client *Client) GetSecurityQuotes(params []proto.Stock) (*proto.GetSecurityQuotesReply, error) {
|
||||
func (client *Client) GetSecurityQuotes(markets []uint8, codes []string) (*proto.GetSecurityQuotesReply, error) {
|
||||
if len(markets) != len(codes) {
|
||||
return nil, errors.New("market code count error")
|
||||
}
|
||||
obj := proto.NewGetSecurityQuotes()
|
||||
var params []proto.Stock
|
||||
for i, market := range markets {
|
||||
params = append(params, proto.Stock{
|
||||
Market: market,
|
||||
Code: codes[i],
|
||||
})
|
||||
}
|
||||
obj.SetParams(&proto.GetSecurityQuotesRequest{StockList: params})
|
||||
err := client.do(obj)
|
||||
if err != nil {
|
||||
@@ -153,9 +164,10 @@ func (client *Client) GetSecurityQuotes(params []proto.Stock) (*proto.GetSecurit
|
||||
}
|
||||
|
||||
// GetSecurityList 获取市场内指定范围内的所有证券代码
|
||||
func (client *Client) GetSecurityList(market uint16, start uint16) (*proto.GetSecurityListReply, error) {
|
||||
func (client *Client) GetSecurityList(market uint8, start uint16) (*proto.GetSecurityListReply, error) {
|
||||
obj := proto.NewGetSecurityList()
|
||||
obj.SetParams(&proto.GetSecurityListRequest{Market: market, Start: start})
|
||||
_market := uint16(market)
|
||||
obj.SetParams(&proto.GetSecurityListRequest{Market: _market, Start: start})
|
||||
err := client.do(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -164,13 +176,13 @@ func (client *Client) GetSecurityList(market uint16, start uint16) (*proto.GetSe
|
||||
}
|
||||
|
||||
// GetSecurityBars 获取股票K线
|
||||
// e Category, byte Market, char* Zqdm, short Start, short& Count, char* Result, char* ErrInfo
|
||||
func (client *Client) GetSecurityBars(category uint16, market uint16, code string, start uint16, count uint16) (*proto.GetSecurityBarsReply, error) {
|
||||
func (client *Client) GetSecurityBars(category uint16, market uint8, code string, start uint16, count uint16) (*proto.GetSecurityBarsReply, error) {
|
||||
obj := proto.NewGetSecurityBars()
|
||||
_code := [6]byte{}
|
||||
_market := uint16(market)
|
||||
copy(_code[:], code)
|
||||
obj.SetParams(&proto.GetSecurityBarsRequest{
|
||||
Market: market,
|
||||
Market: _market,
|
||||
Code: _code,
|
||||
Category: category,
|
||||
Start: start,
|
||||
@@ -184,12 +196,13 @@ func (client *Client) GetSecurityBars(category uint16, market uint16, code strin
|
||||
}
|
||||
|
||||
// GetIndexBars 获取指数K线
|
||||
func (client *Client) GetIndexBars(category uint16, market uint16, code string, start uint16, count uint16) (*proto.GetIndexBarsReply, error) {
|
||||
func (client *Client) GetIndexBars(category uint16, market uint8, code string, start uint16, count uint16) (*proto.GetIndexBarsReply, error) {
|
||||
obj := proto.NewGetIndexBars()
|
||||
_code := [6]byte{}
|
||||
_market := uint16(market)
|
||||
copy(_code[:], code)
|
||||
obj.SetParams(&proto.GetIndexBarsRequest{
|
||||
Market: market,
|
||||
Market: _market,
|
||||
Code: _code,
|
||||
Category: category,
|
||||
Start: start,
|
||||
@@ -203,12 +216,13 @@ func (client *Client) GetIndexBars(category uint16, market uint16, code string,
|
||||
}
|
||||
|
||||
// GetMinuteTimeData 获取分时图数据
|
||||
func (client *Client) GetMinuteTimeData(market uint16, code string) (*proto.GetMinuteTimeDataReply, error) {
|
||||
func (client *Client) GetMinuteTimeData(market uint8, code string) (*proto.GetMinuteTimeDataReply, error) {
|
||||
obj := proto.NewGetMinuteTimeData()
|
||||
_code := [6]byte{}
|
||||
_market := uint16(market)
|
||||
copy(_code[:], code)
|
||||
obj.SetParams(&proto.GetMinuteTimeDataRequest{
|
||||
Market: market,
|
||||
Market: _market,
|
||||
Code: _code,
|
||||
})
|
||||
err := client.do(obj)
|
||||
@@ -219,13 +233,13 @@ func (client *Client) GetMinuteTimeData(market uint16, code string) (*proto.GetM
|
||||
}
|
||||
|
||||
// GetHistoryMinuteTimeData 获取历史分时图数据
|
||||
func (client *Client) GetHistoryMinuteTimeData(date uint32, market uint16, code string) (*proto.GetHistoryMinuteTimeDataReply, error) {
|
||||
func (client *Client) GetHistoryMinuteTimeData(date uint32, market uint8, code string) (*proto.GetHistoryMinuteTimeDataReply, error) {
|
||||
obj := proto.NewGetHistoryMinuteTimeData()
|
||||
_code := [6]byte{}
|
||||
copy(_code[:], code)
|
||||
obj.SetParams(&proto.GetHistoryMinuteTimeDataRequest{
|
||||
Date: date,
|
||||
Market: uint8(market),
|
||||
Market: market,
|
||||
Code: _code,
|
||||
})
|
||||
err := client.do(obj)
|
||||
@@ -236,12 +250,13 @@ func (client *Client) GetHistoryMinuteTimeData(date uint32, market uint16, code
|
||||
}
|
||||
|
||||
// GetTransactionData 获取分时成交
|
||||
func (client *Client) GetTransactionData(market uint16, code string, start uint16, count uint16) (*proto.GetTransactionDataReply, error) {
|
||||
func (client *Client) GetTransactionData(market uint8, code string, start uint16, count uint16) (*proto.GetTransactionDataReply, error) {
|
||||
obj := proto.NewGetTransactionData()
|
||||
_code := [6]byte{}
|
||||
_market := uint16(market)
|
||||
copy(_code[:], code)
|
||||
obj.SetParams(&proto.GetTransactionDataRequest{
|
||||
Market: market,
|
||||
Market: _market,
|
||||
Code: _code,
|
||||
Start: start,
|
||||
Count: count,
|
||||
@@ -254,13 +269,14 @@ func (client *Client) GetTransactionData(market uint16, code string, start uint1
|
||||
}
|
||||
|
||||
// GetHistoryTransactionData 获取历史分时成交
|
||||
func (client *Client) GetHistoryTransactionData(date uint32, market uint16, code string, start uint16, count uint16) (*proto.GetHistoryTransactionDataReply, error) {
|
||||
func (client *Client) GetHistoryTransactionData(date uint32, market uint8, code string, start uint16, count uint16) (*proto.GetHistoryTransactionDataReply, error) {
|
||||
obj := proto.NewGetHistoryTransactionData()
|
||||
_code := [6]byte{}
|
||||
_market := uint16(market)
|
||||
copy(_code[:], code)
|
||||
obj.SetParams(&proto.GetHistoryTransactionDataRequest{
|
||||
Date: date,
|
||||
Market: market,
|
||||
Market: _market,
|
||||
Code: _code,
|
||||
Start: start,
|
||||
Count: count,
|
||||
|
||||
@@ -8,19 +8,16 @@ import (
|
||||
|
||||
var opt = &Opt{
|
||||
Host: "119.147.212.81",
|
||||
//Host: "58.63.254.191",
|
||||
//Host: "218.16.117.138",
|
||||
//Host: "222.85.139.177",
|
||||
Port: 7709,
|
||||
}
|
||||
|
||||
func prepare() *Client {
|
||||
api := NewClient(opt)
|
||||
r, err := api.Connect()
|
||||
reply, err := api.Connect()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(r)
|
||||
fmt.Println(reply.Info)
|
||||
return api
|
||||
}
|
||||
|
||||
@@ -28,10 +25,9 @@ func Test_tdx_Connect(t *testing.T) {
|
||||
api := NewClient(opt)
|
||||
reply, err := api.Connect()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
fmt.Println(reply)
|
||||
|
||||
fmt.Println(reply.Info)
|
||||
_ = api.Disconnect()
|
||||
|
||||
}
|
||||
@@ -40,24 +36,23 @@ func Test_tdx_GetSecurityCount(t *testing.T) {
|
||||
api := prepare()
|
||||
reply, err := api.GetSecurityCount(MarketSh)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
fmt.Println(reply)
|
||||
fmt.Println(reply.Count)
|
||||
|
||||
_ = api.Disconnect()
|
||||
}
|
||||
|
||||
func Test_tdx_GetSecurityQuotes(t *testing.T) {
|
||||
api := prepare()
|
||||
params := []proto.Stock{}
|
||||
params = append(params, proto.Stock{Market: MarketSh, Code: "002062"})
|
||||
params = append(params, proto.Stock{Market: MarketSh, Code: "000001"})
|
||||
reply, err := api.GetSecurityQuotes(params)
|
||||
reply, err := api.GetSecurityQuotes([]uint8{MarketSh}, []string{"002062"})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
|
||||
fmt.Println(reply)
|
||||
for _, obj := range reply.List {
|
||||
fmt.Println(obj)
|
||||
}
|
||||
|
||||
_ = api.Disconnect()
|
||||
|
||||
@@ -67,10 +62,11 @@ func Test_tdx_GetSecurityList(t *testing.T) {
|
||||
api := prepare()
|
||||
reply, err := api.GetSecurityList(MarketSh, 0)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
for _, obj := range reply.List {
|
||||
fmt.Println(obj)
|
||||
}
|
||||
|
||||
fmt.Println(reply)
|
||||
|
||||
_ = api.Disconnect()
|
||||
|
||||
@@ -81,12 +77,11 @@ func Test_tdx_GetSecurityBars(t *testing.T) {
|
||||
api := prepare()
|
||||
reply, err := api.GetSecurityBars(proto.KLINE_TYPE_RI_K, 0, "000001", 0, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
|
||||
fmt.Println(reply)
|
||||
for _, bar := range reply.List {
|
||||
fmt.Println(bar)
|
||||
for _, obj := range reply.List {
|
||||
fmt.Println(obj)
|
||||
}
|
||||
|
||||
_ = api.Disconnect()
|
||||
@@ -95,16 +90,14 @@ func Test_tdx_GetSecurityBars(t *testing.T) {
|
||||
|
||||
func Test_tdx_GetIndexBars(t *testing.T) {
|
||||
// GetSecurityBars 与 GetIndexBars 使用同一个接口靠market区分
|
||||
|
||||
api := prepare()
|
||||
reply, err := api.GetIndexBars(proto.KLINE_TYPE_RI_K, 1, "000001", 0, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
|
||||
fmt.Println(reply)
|
||||
for _, bar := range reply.List {
|
||||
fmt.Println(bar)
|
||||
for _, obj := range reply.List {
|
||||
fmt.Println(obj)
|
||||
}
|
||||
|
||||
_ = api.Disconnect()
|
||||
@@ -115,12 +108,11 @@ func Test_tdx_GetMinuteTimeData(t *testing.T) {
|
||||
api := prepare()
|
||||
reply, err := api.GetMinuteTimeData(0, "159607")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
|
||||
fmt.Println(reply)
|
||||
for _, bar := range reply.List {
|
||||
fmt.Println(bar)
|
||||
for _, obj := range reply.List {
|
||||
fmt.Println(obj)
|
||||
}
|
||||
|
||||
_ = api.Disconnect()
|
||||
@@ -132,12 +124,11 @@ func Test_tdx_GetHistoryMinuteTimeData(t *testing.T) {
|
||||
//reply, err := api.GetHistoryMinuteTimeData(20220511, 0, "159607")
|
||||
reply, err := api.GetHistoryMinuteTimeData(20220511, 0, "159607")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
|
||||
fmt.Println(reply)
|
||||
for _, bar := range reply.List {
|
||||
fmt.Println(bar)
|
||||
for _, obj := range reply.List {
|
||||
fmt.Println(obj)
|
||||
}
|
||||
|
||||
_ = api.Disconnect()
|
||||
@@ -149,12 +140,11 @@ func Test_tdx_GetTransactionData(t *testing.T) {
|
||||
//reply, err := api.GetHistoryMinuteTimeData(20220511, 0, "159607")
|
||||
reply, err := api.GetTransactionData(0, "159607", 0, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
|
||||
fmt.Println(reply)
|
||||
for _, bar := range reply.List {
|
||||
fmt.Println(bar)
|
||||
for _, obj := range reply.List {
|
||||
fmt.Println(obj)
|
||||
}
|
||||
|
||||
_ = api.Disconnect()
|
||||
@@ -166,12 +156,11 @@ func Test_tdx_GetHistoryTransactionData(t *testing.T) {
|
||||
//reply, err := api.GetHistoryMinuteTimeData(20220511, 0, "159607")
|
||||
reply, err := api.GetHistoryTransactionData(20220511, 0, "159607", 0, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Errorf("error:%s", err)
|
||||
}
|
||||
|
||||
fmt.Println(reply)
|
||||
for _, bar := range reply.List {
|
||||
fmt.Println(bar)
|
||||
for _, obj := range reply.List {
|
||||
fmt.Println(obj)
|
||||
}
|
||||
|
||||
_ = api.Disconnect()
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type GetHistoryMinuteTimeData struct {
|
||||
@@ -77,7 +76,6 @@ func (obj *GetHistoryMinuteTimeData) Serialize() ([]byte, error) {
|
||||
///20150520\t4.756000\t4.850000\t4.960000\t4.756000\t353161092\t1722953216.000000”
|
||||
func (obj *GetHistoryMinuteTimeData) UnSerialize(header interface{}, data []byte) error {
|
||||
obj.respHeader = header.(*RespHeader)
|
||||
fmt.Println(hex.EncodeToString(data))
|
||||
|
||||
pos := 0
|
||||
err := binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &obj.reply.Count)
|
||||
|
||||
@@ -89,8 +89,6 @@ func (obj *GetIndexBars) Serialize() ([]byte, error) {
|
||||
func (obj *GetIndexBars) UnSerialize(header interface{}, data []byte) error {
|
||||
obj.respHeader = header.(*RespHeader)
|
||||
|
||||
fmt.Println(hex.EncodeToString(data))
|
||||
|
||||
pos := 0
|
||||
err := binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &obj.reply.Count)
|
||||
pos += 2
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type GetMinuteTimeData struct {
|
||||
@@ -79,7 +78,7 @@ func (obj *GetMinuteTimeData) Serialize() ([]byte, error) {
|
||||
///20150520\t4.756000\t4.850000\t4.960000\t4.756000\t353161092\t1722953216.000000”
|
||||
func (obj *GetMinuteTimeData) UnSerialize(header interface{}, data []byte) error {
|
||||
obj.respHeader = header.(*RespHeader)
|
||||
fmt.Println(hex.EncodeToString(data))
|
||||
|
||||
pos := 0
|
||||
err := binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &obj.reply.Count)
|
||||
// 跳过4个字节
|
||||
|
||||
@@ -41,8 +41,8 @@ func (obj *GetSecurityCount) SetParams(req *GetSecurityCountRequest) {
|
||||
}
|
||||
|
||||
func (obj *GetSecurityCount) Serialize() ([]byte, error) {
|
||||
obj.reqHeader.PkgLen1 = 2 + uint16(len(obj.contentHex)) + 2
|
||||
obj.reqHeader.PkgLen2 = 2 + uint16(len(obj.contentHex)) + 2
|
||||
obj.reqHeader.PkgLen1 = 2 + 4 + 2
|
||||
obj.reqHeader.PkgLen2 = 2 + 4 + 2
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
err := binary.Write(buf, binary.LittleEndian, obj.reqHeader)
|
||||
|
||||
@@ -3,7 +3,7 @@ package proto
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"gotdx/utils"
|
||||
"gotdx/util"
|
||||
)
|
||||
|
||||
type GetSecurityList struct {
|
||||
@@ -88,7 +88,7 @@ func (obj *GetSecurityList) UnSerialize(header interface{}, data []byte) error {
|
||||
binary.Read(bytes.NewBuffer(data[pos:pos+8]), binary.LittleEndian, &name)
|
||||
pos += 8
|
||||
|
||||
ele.Code = utils.Utf8ToGbk(name[:])
|
||||
ele.Code = util.Utf8ToGbk(name[:])
|
||||
|
||||
pos += 4
|
||||
binary.Read(bytes.NewBuffer(data[pos:pos+1]), binary.LittleEndian, &ele.DecimalPoint)
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"gotdx/utils"
|
||||
"gotdx/util"
|
||||
)
|
||||
|
||||
type GetSecurityQuotes struct {
|
||||
@@ -146,7 +146,7 @@ func (obj *GetSecurityQuotes) UnSerialize(header interface{}, data []byte) error
|
||||
binary.Read(bytes.NewBuffer(data[pos:pos+6]), binary.LittleEndian, &code)
|
||||
//enc := mahonia.NewDecoder("gbk")
|
||||
//ele.Code = enc.ConvertString(string(code[:]))
|
||||
ele.Code = utils.Utf8ToGbk(code[:])
|
||||
ele.Code = util.Utf8ToGbk(code[:])
|
||||
pos += 6
|
||||
binary.Read(bytes.NewBuffer(data[pos:pos+2]), binary.LittleEndian, &ele.Active1)
|
||||
pos += 2
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"gotdx/utils"
|
||||
"gotdx/util"
|
||||
)
|
||||
|
||||
type Hello1 struct {
|
||||
@@ -59,7 +59,7 @@ func (obj *Hello1) Serialize() ([]byte, error) {
|
||||
func (obj *Hello1) UnSerialize(header interface{}, data []byte) error {
|
||||
obj.respHeader = header.(*RespHeader)
|
||||
|
||||
serverInfo := utils.Utf8ToGbk(data[68:])
|
||||
serverInfo := util.Utf8ToGbk(data[68:])
|
||||
|
||||
obj.reply.Info = serverInfo
|
||||
return nil
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"gotdx/utils"
|
||||
"gotdx/util"
|
||||
)
|
||||
|
||||
type Hello2 struct {
|
||||
@@ -57,7 +57,7 @@ func (obj *Hello2) Serialize() ([]byte, error) {
|
||||
func (obj *Hello2) UnSerialize(header interface{}, data []byte) error {
|
||||
obj.respHeader = header.(*RespHeader)
|
||||
|
||||
serverInfo := utils.Utf8ToGbk(data[58:])
|
||||
serverInfo := util.Utf8ToGbk(data[58:])
|
||||
//fmt.Println(hex.EncodeToString(data))
|
||||
obj.reply.Info = serverInfo
|
||||
return nil
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
Reference in New Issue
Block a user