forked from bensema/gotdx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_exquote.go
More file actions
258 lines (227 loc) · 8.25 KB
/
Copy pathclient_exquote.go
File metadata and controls
258 lines (227 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package gotdx
import "github.com/bensema/gotdx/proto"
// GetExServerInfo 获取扩展市场服务信息
func (client *Client) GetExServerInfo() (*proto.ExServerInfoReply, error) {
obj := proto.NewExServerInfo()
return executeProtocol(client, obj)
}
// ExGetCount 获取扩展市场标的数量
func (client *Client) ExGetCount() (*proto.ExGetCountReply, error) {
obj := proto.NewExGetCount()
return executeProtocol(client, obj)
}
// ExGetCategoryList 获取扩展市场分类列表
func (client *Client) ExGetCategoryList() (*proto.ExGetCategoryListReply, error) {
obj := proto.NewExGetCategoryList()
return executeProtocol(client, obj)
}
// ExGetList 获取扩展市场标的列表
func (client *Client) ExGetList(start uint32, count uint16) (*proto.ExGetListReply, error) {
if count == 0 {
count = DefaultExListCount
}
obj := proto.NewExGetList(&proto.ExGetListRequest{Start: start, Count: count})
return executeProtocol(client, obj)
}
// ExGetListExtra 获取扩展市场试验列表
func (client *Client) ExGetListExtra(a uint16, b uint16, count uint16) (*proto.ExGetListExtraReply, error) {
obj := proto.NewExGetListExtra(&proto.ExGetListExtraRequest{A: a, B: b, Count: count})
return executeProtocol(client, obj)
}
// ExGetQuotesList 获取扩展市场行情列表
func (client *Client) ExGetQuotesList(category uint8, start uint16, count uint16, sortType uint16, reverse bool) (*proto.ExGetQuotesListReply, error) {
if count == 0 {
count = DefaultExQuotesCount
}
obj := proto.NewExGetQuotesList(&proto.ExGetQuotesListRequest{
Category: category,
SortType: sortType,
Start: start,
Count: count,
SortReverse: quotesSortReverse(sortType, reverse),
})
return executeProtocol(client, obj)
}
// ExGetQuote 获取单个扩展市场行情
func (client *Client) ExGetQuote(category uint8, code string) (*proto.ExGetQuoteReply, error) {
obj := proto.NewExGetQuote(&proto.ExGetQuoteRequest{Category: category, Code: makeCode9(code)})
return executeProtocol(client, obj)
}
// ExGetQuotes 获取批量扩展市场行情
func (client *Client) ExGetQuotes(categories []uint8, codes []string) (*proto.ExGetQuotesReply, error) {
stocks, err := makeExStocks(categories, codes)
if err != nil {
return nil, err
}
obj := proto.NewExGetQuotes(&proto.ExGetQuotesRequest{Stocks: stocks})
return executeProtocol(client, obj)
}
// ExGetQuotes2 获取批量扩展市场行情,兼容 pytdx2 的第二种批量接口
func (client *Client) ExGetQuotes2(categories []uint8, codes []string) (*proto.ExGetQuotesReply, error) {
stocks, err := makeExStocks(categories, codes)
if err != nil {
return nil, err
}
obj := proto.NewExGetQuotes2(&proto.ExGetQuotesRequest{Stocks: stocks})
return executeProtocol(client, obj)
}
// ExGetKLine 获取扩展市场K线
func (client *Client) ExGetKLine(category uint8, code string, period uint16, start uint32, count uint16, times uint16) (*proto.ExGetKLineReply, error) {
obj := proto.NewExGetKLine(&proto.ExGetKLineRequest{
Category: category,
Code: makeCode9(code),
Period: period,
Times: times,
Start: start,
Count: count,
})
return executeProtocol(client, obj)
}
// ExGetExperiment2487 获取扩展市场试验报价 0x2487
func (client *Client) ExGetExperiment2487(category uint8, code string) (*proto.ExExperiment2487Reply, error) {
obj := proto.NewExExperiment2487(&proto.ExExperiment2487Request{Category: category, Code: makeCode23(code)})
return executeProtocol(client, obj)
}
// ExGetExperiment2488 获取扩展市场试验报价 0x2488
func (client *Client) ExGetExperiment2488(category uint8, code string, mode uint16) (*proto.ExExperiment2488Reply, error) {
obj := proto.NewExExperiment2488(&proto.ExExperiment2488Request{
Category: category,
Code: makeCode23(code),
Mode: mode,
})
return executeProtocol(client, obj)
}
// ExGetKLine2 获取扩展市场 K 线协议 0x2489
func (client *Client) ExGetKLine2(category uint8, code string, period uint16, start uint32, count uint32, times uint16) (*proto.ExGetKLine2Reply, error) {
obj := proto.NewExGetKLine2(&proto.ExGetKLine2Request{
Category: category,
Code: makeCode23(code),
Period: period,
Times: times,
Start: start,
Count: count,
})
return executeProtocol(client, obj)
}
// ExGetHistoryTransaction 获取扩展市场历史成交
func (client *Client) ExGetHistoryTransaction(date uint32, category uint8, code string) (*proto.ExGetHistoryTransactionReply, error) {
obj := proto.NewExGetHistoryTransaction(&proto.ExGetHistoryTransactionRequest{
Date: date,
Category: category,
Code: makeFixed43(code),
})
return executeProtocol(client, obj)
}
// ExGetTickChart 获取扩展市场当日分时图
func (client *Client) ExGetTickChart(category uint8, code string) (*proto.ExGetTickChartReply, error) {
obj := proto.NewExGetTickChart(&proto.ExGetTickChartRequest{
Category: category,
Code: makeCode23(code),
})
return executeProtocol(client, obj)
}
// ExGetHistoryTickChart 获取扩展市场历史分时图
func (client *Client) ExGetHistoryTickChart(date uint32, category uint8, code string) (*proto.ExGetHistoryTickChartReply, error) {
obj := proto.NewExGetHistoryTickChart(&proto.ExGetHistoryTickChartRequest{
Date: date,
Category: category,
Code: makeCode23(code),
})
return executeProtocol(client, obj)
}
// ExGetChartSampling 获取扩展市场抽样图
func (client *Client) ExGetChartSampling(category uint8, code string) (*proto.ExGetChartSamplingReply, error) {
obj := proto.NewExGetChartSampling(&proto.ExGetChartSamplingRequest{
Category: uint16(category),
Code: makeCode22(code),
})
return executeProtocol(client, obj)
}
// ExGetBoardList 获取扩展市场板块榜单
func (client *Client) ExGetBoardList(boardType uint16, start uint16, pageSize uint16) (*proto.ExGetBoardListReply, error) {
obj := proto.NewExGetBoardList(&proto.ExGetBoardListRequest{
PageSize: pageSize,
BoardType: boardType,
Start: start,
})
return executeProtocol(client, obj)
}
// ExGetMapping2562 获取扩展市场映射信息
func (client *Client) ExGetMapping2562(market uint16, start uint32, count uint32) (*proto.ExMapping2562Reply, error) {
obj := proto.NewExMapping2562(&proto.ExMapping2562Request{Market: market, Start: start, Count: count})
return executeProtocol(client, obj)
}
// ExGetFileMeta 获取扩展市场文件元信息
func (client *Client) ExGetFileMeta(filename string) (*proto.GetFileMetaReply, error) {
obj := proto.NewExGetFileMeta(&proto.GetFileMetaRequest{Filename: makeFixed40(filename)})
return executeProtocol(client, obj)
}
// ExDownloadFile 下载扩展市场文件片段
func (client *Client) ExDownloadFile(filename string, start uint32, size uint32) (*proto.DownloadFileReply, error) {
obj := proto.NewExDownloadFile(&proto.ExDownloadFileRequest{
Start: start,
Size: size,
Filename: makeFixed40(filename),
})
return executeProtocol(client, obj)
}
// ExDownloadFullFile 下载完整扩展市场文件
func (client *Client) ExDownloadFullFile(filename string, size uint32) ([]byte, error) {
if size == 0 {
meta, err := client.ExGetFileMeta(filename)
if err == nil {
size = meta.Size
}
}
var result []byte
var downloaded uint32
for {
reply, err := client.ExDownloadFile(filename, downloaded, DefaultDownloadSize)
if err != nil {
return nil, err
}
if reply.Size == 0 {
break
}
result = append(result, reply.Data...)
downloaded += reply.Size
if size != 0 && downloaded >= size {
break
}
}
return result, nil
}
// ExGetTable 获取扩展市场表格内容
func (client *Client) ExGetTable() (string, error) {
return client.getExTable(false)
}
// ExGetTableDetail 获取扩展市场详细表格内容
func (client *Client) ExGetTableDetail() (string, error) {
return client.getExTable(true)
}
func (client *Client) getExTable(detail bool) (string, error) {
start := uint32(0)
content := ""
for {
var (
reply *proto.ExGetTableChunkReply
err error
)
if detail {
obj := proto.NewExGetTableDetail(start)
reply, err = executeProtocol(client, obj.ExGetTableChunk)
} else {
obj := proto.NewExGetTable(start)
reply, err = executeProtocol(client, obj.ExGetTableChunk)
}
if err != nil {
return "", err
}
content += reply.Content
if reply.Count == 0 {
break
}
start += reply.Count
}
return content, nil
}