减少方法名称长度,去掉固定前缀Stock

This commit is contained in:
injoyai
2024-10-31 10:16:49 +08:00
parent fc604d1eff
commit c6e701af1b
27 changed files with 502 additions and 390 deletions

266
client.go
View File

@@ -1,7 +1,6 @@
package tdx
import (
"errors"
"fmt"
"github.com/injoyai/base/maps"
"github.com/injoyai/base/maps/wait/v2"
@@ -102,26 +101,26 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) {
case protocol.TypeHeart:
case protocol.TypeStockCount:
resp, err = protocol.MStockCount.Decode(f.Data)
case protocol.TypeCount:
resp, err = protocol.MCount.Decode(f.Data)
case protocol.TypeStockList:
resp, err = protocol.MStockList.Decode(f.Data)
case protocol.TypeCode:
resp, err = protocol.MCode.Decode(f.Data)
case protocol.TypeStockQuote:
resp = protocol.MStockQuote.Decode(f.Data)
case protocol.TypeQuote:
resp = protocol.MQuote.Decode(f.Data)
case protocol.TypeStockMinute:
resp, err = protocol.MStockMinute.Decode(f.Data)
case protocol.TypeMinute:
resp, err = protocol.MMinute.Decode(f.Data)
case protocol.TypeStockMinuteTrade:
resp, err = protocol.MStockMinuteTrade.Decode(f.Data, conv.String(val)) //todo
case protocol.TypeMinuteTrade:
resp, err = protocol.MMinuteTrade.Decode(f.Data, conv.String(val)) //todo
case protocol.TypeStockHistoryMinuteTrade:
resp, err = protocol.MStockHistoryMinuteTrade.Decode(f.Data, conv.String(val))
case protocol.TypeHistoryMinuteTrade:
resp, err = protocol.MHistoryMinuteTrade.Decode(f.Data, conv.String(val))
case protocol.TypeStockKline:
resp, err = protocol.MStockKline.Decode(f.Data, protocol.TypeKline(conv.Uint16(val)))
case protocol.TypeKline:
resp, err = protocol.MKline.Decode(f.Data, conv.Uint8(val))
default:
err = fmt.Errorf("通讯类型未解析:0x%X", f.Type)
@@ -149,36 +148,36 @@ func (this *Client) SendFrame(f *protocol.Frame, cache ...any) (any, error) {
return this.Wait.Wait(conv.String(this.msgID))
}
// GetStockCount 获取市场内的股票数量
func (this *Client) GetStockCount(exchange protocol.Exchange) (*protocol.StockCountResp, error) {
f := protocol.MStockCount.Frame(exchange)
// GetCount 获取市场内的股票数量
func (this *Client) GetCount(exchange protocol.Exchange) (*protocol.CountResp, error) {
f := protocol.MCount.Frame(exchange)
result, err := this.SendFrame(f)
if err != nil {
return nil, err
}
return result.(*protocol.StockCountResp), nil
return result.(*protocol.CountResp), nil
}
// GetStockList 获取市场内指定范围内的所有证券代码,一次固定返回1000只,上证股票有效范围370-1480
// GetCode 获取市场内指定范围内的所有证券代码,一次固定返回1000只,上证股票有效范围370-1480
// 上证前370只是395/399开头的(中证500/总交易等辅助类),在后面的话是一些100开头的国债
// 600开头的股票是上证A股属于大盘股其中6006开头的股票是最早上市的股票 6016开头的股票为大盘蓝筹股900开头的股票是上证B股
// 000开头的股票是深证A股001、002开头的股票也都属于深证A股 其中002开头的股票是深证A股中小企业股票200开头的股票是深证B股
// 300开头的股票是创业板股票400开头的股票是三板市场股票。
func (this *Client) GetStockList(exchange protocol.Exchange, start uint16) (*protocol.StockListResp, error) {
f := protocol.MStockList.Frame(exchange, start)
func (this *Client) GetCode(exchange protocol.Exchange, start uint16) (*protocol.CodeResp, error) {
f := protocol.MCode.Frame(exchange, start)
result, err := this.SendFrame(f)
if err != nil {
return nil, err
}
return result.(*protocol.StockListResp), nil
return result.(*protocol.CodeResp), nil
}
// GetStockAll 通过多次请求的方式获取全部证券代码
func (this *Client) GetStockAll(exchange protocol.Exchange) (*protocol.StockListResp, error) {
resp := &protocol.StockListResp{}
// GetCodeAll 通过多次请求的方式获取全部证券代码
func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp, error) {
resp := &protocol.CodeResp{}
size := uint16(1000)
for start := uint16(0); ; start += size {
r, err := this.GetStockList(exchange, start)
r, err := this.GetCode(exchange, start)
if err != nil {
return nil, err
}
@@ -191,9 +190,9 @@ func (this *Client) GetStockAll(exchange protocol.Exchange) (*protocol.StockList
return resp, nil
}
// GetStockQuotes 获取盘口五档报价
func (this *Client) GetStockQuotes(m map[protocol.Exchange]string) (protocol.StockQuotesResp, error) {
f, err := protocol.MStockQuote.Frame(m)
// GetQuote 获取盘口五档报价
func (this *Client) GetQuote(m map[protocol.Exchange]string) (protocol.QuotesResp, error) {
f, err := protocol.MQuote.Frame(m)
if err != nil {
return nil, err
}
@@ -201,12 +200,12 @@ func (this *Client) GetStockQuotes(m map[protocol.Exchange]string) (protocol.Sto
if err != nil {
return nil, err
}
return result.(protocol.StockQuotesResp), nil
return result.(protocol.QuotesResp), nil
}
// GetStockMinute 获取分时数据,todo 解析好像不对
func (this *Client) GetStockMinute(exchange protocol.Exchange, code string) (*protocol.StockMinuteResp, error) {
f, err := protocol.MStockMinute.Frame(exchange, code)
// GetMinute 获取分时数据,todo 解析好像不对
func (this *Client) GetMinute(exchange protocol.Exchange, code string) (*protocol.MinuteResp, error) {
f, err := protocol.MMinute.Frame(exchange, code)
if err != nil {
return nil, err
}
@@ -214,31 +213,33 @@ func (this *Client) GetStockMinute(exchange protocol.Exchange, code string) (*pr
if err != nil {
return nil, err
}
return result.(*protocol.StockMinuteResp), nil
return result.(*protocol.MinuteResp), nil
}
// GetStockMinuteTrade 获取分时交易详情,服务器最多返回1800条,count-start<=1800
func (this *Client) GetStockMinuteTrade(exchange protocol.Exchange, code string, start, count uint16) (*protocol.StockMinuteTradeResp, error) {
if count > 1800 {
return nil, errors.New("数量不能超过1800")
}
f, err := protocol.MStockMinuteTrade.Frame(exchange, code, start, count)
// GetMinuteTrade 获取分时交易详情,服务器最多返回1800条,count-start<=1800
func (this *Client) GetMinuteTrade(req protocol.MinuteTradeReq) (*protocol.MinuteTradeResp, error) {
f, err := protocol.MMinuteTrade.Frame(req)
if err != nil {
return nil, err
}
result, err := this.SendFrame(f, code)
result, err := this.SendFrame(f, req.Code)
if err != nil {
return nil, err
}
return result.(*protocol.StockMinuteTradeResp), nil
return result.(*protocol.MinuteTradeResp), nil
}
// GetStockMinuteTradeAll 获取分时全部交易详情,todo 只做参考 因为交易实时在进行,然后又是分页读取的,所以会出现读取间隔内产生的交易会丢失
func (this *Client) GetStockMinuteTradeAll(exchange protocol.Exchange, code string) (*protocol.StockMinuteTradeResp, error) {
resp := &protocol.StockMinuteTradeResp{}
// GetMinuteTradeAll 获取分时全部交易详情,todo 只做参考 因为交易实时在进行,然后又是分页读取的,所以会出现读取间隔内产生的交易会丢失
func (this *Client) GetMinuteTradeAll(exchange protocol.Exchange, code string) (*protocol.MinuteTradeResp, error) {
resp := &protocol.MinuteTradeResp{}
size := uint16(1800)
for i := uint16(0); ; i += size {
r, err := this.GetStockMinuteTrade(exchange, code, i, size)
for start := uint16(0); ; start += size {
r, err := this.GetMinuteTrade(protocol.MinuteTradeReq{
Exchange: exchange,
Code: code,
Start: start,
Count: size,
})
if err != nil {
return nil, err
}
@@ -252,33 +253,36 @@ func (this *Client) GetStockMinuteTradeAll(exchange protocol.Exchange, code stri
return resp, nil
}
// GetStockHistoryMinuteTrade 获取历史分时交易,,只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000
func (this *Client) GetStockHistoryMinuteTrade(t time.Time, exchange protocol.Exchange, code string, start, count uint16) (*protocol.StockHistoryMinuteTradeResp, error) {
if count > 2000 {
return nil, errors.New("数量不能超过2000")
}
f, err := protocol.MStockHistoryMinuteTrade.Frame(t, exchange, code, start, count)
// GetHistoryMinuteTrade 获取历史分时交易,,只能获取昨天及之前的数据,服务器最多返回2000条,count-start<=2000,如果日期输入错误,则返回0
func (this *Client) GetHistoryMinuteTrade(req protocol.HistoryMinuteTradeReq) (*protocol.HistoryMinuteTradeResp, error) {
f, err := protocol.MHistoryMinuteTrade.Frame(req)
if err != nil {
return nil, err
}
result, err := this.SendFrame(f, code)
result, err := this.SendFrame(f, req.Code)
if err != nil {
return nil, err
}
return result.(*protocol.StockHistoryMinuteTradeResp), nil
return result.(*protocol.HistoryMinuteTradeResp), nil
}
// GetStockHistoryMinuteTradeAll 获取历史分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
func (this *Client) GetStockHistoryMinuteTradeAll(exchange protocol.Exchange, code string) (*protocol.StockMinuteTradeResp, error) {
resp := &protocol.StockMinuteTradeResp{}
// GetHistoryMinuteTradeAll 获取历史分时全部交易,通过多次请求来拼接,只能获取昨天及之前的数据
func (this *Client) GetHistoryMinuteTradeAll(req protocol.HistoryMinuteTradeAllReq) (*protocol.HistoryMinuteTradeResp, error) {
resp := &protocol.HistoryMinuteTradeResp{}
size := uint16(2000)
for i := uint16(0); ; i += size {
r, err := this.GetStockMinuteTrade(exchange, code, i, size)
for start := uint16(0); ; start += size {
r, err := this.GetHistoryMinuteTrade(protocol.HistoryMinuteTradeReq{
Date: req.Date,
Exchange: req.Exchange,
Code: req.Code,
Start: start,
Count: size,
})
if err != nil {
return nil, err
}
resp.Count += r.Count
resp.List = append(resp.List, r.List...)
resp.List = append(r.List, resp.List...)
if r.Count < size {
break
}
@@ -286,26 +290,26 @@ func (this *Client) GetStockHistoryMinuteTradeAll(exchange protocol.Exchange, co
return resp, nil
}
// GetStockKline 获取k线数据
func (this *Client) GetStockKline(Type protocol.TypeKline, req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
f, err := protocol.MStockKline.Frame(Type, req)
// GetKline 获取k线数据
func (this *Client) GetKline(Type uint8, req protocol.KlineReq) (*protocol.KlineResp, error) {
f, err := protocol.MKline.Frame(Type, req)
if err != nil {
return nil, err
}
result, err := this.SendFrame(f, Type.Uint16())
result, err := this.SendFrame(f, Type)
if err != nil {
return nil, err
}
return result.(*protocol.StockKlineResp), nil
return result.(*protocol.KlineResp), nil
}
// GetStockKlineAll 获取全部k线数据
func (this *Client) GetStockKlineAll(Type protocol.TypeKline, exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
resp := &protocol.StockKlineResp{}
// GetKlineAll 获取全部k线数据
func (this *Client) GetKlineAll(Type uint8, exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
resp := &protocol.KlineResp{}
size := uint16(800)
var last *protocol.StockKline
var last *protocol.Kline
for i := uint16(0); ; i += size {
r, err := this.GetStockKline(Type, &protocol.StockKlineReq{
r, err := this.GetKline(Type, protocol.KlineReq{
Exchange: exchange,
Code: code,
Start: i,
@@ -329,102 +333,102 @@ func (this *Client) GetStockKlineAll(Type protocol.TypeKline, exchange protocol.
return resp, nil
}
// GetStockKlineMinute 获取一分钟k线数据
func (this *Client) GetStockKlineMinute(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKlineMinute, req)
// GetKlineMinute 获取一分钟k线数据
func (this *Client) GetKlineMinute(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKlineMinute, req)
}
// GetStockKlineMinuteAll 获取一分钟k线全部数据
func (this *Client) GetStockKlineMinuteAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKlineMinute, exchange, code)
// GetKlineMinuteAll 获取一分钟k线全部数据
func (this *Client) GetKlineMinuteAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKlineMinute, exchange, code)
}
// GetStockKline5Minute 获取五分钟k线数据
func (this *Client) GetStockKline5Minute(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKline5Minute, req)
// GetKline5Minute 获取五分钟k线数据
func (this *Client) GetKline5Minute(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKline5Minute, req)
}
// GetStockKline5MinuteAll 获取5分钟k线全部数据
func (this *Client) GetStockKline5MinuteAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKline5Minute, exchange, code)
// GetKline5MinuteAll 获取5分钟k线全部数据
func (this *Client) GetKline5MinuteAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKline5Minute, exchange, code)
}
// GetStockKline15Minute 获取十五分钟k线数据
func (this *Client) GetStockKline15Minute(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKline15Minute, req)
// GetKline15Minute 获取十五分钟k线数据
func (this *Client) GetKline15Minute(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKline15Minute, req)
}
// GetStockKline15MinuteAll 获取十五分钟k线全部数据
func (this *Client) GetStockKline15MinuteAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKline15Minute, exchange, code)
// GetKline15MinuteAll 获取十五分钟k线全部数据
func (this *Client) GetKline15MinuteAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKline15Minute, exchange, code)
}
// GetStockKline30Minute 获取三十分钟k线数据
func (this *Client) GetStockKline30Minute(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKline30Minute, req)
// GetKline30Minute 获取三十分钟k线数据
func (this *Client) GetKline30Minute(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKline30Minute, req)
}
// GetStockKline30MinuteAll 获取三十分钟k线全部数据
func (this *Client) GetStockKline30MinuteAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKline30Minute, exchange, code)
// GetKline30MinuteAll 获取三十分钟k线全部数据
func (this *Client) GetKline30MinuteAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKline30Minute, exchange, code)
}
// GetStockKlineHour 获取小时k线数据
func (this *Client) GetStockKlineHour(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKlineHour, req)
// GetKlineHour 获取小时k线数据
func (this *Client) GetKlineHour(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKlineHour, req)
}
// GetStockKlineHourAll 获取小时k线全部数据
func (this *Client) GetStockKlineHourAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKlineHour, exchange, code)
// GetKlineHourAll 获取小时k线全部数据
func (this *Client) GetKlineHourAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKlineHour, exchange, code)
}
// GetStockKlineDay 获取日k线数据
func (this *Client) GetStockKlineDay(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKlineDay, req)
// GetKlineDay 获取日k线数据
func (this *Client) GetKlineDay(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKlineDay, req)
}
// GetStockKlineDayAll 获取日k线全部数据
func (this *Client) GetStockKlineDayAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKlineDay, exchange, code)
// GetKlineDayAll 获取日k线全部数据
func (this *Client) GetKlineDayAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKlineDay, exchange, code)
}
// GetStockKlineWeek 获取周k线数据
func (this *Client) GetStockKlineWeek(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKlineWeek, req)
// GetKlineWeek 获取周k线数据
func (this *Client) GetKlineWeek(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKlineWeek, req)
}
// GetStockKlineWeekAll 获取周k线全部数据
func (this *Client) GetStockKlineWeekAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKlineWeek, exchange, code)
// GetKlineWeekAll 获取周k线全部数据
func (this *Client) GetKlineWeekAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKlineWeek, exchange, code)
}
// GetStockKlineMonth 获取月k线数据
func (this *Client) GetStockKlineMonth(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKlineMonth, req)
func (this *Client) GetStockKlineMonth(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKlineMonth, req)
}
// GetStockKlineMonthAll 获取月k线全部数据
func (this *Client) GetStockKlineMonthAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKlineMonth, exchange, code)
// GetKlineMonthAll 获取月k线全部数据
func (this *Client) GetKlineMonthAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKlineMonth, exchange, code)
}
// GetStockKlineQuarter 获取季k线数据
func (this *Client) GetStockKlineQuarter(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKlineQuarter, req)
// GetKlineQuarter 获取季k线数据
func (this *Client) GetKlineQuarter(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKlineQuarter, req)
}
// GetStockKlineQuarterAll 获取季k线全部数据
func (this *Client) GetStockKlineQuarterAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKlineQuarter, exchange, code)
// GetKlineQuarterAll 获取季k线全部数据
func (this *Client) GetKlineQuarterAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKlineQuarter, exchange, code)
}
// GetStockKlineYear 获取年k线数据
func (this *Client) GetStockKlineYear(req *protocol.StockKlineReq) (*protocol.StockKlineResp, error) {
return this.GetStockKline(protocol.TypeKlineYear, req)
// GetKlineYear 获取年k线数据
func (this *Client) GetKlineYear(req protocol.KlineReq) (*protocol.KlineResp, error) {
return this.GetKline(protocol.TypeKlineYear, req)
}
// GetStockKlineYearAll 获取年k线数据
func (this *Client) GetStockKlineYearAll(exchange protocol.Exchange, code string) (*protocol.StockKlineResp, error) {
return this.GetStockKlineAll(protocol.TypeKlineYear, exchange, code)
// GetKlineYearAll 获取年k线数据
func (this *Client) GetKlineYearAll(exchange protocol.Exchange, code string) (*protocol.KlineResp, error) {
return this.GetKlineAll(protocol.TypeKlineYear, exchange, code)
}