package base //一个更加简单的计算K线的接口 //提供 //1. Peek //2. Align //3. Next import "io" import "unsafe" import "log" var _ = log.Println type CandleSimple struct { candle *Candle tick TickGo retTick TickGo nextTick TickGo num int peek int ret result symbol int point int period int maxComp int minPeriod int isfirst bool } func NewCandleSimple(symbolId int, period int, trans *TimezoneTrans) (*CandleSimple, error) { //最后点差部分要根据每个货币对的情况重新计算 //现在的点差 = (ask - bid) * 100 candle, err := NewCandle(period, 1, nil, 0) if err != nil { return nil, err } if trans == nil { trans , err = NewTimezoneTrans("UTC", 0, "UTC", 0, 0) if err != nil { return nil, err } } candle.SetTimezoneTrans(trans) calc := CandleSimple{} calc.symbol = symbolId calc.candle = candle calc.point = 1 calc.period = period calc.maxComp = 12 * 3600 calc.minPeriod = 60 calc.isfirst = true return &calc, nil } func (calc *CandleSimple) Close() error { calc.candle.Free() return nil } func (calc *CandleSimple) SetConf(key int, conf interface{}) (int, error) { if key == CANDLE_AUTOCOMPLETE_MAX { calc.maxComp = conf.(int) } return calc.candle.Set(key, conf) } func (calc *CandleSimple) SetMinPeriod(period int) { calc.minPeriod = period } func (calc *CandleSimple) PeriodSecond() int { return calc.period } func (calc *CandleSimple) NodataAlignTime() int { return calc.maxComp } func (calc *CandleSimple) Set(tick TickGo) int { calc.tick = tick calc.num = calc.candle.UpdateTick((*Tick)(unsafe.Pointer(&calc.tick))) if calc.num == 0 { return 0 } return calc.num - 1 } func (calc *CandleSimple) Next() (*OhlcGo, bool, error) { if calc.peek > 0 { calc.peek = 0 return calc.ret.data, calc.ret.isnew, calc.ret.err } if calc.num > 0 { calc.num-- calc.ret.isnew = true return calc.readNext() } else if calc.num == 0 { calc.ret.isnew = false calc.num-- return calc.readNext() } return nil, false, io.EOF } func (calc *CandleSimple) readNext() (*OhlcGo, bool, error) { ohlc := Ohlc{} calc.candle.Next(&ohlc) tmp := ohlc.ToGOStruct() calc.ret.data = &tmp calc.ret.err = nil return calc.ret.data, calc.ret.isnew, calc.ret.err } //只能向前查看一个数据 func (calc *CandleSimple) Peek() (*OhlcGo, bool, error) { if calc.peek == 0 || calc.ret.err == ErrNoData { calc.peek = 0 calc.ret.data, calc.ret.isnew, calc.ret.err = calc.Next() } calc.peek++ return calc.ret.data, calc.ret.isnew, calc.ret.err } func (calc *CandleSimple) LastTick() TickGo { return calc.tick }