Skip to content

Commit

Permalink
binance: define event types and add partial depth detection
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Feb 6, 2024
1 parent 97b922a commit fce6030
Showing 1 changed file with 47 additions and 20 deletions.
67 changes: 47 additions & 20 deletions pkg/exchange/binance/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

type EventType = string

const (
EventTypeKLine EventType = "kline"
EventTypeOutboundAccountPosition EventType = "outboundAccountPosition"
EventTypeOutboundAccountInfo EventType = "outboundAccountInfo"
EventTypeBalanceUpdate EventType = "balanceUpdate"
EventTypeExecutionReport EventType = "executionReport"
EventTypeDepthUpdate EventType = "depthUpdate"
EventTypeListenKeyExpired EventType = "listenKeyExpired"

// Our side defines the following event types since binance doesn't
// define the event name from the server messages.
//
EventTypeBookTicker EventType = "bookTicker"
EventTypePartialDepth EventType = "partialDepth"
)

type EventBase struct {
Event string `json:"e"` // event name
Time types.MillisecondTimestamp `json:"E"` // event time
Expand Down Expand Up @@ -306,65 +324,69 @@ func parseWebSocketEvent(message []byte) (interface{}, error) {
}

eventType := string(val.GetStringBytes("e"))
if eventType == "" && IsBookTicker(val) {
eventType = "bookTicker"
if eventType == "" {
if IsBookTicker(val) {
eventType = EventTypeBookTicker
} else if IsPartialDepth(val) {
eventType = EventTypePartialDepth
}
}

switch eventType {
case "kline":
case EventTypeKLine:
var event KLineEvent
err := json.Unmarshal([]byte(message), &event)
err := json.Unmarshal(message, &event)
return &event, err
case "bookTicker":
case EventTypeBookTicker:
var event BookTickerEvent
err := json.Unmarshal([]byte(message), &event)
event.Event = eventType
return &event, err

case "outboundAccountPosition":
case EventTypeOutboundAccountPosition:
var event OutboundAccountPositionEvent
err = json.Unmarshal([]byte(message), &event)
err = json.Unmarshal(message, &event)
return &event, err

case "outboundAccountInfo":
case EventTypeOutboundAccountInfo:
var event OutboundAccountInfoEvent
err = json.Unmarshal([]byte(message), &event)
err = json.Unmarshal(message, &event)
return &event, err

case "balanceUpdate":
case EventTypeBalanceUpdate:
var event BalanceUpdateEvent
err = json.Unmarshal([]byte(message), &event)
err = json.Unmarshal(message, &event)
return &event, err

case "executionReport":
case EventTypeExecutionReport:
var event ExecutionReportEvent
err = json.Unmarshal([]byte(message), &event)
err = json.Unmarshal(message, &event)
return &event, err

case "depthUpdate":
case EventTypeDepthUpdate:
return parseDepthEvent(val)

case "listenKeyExpired":
case EventTypeListenKeyExpired:
var event ListenKeyExpired
err = json.Unmarshal([]byte(message), &event)
err = json.Unmarshal(message, &event)
return &event, err

case "trade":
var event MarketTradeEvent
err = json.Unmarshal([]byte(message), &event)
err = json.Unmarshal(message, &event)
return &event, err

case "aggTrade":
var event AggTradeEvent
err = json.Unmarshal([]byte(message), &event)
err = json.Unmarshal(message, &event)
return &event, err
case "forceOrder":
var event ForceOrderEvent
err = json.Unmarshal([]byte(message), &event)
err = json.Unmarshal(message, &event)
return &event, err
}

// futures stream
// events for futures
switch eventType {

// futures market data stream
Expand Down Expand Up @@ -421,6 +443,11 @@ func IsBookTicker(val *fastjson.Value) bool {
val.Exists("B") && val.Exists("a") && val.Exists("A")
}

func IsPartialDepth(val *fastjson.Value) bool {
return val.Exists("lastUpdateId") &&
val.Exists("bids") && val.Exists("bids")
}

type DepthEntry struct {
PriceLevel fixedpoint.Value
Quantity fixedpoint.Value
Expand Down

0 comments on commit fce6030

Please sign in to comment.