mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
binance: add futures parser
This commit is contained in:
parent
f78a7d37a2
commit
d691bfa106
|
@ -88,10 +88,10 @@ type ExecutionReportEvent struct {
|
|||
CurrentOrderStatus string `json:"X"`
|
||||
|
||||
OrderID int64 `json:"i"`
|
||||
Ignored int64 `json:"I"`
|
||||
Ignored int64 `json:"I"`
|
||||
|
||||
TradeID int64 `json:"t"`
|
||||
TransactionTime int64 `json:"T"`
|
||||
TransactionTime int64 `json:"T"`
|
||||
|
||||
LastExecutedQuantity string `json:"l"`
|
||||
LastExecutedPrice string `json:"L"`
|
||||
|
@ -264,6 +264,12 @@ func ParseEvent(message string) (interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
//res, err := json.MarshalIndent(message, "", " ")
|
||||
//if err != nil {
|
||||
// log.Fatal(err)
|
||||
//}
|
||||
//str := strings.ReplaceAll(string(res), "\\", "")
|
||||
//fmt.Println(str)
|
||||
eventType := string(val.GetStringBytes("e"))
|
||||
if eventType == "" && IsBookTicker(val) {
|
||||
eventType = "bookticker"
|
||||
|
@ -319,6 +325,18 @@ func ParseEvent(message string) (interface{}, error) {
|
|||
err := json.Unmarshal([]byte(message), &event)
|
||||
return &event, err
|
||||
|
||||
// Event: Balance and Position Update
|
||||
case "ACCOUNT_UPDATE":
|
||||
var event AccountUpdateEvent
|
||||
err := json.Unmarshal([]byte(message), &event)
|
||||
return &event, err
|
||||
|
||||
// Event: Order Update
|
||||
case "ACCOUNT_CONFIG_UPDATE":
|
||||
var event AccountConfigUpdateEvent
|
||||
err := json.Unmarshal([]byte(message), &event)
|
||||
return &event, err
|
||||
|
||||
default:
|
||||
id := val.GetInt("id")
|
||||
if id > 0 {
|
||||
|
@ -702,6 +720,31 @@ func (e *OrderTradeUpdateEvent) OrderFutures() (*types.Order, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
type AccountUpdate struct {
|
||||
EventReasonType string `json:"m"`
|
||||
Balances []*futures.Balance `json:"B,omitempty"`
|
||||
Positions []*futures.AccountPosition `json:"P,omitempty"`
|
||||
}
|
||||
|
||||
type AccountUpdateEvent struct {
|
||||
EventBase
|
||||
Transaction int64 `json:"T"`
|
||||
|
||||
AccountUpdate AccountUpdate `json:"a"`
|
||||
}
|
||||
|
||||
type AccountConfig struct {
|
||||
Symbol string `json:"s"`
|
||||
Leverage fixedpoint.Value `json:"l"`
|
||||
}
|
||||
|
||||
type AccountConfigUpdateEvent struct {
|
||||
EventBase
|
||||
Transaction int64 `json:"T"`
|
||||
|
||||
AccountConfig AccountConfig `json:"ac"`
|
||||
}
|
||||
|
||||
type EventBase struct {
|
||||
Event string `json:"e"` // event
|
||||
Time int64 `json:"E"`
|
||||
|
|
|
@ -212,3 +212,199 @@ func TestParseOrderUpdate(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.NotNil(t, orderUpdate)
|
||||
}
|
||||
|
||||
func TestFuturesResponseParsing(t *testing.T) {
|
||||
type testcase struct {
|
||||
input string
|
||||
}
|
||||
|
||||
var testcases = []testcase{
|
||||
{
|
||||
input: `{
|
||||
"e": "ORDER_TRADE_UPDATE",
|
||||
"T": 1639933384755,
|
||||
"E": 1639933384763,
|
||||
"o": {
|
||||
"s": "BTCUSDT",
|
||||
"c": "x-NSUYEBKMe60cf610-f5c7-49a4-9c1",
|
||||
"S": "SELL",
|
||||
"o": "MARKET",
|
||||
"f": "GTC",
|
||||
"q": "0.001",
|
||||
"p": "0",
|
||||
"ap": "0",
|
||||
"sp": "0",
|
||||
"x": "NEW",
|
||||
"X": "NEW",
|
||||
"i": 38541728873,
|
||||
"l": "0",
|
||||
"z": "0",
|
||||
"L": "0",
|
||||
"T": 1639933384755,
|
||||
"t": 0,
|
||||
"b": "0",
|
||||
"a": "0",
|
||||
"m": false,
|
||||
"R": false,
|
||||
"wt": "CONTRACT_PRICE",
|
||||
"ot": "MARKET",
|
||||
"ps": "BOTH",
|
||||
"cp": false,
|
||||
"rp": "0",
|
||||
"pP": false,
|
||||
"si": 0,
|
||||
"ss": 0
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
input: `{
|
||||
"e": "ACCOUNT_UPDATE",
|
||||
"T": 1639933384755,
|
||||
"E": 1639933384763,
|
||||
"a": {
|
||||
"B": [
|
||||
{
|
||||
"a": "USDT",
|
||||
"wb": "86.94966888",
|
||||
"cw": "86.94966888",
|
||||
"bc": "0"
|
||||
}
|
||||
],
|
||||
"P": [
|
||||
{
|
||||
"s": "BTCUSDT",
|
||||
"pa": "-0.001",
|
||||
"ep": "47202.40000",
|
||||
"cr": "7.78107001",
|
||||
"up": "-0.00233523",
|
||||
"mt": "cross",
|
||||
"iw": "0",
|
||||
"ps": "BOTH",
|
||||
"ma": "USDT"
|
||||
}
|
||||
],
|
||||
"m": "ORDER"
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
input: `{
|
||||
"e": "ORDER_TRADE_UPDATE",
|
||||
"T": 1639933384755,
|
||||
"E": 1639933384763,
|
||||
"o": {
|
||||
"s": "BTCUSDT",
|
||||
"c": "x-NSUYEBKMe60cf610-f5c7-49a4-9c1",
|
||||
"S": "SELL",
|
||||
"o": "MARKET",
|
||||
"f": "GTC",
|
||||
"q": "0.001",
|
||||
"p": "0",
|
||||
"ap": "47202.40000",
|
||||
"sp": "0",
|
||||
"x": "TRADE",
|
||||
"X": "FILLED",
|
||||
"i": 38541728873,
|
||||
"l": "0.001",
|
||||
"z": "0.001",
|
||||
"L": "47202.40",
|
||||
"n": "0.01888095",
|
||||
"N": "USDT",
|
||||
"T": 1639933384755,
|
||||
"t": 1741505949,
|
||||
"b": "0",
|
||||
"a": "0",
|
||||
"m": false,
|
||||
"R": false,
|
||||
"wt": "CONTRACT_PRICE",
|
||||
"ot": "MARKET",
|
||||
"ps": "BOTH",
|
||||
"cp": false,
|
||||
"rp": "0",
|
||||
"pP": false,
|
||||
"si": 0,
|
||||
"ss": 0
|
||||
}
|
||||
}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
payload := testcase.input
|
||||
payload = jsCommentTrimmer.ReplaceAllLiteralString(payload, "")
|
||||
event, err := ParseEvent(payload)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOrderFuturesUpdate(t *testing.T) {
|
||||
payload := `{
|
||||
"e": "ORDER_TRADE_UPDATE",
|
||||
"T": 1639933384755,
|
||||
"E": 1639933384763,
|
||||
"o": {
|
||||
"s": "BTCUSDT",
|
||||
"c": "x-NSUYEBKMe60cf610-f5c7-49a4-9c1",
|
||||
"S": "SELL",
|
||||
"o": "MARKET",
|
||||
"f": "GTC",
|
||||
"q": "0.001",
|
||||
"p": "0",
|
||||
"ap": "47202.40000",
|
||||
"sp": "0",
|
||||
"x": "TRADE",
|
||||
"X": "FILLED",
|
||||
"i": 38541728873,
|
||||
"l": "0.001",
|
||||
"z": "0.001",
|
||||
"L": "47202.40",
|
||||
"n": "0.01888095",
|
||||
"N": "USDT",
|
||||
"T": 1639933384755,
|
||||
"t": 1741505949,
|
||||
"b": "0",
|
||||
"a": "0",
|
||||
"m": false,
|
||||
"R": false,
|
||||
"wt": "CONTRACT_PRICE",
|
||||
"ot": "MARKET",
|
||||
"ps": "BOTH",
|
||||
"cp": false,
|
||||
"rp": "0",
|
||||
"pP": false,
|
||||
"si": 0,
|
||||
"ss": 0
|
||||
}
|
||||
}`
|
||||
|
||||
payload = jsCommentTrimmer.ReplaceAllLiteralString(payload, "")
|
||||
|
||||
event, err := ParseEvent(payload)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, event)
|
||||
|
||||
orderTradeEvent, ok := event.(*OrderTradeUpdateEvent)
|
||||
assert.True(t, ok)
|
||||
assert.NotNil(t, orderTradeEvent)
|
||||
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.Symbol, "BTCUSDT")
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.Side, "SELL")
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.ClientOrderID, "x-NSUYEBKMe60cf610-f5c7-49a4-9c1")
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.OrderType, "MARKET")
|
||||
assert.Equal(t, orderTradeEvent.Time, int64(1639933384763))
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.OrderTradeTime, int64(1639933384755))
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.OriginalQuantity, "0.001")
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.OrderLastFilledQuantity, "0.001")
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.OrderFilledAccumulatedQuantity, "0.001")
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.CurrentExecutionType, "TRADE")
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.CurrentOrderStatus, "FILLED")
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.LastFilledPrice, "47202.40")
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.OrderId, int64(38541728873))
|
||||
assert.Equal(t, orderTradeEvent.OrderTrade.TradeId, int64(1741505949))
|
||||
|
||||
orderUpdate, err := orderTradeEvent.OrderFutures()
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, orderUpdate)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user