candle_simple.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package base
  2. //一个更加简单的计算K线的接口
  3. //提供
  4. //1. Peek
  5. //2. Align
  6. //3. Next
  7. import "io"
  8. import "unsafe"
  9. import "log"
  10. var _ = log.Println
  11. type CandleSimple struct {
  12. candle *Candle
  13. tick TickGo
  14. retTick TickGo
  15. nextTick TickGo
  16. num int
  17. peek int
  18. ret result
  19. symbol int
  20. point int
  21. period int
  22. maxComp int
  23. minPeriod int
  24. isfirst bool
  25. }
  26. func NewCandleSimple(symbolId int, period int, trans *TimezoneTrans) (*CandleSimple, error) {
  27. //最后点差部分要根据每个货币对的情况重新计算
  28. //现在的点差 = (ask - bid) * 100
  29. candle, err := NewCandle(period, 1, nil, 0)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if trans == nil {
  34. trans , err = NewTimezoneTrans("UTC", 0, "UTC", 0, 0)
  35. if err != nil {
  36. return nil, err
  37. }
  38. }
  39. candle.SetTimezoneTrans(trans)
  40. calc := CandleSimple{}
  41. calc.symbol = symbolId
  42. calc.candle = candle
  43. calc.point = 1
  44. calc.period = period
  45. calc.maxComp = 12 * 3600
  46. calc.minPeriod = 60
  47. calc.isfirst = true
  48. return &calc, nil
  49. }
  50. func (calc *CandleSimple) Close() error {
  51. calc.candle.Free()
  52. return nil
  53. }
  54. func (calc *CandleSimple) SetConf(key int, conf interface{}) (int, error) {
  55. if key == CANDLE_AUTOCOMPLETE_MAX {
  56. calc.maxComp = conf.(int)
  57. }
  58. return calc.candle.Set(key, conf)
  59. }
  60. func (calc *CandleSimple) SetMinPeriod(period int) {
  61. calc.minPeriod = period
  62. }
  63. func (calc *CandleSimple) PeriodSecond() int {
  64. return calc.period
  65. }
  66. func (calc *CandleSimple) NodataAlignTime() int {
  67. return calc.maxComp
  68. }
  69. func (calc *CandleSimple) Set(tick TickGo) int {
  70. calc.tick = tick
  71. calc.num = calc.candle.UpdateTick((*Tick)(unsafe.Pointer(&calc.tick)))
  72. if calc.num == 0 {
  73. return 0
  74. }
  75. return calc.num - 1
  76. }
  77. func (calc *CandleSimple) Next() (*OhlcGo, bool, error) {
  78. if calc.peek > 0 {
  79. calc.peek = 0
  80. return calc.ret.data, calc.ret.isnew, calc.ret.err
  81. }
  82. if calc.num > 0 {
  83. calc.num--
  84. calc.ret.isnew = true
  85. return calc.readNext()
  86. } else if calc.num == 0 {
  87. calc.ret.isnew = false
  88. calc.num--
  89. return calc.readNext()
  90. }
  91. return nil, false, io.EOF
  92. }
  93. func (calc *CandleSimple) readNext() (*OhlcGo, bool, error) {
  94. ohlc := Ohlc{}
  95. calc.candle.Next(&ohlc)
  96. tmp := ohlc.ToGOStruct()
  97. calc.ret.data = &tmp
  98. calc.ret.err = nil
  99. return calc.ret.data, calc.ret.isnew, calc.ret.err
  100. }
  101. //只能向前查看一个数据
  102. func (calc *CandleSimple) Peek() (*OhlcGo, bool, error) {
  103. if calc.peek == 0 || calc.ret.err == ErrNoData {
  104. calc.peek = 0
  105. calc.ret.data, calc.ret.isnew, calc.ret.err = calc.Next()
  106. }
  107. calc.peek++
  108. return calc.ret.data, calc.ret.isnew, calc.ret.err
  109. }
  110. func (calc *CandleSimple) LastTick() TickGo {
  111. return calc.tick
  112. }