Event.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package response
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. const (
  8. InstrumentId = 0
  9. Timestamp = 1
  10. Bids = 2
  11. Asks = 3
  12. MarketClose = 4
  13. DailyHigh = 5
  14. DailyLow = 6
  15. ValuationBid = 7
  16. ValuationAsk = 8
  17. LastTraded = 9
  18. )
  19. //var OrderType
  20. type PricePoint struct {
  21. Price, Quantity float64
  22. }
  23. func (pp PricePoint) String() string {
  24. return fmt.Sprint(pp.Quantity) + "@" + fmt.Sprint(pp.Price)
  25. }
  26. type OrderBookEvent struct {
  27. InstrumentId, Timestamp, MktClosePriceTimestamp int64
  28. MktClosePrice, DailyHighestTradedPrice, DailyLowestTradedPrice float64
  29. HasMarketClosePrice, HasDailyHighestTradedPrice, HasDailyLowestTradedPrice bool
  30. HasValuationBidPrice, HasValuationAskPrice, HasLastTradedPrice bool
  31. ValuationBidPrice, ValuationAskPrice, LastTradedPrice float64
  32. BidPrices, AskPrices []PricePoint
  33. }
  34. func NewOrderBookEvent(eventData string) *OrderBookEvent {
  35. dataList := strings.Split(eventData, "|")
  36. event := OrderBookEvent{}
  37. event.InstrumentId, _ = strconv.ParseInt(dataList[InstrumentId], 10, 64)
  38. event.Timestamp, _ = strconv.ParseInt(dataList[Timestamp], 16, 64)
  39. event.BidPrices = parsePrices(dataList[Bids])
  40. event.AskPrices = parsePrices(dataList[Asks])
  41. event.HasMarketClosePrice, event.MktClosePrice, event.MktClosePriceTimestamp = parseMarket(dataList[MarketClose])
  42. event.HasDailyHighestTradedPrice, event.DailyHighestTradedPrice = parseFloat(dataList[DailyHigh])
  43. event.HasDailyLowestTradedPrice, event.DailyLowestTradedPrice = parseFloat(dataList[DailyLow])
  44. event.HasValuationBidPrice, event.ValuationBidPrice = parseFloat(dataList[ValuationBid])
  45. event.HasValuationAskPrice, event.ValuationAskPrice = parseFloat(dataList[ValuationAsk])
  46. event.HasLastTradedPrice, event.LastTradedPrice = parseFloat(dataList[LastTraded])
  47. return &event
  48. }
  49. func (o *OrderBookEvent) GetIndex() int64 {
  50. return o.Timestamp
  51. }
  52. func (o *OrderBookEvent) Encode() ([]byte, error) {
  53. data := make([]string, 10)
  54. data[InstrumentId] = fmt.Sprint(o.InstrumentId)
  55. data[Timestamp] = fmt.Sprintf("%x", o.Timestamp)
  56. data[Bids] = joinPrice(o.BidPrices)
  57. data[Asks] = joinPrice(o.AskPrices)
  58. data[MarketClose] = joinMarketClose(o)
  59. if o.HasDailyHighestTradedPrice {
  60. data[DailyHigh] = fmt.Sprint(o.DailyHighestTradedPrice)
  61. }
  62. if o.HasDailyLowestTradedPrice {
  63. data[DailyLow] = fmt.Sprint(o.DailyLowestTradedPrice)
  64. }
  65. if o.HasValuationBidPrice {
  66. data[ValuationBid] = fmt.Sprint(o.ValuationBidPrice)
  67. }
  68. if o.HasValuationAskPrice {
  69. data[ValuationAsk] = fmt.Sprint(o.ValuationAskPrice)
  70. }
  71. if o.HasLastTradedPrice {
  72. data[LastTraded] = fmt.Sprint(o.LastTradedPrice)
  73. }
  74. s := "<ob2>" + strings.Join(data, "|") + "</ob2>"
  75. return []byte(s), nil
  76. }
  77. func joinPrice(pps []PricePoint) string {
  78. if len(pps) == 0 {
  79. return ""
  80. }
  81. data := make([]string, len(pps))
  82. for i := 0; i < len(data); i++ {
  83. data[i] = fmt.Sprint(pps[i])
  84. }
  85. return strings.Join(data, ";")
  86. }
  87. func joinMarketClose(o *OrderBookEvent) string {
  88. if o.HasMarketClosePrice {
  89. return fmt.Sprint(o.MktClosePrice) + ";" + fmt.Sprintf("%x", o.MktClosePriceTimestamp)
  90. }
  91. return ""
  92. }
  93. func (o *OrderBookEvent) GetType() int64 {
  94. return o.InstrumentId
  95. }
  96. // func (event *OrderBookEvent) ToTickGo() *base.TickGo {
  97. // symbolId, err := markinfo.BookIdToSymbolId(int(event.InstrumentId))
  98. // if err != nil {
  99. // return &base.TickGo{}
  100. // }
  101. // return event.TickGo(symbolId)
  102. // }
  103. // func (event *OrderBookEvent) TickGo(symbolId int) *base.TickGo {
  104. // if len(event.BidPrices) == 0 || len(event.AskPrices) == 0 {
  105. // return &base.TickGo{}
  106. // }
  107. // tick := &base.TickGo{}
  108. // tick.Symbol = int16(symbolId)
  109. // tick.Time = int32(event.Timestamp / 1000)
  110. // tick.Ms = int16(event.Timestamp % 1000)
  111. // tick.Bid = float32(event.BidPrices[0].Price)
  112. // tick.Ask = float32(event.AskPrices[0].Price)
  113. // tick.Bidv = int32(event.BidPrices[0].Quantity)
  114. // tick.Askv = int32(event.AskPrices[0].Quantity)
  115. // return tick
  116. // }
  117. func parsePrices(data string) []PricePoint {
  118. if data == "" {
  119. return make([]PricePoint, 0)
  120. }
  121. dataList := strings.Split(data, ";")
  122. PriceList := make([]PricePoint, len(dataList), len(dataList))
  123. index := 0
  124. for _, d := range dataList {
  125. dList := strings.Split(d, "@")
  126. if len(dList) > 1 {
  127. p, _ := strconv.ParseFloat(dList[0], 64)
  128. q, _ := strconv.ParseFloat(dList[1], 64)
  129. PriceList[index] = PricePoint{q, p}
  130. index++
  131. }
  132. }
  133. return PriceList[0:index]
  134. }
  135. func parseMarket(data string) (bool, float64, int64) {
  136. dataList := strings.Split(data, ";")
  137. if len(dataList) == 2 && len(dataList[0]) > 0 {
  138. p, errP := strconv.ParseFloat(dataList[0], 64)
  139. if errP != nil {
  140. return false, 0, 0
  141. }
  142. t, _ := strconv.ParseInt(dataList[1], 16, 64)
  143. return true, p, t
  144. }
  145. return false, 0, 0
  146. }
  147. func parseFloat(data string) (bool, float64) {
  148. p, err := strconv.ParseFloat(data, 64)
  149. return err == nil, p
  150. }