diff --git a/client_test.go b/client_test.go index 07ef967..7b212f0 100644 --- a/client_test.go +++ b/client_test.go @@ -11,7 +11,7 @@ var opt = &Opt{ Port: 7709, } -func prepare() *Client { +func newClient() *Client { api := NewClient(opt) reply, err := api.Connect() if err != nil { @@ -24,30 +24,29 @@ func prepare() *Client { func Test_tdx_Connect(t *testing.T) { fmt.Println("================ Connect ================") api := NewClient(opt) + defer api.Disconnect() reply, err := api.Connect() if err != nil { t.Errorf("error:%s", err) } fmt.Println(reply.Info) - _ = api.Disconnect() - } func Test_tdx_GetSecurityCount(t *testing.T) { fmt.Println("================ GetSecurityCount ================") - api := prepare() + api := newClient() + defer api.Disconnect() reply, err := api.GetSecurityCount(MarketSh) if err != nil { t.Errorf("error:%s", err) } fmt.Println(reply.Count) - - _ = api.Disconnect() } func Test_tdx_GetSecurityQuotes(t *testing.T) { fmt.Println("================ GetSecurityQuotes ================") - api := prepare() + api := newClient() + defer api.Disconnect() reply, err := api.GetSecurityQuotes([]uint8{MarketSh}, []string{"002062"}) if err != nil { t.Errorf("error:%s", err) @@ -56,14 +55,12 @@ func Test_tdx_GetSecurityQuotes(t *testing.T) { for _, obj := range reply.List { fmt.Printf("%+v \n", obj) } - - _ = api.Disconnect() - } func Test_tdx_GetSecurityList(t *testing.T) { fmt.Println("================ GetSecurityList ================") - api := prepare() + api := newClient() + defer api.Disconnect() reply, err := api.GetSecurityList(MarketSh, 0) if err != nil { t.Errorf("error:%s", err) @@ -71,15 +68,13 @@ func Test_tdx_GetSecurityList(t *testing.T) { for _, obj := range reply.List { fmt.Printf("%+v \n", obj) } - - _ = api.Disconnect() - } func Test_tdx_GetSecurityBars(t *testing.T) { fmt.Println("================ GetSecurityBars ================") // GetSecurityBars 与 GetIndexBars 使用同一个接口靠market区分 - api := prepare() + api := newClient() + defer api.Disconnect() reply, err := api.GetSecurityBars(proto.KLINE_TYPE_RI_K, 0, "000001", 0, 10) if err != nil { t.Errorf("error:%s", err) @@ -88,15 +83,13 @@ func Test_tdx_GetSecurityBars(t *testing.T) { for _, obj := range reply.List { fmt.Printf("%+v \n", obj) } - - _ = api.Disconnect() - } func Test_tdx_GetIndexBars(t *testing.T) { fmt.Println("================ GetIndexBars ================") // GetSecurityBars 与 GetIndexBars 使用同一个接口靠market区分 - api := prepare() + api := newClient() + defer api.Disconnect() reply, err := api.GetIndexBars(proto.KLINE_TYPE_RI_K, 1, "000001", 0, 10) if err != nil { t.Errorf("error:%s", err) @@ -105,14 +98,12 @@ func Test_tdx_GetIndexBars(t *testing.T) { for _, obj := range reply.List { fmt.Printf("%+v \n", obj) } - - _ = api.Disconnect() - } func Test_tdx_GetMinuteTimeData(t *testing.T) { fmt.Println("================ GetMinuteTimeData ================") - api := prepare() + api := newClient() + defer api.Disconnect() reply, err := api.GetMinuteTimeData(0, "159607") if err != nil { t.Errorf("error:%s", err) @@ -121,14 +112,12 @@ func Test_tdx_GetMinuteTimeData(t *testing.T) { for _, obj := range reply.List { fmt.Printf("%+v \n", obj) } - - _ = api.Disconnect() - } func Test_tdx_GetHistoryMinuteTimeData(t *testing.T) { fmt.Println("================ GetHistoryMinuteTimeData ================") - api := prepare() + api := newClient() + defer api.Disconnect() //reply, err := api.GetHistoryMinuteTimeData(20220511, 0, "159607") reply, err := api.GetHistoryMinuteTimeData(20220511, 0, "159607") if err != nil { @@ -138,16 +127,14 @@ func Test_tdx_GetHistoryMinuteTimeData(t *testing.T) { for _, obj := range reply.List { fmt.Printf("%+v \n", obj) } - - _ = api.Disconnect() - } func Test_tdx_GetTransactionData(t *testing.T) { fmt.Println("================ GetTransactionData ================") - api := prepare() + api := newClient() + defer api.Disconnect() //reply, err := api.GetHistoryMinuteTimeData(20220511, 0, "159607") - reply, err := api.GetTransactionData(0, "159607", 0, 10) + reply, err := api.GetTransactionData(MarketSh, "159607", 0, 10) if err != nil { t.Errorf("error:%s", err) } @@ -155,16 +142,13 @@ func Test_tdx_GetTransactionData(t *testing.T) { for _, obj := range reply.List { fmt.Printf("%+v \n", obj) } - - _ = api.Disconnect() - } func Test_tdx_GetHistoryTransactionData(t *testing.T) { fmt.Println("================ GetHistoryTransactionData ================") - api := prepare() - //reply, err := api.GetHistoryMinuteTimeData(20220511, 0, "159607") - reply, err := api.GetHistoryTransactionData(20220511, 0, "159607", 0, 10) + api := newClient() + defer api.Disconnect() + reply, err := api.GetHistoryTransactionData(20230922, MarketSh, "159607", 0, 10) if err != nil { t.Errorf("error:%s", err) } @@ -172,7 +156,4 @@ func Test_tdx_GetHistoryTransactionData(t *testing.T) { for _, obj := range reply.List { fmt.Printf("%+v \n", obj) } - - _ = api.Disconnect() - } diff --git a/proto/get_history_minute_time_data.go b/proto/get_history_minute_time_data.go index 046e755..acb5b44 100644 --- a/proto/get_history_minute_time_data.go +++ b/proto/get_history_minute_time_data.go @@ -48,6 +48,7 @@ func NewGetHistoryMinuteTimeData() *GetHistoryMinuteTimeData { obj.contentHex = "" return obj } + func (obj *GetHistoryMinuteTimeData) SetParams(req *GetHistoryMinuteTimeDataRequest) { obj.request = req } @@ -71,9 +72,9 @@ func (obj *GetHistoryMinuteTimeData) Serialize() ([]byte, error) { } // 结果数据都是\n,\t分隔的中文字符串,比如查询K线数据,返回的结果字符串就形如 -///“时间\t开盘价\t收盘价\t最高价\t最低价\t成交量\t成交额\n -///20150519\t4.644000\t4.732000\t4.747000\t4.576000\t146667487\t683638848.000000\n -///20150520\t4.756000\t4.850000\t4.960000\t4.756000\t353161092\t1722953216.000000” +// /“时间\t开盘价\t收盘价\t最高价\t最低价\t成交量\t成交额\n +// /20150519\t4.644000\t4.732000\t4.747000\t4.576000\t146667487\t683638848.000000\n +// /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) diff --git a/proto/get_history_transaction_data.go b/proto/get_history_transaction_data.go index b91253d..a2df8b7 100644 --- a/proto/get_history_transaction_data.go +++ b/proto/get_history_transaction_data.go @@ -54,6 +54,7 @@ func NewGetHistoryTransactionData() *GetHistoryTransactionData { obj.contentHex = "" return obj } + func (obj *GetHistoryTransactionData) SetParams(req *GetHistoryTransactionDataRequest) { obj.request = req } diff --git a/proto/get_index_bars.go b/proto/get_index_bars.go index f9230c9..1646315 100644 --- a/proto/get_index_bars.go +++ b/proto/get_index_bars.go @@ -63,6 +63,7 @@ func NewGetIndexBars() *GetIndexBars { obj.contentHex = "00000000000000000000" return obj } + func (obj *GetIndexBars) SetParams(req *GetIndexBarsRequest) { obj.request = req obj.request.I = 1 diff --git a/proto/get_minute_time_data.go b/proto/get_minute_time_data.go index 6d6d1a7..88ef6ee 100644 --- a/proto/get_minute_time_data.go +++ b/proto/get_minute_time_data.go @@ -50,6 +50,7 @@ func NewGetMinuteTimeData() *GetMinuteTimeData { obj.contentHex = "" return obj } + func (obj *GetMinuteTimeData) SetParams(req *GetMinuteTimeDataRequest) { obj.request = req } @@ -73,9 +74,9 @@ func (obj *GetMinuteTimeData) Serialize() ([]byte, error) { } // 结果数据都是\n,\t分隔的中文字符串,比如查询K线数据,返回的结果字符串就形如 -///“时间\t开盘价\t收盘价\t最高价\t最低价\t成交量\t成交额\n -///20150519\t4.644000\t4.732000\t4.747000\t4.576000\t146667487\t683638848.000000\n -///20150520\t4.756000\t4.850000\t4.960000\t4.756000\t353161092\t1722953216.000000” +// /“时间\t开盘价\t收盘价\t最高价\t最低价\t成交量\t成交额\n +// /20150519\t4.644000\t4.732000\t4.747000\t4.576000\t146667487\t683638848.000000\n +// /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) diff --git a/proto/get_security_bars.go b/proto/get_security_bars.go index 4f1ed23..43d1012 100644 --- a/proto/get_security_bars.go +++ b/proto/get_security_bars.go @@ -63,6 +63,7 @@ func NewGetSecurityBars() *GetSecurityBars { obj.contentHex = "00000000000000000000" return obj } + func (obj *GetSecurityBars) SetParams(req *GetSecurityBarsRequest) { obj.request = req obj.request.I = 1 @@ -87,9 +88,9 @@ func (obj *GetSecurityBars) Serialize() ([]byte, error) { } // 结果数据都是\n,\t分隔的中文字符串,比如查询K线数据,返回的结果字符串就形如 -///“时间\t开盘价\t收盘价\t最高价\t最低价\t成交量\t成交额\n -///20150519\t4.644000\t4.732000\t4.747000\t4.576000\t146667487\t683638848.000000\n -///20150520\t4.756000\t4.850000\t4.960000\t4.756000\t353161092\t1722953216.000000” +// /“时间\t开盘价\t收盘价\t最高价\t最低价\t成交量\t成交额\n +// /20150519\t4.644000\t4.732000\t4.747000\t4.576000\t146667487\t683638848.000000\n +// /20150520\t4.756000\t4.850000\t4.960000\t4.756000\t353161092\t1722953216.000000” func (obj *GetSecurityBars) UnSerialize(header interface{}, data []byte) error { obj.respHeader = header.(*RespHeader) diff --git a/proto/get_security_count.go b/proto/get_security_count.go index da34680..1ae8ab1 100644 --- a/proto/get_security_count.go +++ b/proto/get_security_count.go @@ -36,6 +36,7 @@ func NewGetSecurityCount() *GetSecurityCount { obj.contentHex = "75c73301" // 未解 return obj } + func (obj *GetSecurityCount) SetParams(req *GetSecurityCountRequest) { obj.request = req } diff --git a/proto/get_transaction_data.go b/proto/get_transaction_data.go index 4f5c065..b925440 100644 --- a/proto/get_transaction_data.go +++ b/proto/get_transaction_data.go @@ -53,6 +53,7 @@ func NewGetTransactionData() *GetTransactionData { obj.contentHex = "" return obj } + func (obj *GetTransactionData) SetParams(req *GetTransactionDataRequest) { obj.request = req } diff --git a/proto/proto.go b/proto/proto.go index b169e4b..c1c0877 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -34,34 +34,21 @@ const ( KMSG_TRANSACTIONDATA = 0x0fc5 // 分笔成交信息 KMSG_XDXRINFO = 0x000f // 除权除息信息 - /* - K线种类 - # K 线种类 - # 0 - 5 分钟K 线 - # 1 - 15 分钟K 线 - # 2 - 30 分钟K 线 - # 3 - 1 小时K 线 - # 4 - 日K 线 - # 5 - 周K 线 - # 6 - 月K 线 - # 7 - 1 分钟 - # 8 - 1 分钟K 线 - # 9 - 日K 线 - # 10 - 季K 线 - # 11 - 年K 线 - */ - KLINE_TYPE_5MIN = 0 - KLINE_TYPE_15MIN = 1 - KLINE_TYPE_30MIN = 2 - KLINE_TYPE_1HOUR = 3 - KLINE_TYPE_DAILY = 4 - KLINE_TYPE_WEEKLY = 5 - KLINE_TYPE_MONTHLY = 6 - KLINE_TYPE_EXHQ_1MIN = 7 - KLINE_TYPE_1MIN = 8 - KLINE_TYPE_RI_K = 9 - KLINE_TYPE_3MONTH = 10 - KLINE_TYPE_YEARLY = 11 +) + +const ( + KLINE_TYPE_5MIN = 0 // 5 分钟K 线 + KLINE_TYPE_15MIN = 1 // 15 分钟K 线 + KLINE_TYPE_30MIN = 2 // 30 分钟K 线 + KLINE_TYPE_1HOUR = 3 // 1 小时K 线 + KLINE_TYPE_DAILY = 4 // 日K 线 + KLINE_TYPE_WEEKLY = 5 // 周K 线 + KLINE_TYPE_MONTHLY = 6 // 月K 线 + KLINE_TYPE_EXHQ_1MIN = 7 // 1 分钟 + KLINE_TYPE_1MIN = 8 // 1 分钟K 线 + KLINE_TYPE_RI_K = 9 // 日K 线 + KLINE_TYPE_3MONTH = 10 // 季K 线 + KLINE_TYPE_YEARLY = 11 // 年K 线 ) type Msg interface { @@ -288,11 +275,10 @@ func getvolume(ivol int) (volume float64) { } func baseUnit(code string) float64 { - c := code[:2] - switch c { + switch code[:2] { case "60", "30", "68", "00": return 100.0 - + default: + return 1000.0 } - return 1000.0 }