Files
qsdk/tushare/cal.go
2026-05-03 18:53:48 +08:00

79 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tushare
import (
"time"
"git.apinb.com/quant/qsdk/conv"
)
/*
TradeCal 获取交易日历
exchange: 交易所代码SSE 上交所SZSE 深交所BSE 北交所,空字符串代表所有
start_date: 开始日期,格式:YYYYMMDD
end_date: 结束日期,格式:YYYYMMDD
is_open: 是否开盘,'0'休市,'1'交易,空字符串代表所有
*/
func (cli *TushareClient) TradeCal(exchange, start_date, end_date, is_open string) (*TushareRespData, error) {
params := map[string]any{}
if exchange != "" {
params["exchange"] = exchange
}
if start_date != "" {
params["start_date"] = start_date
}
if end_date != "" {
params["end_date"] = end_date
}
if is_open != "" {
params["is_open"] = is_open
}
req := TushareReq{
APIName: "trade_cal",
Params: params,
}
fields := []map[string]string{
{"exchange": "交易所代码"},
{"cal_date": "日历日期"},
{"is_open": "是否开盘"},
{"pretrade_date": "上一交易日"},
}
return cli.Do(req, fields)
}
func (cli *TushareClient) LastTradeDay() string {
result, err := cli.TradeCal("SSE", time.Now().AddDate(0, 0, -15).Format("20060102"), time.Now().Format("20060102"), "1")
if err != nil {
return ""
}
cal := result.Map()
if len(cal) == 0 {
return ""
}
return conv.AnyToString(cal[0]["cal_date"])
}
func (cli *TushareClient) LimitTradeDay(limit int) []string {
if limit <= 0 {
return []string{}
}
if limit > 100 {
limit = 100
}
result, err := cli.TradeCal("SSE", time.Now().AddDate(0, 0, -100).Format("20060102"), time.Now().Format("20060102"), "1")
if err != nil {
return []string{}
}
cal := result.Map()
days := []string{}
for i := 0; i < limit; i++ {
item := cal[i]
days = append(days, conv.AnyToString(item["cal_date"]))
}
return days
}