ds_cfix.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package tick
  3. // 本文件实现cfix数据源接口, 实时数据和历史数据的获取和保存
  4. import (
  5. "fmt"
  6. "log"
  7. "strings"
  8. "tickserver/api/fix"
  9. "tickserver/markinfo"
  10. "tickserver/server/market"
  11. )
  12. // CFixDS实现了dataSource接口, 并对cfix的历史数据和实时数据保存
  13. type CFixDS struct {
  14. *DSBase
  15. conf *DsConf
  16. }
  17. var cfixInss = []int{
  18. markinfo.AUDCAD,
  19. markinfo.AUDCHF,
  20. markinfo.AUDJPY,
  21. markinfo.AUDNZD,
  22. markinfo.AUDUSD,
  23. markinfo.CADJPY,
  24. markinfo.CHFJPY,
  25. markinfo.EURAUD,
  26. markinfo.EURCAD,
  27. markinfo.EURCHF,
  28. markinfo.EURGBP,
  29. markinfo.EURJPY,
  30. markinfo.EURNZD,
  31. markinfo.EURUSD,
  32. markinfo.GBPAUD,
  33. markinfo.GBPCAD,
  34. markinfo.GBPCHF,
  35. markinfo.GBPJPY,
  36. markinfo.GBPUSD,
  37. markinfo.NZDJPY,
  38. markinfo.NZDUSD,
  39. markinfo.OILUSD,
  40. markinfo.SGDJPY,
  41. markinfo.USDCAD,
  42. markinfo.USDCHF,
  43. markinfo.USDJPY,
  44. markinfo.USDPLN,
  45. markinfo.XAGUSD,
  46. markinfo.XAGEUR,
  47. markinfo.XAUUSD,
  48. markinfo.XAUEUR,
  49. markinfo.ZARJPY,
  50. }
  51. func init() {
  52. drivers[CFix] = newCFixDS
  53. }
  54. func newCFixDS(conf *DsConf) (DataSource, error) {
  55. cfixds := &CFixDS{
  56. DSBase: NewDsBase(conf),
  57. conf: conf,
  58. }
  59. cfixds.insMap = cfixInsMap()
  60. return cfixds, nil
  61. }
  62. func (cfixds *CFixDS) Name() string {
  63. return CFix
  64. }
  65. func (cfixds *CFixDS) Run() {
  66. log.Println("CFixDS.Run")
  67. ss, err := fix.NewSessionSettings(cfixds.conf.CfgFile)
  68. if err != nil {
  69. panic(err)
  70. }
  71. sourdatach := make(chan *fix.TickFull, 1024)
  72. go cfixds.onMarket(sourdatach)
  73. app := fix.NewMarketInfoApp(sourdatach)
  74. //fmt.Println(app, ss, cfixds.conf.CfgFile)
  75. trade, err := fix.NewAppTradeClient(app, ss)
  76. if err != nil {
  77. panic(err)
  78. }
  79. fmt.Println("cfix run start")
  80. trade.Start()
  81. trade.Run()
  82. fmt.Println("cfix run end")
  83. trade.Stop()
  84. trade.Free()
  85. fmt.Println("cfix stop")
  86. }
  87. func (cfixds *CFixDS) onMarket(sourdatach chan *fix.TickFull) {
  88. for {
  89. tf := <-sourdatach
  90. mk := &Market{}
  91. mk.Type = IntCFix
  92. symbol := string(tf.Symbol[:7])
  93. ss := strings.Split(symbol, "/")
  94. if len(ss) != 2 {
  95. continue
  96. }
  97. symbol = ss[0] + ss[1]
  98. symbolId, err := markinfo.SymbolId(symbol)
  99. if err != nil {
  100. continue
  101. }
  102. mk.InsId = int64(symbolId)
  103. mk.Asks = make([]PP, tf.AskCount)
  104. mk.Bids = make([]PP, tf.BidCount)
  105. for i := 0; i < int(tf.AskCount); i++ {
  106. mk.Asks[i][0] = tf.AskPrice[i]
  107. mk.Asks[i][1] = tf.AskVolume[i]
  108. }
  109. for i := 0; i < int(tf.BidCount); i++ {
  110. mk.Bids[i][0] = tf.BidPrice[i]
  111. mk.Bids[i][1] = tf.BidVolume[i]
  112. }
  113. mk.Timestamp = int64(tf.Time)*1000 + int64(tf.Millisecond)
  114. mk.Close = tf.BidPrice[0]
  115. mk.High = tf.BidPrice[0]
  116. mk.Low = tf.BidPrice[0]
  117. mk.Open = tf.BidPrice[0]
  118. mk.AllAmount = tf.BidVolume[0]
  119. mk.AllVolume = tf.BidVolume[0]
  120. mk.LastPrice = tf.BidPrice[0]
  121. mk.LastVolume = tf.BidVolume[0]
  122. cfixds.Save(mk)
  123. }
  124. }
  125. func cfixInsMap() map[int64]*Instrument {
  126. insMap := make(map[int64]*Instrument)
  127. for _, id := range cfixInss {
  128. x, _ := markinfo.SymbolName(id)
  129. u, _ := markinfo.SymbolUint(x)
  130. ins := &Instrument{
  131. Id: int64(id),
  132. Name: x,
  133. ExId: CFix,
  134. Type: market.Forex,
  135. PriceInc: u,
  136. //StartTime: time.Now().Unix() * 1000,
  137. }
  138. insMap[int64(id)] = ins
  139. }
  140. return insMap
  141. }