123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package markinfo
- import "fmt"
- import "math"
- import "errors"
- import "sync"
- import "strconv"
- func init() {
- //symbol map
- symbolMap = make(map[string]int)
- for k, v := range symbols {
- symbolMap[v] = k
- }
- //symbolIdBookId
- symbolIdBookId = make(map[int]int)
- for k, v := range bookIdSymbolId {
- symbolIdBookId[v] = k
- }
- //单位初始化
- symbolUnit = make(map[string]float64)
- for k, v := range symbolUnitInt {
- symbolUnit[symbols[k]] = v
- }
- }
- var symbolNameMu sync.Mutex
- const (
- DIRECT_RATES = iota
- INDIRECT_RATES
- CROSS_RATES
- FUTURES_RATES
- )
- func BookIdToSymbolId(bookId int) (symbolId int, err error) {
- if symbolId, ok := bookIdSymbolId[bookId]; ok {
- return symbolId, nil
- }
- return 0, fmt.Errorf("BookIdToSymbolId:: bookId %d error.", bookId)
- }
- func SymbolIdToBookId(symbolId int) (bookId int, err error) {
- if bookId, ok := symbolIdBookId[symbolId]; ok {
- return bookId, nil
- }
- return 0, fmt.Errorf("SymbolIdToBookId:: symbolId %d error.", symbolId)
- }
- func GetPoint(sid int, price1, price2 float64) (float64, error) {
- if sid < 0 || sid >= SymbolCount {
- return 0.0, fmt.Errorf("GetPoint symbolId %d not exist", sid)
- }
- correct := 1.0
- if sid == XAUUSD {
- correct = 10.0
- }
- offset := price1 - price2
- unit := symbolUnitInt[sid]
- ret := offset / (10 * unit * correct)
- return ret, nil
- }
- func PointCorrect(symbol string) float64 {
- if symbol == "XAUUSD" {
- return 10.0
- }
- if symbol == "XAGUSD" || symbol == "OILUSD" {
- return 1.0
- }
- return 1.0
- }
- func BaseCorrect(symbol string) float64 {
- if symbol == "XAUUSD" {
- return 1000.0
- }
- if symbol == "XAGUSD" || symbol == "OILUSD" {
- return 100.0
- }
- return 1.0
- }
- func SymbolType(symbol string) (t int, err error) {
- id, ok := symbolMap[symbol]
- if !ok {
- return 0, fmt.Errorf("SymbolGetType symbol %s not exist", symbol)
- }
- r := []rune(symbol)
- if string(r[0:3]) == "USD" {
- return INDIRECT_RATES, nil
- }
- if string(r[3:]) == "USD" {
- return DIRECT_RATES, nil
- }
- if id > FXCount && id < FuturesCount {
- return FUTURES_RATES, nil
- }
- return CROSS_RATES, nil
- }
- func SymbolPartId(symbol string, part int) (int, error) {
- var symbolPart string
- if len(symbol) != 6 {
- return -1, fmt.Errorf("SymbolPartId error symbol name %s", symbol)
- }
- r := []rune(symbol)
- if part == 1 {
- symbolPart = string(r[0:3])
- } else {
- symbolPart = string(r[3:])
- }
- symbolPart1 := symbolPart + "USD"
- if id, ok := symbolMap[symbolPart1]; ok {
- return id, nil
- }
- symbolPart1 = "USD" + symbolPart
- if id, ok := symbolMap[symbolPart1]; ok {
- return id, nil
- }
- return -1, fmt.Errorf("symbolPartId error symbol name %s not a cross symbol", symbol)
- }
- func SymbolUint(symbol string) (t float64, err error) {
- value, ok := symbolUnit[symbol]
- if !ok {
- return 0.01, nil
- }
- return value, nil
- }
- func SymbolPoint(symbol string) (t int, err error) {
- unit, err := SymbolUint(symbol)
- if err != nil {
- return 0, err
- }
- return -int(math.Log10(unit)), nil
- }
- func SymbolId(symbol string) (t int, err error) {
- value, ok := symbolMap[symbol]
- //fmt.Println("symbol=", symbolMap[symbol])
- if !ok {
- return 0, fmt.Errorf("SymbolID symbol %s not exist", symbol)
- }
- return value, nil
- }
- func symbolName(id int) (t string, err error) {
- value, ok := symbols[id]
- if !ok {
- return "", fmt.Errorf("SymbolName symbol_id %d not exist", id)
- }
- return value, nil
- }
- func SymbolMap() (t map[string]int) {
- return symbolMap
- }
- func GetStockSymbolList(in []string) ([]int, error) {
- err := getStockSymbol()
- if err != nil {
- return nil, err
- }
- var ret []int
- for _, v := range stockSymbol {
- name , _ := CustomSymbolName(v+1000)
- if in != nil {
- con := true;
- for i := 0; i < len(in); i++ {
- if in[i] == name {
- con = false
- break
- }
- }
- if con { //not found
- continue
- }
- }
- ret = append(ret, v + 1000)
- }
- return ret, nil
- }
- func GetStockSymbolNameList() ([]string, error) {
- err := getStockSymbol()
- if err != nil {
- return nil, err
- }
- var ret []string
- for _, v := range stockName {
- ret = append(ret, v)
- }
- return ret, nil
- }
- func GetLmaxSymbolList() ([]int, error) {
- var ret []int
- for i := 0; i < SymbolCount; i++ {
- ret = append(ret, i)
- }
- return ret, nil
- }
- func CustomSymbolId(ty string, insId int) (t int, err error) {
- err = getStockSymbol()
- if err != nil {
- return 0, err
- }
- if ty == "lmax" {
- return BookIdToSymbolId(insId)
- }
- if ty == "sina" || ty == "dzh" {//1000-6000
- if id, ok := stockSymbol[int64(insId)]; ok {
- return 1000 + id, nil
- }
- return 0, errors.New("sina stock symbol error")
- }
- return 0, errors.New("CustomSymbolId error type")
- }
- func SymbolName(id int) (t string, err error) {
- err = getStockSymbol()
- if err != nil {
- return "", err
- }
- if id < 1000 {
- return symbolName(id)
- }
- if id >= 1000 && id <= 6000 {
- symbolNameMu.Lock()
- defer symbolNameMu.Unlock()
- return stockName[id-1000], nil
- }
- return "", errors.New("CustomSymbolName not in range")
- }
- func CustomSymbolName(id int) (t string, err error) {
- return SymbolName(id)
- }
- func SymbolId2(symbol string) (t int, err error) {
- err = getStockSymbol()
- if err != nil {
- return 0, err
- }
- if len(symbol) < 2 {
- return 0, errors.New("SymbolId2 error symbol")
- }
- if symbol[0:2] == "sh" || symbol[0:2] == "sz" {
- insId, err := strconv.ParseInt(symbol[2:], 10, 64)
- if err != nil {
- return 0, err
- }
- if symbol == "sh000300" {
- insId = 999998
- }
- if symbol == "sh000001" {
- insId = 999999
- }
- if id, ok := stockSymbol[int64(insId)]; ok {
- return 1000 + id, nil
- }
- return 0, errors.New("SymbolId2 sina stock symbol error")
- } else {
- return SymbolId(symbol)
- }
- return 0, errors.New("SymbolId2 error type")
- }
|