ds_chbtc.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package tick
  3. // 本文件实现chbtc数据源的tick数据获取下载和保存
  4. import (
  5. "log"
  6. "net/http"
  7. "time"
  8. "tickserver/markinfo"
  9. "tickserver/server/market"
  10. coinapi "github.com/nntaoli/crypto_coin_api"
  11. "github.com/nntaoli/crypto_coin_api/chbtc"
  12. )
  13. var chbtcInss = []int{
  14. markinfo.BTCCNY,
  15. markinfo.ETCCNY,
  16. markinfo.ETHCNY,
  17. }
  18. // ChbtcDS 实现数据源dataSource接口的定义
  19. type ChbtcDS struct {
  20. *DSBase
  21. conf *DsConf
  22. chbtc *chbtc.Chbtc
  23. lastprice []float64
  24. lastvolume []float64
  25. }
  26. func init() {
  27. drivers[Chbtc] = newChbtcDS
  28. }
  29. func newChbtcDS(conf *DsConf) (DataSource, error) {
  30. log.Println("newChbtcDS")
  31. cds := &ChbtcDS{
  32. DSBase: NewDsBase(conf),
  33. conf: conf,
  34. lastprice: make([]float64, len(chbtcInss)),
  35. lastvolume: make([]float64, len(chbtcInss)),
  36. }
  37. cds.insMap = chbtcInsMap()
  38. cds.chbtc = chbtc.New(&http.Client{}, "", "")
  39. return cds, nil
  40. }
  41. func (cds *ChbtcDS) Name() string {
  42. return Chbtc
  43. }
  44. func (cds *ChbtcDS) Run() {
  45. log.Println("ChbtcDS.run")
  46. for {
  47. for k, _ := range chbtcInss {
  48. cds.getChbtcData(k)
  49. time.Sleep(time.Second)
  50. }
  51. }
  52. }
  53. func chbtcInsMap() map[int64]*Instrument {
  54. insMap := make(map[int64]*Instrument)
  55. for _, id := range chbtcInss {
  56. x, _ := markinfo.SymbolName(id)
  57. u, _ := markinfo.SymbolUint(x)
  58. ins := &Instrument{
  59. Id: int64(id),
  60. Name: x,
  61. ExId: Btc,
  62. Type: market.Btcs,
  63. PriceInc: u,
  64. StartTime: time.Now().Unix() * 1000,
  65. }
  66. insMap[int64(id)] = ins
  67. }
  68. return insMap
  69. }
  70. func (cds *ChbtcDS) getChbtcData(index int) {
  71. var currency coinapi.CurrencyPair
  72. if chbtcInss[index] == markinfo.BTCCNY {
  73. currency = coinapi.BTC_CNY
  74. }
  75. if chbtcInss[index] == markinfo.ETCCNY {
  76. currency = coinapi.ETC_CNY
  77. }
  78. if chbtcInss[index] == markinfo.ETHCNY {
  79. currency = coinapi.ETH_CNY
  80. }
  81. ticker, _ := cds.chbtc.GetTicker(currency)
  82. if ticker != nil && (ticker.Last != cds.lastprice[index] || ticker.Vol != cds.lastvolume[index]) {
  83. cds.lastprice[index] = ticker.Last
  84. cds.lastvolume[index] = ticker.Vol
  85. mk := &Market{}
  86. mk.Type = IntChbtc
  87. mk.InsId = int64(chbtcInss[index])
  88. mk.Timestamp = int64(ticker.Date)
  89. var ask, bid PP
  90. ask[0] = ticker.Sell
  91. bid[0] = ticker.Buy
  92. mk.Asks = append(mk.Asks, ask)
  93. mk.Bids = append(mk.Bids, bid)
  94. mk.High = ticker.High
  95. mk.LastPrice = ticker.Last
  96. mk.Low = ticker.Low
  97. mk.LastVolume = ticker.Vol
  98. cds.Save(mk)
  99. }
  100. }