Fetch the repository succeeded.
This action will force synchronization from 王布衣/gotdx, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
golang实现的一个通达信数据协议库
第一次运行时, 连接池会探测服务器网络速度会慢一些, 网络测速后会缓存到本地。
package tdx
import (
"gitee.com/quant1x/gotdx/proto"
"gitee.com/quant1x/gotdx/quotes"
"strings"
)
var (
stdApi *quotes.StdApi = nil
)
func prepare() *quotes.StdApi {
if stdApi == nil {
std_api, err := quotes.NewStdApi()
if err != nil {
return nil
}
stdApi = std_api
}
return stdApi
}
func startsWith(str string, prefixs []string) bool {
if len(str) == 0 || len(prefixs) == 0 {
return false
}
for _, prefix := range prefixs {
if strings.HasPrefix(str, prefix) {
return true
}
}
return false
}
// 判断股票ID对应的证券市场匹配规则
//
// ['50', '51', '60', '90', '110'] 为 sh
// ['00', '12','13', '18', '15', '16', '18', '20', '30', '39', '115'] 为 sz
// ['5', '6', '9'] 开头的为 sh, 其余为 sz
func getStockMarket(symbol string) string {
//:param string: False 返回市场ID,否则市场缩写名称
//:param symbol: 股票ID, 若以 'sz', 'sh' 开头直接返回对应类型,否则使用内置规则判断
//:return 'sh' or 'sz'
market := "sh"
if startsWith(symbol, []string{"sh", "sz", "SH", "SZ"}) {
market = strings.ToLower(symbol[0:2])
} else if startsWith(symbol, []string{"50", "51", "60", "68", "90", "110", "113", "132", "204"}) {
market = "sh"
} else if startsWith(symbol, []string{"00", "12", "13", "18", "15", "16", "18", "20", "30", "39", "115", "1318"}) {
market = "sz"
} else if startsWith(symbol, []string{"5", "6", "9", "7"}) {
market = "sh"
} else if startsWith(symbol, []string{"4", "8"}) {
market = "bj"
}
return market
}
func getStockMarketId(symbol string) uint8 {
market := getStockMarket(symbol)
marketId := proto.MarketShangHai
if market == "sh" {
marketId = proto.MarketShangHai
} else if market == "sz" {
marketId = proto.MarketShenZhen
} else if market == "bj" {
marketId = proto.MarketBeiJing
}
//# logger.debug(f"market => {market}")
return uint8(marketId)
}
// GetKLine 获取日K线
func GetKLine(code string, start uint16, count uint16) *quotes.SecurityBarsReply {
api := prepare()
marketId := getStockMarketId(code)
data, _ := api.GetKLine(marketId, code, proto.KLINE_TYPE_RI_K, start, count)
return data
}
package tdx
import (
"fmt"
"testing"
)
func TestGetKLine(t *testing.T) {
data := GetKLine("000002", 0, 1)
fmt.Println(data)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。