symbol.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. package markinfo
  2. import "fmt"
  3. import "math"
  4. const (
  5. EURUSD = iota
  6. GBPUSD
  7. USDJPY
  8. USDCHF
  9. AUDUSD
  10. USDCAD
  11. NZDUSD
  12. EURGBP
  13. EURJPY
  14. EURCHF
  15. EURAUD
  16. EURCAD
  17. GBPCHF
  18. GBPJPY
  19. CHFJPY
  20. CADJPY
  21. AUDJPY
  22. AUDCAD
  23. USDMXN
  24. AUDNZD
  25. XAGUSD
  26. XAUUSD
  27. OILUSD
  28. USDTRY
  29. GBPAUD //from lmax symbol list
  30. GBPCAD
  31. EURCZK
  32. GBPCZK
  33. USDCZK
  34. EURDKK
  35. GBPDKK
  36. USDDKK
  37. EURHKD
  38. GBPHKD
  39. USDHKD
  40. EURHUF
  41. GBPHUF
  42. USDHUF
  43. EURMXN
  44. GBPMXN
  45. EURNOK
  46. GBPNOK
  47. USDNOK
  48. EURNZD
  49. GBPNZD
  50. EURPLN
  51. GBPPLN
  52. USDPLN
  53. EURSEK
  54. GBPSEK
  55. USDSEK
  56. EURSGD
  57. GBPSGD
  58. USDSGD
  59. EURTRY
  60. GBPTRY
  61. EURZAR
  62. GBPZAR
  63. USDZAR
  64. NZDJPY
  65. AUDCHF
  66. CADCHF
  67. NZDCAD
  68. NZDCHF
  69. NZDSGD
  70. SGDJPY
  71. CurrencyCount
  72. UK100
  73. WS30
  74. SPX
  75. NDX
  76. GDAXI
  77. FCHI
  78. JAPEN225
  79. STOXX50E
  80. AEX
  81. CLQ2
  82. CLU2
  83. CLV2
  84. LCOQ2
  85. LCOU2
  86. LCOV2
  87. GCQ2
  88. LGON2
  89. LGOQ2
  90. LGOU2
  91. FXCount
  92. SH000300
  93. IFMI
  94. FuturesCount
  95. SymbolCount
  96. )
  97. var symbolMap map[string]int
  98. var symbols = map[int]string{
  99. EURUSD: "EURUSD",
  100. GBPUSD: "GBPUSD",
  101. USDJPY: "USDJPY",
  102. USDCHF: "USDCHF",
  103. AUDUSD: "AUDUSD",
  104. USDCAD: "USDCAD",
  105. NZDUSD: "NZDUSD",
  106. EURGBP: "EURGBP",
  107. EURJPY: "EURJPY",
  108. EURCHF: "EURCHF",
  109. EURAUD: "EURAUD",
  110. EURCAD: "EURCAD",
  111. GBPCHF: "GBPCHF",
  112. GBPJPY: "GBPJPY",
  113. CHFJPY: "CHFJPY",
  114. CADJPY: "CADJPY",
  115. AUDJPY: "AUDJPY",
  116. AUDCAD: "AUDCAD",
  117. USDMXN: "USDMXN",
  118. AUDNZD: "AUDNZD",
  119. XAGUSD: "XAGUSD",
  120. XAUUSD: "XAUUSD",
  121. OILUSD: "OILUSD",
  122. USDTRY: "USDTRY",
  123. GBPAUD: "GBPAUD",
  124. GBPCAD: "GBPCAD",
  125. EURCZK: "EURCZK",
  126. GBPCZK: "GBPCZK",
  127. USDCZK: "USDCZK",
  128. EURDKK: "EURDKK",
  129. GBPDKK: "GBPDKK",
  130. USDDKK: "USDDKK",
  131. EURHKD: "EURHKD",
  132. GBPHKD: "GBPHKD",
  133. USDHKD: "USDHKD",
  134. EURHUF: "EURHUF",
  135. GBPHUF: "GBPHUF",
  136. USDHUF: "USDHUF",
  137. EURMXN: "EURMXN",
  138. GBPMXN: "GBPMXN",
  139. EURNOK: "EURNOK",
  140. GBPNOK: "GBPNOK",
  141. USDNOK: "USDNOK",
  142. EURNZD: "EURNZD",
  143. GBPNZD: "GBPNZD",
  144. EURPLN: "EURPLN",
  145. GBPPLN: "GBPPLN",
  146. USDPLN: "USDPLN",
  147. EURSEK: "EURSEK",
  148. GBPSEK: "GBPSEK",
  149. USDSEK: "USDSEK",
  150. EURSGD: "EURSGD",
  151. GBPSGD: "GBPSGD",
  152. USDSGD: "USDSGD",
  153. EURTRY: "EURTRY",
  154. GBPTRY: "GBPTRY",
  155. EURZAR: "EURZAR",
  156. GBPZAR: "GBPZAR",
  157. USDZAR: "USDZAR",
  158. NZDJPY: "NZDJPY",
  159. AUDCHF: "AUDCHF",
  160. CADCHF: "CADCHF",
  161. NZDCAD: "NZDCAD",
  162. NZDCHF: "NZDCHF",
  163. NZDSGD: "NZDSGD",
  164. SGDJPY: "SGDJPY",
  165. UK100: "UK100",
  166. WS30: "WS30",
  167. SPX: "SPX",
  168. NDX: "NDX",
  169. GDAXI: "GDAXI",
  170. FCHI: "FCHI",
  171. JAPEN225: "JAPEN225",
  172. STOXX50E: "STOXX50E",
  173. AEX: "AEX",
  174. CLQ2: "CLQ2",
  175. CLU2: "CLU2",
  176. CLV2: "CLV2",
  177. LCOQ2: "LCOQ2",
  178. LCOU2: "LCOU2",
  179. LCOV2: "LCOV2",
  180. GCQ2: "GCQ2",
  181. LGON2: "LGON2",
  182. LGOQ2: "LGOQ2",
  183. LGOU2: "LGOU2",
  184. FXCount: "FXCount",
  185. SH000300: "SH000300",
  186. IFMI: "IFMI",
  187. FuturesCount: "FuturesCount",
  188. SymbolCount: "SymbolCount",
  189. }
  190. var symbolUnit map[string]float64
  191. var symbolUnitInt = map[int]float64{
  192. EURUSD: 0.00001,
  193. GBPUSD: 0.00001,
  194. USDJPY: 0.001,
  195. USDCHF: 0.00001,
  196. AUDUSD: 0.00001,
  197. USDCAD: 0.00001,
  198. NZDUSD: 0.00001,
  199. EURGBP: 0.00001,
  200. EURJPY: 0.001,
  201. EURCHF: 0.00001,
  202. EURAUD: 0.00001,
  203. EURCAD: 0.00001,
  204. GBPCHF: 0.00001,
  205. GBPJPY: 0.001,
  206. CHFJPY: 0.001,
  207. CADJPY: 0.001,
  208. AUDJPY: 0.001,
  209. AUDCAD: 0.00001,
  210. USDMXN: 0.00001,
  211. AUDNZD: 0.00001,
  212. XAGUSD: 0.001,
  213. XAUUSD: 0.001,
  214. OILUSD: 0.001,
  215. USDTRY: 0.00001,
  216. GBPAUD: 0.00001,
  217. GBPCAD: 0.00001,
  218. EURCZK: 0.0001,
  219. GBPCZK: 0.0001,
  220. USDCZK: 0.0001,
  221. EURDKK: 0.00001,
  222. GBPDKK: 0.00001,
  223. USDDKK: 0.00001,
  224. EURHKD: 0.00001,
  225. GBPHKD: 0.00001,
  226. USDHKD: 0.00001,
  227. EURHUF: 0.001,
  228. GBPHUF: 0.001,
  229. USDHUF: 0.001,
  230. EURMXN: 0.00001,
  231. GBPMXN: 0.00001,
  232. EURNOK: 0.00001,
  233. GBPNOK: 0.00001,
  234. USDNOK: 0.00001,
  235. EURNZD: 0.00001,
  236. GBPNZD: 0.00001,
  237. EURPLN: 0.00001,
  238. GBPPLN: 0.00001,
  239. USDPLN: 0.00001,
  240. EURSEK: 0.00001,
  241. GBPSEK: 0.00001,
  242. USDSEK: 0.00001,
  243. EURSGD: 0.00001,
  244. GBPSGD: 0.00001,
  245. USDSGD: 0.00001,
  246. EURTRY: 0.00001,
  247. GBPTRY: 0.00001,
  248. EURZAR: 0.00001,
  249. GBPZAR: 0.00001,
  250. USDZAR: 0.00001,
  251. NZDJPY: 0.001,
  252. AUDCHF: 0.00001,
  253. CADCHF: 0.00001,
  254. NZDCAD: 0.00001,
  255. NZDCHF: 0.00001,
  256. NZDSGD: 0.00001,
  257. SGDJPY: 0.001,
  258. UK100: 0.1,
  259. WS30: 0.1,
  260. SPX: 0.1,
  261. NDX: 0.1,
  262. GDAXI: 0.1,
  263. FCHI: 0.1,
  264. JAPEN225: 1.0,
  265. STOXX50E: 0.1,
  266. AEX: 0.1,
  267. CLQ2: 0.1,
  268. CLU2: 0.1,
  269. CLV2: 0.1,
  270. LCOQ2: 0.1,
  271. LCOU2: 0.1,
  272. LCOV2: 0.1,
  273. GCQ2: 0.1,
  274. LGON2: 0.1,
  275. LGOQ2: 0.1,
  276. LGOU2: 0.1,
  277. SH000300: 0.01,
  278. IFMI: 0.01,
  279. }
  280. var symbolIdBookId map[int]int
  281. var bookIdSymbolId = map[int]int{
  282. 4008: AUDJPY,
  283. 4007: AUDUSD,
  284. 4009: CHFJPY,
  285. 4016: EURAUD,
  286. 4015: EURCAD,
  287. 4011: EURCHF,
  288. 4003: EURGBP,
  289. 4006: EURJPY,
  290. 4001: EURUSD,
  291. 4017: GBPAUD,
  292. 4014: GBPCAD,
  293. 4012: GBPCHF,
  294. 4005: GBPJPY,
  295. 4002: GBPUSD,
  296. 4013: USDCAD,
  297. 4010: USDCHF,
  298. 4004: USDJPY,
  299. 100479: EURCZK,
  300. 100481: GBPCZK,
  301. 100483: USDCZK,
  302. 100485: EURDKK,
  303. 100487: GBPDKK,
  304. 100489: USDDKK,
  305. 100491: EURHKD,
  306. 100493: GBPHKD,
  307. 100495: USDHKD,
  308. 100497: EURHUF,
  309. 100499: GBPHUF,
  310. 100501: USDHUF,
  311. 100503: EURMXN,
  312. 100505: GBPMXN,
  313. 100507: USDMXN,
  314. 100509: EURNOK,
  315. 100511: GBPNOK,
  316. 100513: USDNOK,
  317. 100515: EURNZD,
  318. 100517: GBPNZD,
  319. 100519: EURPLN,
  320. 100521: GBPPLN,
  321. 100523: USDPLN,
  322. 100525: EURSEK,
  323. 100527: GBPSEK,
  324. 100529: USDSEK,
  325. 100531: EURSGD,
  326. 100533: GBPSGD,
  327. 100535: USDSGD,
  328. 100537: EURTRY,
  329. 100539: GBPTRY,
  330. 100541: USDTRY,
  331. 100543: EURZAR,
  332. 100545: GBPZAR,
  333. 100547: USDZAR,
  334. 100613: NZDUSD,
  335. 100615: AUDNZD,
  336. 100617: NZDJPY,
  337. 100619: AUDCHF,
  338. 100667: AUDCAD,
  339. 100671: CADCHF,
  340. 100669: CADJPY,
  341. 100673: NZDCAD,
  342. 100675: NZDCHF,
  343. 100677: NZDSGD,
  344. 100678: SGDJPY,
  345. 100089: UK100,
  346. 100091: WS30,
  347. 100093: SPX,
  348. 100095: NDX,
  349. 100097: GDAXI,
  350. 100099: FCHI,
  351. 100101: STOXX50E,
  352. 100103: AEX,
  353. 100105: JAPEN225,
  354. 100637: XAUUSD,
  355. 100639: XAGUSD,
  356. 100699: CLQ2,
  357. 100705: CLU2,
  358. 100711: CLV2,
  359. 100697: LCOQ2,
  360. 100703: LCOU2,
  361. 100709: LCOV2,
  362. 100659: GCQ2,
  363. 100695: LGON2,
  364. 100701: LGOQ2,
  365. 100707: LGOU2,
  366. }
  367. func init() {
  368. //symbol map
  369. symbolMap = make(map[string]int)
  370. for k, v := range symbols {
  371. symbolMap[v] = k
  372. }
  373. //symbolIdBookId
  374. symbolIdBookId = make(map[int]int)
  375. for k, v := range bookIdSymbolId {
  376. symbolIdBookId[v] = k
  377. }
  378. //单位初始化
  379. symbolUnit = make(map[string]float64)
  380. for k, v := range symbolUnitInt {
  381. symbolUnit[symbols[k]] = v
  382. }
  383. }
  384. const (
  385. DIRECT_RATES = iota
  386. INDIRECT_RATES
  387. CROSS_RATES
  388. FUTURES_RATES
  389. )
  390. func BookIdToSymbolId(bookId int) (symbolId int, err error) {
  391. if symbolId, ok := bookIdSymbolId[bookId]; ok {
  392. return symbolId, nil
  393. }
  394. return 0, fmt.Errorf("BookIdToSymbolId:: bookId %d error.", bookId)
  395. }
  396. func SymbolIdToBookId(symbolId int) (bookId int, err error) {
  397. if bookId, ok := symbolIdBookId[symbolId]; ok {
  398. return bookId, nil
  399. }
  400. return 0, fmt.Errorf("SymbolIdToBookId:: symbolId %d error.", symbolId)
  401. }
  402. func GetPoint(sid int, price1, price2 float64) (float64, error) {
  403. if sid < 0 || sid >= SymbolCount {
  404. return 0.0, fmt.Errorf("GetPoint symbolId %d not exist", sid)
  405. }
  406. correct := 1.0
  407. if sid == XAUUSD {
  408. correct = 10.0
  409. }
  410. offset := price1 - price2
  411. unit := symbolUnitInt[sid]
  412. ret := offset / (10 * unit * correct)
  413. return ret, nil
  414. }
  415. func PointCorrect(symbol string) float64 {
  416. if symbol == "XAUUSD" {
  417. return 10.0
  418. }
  419. if symbol == "XAGUSD" || symbol == "OILUSD" {
  420. return 1.0
  421. }
  422. return 1.0
  423. }
  424. func BaseCorrect(symbol string) float64 {
  425. if symbol == "XAUUSD" {
  426. return 1000.0
  427. }
  428. if symbol == "XAGUSD" || symbol == "OILUSD" {
  429. return 100.0
  430. }
  431. return 1.0
  432. }
  433. func SymbolType(symbol string) (t int, err error) {
  434. id, ok := symbolMap[symbol]
  435. if !ok {
  436. return 0, fmt.Errorf("SymbolGetType symbol %s not exist", symbol)
  437. }
  438. r := []rune(symbol)
  439. if string(r[0:3]) == "USD" {
  440. return INDIRECT_RATES, nil
  441. }
  442. if string(r[3:]) == "USD" {
  443. return DIRECT_RATES, nil
  444. }
  445. if id > FXCount && id < FuturesCount {
  446. return FUTURES_RATES, nil
  447. }
  448. return CROSS_RATES, nil
  449. }
  450. func SymbolPartId(symbol string, part int) (int, error) {
  451. var symbolPart string
  452. if len(symbol) != 6 {
  453. return -1, fmt.Errorf("SymbolPartId error symbol name %s", symbol)
  454. }
  455. r := []rune(symbol)
  456. if part == 1 {
  457. symbolPart = string(r[0:3])
  458. } else {
  459. symbolPart = string(r[3:])
  460. }
  461. symbolPart1 := symbolPart + "USD"
  462. if id, ok := symbolMap[symbolPart1]; ok {
  463. return id, nil
  464. }
  465. symbolPart1 = "USD" + symbolPart
  466. if id, ok := symbolMap[symbolPart1]; ok {
  467. return id, nil
  468. }
  469. return -1, fmt.Errorf("symbolPartId error symbol name %s not a cross symbol", symbol)
  470. }
  471. func SymbolUint(symbol string) (t float64, err error) {
  472. value, ok := symbolUnit[symbol]
  473. if !ok {
  474. return 0, fmt.Errorf("SymbolGetUint symbol %s not exist", symbol)
  475. }
  476. return value, nil
  477. }
  478. func SymbolPoint(symbol string) (t int, err error) {
  479. unit, err := SymbolUint(symbol)
  480. if err != nil {
  481. return 0, err
  482. }
  483. return -int(math.Log10(unit)), nil
  484. }
  485. func SymbolId(symbol string) (t int, err error) {
  486. value, ok := symbolMap[symbol]
  487. if !ok {
  488. return 0, fmt.Errorf("SymbolID symbol %s not exist", symbol)
  489. }
  490. return value, nil
  491. }
  492. func SymbolName(id int) (t string, err error) {
  493. value, ok := symbols[id]
  494. if !ok {
  495. return "", fmt.Errorf("SymbolName symbol_id %d not exist", id)
  496. }
  497. return value, nil
  498. }
  499. func SymbolMap() (t map[string]int) {
  500. return symbolMap
  501. }