ds_yunbi.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package tick
  3. // 本文件实现yunbi数据源的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/yunbi"
  12. )
  13. var yunbiInss = []int{
  14. markinfo.BTCCNY,
  15. markinfo.ETCCNY,
  16. markinfo.ETHCNY,
  17. }
  18. // YunbiDS 实现数据源dataSource接口的定义
  19. type YunbiDS struct {
  20. *DSBase
  21. conf *DsConf
  22. yunbi *yunbi.YunBi
  23. lastprice []float64
  24. lastvolume []float64
  25. }
  26. func init() {
  27. drivers[Yunbi] = newYunbiDS
  28. }
  29. func newYunbiDS(conf *DsConf) (DataSource, error) {
  30. log.Println("newYunbiDS")
  31. yds := &YunbiDS{
  32. DSBase: NewDsBase(conf),
  33. conf: conf,
  34. lastprice: make([]float64, len(yunbiInss)),
  35. lastvolume: make([]float64, len(yunbiInss)),
  36. }
  37. yds.insMap = yunbiInsMap()
  38. yds.yunbi = yunbi.New(&http.Client{}, "", "")
  39. return yds, nil
  40. }
  41. func (yds *YunbiDS) Name() string {
  42. return Yunbi
  43. }
  44. func (yds *YunbiDS) Run() {
  45. log.Println("YunbiDS.run")
  46. for {
  47. for k, _ := range yunbiInss {
  48. yds.getYunbiData(k)
  49. time.Sleep(time.Second)
  50. }
  51. }
  52. }
  53. func yunbiInsMap() map[int64]*Instrument {
  54. insMap := make(map[int64]*Instrument)
  55. for _, id := range yunbiInss {
  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 (yds *YunbiDS) getYunbiData(index int) {
  71. var currency coinapi.CurrencyPair
  72. if yunbiInss[index] == markinfo.BTCCNY {
  73. currency = coinapi.BTC_CNY
  74. }
  75. if yunbiInss[index] == markinfo.ETCCNY {
  76. currency = coinapi.ETC_CNY
  77. }
  78. if yunbiInss[index] == markinfo.ETHCNY {
  79. currency = coinapi.ETH_CNY
  80. }
  81. ticker, _ := yds.yunbi.GetTicker(currency)
  82. if ticker != nil && (ticker.Last != yds.lastprice[index] || ticker.Vol != yds.lastvolume[index]) {
  83. yds.lastprice[index] = ticker.Last
  84. yds.lastvolume[index] = ticker.Vol
  85. mk := &Market{}
  86. mk.Type = IntYunbi
  87. mk.InsId = int64(yunbiInss[index])
  88. mk.Timestamp = int64(ticker.Date) * 1000
  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. yds.Save(mk)
  99. }
  100. }