dev 2
This commit is contained in:
94
go-client/apps/zt/logic/stock.go
Normal file
94
go-client/apps/zt/logic/stock.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var exchangeAlias = map[string]string{
|
||||
"SSE": "SH", "SHSE": "SH", "XSHG": "SH",
|
||||
"SZSE": "SZ", "XSHE": "SZ",
|
||||
"BSE": "BJ", "BJSE": "BJ",
|
||||
}
|
||||
|
||||
func stockCodeFromMap(item map[string]string) string {
|
||||
code := strings.ToUpper(strings.TrimSpace(mapGet(item, "m_strInstrumentID", "StockCode", "stock_code", "code")))
|
||||
ex := mapGet(item, "m_strExchangeID", "exchange", "exchange_id")
|
||||
return normalizeCode(code, ex)
|
||||
}
|
||||
|
||||
func normalizeCode(code, exchange string) string {
|
||||
code = strings.ToUpper(strings.TrimSpace(code))
|
||||
if code == "" {
|
||||
return ""
|
||||
}
|
||||
if i := strings.LastIndex(code, "."); i >= 0 {
|
||||
symbol, ex := code[:i], code[i+1:]
|
||||
ex = canonExchange(ex)
|
||||
if ex == "SH" || ex == "SZ" || ex == "BJ" {
|
||||
return symbol + "." + ex
|
||||
}
|
||||
return ""
|
||||
}
|
||||
ex := canonExchange(exchange)
|
||||
if ex == "" && looksDigits(code, 6) {
|
||||
switch {
|
||||
case strings.HasPrefix(code, "92") || code[0] == '4' || code[0] == '8':
|
||||
ex = "BJ"
|
||||
case code[0] == '5' || code[0] == '6' || code[0] == '9' || strings.HasPrefix(code, "11"):
|
||||
ex = "SH"
|
||||
case code[0] == '0' || code[0] == '1' || code[0] == '2' || code[0] == '3':
|
||||
ex = "SZ"
|
||||
}
|
||||
}
|
||||
if ex == "SH" || ex == "SZ" || ex == "BJ" {
|
||||
return code + "." + ex
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func canonExchange(ex string) string {
|
||||
ex = strings.ToUpper(strings.TrimSpace(ex))
|
||||
if v, ok := exchangeAlias[ex]; ok {
|
||||
return v
|
||||
}
|
||||
return ex
|
||||
}
|
||||
|
||||
func looksDigits(s string, n int) bool {
|
||||
if len(s) != n {
|
||||
return false
|
||||
}
|
||||
for _, r := range s {
|
||||
if !unicode.IsDigit(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func mapGet(item map[string]string, names ...string) string {
|
||||
for _, name := range names {
|
||||
if v := strings.TrimSpace(item[name]); v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func asIntS(s string) int {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
var n int
|
||||
_, _ = fmt.Sscanf(s, "%d", &n)
|
||||
if n == 0 {
|
||||
var f float64
|
||||
if _, err := fmt.Sscanf(s, "%f", &f); err == nil {
|
||||
return int(f)
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
Reference in New Issue
Block a user