123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
- package tick
- // 本文件实现chbtc数据源的tick数据获取下载和保存
- import (
- "log"
- "net/http"
- "time"
- "tickserver/markinfo"
- "tickserver/server/market"
- coinapi "github.com/nntaoli/crypto_coin_api"
- "github.com/nntaoli/crypto_coin_api/chbtc"
- )
- var chbtcInss = []int{
- markinfo.BTCCNY,
- markinfo.ETCCNY,
- markinfo.ETHCNY,
- }
- // ChbtcDS 实现数据源dataSource接口的定义
- type ChbtcDS struct {
- *DSBase
- conf *DsConf
- chbtc *chbtc.Chbtc
- lastprice []float64
- lastvolume []float64
- }
- func init() {
- drivers[Chbtc] = newChbtcDS
- }
- func newChbtcDS(conf *DsConf) (DataSource, error) {
- log.Println("newChbtcDS")
- cds := &ChbtcDS{
- DSBase: NewDsBase(conf),
- conf: conf,
- lastprice: make([]float64, len(chbtcInss)),
- lastvolume: make([]float64, len(chbtcInss)),
- }
- cds.insMap = chbtcInsMap()
- cds.chbtc = chbtc.New(&http.Client{}, "", "")
- return cds, nil
- }
- func (cds *ChbtcDS) Name() string {
- return Chbtc
- }
- func (cds *ChbtcDS) Run() {
- log.Println("ChbtcDS.run")
- for {
- for k, _ := range chbtcInss {
- cds.getChbtcData(k)
- time.Sleep(time.Second)
- }
- }
- }
- func chbtcInsMap() map[int64]*Instrument {
- insMap := make(map[int64]*Instrument)
- for _, id := range chbtcInss {
- x, _ := markinfo.SymbolName(id)
- u, _ := markinfo.SymbolUint(x)
- ins := &Instrument{
- Id: int64(id),
- Name: x,
- ExId: Btc,
- Type: market.Btcs,
- PriceInc: u,
- StartTime: time.Now().Unix() * 1000,
- }
- insMap[int64(id)] = ins
- }
- return insMap
- }
- func (cds *ChbtcDS) getChbtcData(index int) {
- var currency coinapi.CurrencyPair
- if chbtcInss[index] == markinfo.BTCCNY {
- currency = coinapi.BTC_CNY
- }
- if chbtcInss[index] == markinfo.ETCCNY {
- currency = coinapi.ETC_CNY
- }
- if chbtcInss[index] == markinfo.ETHCNY {
- currency = coinapi.ETH_CNY
- }
- ticker, _ := cds.chbtc.GetTicker(currency)
- if ticker != nil && (ticker.Last != cds.lastprice[index] || ticker.Vol != cds.lastvolume[index]) {
- cds.lastprice[index] = ticker.Last
- cds.lastvolume[index] = ticker.Vol
- mk := &Market{}
- mk.Type = IntChbtc
- mk.InsId = int64(chbtcInss[index])
- mk.Timestamp = int64(ticker.Date)
- var ask, bid PP
- ask[0] = ticker.Sell
- bid[0] = ticker.Buy
- mk.Asks = append(mk.Asks, ask)
- mk.Bids = append(mk.Bids, bid)
- mk.High = ticker.High
- mk.LastPrice = ticker.Last
- mk.Low = ticker.Low
- mk.LastVolume = ticker.Vol
- cds.Save(mk)
- }
- }
|