123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package response
- import (
- "fmt"
- "strconv"
- "strings"
- )
- const (
- InstrumentId = 0
- Timestamp = 1
- Bids = 2
- Asks = 3
- MarketClose = 4
- DailyHigh = 5
- DailyLow = 6
- ValuationBid = 7
- ValuationAsk = 8
- LastTraded = 9
- )
- //var OrderType
- type PricePoint struct {
- Price, Quantity float64
- }
- func (pp PricePoint) String() string {
- return fmt.Sprint(pp.Quantity) + "@" + fmt.Sprint(pp.Price)
- }
- type OrderBookEvent struct {
- InstrumentId, Timestamp, MktClosePriceTimestamp int64
- MktClosePrice, DailyHighestTradedPrice, DailyLowestTradedPrice float64
- HasMarketClosePrice, HasDailyHighestTradedPrice, HasDailyLowestTradedPrice bool
- HasValuationBidPrice, HasValuationAskPrice, HasLastTradedPrice bool
- ValuationBidPrice, ValuationAskPrice, LastTradedPrice float64
- BidPrices, AskPrices []PricePoint
- }
- func NewOrderBookEvent(eventData string) *OrderBookEvent {
- dataList := strings.Split(eventData, "|")
- event := OrderBookEvent{}
- event.InstrumentId, _ = strconv.ParseInt(dataList[InstrumentId], 10, 64)
- event.Timestamp, _ = strconv.ParseInt(dataList[Timestamp], 16, 64)
- event.BidPrices = parsePrices(dataList[Bids])
- event.AskPrices = parsePrices(dataList[Asks])
- event.HasMarketClosePrice, event.MktClosePrice, event.MktClosePriceTimestamp = parseMarket(dataList[MarketClose])
- event.HasDailyHighestTradedPrice, event.DailyHighestTradedPrice = parseFloat(dataList[DailyHigh])
- event.HasDailyLowestTradedPrice, event.DailyLowestTradedPrice = parseFloat(dataList[DailyLow])
- event.HasValuationBidPrice, event.ValuationBidPrice = parseFloat(dataList[ValuationBid])
- event.HasValuationAskPrice, event.ValuationAskPrice = parseFloat(dataList[ValuationAsk])
- event.HasLastTradedPrice, event.LastTradedPrice = parseFloat(dataList[LastTraded])
- return &event
- }
- func (o *OrderBookEvent) GetIndex() int64 {
- return o.Timestamp
- }
- func (o *OrderBookEvent) Encode() ([]byte, error) {
- data := make([]string, 10)
- data[InstrumentId] = fmt.Sprint(o.InstrumentId)
- data[Timestamp] = fmt.Sprintf("%x", o.Timestamp)
- data[Bids] = joinPrice(o.BidPrices)
- data[Asks] = joinPrice(o.AskPrices)
- data[MarketClose] = joinMarketClose(o)
- if o.HasDailyHighestTradedPrice {
- data[DailyHigh] = fmt.Sprint(o.DailyHighestTradedPrice)
- }
- if o.HasDailyLowestTradedPrice {
- data[DailyLow] = fmt.Sprint(o.DailyLowestTradedPrice)
- }
- if o.HasValuationBidPrice {
- data[ValuationBid] = fmt.Sprint(o.ValuationBidPrice)
- }
- if o.HasValuationAskPrice {
- data[ValuationAsk] = fmt.Sprint(o.ValuationAskPrice)
- }
- if o.HasLastTradedPrice {
- data[LastTraded] = fmt.Sprint(o.LastTradedPrice)
- }
- s := "<ob2>" + strings.Join(data, "|") + "</ob2>"
- return []byte(s), nil
- }
- func joinPrice(pps []PricePoint) string {
- if len(pps) == 0 {
- return ""
- }
- data := make([]string, len(pps))
- for i := 0; i < len(data); i++ {
- data[i] = fmt.Sprint(pps[i])
- }
- return strings.Join(data, ";")
- }
- func joinMarketClose(o *OrderBookEvent) string {
- if o.HasMarketClosePrice {
- return fmt.Sprint(o.MktClosePrice) + ";" + fmt.Sprintf("%x", o.MktClosePriceTimestamp)
- }
- return ""
- }
- func (o *OrderBookEvent) GetType() int64 {
- return o.InstrumentId
- }
- // func (event *OrderBookEvent) ToTickGo() *base.TickGo {
- // symbolId, err := markinfo.BookIdToSymbolId(int(event.InstrumentId))
- // if err != nil {
- // return &base.TickGo{}
- // }
- // return event.TickGo(symbolId)
- // }
- // func (event *OrderBookEvent) TickGo(symbolId int) *base.TickGo {
- // if len(event.BidPrices) == 0 || len(event.AskPrices) == 0 {
- // return &base.TickGo{}
- // }
- // tick := &base.TickGo{}
- // tick.Symbol = int16(symbolId)
- // tick.Time = int32(event.Timestamp / 1000)
- // tick.Ms = int16(event.Timestamp % 1000)
- // tick.Bid = float32(event.BidPrices[0].Price)
- // tick.Ask = float32(event.AskPrices[0].Price)
- // tick.Bidv = int32(event.BidPrices[0].Quantity)
- // tick.Askv = int32(event.AskPrices[0].Quantity)
- // return tick
- // }
- func parsePrices(data string) []PricePoint {
- if data == "" {
- return make([]PricePoint, 0)
- }
- dataList := strings.Split(data, ";")
- PriceList := make([]PricePoint, len(dataList), len(dataList))
- index := 0
- for _, d := range dataList {
- dList := strings.Split(d, "@")
- if len(dList) > 1 {
- p, _ := strconv.ParseFloat(dList[0], 64)
- q, _ := strconv.ParseFloat(dList[1], 64)
- PriceList[index] = PricePoint{q, p}
- index++
- }
- }
- return PriceList[0:index]
- }
- func parseMarket(data string) (bool, float64, int64) {
- dataList := strings.Split(data, ";")
- if len(dataList) == 2 && len(dataList[0]) > 0 {
- p, errP := strconv.ParseFloat(dataList[0], 64)
- if errP != nil {
- return false, 0, 0
- }
- t, _ := strconv.ParseInt(dataList[1], 16, 64)
- return true, p, t
- }
- return false, 0, 0
- }
- func parseFloat(data string) (bool, float64) {
- p, err := strconv.ParseFloat(data, 64)
- return err == nil, p
- }
|