新增按交易所同步股票功能,支持 SSE/SZSE/BSE

This commit is contained in:
2026-07-04 14:51:52 +08:00
parent 44fb305f8a
commit d370d2e10a
5 changed files with 145 additions and 42 deletions
+49 -21
View File
@@ -25,11 +25,22 @@ type StockBasic struct {
Name string
Area string
Industry string
Fullname string
Enname string
Cnspell string
Market string
Exchange string
CurrType string
ListStatus string
ListDate string
DelistDate string
IsHs string
ActName string
ActEntType string
}
const stockBasicFields = "ts_code,symbol,name,area,industry,fullname,enname,cnspell,market,exchange,curr_type,list_status,list_date,delist_date,is_hs,act_name,act_ent_type"
// Client 封装 Tushare Pro HTTP API 调用。
type Client struct {
token string
@@ -46,43 +57,60 @@ func NewClient() *Client {
}
}
func parseStockBasic(row []any, col map[string]int) StockBasic {
return StockBasic{
TsCode: stringAt(row, col["ts_code"]),
Symbol: stringAt(row, col["symbol"]),
Name: stringAt(row, col["name"]),
Area: stringAt(row, col["area"]),
Industry: stringAt(row, col["industry"]),
Fullname: stringAt(row, col["fullname"]),
Enname: stringAt(row, col["enname"]),
Cnspell: stringAt(row, col["cnspell"]),
Market: stringAt(row, col["market"]),
Exchange: stringAt(row, col["exchange"]),
CurrType: stringAt(row, col["curr_type"]),
ListStatus: stringAt(row, col["list_status"]),
ListDate: stringAt(row, col["list_date"]),
DelistDate: stringAt(row, col["delist_date"]),
IsHs: stringAt(row, col["is_hs"]),
ActName: stringAt(row, col["act_name"]),
ActEntType: stringAt(row, col["act_ent_type"]),
}
}
// ListStocks 通过 stock_basic 接口获取全部上市股票基础信息。
func (c *Client) ListStocks() ([]StockBasic, error) {
params := map[string]any{
"list_status": "L",
"fields": "ts_code,symbol,name,area,industry,market,exchange,list_status",
"fields": stockBasicFields,
}
return c.listStockBasic(params)
}
// ListStocksByExchange 按交易所获取股票基础信息。
func (c *Client) ListStocksByExchange(exchange string) ([]StockBasic, error) {
params := map[string]any{
"exchange": exchange,
"fields": stockBasicFields,
}
return c.listStockBasic(params)
}
func (c *Client) listStockBasic(params map[string]any) ([]StockBasic, error) {
fields, items, err := c.call("stock_basic", params)
if err != nil {
return nil, fmt.Errorf("stock_basic: %w", err)
}
col := buildColumnMap(fields)
idxTsCode := col["ts_code"]
idxSymbol := col["symbol"]
idxName := col["name"]
idxArea := col["area"]
idxIndustry := col["industry"]
idxMarket := col["market"]
idxExchange := col["exchange"]
idxListStatus := col["list_status"]
stocks := make([]StockBasic, 0, len(items))
for _, row := range items {
code := stringAt(row, idxTsCode)
code := stringAt(row, col["ts_code"])
if code == "" {
continue
}
stocks = append(stocks, StockBasic{
TsCode: code,
Symbol: stringAt(row, idxSymbol),
Name: stringAt(row, idxName),
Area: stringAt(row, idxArea),
Industry: stringAt(row, idxIndustry),
Market: stringAt(row, idxMarket),
Exchange: stringAt(row, idxExchange),
ListStatus: stringAt(row, idxListStatus),
})
stocks = append(stocks, parseStockBasic(row, col))
}
return stocks, nil
}