123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
- package tick
- // 本文件实现DataSource数据源的tick数据获取下载和保存
- import (
- "log"
- "sync"
- "time"
- )
- var Debug bool = false // for debug
- func SetDebug(debug bool) {
- Debug = debug
- }
- // 数据源接口
- // lmax, easyforex, oanda, ctp以及未来的macoin等均实现此接口
- type DataSource interface {
- Name() string
- Run()
- GetInstrument() []Instrument
- GetMarket() chan *Market
- }
- type DSBase struct {
- conf *DsConf //配置
- chM chan *Market
- insMap map[int64]*Instrument
- countTotal int64
- countThrow int64
- mu sync.Mutex
- }
- func NewDsBase(confIn *DsConf) *DSBase {
- dsb := &DSBase{
- chM: make(chan *Market, 10240),
- insMap: make(map[int64]*Instrument),
- conf: confIn,
- }
- return dsb
- }
- func (dsb *DSBase) Save(m *Market) {
- dsb.countTotal++
- select {
- case dsb.chM <- m:
- default:
- dsb.countThrow++
- //if dsb.countThrow%1000 == 0 {
- //log.Println("@@@:Save:", DataTypeName(int(m.Type)), m, m.InsId, m.LastPrice, dsb.countTotal, dsb.countThrow)
- //}
- //if m.Type == IntTdx && m.InsId == 1 {
- //log.Println("[DS.Save]data trace")
- //}
- }
- }
- func (dsb *DSBase) GetInstrument() []Instrument {
- dsb.mu.Lock()
- defer dsb.mu.Unlock()
- var ret []Instrument
- for _, in := range dsb.insMap {
- ret = append(ret, *in)
- }
- return ret
- }
- func (dsb *DSBase) GetMarket() chan *Market {
- return dsb.chM
- }
- func debugDelay(prefix, insId string, ts int64) {
- // for debug delay
- now := time.Now()
- d := int64(float64(now.UnixNano())*1e-6) - ts
- if Debug && d > 1000 {
- //log.Println(prefix, "delay > 1000ms", insId, d, getTime(ts))
- }
- }
- func printDelay(prefix, insId string, ts int64) {
- // for debug delay
- now := time.Now()
- d := int64(float64(now.UnixNano())*1e-6) - ts
- log.Println(prefix, "delay ", d, insId, getTime(ts))
- }
- func getTime(ts int64) time.Time {
- if ts < 0 {
- return time.Now()
- }
- return time.Unix(ts/1000, (ts%1000)*1e6)
- }
|