symbol.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package markinfo
  2. import "fmt"
  3. import "math"
  4. import "errors"
  5. import "sync"
  6. import "strconv"
  7. func init() {
  8. //symbol map
  9. symbolMap = make(map[string]int)
  10. for k, v := range symbols {
  11. symbolMap[v] = k
  12. }
  13. //symbolIdBookId
  14. symbolIdBookId = make(map[int]int)
  15. for k, v := range bookIdSymbolId {
  16. symbolIdBookId[v] = k
  17. }
  18. //单位初始化
  19. symbolUnit = make(map[string]float64)
  20. for k, v := range symbolUnitInt {
  21. symbolUnit[symbols[k]] = v
  22. }
  23. }
  24. var symbolNameMu sync.Mutex
  25. const (
  26. DIRECT_RATES = iota
  27. INDIRECT_RATES
  28. CROSS_RATES
  29. FUTURES_RATES
  30. )
  31. func BookIdToSymbolId(bookId int) (symbolId int, err error) {
  32. if symbolId, ok := bookIdSymbolId[bookId]; ok {
  33. return symbolId, nil
  34. }
  35. return 0, fmt.Errorf("BookIdToSymbolId:: bookId %d error.", bookId)
  36. }
  37. func SymbolIdToBookId(symbolId int) (bookId int, err error) {
  38. if bookId, ok := symbolIdBookId[symbolId]; ok {
  39. return bookId, nil
  40. }
  41. return 0, fmt.Errorf("SymbolIdToBookId:: symbolId %d error.", symbolId)
  42. }
  43. func GetPoint(sid int, price1, price2 float64) (float64, error) {
  44. if sid < 0 || sid >= SymbolCount {
  45. return 0.0, fmt.Errorf("GetPoint symbolId %d not exist", sid)
  46. }
  47. correct := 1.0
  48. if sid == XAUUSD {
  49. correct = 10.0
  50. }
  51. offset := price1 - price2
  52. unit := symbolUnitInt[sid]
  53. ret := offset / (10 * unit * correct)
  54. return ret, nil
  55. }
  56. func PointCorrect(symbol string) float64 {
  57. if symbol == "XAUUSD" {
  58. return 10.0
  59. }
  60. if symbol == "XAGUSD" || symbol == "OILUSD" {
  61. return 1.0
  62. }
  63. return 1.0
  64. }
  65. func BaseCorrect(symbol string) float64 {
  66. if symbol == "XAUUSD" {
  67. return 1000.0
  68. }
  69. if symbol == "XAGUSD" || symbol == "OILUSD" {
  70. return 100.0
  71. }
  72. return 1.0
  73. }
  74. func SymbolType(symbol string) (t int, err error) {
  75. id, ok := symbolMap[symbol]
  76. if !ok {
  77. return 0, fmt.Errorf("SymbolGetType symbol %s not exist", symbol)
  78. }
  79. r := []rune(symbol)
  80. if string(r[0:3]) == "USD" {
  81. return INDIRECT_RATES, nil
  82. }
  83. if string(r[3:]) == "USD" {
  84. return DIRECT_RATES, nil
  85. }
  86. if id > FXCount && id < FuturesCount {
  87. return FUTURES_RATES, nil
  88. }
  89. return CROSS_RATES, nil
  90. }
  91. func SymbolPartId(symbol string, part int) (int, error) {
  92. var symbolPart string
  93. if len(symbol) != 6 {
  94. return -1, fmt.Errorf("SymbolPartId error symbol name %s", symbol)
  95. }
  96. r := []rune(symbol)
  97. if part == 1 {
  98. symbolPart = string(r[0:3])
  99. } else {
  100. symbolPart = string(r[3:])
  101. }
  102. symbolPart1 := symbolPart + "USD"
  103. if id, ok := symbolMap[symbolPart1]; ok {
  104. return id, nil
  105. }
  106. symbolPart1 = "USD" + symbolPart
  107. if id, ok := symbolMap[symbolPart1]; ok {
  108. return id, nil
  109. }
  110. return -1, fmt.Errorf("symbolPartId error symbol name %s not a cross symbol", symbol)
  111. }
  112. func SymbolUint(symbol string) (t float64, err error) {
  113. value, ok := symbolUnit[symbol]
  114. if !ok {
  115. return 0.01, nil
  116. }
  117. return value, nil
  118. }
  119. func SymbolPoint(symbol string) (t int, err error) {
  120. unit, err := SymbolUint(symbol)
  121. if err != nil {
  122. return 0, err
  123. }
  124. return -int(math.Log10(unit)), nil
  125. }
  126. func SymbolId(symbol string) (t int, err error) {
  127. value, ok := symbolMap[symbol]
  128. //fmt.Println("symbol=", symbolMap[symbol])
  129. if !ok {
  130. return 0, fmt.Errorf("SymbolID symbol %s not exist", symbol)
  131. }
  132. return value, nil
  133. }
  134. func symbolName(id int) (t string, err error) {
  135. value, ok := symbols[id]
  136. if !ok {
  137. return "", fmt.Errorf("SymbolName symbol_id %d not exist", id)
  138. }
  139. return value, nil
  140. }
  141. func SymbolMap() (t map[string]int) {
  142. return symbolMap
  143. }
  144. func GetStockSymbolList(in []string) ([]int, error) {
  145. err := getStockSymbol()
  146. if err != nil {
  147. return nil, err
  148. }
  149. var ret []int
  150. for _, v := range stockSymbol {
  151. name , _ := CustomSymbolName(v+1000)
  152. if in != nil {
  153. con := true;
  154. for i := 0; i < len(in); i++ {
  155. if in[i] == name {
  156. con = false
  157. break
  158. }
  159. }
  160. if con { //not found
  161. continue
  162. }
  163. }
  164. ret = append(ret, v + 1000)
  165. }
  166. return ret, nil
  167. }
  168. func GetStockSymbolNameList() ([]string, error) {
  169. err := getStockSymbol()
  170. if err != nil {
  171. return nil, err
  172. }
  173. var ret []string
  174. for _, v := range stockName {
  175. ret = append(ret, v)
  176. }
  177. return ret, nil
  178. }
  179. func GetLmaxSymbolList() ([]int, error) {
  180. var ret []int
  181. for i := 0; i < SymbolCount; i++ {
  182. ret = append(ret, i)
  183. }
  184. return ret, nil
  185. }
  186. func CustomSymbolId(ty string, insId int) (t int, err error) {
  187. err = getStockSymbol()
  188. if err != nil {
  189. return 0, err
  190. }
  191. if ty == "lmax" {
  192. return BookIdToSymbolId(insId)
  193. }
  194. if ty == "sina" || ty == "dzh" {//1000-6000
  195. if id, ok := stockSymbol[int64(insId)]; ok {
  196. return 1000 + id, nil
  197. }
  198. return 0, errors.New("sina stock symbol error")
  199. }
  200. return 0, errors.New("CustomSymbolId error type")
  201. }
  202. func SymbolName(id int) (t string, err error) {
  203. err = getStockSymbol()
  204. if err != nil {
  205. return "", err
  206. }
  207. if id < 1000 {
  208. return symbolName(id)
  209. }
  210. if id >= 1000 && id <= 6000 {
  211. symbolNameMu.Lock()
  212. defer symbolNameMu.Unlock()
  213. return stockName[id-1000], nil
  214. }
  215. return "", errors.New("CustomSymbolName not in range")
  216. }
  217. func CustomSymbolName(id int) (t string, err error) {
  218. return SymbolName(id)
  219. }
  220. func SymbolId2(symbol string) (t int, err error) {
  221. err = getStockSymbol()
  222. if err != nil {
  223. return 0, err
  224. }
  225. if len(symbol) < 2 {
  226. return 0, errors.New("SymbolId2 error symbol")
  227. }
  228. if symbol[0:2] == "sh" || symbol[0:2] == "sz" {
  229. insId, err := strconv.ParseInt(symbol[2:], 10, 64)
  230. if err != nil {
  231. return 0, err
  232. }
  233. if symbol == "sh000300" {
  234. insId = 999998
  235. }
  236. if symbol == "sh000001" {
  237. insId = 999999
  238. }
  239. if id, ok := stockSymbol[int64(insId)]; ok {
  240. return 1000 + id, nil
  241. }
  242. return 0, errors.New("SymbolId2 sina stock symbol error")
  243. } else {
  244. return SymbolId(symbol)
  245. }
  246. return 0, errors.New("SymbolId2 error type")
  247. }