2020-07-11 05:02:53 +00:00
|
|
|
package binance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-16 04:28:15 +00:00
|
|
|
"time"
|
|
|
|
|
2021-12-23 05:15:27 +00:00
|
|
|
"github.com/adshao/go-binance/v2/futures"
|
|
|
|
|
2021-01-11 05:36:49 +00:00
|
|
|
"github.com/adshao/go-binance/v2"
|
2020-09-16 04:28:15 +00:00
|
|
|
"github.com/valyala/fastjson"
|
|
|
|
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-07-11 05:02:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
executionReport
|
|
|
|
|
|
|
|
{
|
2020-08-06 05:18:41 +00:00
|
|
|
"e": "executionReport", // Event type
|
|
|
|
"E": 1499405658658, // Event time
|
2020-07-11 05:02:53 +00:00
|
|
|
"s": "ETHBTC", // Symbol
|
|
|
|
"c": "mUvoqJxFIILMdfAW5iGSOW", // Client order ID
|
|
|
|
"S": "BUY", // Side
|
|
|
|
"o": "LIMIT", // Order type
|
|
|
|
"f": "GTC", // Time in force
|
|
|
|
"q": "1.00000000", // Order quantity
|
|
|
|
"p": "0.10264410", // Order price
|
|
|
|
"P": "0.00000000", // Stop price
|
|
|
|
"F": "0.00000000", // Iceberg quantity
|
|
|
|
"g": -1, // OrderListId
|
|
|
|
"C": null, // Original client order ID; This is the ID of the order being canceled
|
|
|
|
"x": "NEW", // Current execution type
|
|
|
|
"X": "NEW", // Current order status
|
|
|
|
"r": "NONE", // Order reject reason; will be an error code.
|
|
|
|
"i": 4293153, // Order ID
|
|
|
|
"l": "0.00000000", // Last executed quantity
|
|
|
|
"z": "0.00000000", // Cumulative filled quantity
|
|
|
|
"L": "0.00000000", // Last executed price
|
|
|
|
"n": "0", // Commission amount
|
|
|
|
"N": null, // Commission asset
|
|
|
|
"T": 1499405658657, // Transaction time
|
|
|
|
"t": -1, // Trade ID
|
|
|
|
"I": 8641984, // Ignore
|
|
|
|
"w": true, // Is the order on the book?
|
|
|
|
"m": false, // Is this trade the maker side?
|
|
|
|
"M": false, // Ignore
|
|
|
|
"O": 1499405658657, // Order creation time
|
|
|
|
"Z": "0.00000000", // Cumulative quote asset transacted quantity
|
2021-05-12 10:45:16 +00:00
|
|
|
"Y": "0.00000000", // Last quote asset transacted quantity (i.e. lastPrice * lastQty)
|
2022-05-30 07:55:49 +00:00
|
|
|
"Q": "0.00000000" // Quote Order Quantity
|
2020-07-11 05:02:53 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
type ExecutionReportEvent struct {
|
2020-07-11 05:08:50 +00:00
|
|
|
EventBase
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2021-05-25 13:36:14 +00:00
|
|
|
Symbol string `json:"s"`
|
|
|
|
Side string `json:"S"`
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2021-05-12 10:45:16 +00:00
|
|
|
ClientOrderID string `json:"c"`
|
|
|
|
OriginalClientOrderID string `json:"C"`
|
|
|
|
|
|
|
|
OrderType string `json:"o"`
|
2021-05-25 13:36:14 +00:00
|
|
|
OrderCreationTime int64 `json:"O"`
|
2021-05-12 10:45:16 +00:00
|
|
|
|
2022-01-10 17:41:33 +00:00
|
|
|
TimeInForce string `json:"f"`
|
|
|
|
IcebergQuantity fixedpoint.Value `json:"F"`
|
2021-05-12 10:45:16 +00:00
|
|
|
|
2022-01-10 17:41:33 +00:00
|
|
|
OrderQuantity fixedpoint.Value `json:"q"`
|
|
|
|
QuoteOrderQuantity fixedpoint.Value `json:"Q"`
|
2021-05-12 10:45:16 +00:00
|
|
|
|
2022-01-10 17:41:33 +00:00
|
|
|
OrderPrice fixedpoint.Value `json:"p"`
|
|
|
|
StopPrice fixedpoint.Value `json:"P"`
|
2020-07-11 05:02:53 +00:00
|
|
|
|
|
|
|
IsOnBook bool `json:"w"`
|
2021-05-12 10:45:16 +00:00
|
|
|
|
2021-05-25 13:36:14 +00:00
|
|
|
IsMaker bool `json:"m"`
|
|
|
|
Ignore bool `json:"M"`
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2022-01-10 17:41:33 +00:00
|
|
|
CommissionAmount fixedpoint.Value `json:"n"`
|
|
|
|
CommissionAsset string `json:"N"`
|
2020-07-11 05:02:53 +00:00
|
|
|
|
|
|
|
CurrentExecutionType string `json:"x"`
|
|
|
|
CurrentOrderStatus string `json:"X"`
|
|
|
|
|
2020-10-29 13:09:35 +00:00
|
|
|
OrderID int64 `json:"i"`
|
2021-12-27 22:26:27 +00:00
|
|
|
Ignored int64 `json:"I"`
|
2020-07-11 05:02:53 +00:00
|
|
|
|
|
|
|
TradeID int64 `json:"t"`
|
2021-12-27 22:26:27 +00:00
|
|
|
TransactionTime int64 `json:"T"`
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2022-01-10 17:41:33 +00:00
|
|
|
LastExecutedQuantity fixedpoint.Value `json:"l"`
|
|
|
|
LastExecutedPrice fixedpoint.Value `json:"L"`
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2022-01-10 17:41:33 +00:00
|
|
|
CumulativeFilledQuantity fixedpoint.Value `json:"z"`
|
|
|
|
CumulativeQuoteAssetTransactedQuantity fixedpoint.Value `json:"Z"`
|
2021-05-12 10:45:16 +00:00
|
|
|
|
2022-01-10 17:41:33 +00:00
|
|
|
LastQuoteAssetTransactedQuantity fixedpoint.Value `json:"Y"`
|
2020-10-25 10:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecutionReportEvent) Order() (*types.Order, error) {
|
|
|
|
switch e.CurrentExecutionType {
|
|
|
|
case "NEW", "CANCELED", "REJECTED", "EXPIRED":
|
|
|
|
case "REPLACED":
|
2021-05-12 16:41:23 +00:00
|
|
|
case "TRADE": // For Order FILLED status. And the order has been completed.
|
2020-10-25 10:56:07 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("execution report type is not for order")
|
|
|
|
}
|
|
|
|
|
|
|
|
orderCreationTime := time.Unix(0, e.OrderCreationTime*int64(time.Millisecond))
|
|
|
|
return &types.Order{
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
2022-05-12 10:40:48 +00:00
|
|
|
ClientOrderID: e.ClientOrderID,
|
|
|
|
Symbol: e.Symbol,
|
|
|
|
Side: toGlobalSideType(binance.SideType(e.Side)),
|
|
|
|
Type: toGlobalOrderType(binance.OrderType(e.OrderType)),
|
|
|
|
Quantity: e.OrderQuantity,
|
|
|
|
Price: e.OrderPrice,
|
|
|
|
StopPrice: e.StopPrice,
|
|
|
|
TimeInForce: types.TimeInForce(e.TimeInForce),
|
|
|
|
ReduceOnly: false,
|
|
|
|
ClosePosition: false,
|
2020-10-25 10:56:07 +00:00
|
|
|
},
|
2022-03-18 08:17:04 +00:00
|
|
|
Exchange: types.ExchangeBinance,
|
2022-03-06 10:32:33 +00:00
|
|
|
IsWorking: e.IsOnBook,
|
2020-10-29 13:09:35 +00:00
|
|
|
OrderID: uint64(e.OrderID),
|
2020-10-25 10:56:07 +00:00
|
|
|
Status: toGlobalOrderStatus(binance.OrderStatusType(e.CurrentOrderStatus)),
|
2022-02-03 04:55:25 +00:00
|
|
|
ExecutedQuantity: e.CumulativeFilledQuantity,
|
2021-05-19 17:32:26 +00:00
|
|
|
CreationTime: types.Time(orderCreationTime),
|
2022-03-06 10:32:33 +00:00
|
|
|
UpdateTime: types.Time(orderCreationTime),
|
2020-10-25 10:56:07 +00:00
|
|
|
}, nil
|
2020-07-11 05:02:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 08:07:09 +00:00
|
|
|
func (e *ExecutionReportEvent) Trade() (*types.Trade, error) {
|
2020-07-11 05:02:53 +00:00
|
|
|
if e.CurrentExecutionType != "TRADE" {
|
|
|
|
return nil, errors.New("execution report is not a trade")
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:56:07 +00:00
|
|
|
tt := time.Unix(0, e.TransactionTime*int64(time.Millisecond))
|
2020-07-11 08:07:09 +00:00
|
|
|
return &types.Trade{
|
2021-12-23 05:15:27 +00:00
|
|
|
ID: uint64(e.TradeID),
|
2021-05-16 09:02:23 +00:00
|
|
|
Exchange: types.ExchangeBinance,
|
2020-09-16 04:28:15 +00:00
|
|
|
Symbol: e.Symbol,
|
2020-11-17 06:13:37 +00:00
|
|
|
OrderID: uint64(e.OrderID),
|
2020-10-25 10:56:07 +00:00
|
|
|
Side: toGlobalSideType(binance.SideType(e.Side)),
|
2022-02-03 04:55:25 +00:00
|
|
|
Price: e.LastExecutedPrice,
|
|
|
|
Quantity: e.LastExecutedQuantity,
|
|
|
|
QuoteQuantity: e.LastQuoteAssetTransactedQuantity,
|
2020-09-16 04:28:15 +00:00
|
|
|
IsBuyer: e.Side == "BUY",
|
|
|
|
IsMaker: e.IsMaker,
|
2021-05-19 17:32:26 +00:00
|
|
|
Time: types.Time(tt),
|
2022-02-03 04:55:25 +00:00
|
|
|
Fee: e.CommissionAmount,
|
2020-09-16 04:28:15 +00:00
|
|
|
FeeCurrency: e.CommissionAsset,
|
2020-07-11 05:02:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
balanceUpdate
|
|
|
|
|
|
|
|
{
|
|
|
|
"e": "balanceUpdate", //KLineEvent Type
|
|
|
|
"E": 1573200697110, //KLineEvent Time
|
|
|
|
"a": "BTC", //Asset
|
|
|
|
"d": "100.00000000", //Balance Delta
|
|
|
|
"T": 1573200697068 //Clear Time
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
type BalanceUpdateEvent struct {
|
2020-07-11 05:08:50 +00:00
|
|
|
EventBase
|
2020-07-11 05:02:53 +00:00
|
|
|
|
|
|
|
Asset string `json:"a"`
|
|
|
|
Delta string `json:"d"`
|
|
|
|
ClearTime int64 `json:"T"`
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
outboundAccountInfo
|
|
|
|
|
|
|
|
{
|
|
|
|
"e": "outboundAccountInfo", // KLineEvent type
|
|
|
|
"E": 1499405658849, // KLineEvent time
|
|
|
|
"m": 0, // Maker commission rate (bips)
|
|
|
|
"t": 0, // Taker commission rate (bips)
|
|
|
|
"b": 0, // Buyer commission rate (bips)
|
|
|
|
"s": 0, // Seller commission rate (bips)
|
|
|
|
"T": true, // Can trade?
|
|
|
|
"W": true, // Can withdraw?
|
|
|
|
"D": true, // Can deposit?
|
|
|
|
"u": 1499405658848, // Time of last account update
|
2021-05-23 05:44:08 +00:00
|
|
|
"B": [ // AccountBalances array
|
2020-07-11 05:02:53 +00:00
|
|
|
{
|
|
|
|
"a": "LTC", // Asset
|
|
|
|
"f": "17366.18538083", // Free amount
|
|
|
|
"l": "0.00000000" // Locked amount
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a": "BTC",
|
|
|
|
"f": "10537.85314051",
|
|
|
|
"l": "2.19464093"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a": "ETH",
|
|
|
|
"f": "17902.35190619",
|
|
|
|
"l": "0.00000000"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a": "BNC",
|
|
|
|
"f": "1114503.29769312",
|
|
|
|
"l": "0.00000000"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"a": "NEO",
|
|
|
|
"f": "0.00000000",
|
|
|
|
"l": "0.00000000"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"P": [ // Account Permissions
|
|
|
|
"SPOT"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
type Balance struct {
|
2021-05-30 15:32:01 +00:00
|
|
|
Asset string `json:"a"`
|
|
|
|
Free fixedpoint.Value `json:"f"`
|
|
|
|
Locked fixedpoint.Value `json:"l"`
|
2020-07-11 05:02:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 09:48:30 +00:00
|
|
|
type OutboundAccountPositionEvent struct {
|
|
|
|
EventBase
|
|
|
|
|
|
|
|
LastAccountUpdateTime int `json:"u"`
|
|
|
|
Balances []Balance `json:"B,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:02:53 +00:00
|
|
|
type OutboundAccountInfoEvent struct {
|
2020-07-11 05:08:50 +00:00
|
|
|
EventBase
|
2020-07-11 05:02:53 +00:00
|
|
|
|
|
|
|
MakerCommissionRate int `json:"m"`
|
|
|
|
TakerCommissionRate int `json:"t"`
|
|
|
|
BuyerCommissionRate int `json:"b"`
|
|
|
|
SellerCommissionRate int `json:"s"`
|
|
|
|
|
|
|
|
CanTrade bool `json:"T"`
|
|
|
|
CanWithdraw bool `json:"W"`
|
|
|
|
CanDeposit bool `json:"D"`
|
|
|
|
|
|
|
|
LastAccountUpdateTime int `json:"u"`
|
|
|
|
|
|
|
|
Balances []Balance `json:"B,omitempty"`
|
|
|
|
Permissions []string `json:"P,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResultEvent struct {
|
|
|
|
Result interface{} `json:"result,omitempty"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
}
|
|
|
|
|
2022-01-01 17:54:47 +00:00
|
|
|
func parseWebSocketEvent(message []byte) (interface{}, error) {
|
|
|
|
val, err := fastjson.ParseBytes(message)
|
2021-05-12 10:45:16 +00:00
|
|
|
|
2020-07-11 05:02:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-10 17:41:33 +00:00
|
|
|
// res, err := json.MarshalIndent(message, "", " ")
|
|
|
|
// if err != nil {
|
2021-12-27 22:26:27 +00:00
|
|
|
// log.Fatal(err)
|
2022-01-10 17:41:33 +00:00
|
|
|
// }
|
|
|
|
// str := strings.ReplaceAll(string(res), "\\", "")
|
|
|
|
// fmt.Println(str)
|
2020-07-11 05:02:53 +00:00
|
|
|
eventType := string(val.GetStringBytes("e"))
|
2021-12-22 13:01:11 +00:00
|
|
|
if eventType == "" && IsBookTicker(val) {
|
2022-05-12 10:40:48 +00:00
|
|
|
eventType = "bookTicker"
|
2021-12-22 13:01:11 +00:00
|
|
|
}
|
2020-07-11 05:02:53 +00:00
|
|
|
|
|
|
|
switch eventType {
|
|
|
|
case "kline":
|
2020-07-11 05:08:50 +00:00
|
|
|
var event KLineEvent
|
2020-07-11 05:02:53 +00:00
|
|
|
err := json.Unmarshal([]byte(message), &event)
|
|
|
|
return &event, err
|
2022-05-12 10:40:48 +00:00
|
|
|
case "bookTicker":
|
2021-12-22 13:01:11 +00:00
|
|
|
var event BookTickerEvent
|
|
|
|
err := json.Unmarshal([]byte(message), &event)
|
|
|
|
event.Event = eventType
|
|
|
|
return &event, err
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2020-12-21 09:48:30 +00:00
|
|
|
case "outboundAccountPosition":
|
|
|
|
var event OutboundAccountPositionEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2020-12-21 09:48:30 +00:00
|
|
|
return &event, err
|
|
|
|
|
|
|
|
case "outboundAccountInfo":
|
2020-07-11 05:02:53 +00:00
|
|
|
var event OutboundAccountInfoEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2020-07-11 05:02:53 +00:00
|
|
|
return &event, err
|
|
|
|
|
|
|
|
case "balanceUpdate":
|
|
|
|
var event BalanceUpdateEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2020-07-11 05:02:53 +00:00
|
|
|
return &event, err
|
|
|
|
|
|
|
|
case "executionReport":
|
|
|
|
var event ExecutionReportEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2020-07-11 05:02:53 +00:00
|
|
|
return &event, err
|
|
|
|
|
2020-10-03 12:09:22 +00:00
|
|
|
case "depthUpdate":
|
|
|
|
return parseDepthEvent(val)
|
2021-12-12 07:39:06 +00:00
|
|
|
|
2021-11-15 17:24:36 +00:00
|
|
|
case "markPriceUpdate":
|
|
|
|
var event MarkPriceUpdateEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
|
|
|
return &event, err
|
|
|
|
|
|
|
|
case "listenKeyExpired":
|
|
|
|
var event ListenKeyExpired
|
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2021-11-15 17:24:36 +00:00
|
|
|
return &event, err
|
2021-12-12 07:39:06 +00:00
|
|
|
|
|
|
|
// Binance futures data --------------
|
|
|
|
case "continuousKline":
|
|
|
|
var event ContinuousKLineEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2021-12-12 07:39:06 +00:00
|
|
|
return &event, err
|
|
|
|
|
|
|
|
case "ORDER_TRADE_UPDATE":
|
|
|
|
var event OrderTradeUpdateEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2021-12-12 07:39:06 +00:00
|
|
|
return &event, err
|
|
|
|
|
2021-12-27 22:26:27 +00:00
|
|
|
// Event: Balance and Position Update
|
|
|
|
case "ACCOUNT_UPDATE":
|
|
|
|
var event AccountUpdateEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2021-12-27 22:26:27 +00:00
|
|
|
return &event, err
|
|
|
|
|
|
|
|
// Event: Order Update
|
|
|
|
case "ACCOUNT_CONFIG_UPDATE":
|
|
|
|
var event AccountConfigUpdateEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2021-12-27 22:26:27 +00:00
|
|
|
return &event, err
|
|
|
|
|
2022-03-18 08:17:04 +00:00
|
|
|
case "trade":
|
|
|
|
var event MarketTradeEvent
|
2022-09-11 06:06:55 +00:00
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
2022-03-18 08:17:04 +00:00
|
|
|
return &event, err
|
|
|
|
|
2022-10-17 08:01:46 +00:00
|
|
|
case "aggTrade":
|
|
|
|
var event AggTradeEvent
|
|
|
|
err = json.Unmarshal([]byte(message), &event)
|
|
|
|
return &event, err
|
|
|
|
|
2020-07-11 05:02:53 +00:00
|
|
|
default:
|
|
|
|
id := val.GetInt("id")
|
|
|
|
if id > 0 {
|
|
|
|
return &ResultEvent{ID: id}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unsupported message: %s", message)
|
|
|
|
}
|
2020-07-11 05:08:50 +00:00
|
|
|
|
2021-12-22 13:01:11 +00:00
|
|
|
// IsBookTicker document ref :https://binance-docs.github.io/apidocs/spot/en/#individual-symbol-book-ticker-streams
|
2022-01-10 17:41:33 +00:00
|
|
|
// use key recognition because there's no identify in the content.
|
2021-12-22 13:01:11 +00:00
|
|
|
func IsBookTicker(val *fastjson.Value) bool {
|
|
|
|
return !val.Exists("e") && val.Exists("u") &&
|
|
|
|
val.Exists("s") && val.Exists("b") &&
|
|
|
|
val.Exists("B") && val.Exists("a") && val.Exists("A")
|
|
|
|
}
|
|
|
|
|
2020-10-03 12:09:22 +00:00
|
|
|
type DepthEntry struct {
|
2021-06-28 05:05:17 +00:00
|
|
|
PriceLevel fixedpoint.Value
|
|
|
|
Quantity fixedpoint.Value
|
2020-10-03 12:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DepthEvent struct {
|
|
|
|
EventBase
|
|
|
|
|
2020-10-09 05:21:42 +00:00
|
|
|
Symbol string `json:"s"`
|
|
|
|
FirstUpdateID int64 `json:"U"`
|
|
|
|
FinalUpdateID int64 `json:"u"`
|
2020-10-03 12:09:22 +00:00
|
|
|
|
2021-06-28 05:05:17 +00:00
|
|
|
Bids types.PriceVolumeSlice `json:"b"`
|
|
|
|
Asks types.PriceVolumeSlice `json:"a"`
|
2020-10-03 12:09:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 13:36:14 +00:00
|
|
|
func (e *DepthEvent) String() (o string) {
|
|
|
|
o += fmt.Sprintf("Depth %s bid/ask = ", e.Symbol)
|
|
|
|
|
|
|
|
if len(e.Bids) == 0 {
|
|
|
|
o += "empty"
|
|
|
|
} else {
|
2021-06-28 05:05:17 +00:00
|
|
|
o += e.Bids[0].Price.String()
|
2021-05-25 13:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o += "/"
|
|
|
|
|
|
|
|
if len(e.Asks) == 0 {
|
|
|
|
o += "empty"
|
|
|
|
} else {
|
2021-06-28 05:05:17 +00:00
|
|
|
o += e.Asks[0].Price.String()
|
2021-05-25 13:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o += fmt.Sprintf(" %d ~ %d", e.FirstUpdateID, e.FinalUpdateID)
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2021-05-22 03:36:58 +00:00
|
|
|
func (e *DepthEvent) OrderBook() (book types.SliceOrderBook, err error) {
|
2020-10-05 06:16:55 +00:00
|
|
|
book.Symbol = e.Symbol
|
|
|
|
|
2021-06-28 02:23:47 +00:00
|
|
|
// already in descending order
|
2021-06-28 05:05:17 +00:00
|
|
|
book.Bids = e.Bids
|
|
|
|
book.Asks = e.Asks
|
2021-06-28 02:26:45 +00:00
|
|
|
return book, err
|
2020-10-05 06:16:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 05:05:17 +00:00
|
|
|
func parseDepthEntry(val *fastjson.Value) (*types.PriceVolume, error) {
|
2020-10-03 12:09:22 +00:00
|
|
|
arr, err := val.Array()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(arr) < 2 {
|
|
|
|
return nil, errors.New("incorrect depth entry element length")
|
|
|
|
}
|
|
|
|
|
2021-06-28 05:05:17 +00:00
|
|
|
price, err := fixedpoint.NewFromString(string(arr[0].GetStringBytes()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
quantity, err := fixedpoint.NewFromString(string(arr[1].GetStringBytes()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.PriceVolume{
|
|
|
|
Price: price,
|
|
|
|
Volume: quantity,
|
2020-10-03 12:09:22 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDepthEvent(val *fastjson.Value) (*DepthEvent, error) {
|
|
|
|
var err error
|
|
|
|
var depth = &DepthEvent{
|
|
|
|
EventBase: EventBase{
|
|
|
|
Event: string(val.GetStringBytes("e")),
|
|
|
|
Time: val.GetInt64("E"),
|
|
|
|
},
|
2020-10-09 05:21:42 +00:00
|
|
|
Symbol: string(val.GetStringBytes("s")),
|
2020-10-03 12:09:22 +00:00
|
|
|
FirstUpdateID: val.GetInt64("U"),
|
|
|
|
FinalUpdateID: val.GetInt64("u"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ev := range val.GetArray("b") {
|
|
|
|
entry, err2 := parseDepthEntry(ev)
|
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
depth.Bids = append(depth.Bids, *entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ev := range val.GetArray("a") {
|
|
|
|
entry, err2 := parseDepthEntry(ev)
|
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
depth.Asks = append(depth.Asks, *entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return depth, err
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:17:04 +00:00
|
|
|
type MarketTradeEvent struct {
|
|
|
|
EventBase
|
|
|
|
Symbol string `json:"s"`
|
|
|
|
Quantity fixedpoint.Value `json:"q"`
|
|
|
|
Price fixedpoint.Value `json:"p"`
|
|
|
|
|
|
|
|
BuyerOrderId int64 `json:"b"`
|
|
|
|
SellerOrderId int64 `json:"a"`
|
|
|
|
|
|
|
|
OrderTradeTime int64 `json:"T"`
|
|
|
|
TradeId int64 `json:"t"`
|
|
|
|
|
|
|
|
IsMaker bool `json:"m"`
|
2022-03-18 09:49:59 +00:00
|
|
|
Dummy bool `json:"M"`
|
2022-03-18 08:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
market trade
|
|
|
|
|
|
|
|
{
|
|
|
|
"e": "trade", // Event type
|
|
|
|
"E": 123456789, // Event time
|
|
|
|
"s": "BNBBTC", // Symbol
|
|
|
|
"t": 12345, // Trade ID
|
|
|
|
"p": "0.001", // Price
|
|
|
|
"q": "100", // Quantity
|
|
|
|
"b": 88, // Buyer order ID
|
|
|
|
"a": 50, // Seller order ID
|
|
|
|
"T": 123456785, // Trade time
|
|
|
|
"m": true, // Is the buyer the market maker?
|
|
|
|
"M": true // Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (e *MarketTradeEvent) Trade() types.Trade {
|
|
|
|
tt := time.Unix(0, e.OrderTradeTime*int64(time.Millisecond))
|
2022-03-18 09:49:59 +00:00
|
|
|
var orderId int64
|
|
|
|
var side types.SideType
|
|
|
|
var isBuyer bool
|
|
|
|
if e.IsMaker {
|
|
|
|
orderId = e.SellerOrderId // seller is taker
|
|
|
|
side = types.SideTypeSell
|
|
|
|
isBuyer = false
|
|
|
|
} else {
|
|
|
|
orderId = e.BuyerOrderId // buyer is taker
|
|
|
|
side = types.SideTypeBuy
|
|
|
|
isBuyer = true
|
|
|
|
}
|
2022-03-18 08:17:04 +00:00
|
|
|
return types.Trade{
|
|
|
|
ID: uint64(e.TradeId),
|
|
|
|
Exchange: types.ExchangeBinance,
|
|
|
|
Symbol: e.Symbol,
|
2022-03-18 09:49:59 +00:00
|
|
|
OrderID: uint64(orderId),
|
|
|
|
Side: side,
|
2022-03-18 08:17:04 +00:00
|
|
|
Price: e.Price,
|
|
|
|
Quantity: e.Quantity,
|
2022-10-20 08:43:17 +00:00
|
|
|
QuoteQuantity: e.Quantity.Mul(e.Price),
|
2022-03-18 09:49:59 +00:00
|
|
|
IsBuyer: isBuyer,
|
2022-03-18 08:17:04 +00:00
|
|
|
IsMaker: e.IsMaker,
|
|
|
|
Time: types.Time(tt),
|
|
|
|
Fee: fixedpoint.Zero,
|
|
|
|
FeeCurrency: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 08:01:46 +00:00
|
|
|
type AggTradeEvent struct {
|
|
|
|
EventBase
|
|
|
|
Symbol string `json:"s"`
|
|
|
|
Quantity fixedpoint.Value `json:"q"`
|
|
|
|
Price fixedpoint.Value `json:"p"`
|
|
|
|
FirstTradeId int64 `json:"f"`
|
|
|
|
LastTradeId int64 `json:"l"`
|
|
|
|
OrderTradeTime int64 `json:"T"`
|
|
|
|
IsMaker bool `json:"m"`
|
|
|
|
Dummy bool `json:"M"`
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
aggregate trade
|
|
|
|
{
|
|
|
|
"e": "aggTrade", // Event type
|
|
|
|
"E": 123456789, // Event time
|
|
|
|
"s": "BNBBTC", // Symbol
|
|
|
|
"a": 12345, // Aggregate trade ID
|
|
|
|
"p": "0.001", // Price
|
|
|
|
"q": "100", // Quantity
|
|
|
|
"f": 100, // First trade ID
|
|
|
|
"l": 105, // Last trade ID
|
|
|
|
"T": 123456785, // Trade time
|
|
|
|
"m": true, // Is the buyer the market maker?
|
|
|
|
"M": true // Ignore
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (e *AggTradeEvent) Trade() types.Trade {
|
|
|
|
tt := time.Unix(0, e.OrderTradeTime*int64(time.Millisecond))
|
|
|
|
var side types.SideType
|
|
|
|
var isBuyer bool
|
|
|
|
if e.IsMaker {
|
|
|
|
side = types.SideTypeSell
|
|
|
|
isBuyer = false
|
|
|
|
} else {
|
|
|
|
side = types.SideTypeBuy
|
|
|
|
isBuyer = true
|
|
|
|
}
|
|
|
|
return types.Trade{
|
|
|
|
ID: uint64(e.LastTradeId),
|
|
|
|
Exchange: types.ExchangeBinance,
|
|
|
|
Symbol: e.Symbol,
|
|
|
|
OrderID: 0,
|
|
|
|
Side: side,
|
|
|
|
Price: e.Price,
|
|
|
|
Quantity: e.Quantity,
|
2022-10-20 08:43:17 +00:00
|
|
|
QuoteQuantity: e.Quantity.Mul(e.Price),
|
2022-10-17 08:01:46 +00:00
|
|
|
IsBuyer: isBuyer,
|
|
|
|
IsMaker: e.IsMaker,
|
|
|
|
Time: types.Time(tt),
|
|
|
|
Fee: fixedpoint.Zero,
|
|
|
|
FeeCurrency: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:28:15 +00:00
|
|
|
type KLine struct {
|
|
|
|
StartTime int64 `json:"t"`
|
|
|
|
EndTime int64 `json:"T"`
|
|
|
|
|
|
|
|
Symbol string `json:"s"`
|
|
|
|
Interval string `json:"i"`
|
|
|
|
|
2021-05-30 15:27:39 +00:00
|
|
|
Open fixedpoint.Value `json:"o"`
|
|
|
|
Close fixedpoint.Value `json:"c"`
|
|
|
|
High fixedpoint.Value `json:"h"`
|
|
|
|
Low fixedpoint.Value `json:"l"`
|
2020-09-16 04:28:15 +00:00
|
|
|
|
2021-05-30 15:27:39 +00:00
|
|
|
Volume fixedpoint.Value `json:"v"` // base asset volume (like 10 BTC)
|
|
|
|
QuoteVolume fixedpoint.Value `json:"q"` // quote asset volume
|
2021-03-15 07:37:53 +00:00
|
|
|
|
2021-05-30 15:27:39 +00:00
|
|
|
TakerBuyBaseAssetVolume fixedpoint.Value `json:"V"` // taker buy base asset volume (like 10 BTC)
|
|
|
|
TakerBuyQuoteAssetVolume fixedpoint.Value `json:"Q"` // taker buy quote asset volume (like 1000USDT)
|
2020-09-16 04:28:15 +00:00
|
|
|
|
|
|
|
LastTradeID int `json:"L"`
|
|
|
|
NumberOfTrades int64 `json:"n"`
|
|
|
|
Closed bool `json:"x"`
|
|
|
|
}
|
|
|
|
|
2021-12-12 07:39:06 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
kline
|
|
|
|
|
|
|
|
{
|
|
|
|
"e": "kline", // KLineEvent type
|
|
|
|
"E": 123456789, // KLineEvent time
|
|
|
|
"s": "BNBBTC", // Symbol
|
|
|
|
"k": {
|
|
|
|
"t": 123400000, // Kline start time
|
|
|
|
"T": 123460000, // Kline close time
|
|
|
|
"s": "BNBBTC", // Symbol
|
|
|
|
"i": "1m", // Interval
|
|
|
|
"f": 100, // First trade ID
|
|
|
|
"L": 200, // Last trade ID
|
|
|
|
"o": "0.0010", // Open price
|
|
|
|
"c": "0.0020", // Close price
|
|
|
|
"h": "0.0025", // High price
|
|
|
|
"l": "0.0015", // Low price
|
|
|
|
"v": "1000", // Base asset volume
|
|
|
|
"n": 100, // Number of trades
|
|
|
|
"x": false, // Is this kline closed?
|
|
|
|
"q": "1.0000", // Quote asset volume
|
|
|
|
"V": "500", // Taker buy base asset volume
|
|
|
|
"Q": "0.500", // Taker buy quote asset volume
|
|
|
|
"B": "123456" // Ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-07-11 05:08:50 +00:00
|
|
|
type KLineEvent struct {
|
|
|
|
EventBase
|
2020-09-16 04:28:15 +00:00
|
|
|
Symbol string `json:"s"`
|
|
|
|
KLine KLine `json:"k,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KLine) KLine() types.KLine {
|
|
|
|
return types.KLine{
|
2021-05-30 15:29:13 +00:00
|
|
|
Exchange: types.ExchangeBinance,
|
|
|
|
Symbol: k.Symbol,
|
|
|
|
Interval: types.Interval(k.Interval),
|
2021-12-15 05:04:01 +00:00
|
|
|
StartTime: types.NewTimeFromUnix(0, k.StartTime*int64(time.Millisecond)),
|
|
|
|
EndTime: types.NewTimeFromUnix(0, k.EndTime*int64(time.Millisecond)),
|
2022-02-03 04:55:25 +00:00
|
|
|
Open: k.Open,
|
|
|
|
Close: k.Close,
|
|
|
|
High: k.High,
|
|
|
|
Low: k.Low,
|
|
|
|
Volume: k.Volume,
|
|
|
|
QuoteVolume: k.QuoteVolume,
|
|
|
|
TakerBuyBaseAssetVolume: k.TakerBuyBaseAssetVolume,
|
|
|
|
TakerBuyQuoteAssetVolume: k.TakerBuyQuoteAssetVolume,
|
2021-05-30 15:29:13 +00:00
|
|
|
LastTradeID: uint64(k.LastTradeID),
|
|
|
|
NumberOfTrades: uint64(k.NumberOfTrades),
|
|
|
|
Closed: k.Closed,
|
2020-09-16 04:28:15 +00:00
|
|
|
}
|
2020-07-11 05:08:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 06:06:55 +00:00
|
|
|
type ListenKeyExpired struct {
|
|
|
|
EventBase
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:24:36 +00:00
|
|
|
type MarkPriceUpdateEvent struct {
|
|
|
|
EventBase
|
|
|
|
|
2021-12-12 07:39:06 +00:00
|
|
|
Symbol string `json:"s"`
|
|
|
|
|
|
|
|
MarkPrice fixedpoint.Value `json:"p"`
|
|
|
|
IndexPrice fixedpoint.Value `json:"i"`
|
|
|
|
EstimatedPrice fixedpoint.Value `json:"P"`
|
2021-11-15 17:24:36 +00:00
|
|
|
|
2021-12-12 07:39:06 +00:00
|
|
|
FundingRate fixedpoint.Value `json:"r"`
|
|
|
|
NextFundingTime int64 `json:"T"`
|
2021-11-15 17:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
"e": "markPriceUpdate", // Event type
|
|
|
|
"E": 1562305380000, // Event time
|
|
|
|
"s": "BTCUSDT", // Symbol
|
|
|
|
"p": "11794.15000000", // Mark price
|
|
|
|
"i": "11784.62659091", // Index price
|
|
|
|
"P": "11784.25641265", // Estimated Settle Price, only useful in the last hour before the settlement starts
|
|
|
|
"r": "0.00038167", // Funding rate
|
|
|
|
"T": 1562306400000 // Next funding time
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-11-16 06:26:27 +00:00
|
|
|
type ContinuousKLineEvent struct {
|
|
|
|
EventBase
|
|
|
|
Symbol string `json:"ps"`
|
|
|
|
ct string `json:"ct"`
|
|
|
|
KLine KLine `json:"k,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
"e":"continuous_kline", // Event type
|
|
|
|
"E":1607443058651, // Event time
|
|
|
|
"ps":"BTCUSDT", // Pair
|
|
|
|
"ct":"PERPETUAL" // Contract type
|
|
|
|
"k":{
|
|
|
|
"t":1607443020000, // Kline start time
|
|
|
|
"T":1607443079999, // Kline close time
|
|
|
|
"i":"1m", // Interval
|
|
|
|
"f":116467658886, // First trade ID
|
|
|
|
"L":116468012423, // Last trade ID
|
|
|
|
"o":"18787.00", // Open price
|
|
|
|
"c":"18804.04", // Close price
|
|
|
|
"h":"18804.04", // High price
|
|
|
|
"l":"18786.54", // Low price
|
|
|
|
"v":"197.664", // volume
|
|
|
|
"n": 543, // Number of trades
|
|
|
|
"x":false, // Is this kline closed?
|
|
|
|
"q":"3715253.19494", // Quote asset volume
|
|
|
|
"V":"184.769", // Taker buy volume
|
|
|
|
"Q":"3472925.84746", //Taker buy quote asset volume
|
|
|
|
"B":"0" // Ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-12-12 07:39:06 +00:00
|
|
|
// Similar to the ExecutionReportEvent's fields. But with totally different json key.
|
|
|
|
// e.g., Stop price. So that, we can not merge them.
|
|
|
|
type OrderTrade struct {
|
2022-01-10 17:41:33 +00:00
|
|
|
Symbol string `json:"s"`
|
|
|
|
ClientOrderID string `json:"c"`
|
|
|
|
Side string `json:"S"`
|
|
|
|
OrderType string `json:"o"`
|
|
|
|
TimeInForce string `json:"f"`
|
|
|
|
OriginalQuantity fixedpoint.Value `json:"q"`
|
|
|
|
OriginalPrice fixedpoint.Value `json:"p"`
|
|
|
|
|
|
|
|
AveragePrice fixedpoint.Value `json:"ap"`
|
|
|
|
StopPrice fixedpoint.Value `json:"sp"`
|
|
|
|
CurrentExecutionType string `json:"x"`
|
|
|
|
CurrentOrderStatus string `json:"X"`
|
|
|
|
|
|
|
|
OrderId int64 `json:"i"`
|
|
|
|
OrderLastFilledQuantity fixedpoint.Value `json:"l"`
|
|
|
|
OrderFilledAccumulatedQuantity fixedpoint.Value `json:"z"`
|
|
|
|
LastFilledPrice fixedpoint.Value `json:"L"`
|
|
|
|
|
|
|
|
CommissionAmount fixedpoint.Value `json:"n"`
|
|
|
|
CommissionAsset string `json:"N"`
|
2021-12-12 07:39:06 +00:00
|
|
|
|
|
|
|
OrderTradeTime int64 `json:"T"`
|
|
|
|
TradeId int64 `json:"t"`
|
|
|
|
|
|
|
|
BidsNotional string `json:"b"`
|
|
|
|
AskNotional string `json:"a"`
|
|
|
|
|
|
|
|
IsMaker bool `json:"m"`
|
|
|
|
IsReduceOnly bool ` json:"r"`
|
|
|
|
|
|
|
|
StopPriceWorkingType string `json:"wt"`
|
|
|
|
OriginalOrderType string `json:"ot"`
|
|
|
|
PositionSide string `json:"ps"`
|
|
|
|
RealizedProfit string `json:"rp"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type OrderTradeUpdateEvent struct {
|
|
|
|
EventBase
|
|
|
|
Transaction int64 `json:"T"`
|
|
|
|
OrderTrade OrderTrade `json:"o"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// "e":"ORDER_TRADE_UPDATE", // Event Type
|
|
|
|
// "E":1568879465651, // Event Time
|
|
|
|
// "T":1568879465650, // Transaction Time
|
|
|
|
// "o":{
|
|
|
|
// "s":"BTCUSDT", // Symbol
|
|
|
|
// "c":"TEST", // Client Order Id
|
|
|
|
// // special client order id:
|
|
|
|
// // starts with "autoclose-": liquidation order
|
|
|
|
// // "adl_autoclose": ADL auto close order
|
|
|
|
// "S":"SELL", // Side
|
|
|
|
// "o":"TRAILING_STOP_MARKET", // Order Type
|
|
|
|
// "f":"GTC", // Time in Force
|
|
|
|
// "q":"0.001", // Original Quantity
|
|
|
|
// "p":"0", // Original Price
|
|
|
|
// "ap":"0", // Average Price
|
|
|
|
// "sp":"7103.04", // Stop Price. Please ignore with TRAILING_STOP_MARKET order
|
|
|
|
// "x":"NEW", // Execution Type
|
|
|
|
// "X":"NEW", // Order Status
|
|
|
|
// "i":8886774, // Order Id
|
|
|
|
// "l":"0", // Order Last Filled Quantity
|
|
|
|
// "z":"0", // Order Filled Accumulated Quantity
|
|
|
|
// "L":"0", // Last Filled Price
|
|
|
|
// "N":"USDT", // Commission Asset, will not push if no commission
|
|
|
|
// "n":"0", // Commission, will not push if no commission
|
|
|
|
// "T":1568879465651, // Order Trade Time
|
|
|
|
// "t":0, // Trade Id
|
|
|
|
// "b":"0", // Bids Notional
|
|
|
|
// "a":"9.91", // Ask Notional
|
|
|
|
// "m":false, // Is this trade the maker side?
|
|
|
|
// "R":false, // Is this reduce only
|
|
|
|
// "wt":"CONTRACT_PRICE", // Stop Price Working Type
|
|
|
|
// "ot":"TRAILING_STOP_MARKET", // Original Order Type
|
|
|
|
// "ps":"LONG", // Position Side
|
|
|
|
// "cp":false, // If Close-All, pushed with conditional order
|
|
|
|
// "AP":"7476.89", // Activation Price, only puhed with TRAILING_STOP_MARKET order
|
|
|
|
// "cr":"5.0", // Callback Rate, only puhed with TRAILING_STOP_MARKET order
|
|
|
|
// "rp":"0" // Realized Profit of the trade
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (e *OrderTradeUpdateEvent) OrderFutures() (*types.Order, error) {
|
|
|
|
|
|
|
|
switch e.OrderTrade.CurrentExecutionType {
|
|
|
|
case "NEW", "CANCELED", "EXPIRED":
|
|
|
|
case "CALCULATED - Liquidation Execution":
|
|
|
|
case "TRADE": // For Order FILLED status. And the order has been completed.
|
|
|
|
default:
|
|
|
|
return nil, errors.New("execution report type is not for futures order")
|
|
|
|
}
|
|
|
|
|
|
|
|
orderCreationTime := time.Unix(0, e.OrderTrade.OrderTradeTime*int64(time.Millisecond))
|
|
|
|
return &types.Order{
|
|
|
|
Exchange: types.ExchangeBinance,
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
|
|
|
Symbol: e.OrderTrade.Symbol,
|
|
|
|
ClientOrderID: e.OrderTrade.ClientOrderID,
|
|
|
|
Side: toGlobalFuturesSideType(futures.SideType(e.OrderTrade.Side)),
|
|
|
|
Type: toGlobalFuturesOrderType(futures.OrderType(e.OrderTrade.OrderType)),
|
2022-02-03 04:55:25 +00:00
|
|
|
Quantity: e.OrderTrade.OriginalQuantity,
|
|
|
|
Price: e.OrderTrade.OriginalPrice,
|
2022-02-18 05:52:13 +00:00
|
|
|
TimeInForce: types.TimeInForce(e.OrderTrade.TimeInForce),
|
2021-12-12 07:39:06 +00:00
|
|
|
},
|
|
|
|
OrderID: uint64(e.OrderTrade.OrderId),
|
|
|
|
Status: toGlobalFuturesOrderStatus(futures.OrderStatusType(e.OrderTrade.CurrentOrderStatus)),
|
2022-02-03 04:55:25 +00:00
|
|
|
ExecutedQuantity: e.OrderTrade.OrderFilledAccumulatedQuantity,
|
2021-12-12 07:39:06 +00:00
|
|
|
CreationTime: types.Time(orderCreationTime),
|
|
|
|
}, nil
|
2020-07-11 05:08:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 11:28:12 +00:00
|
|
|
func (e *OrderTradeUpdateEvent) TradeFutures() (*types.Trade, error) {
|
|
|
|
if e.OrderTrade.CurrentExecutionType != "TRADE" {
|
|
|
|
return nil, errors.New("execution report is not a futures trade")
|
|
|
|
}
|
|
|
|
|
|
|
|
tt := time.Unix(0, e.OrderTrade.OrderTradeTime*int64(time.Millisecond))
|
|
|
|
return &types.Trade{
|
|
|
|
ID: uint64(e.OrderTrade.TradeId),
|
|
|
|
Exchange: types.ExchangeBinance,
|
|
|
|
Symbol: e.OrderTrade.Symbol,
|
|
|
|
OrderID: uint64(e.OrderTrade.OrderId),
|
|
|
|
Side: toGlobalSideType(binance.SideType(e.OrderTrade.Side)),
|
2022-02-03 04:55:25 +00:00
|
|
|
Price: e.OrderTrade.LastFilledPrice,
|
|
|
|
Quantity: e.OrderTrade.OrderLastFilledQuantity,
|
2022-06-05 08:23:12 +00:00
|
|
|
QuoteQuantity: e.OrderTrade.LastFilledPrice.Mul(e.OrderTrade.OrderLastFilledQuantity),
|
2022-01-13 11:28:12 +00:00
|
|
|
IsBuyer: e.OrderTrade.Side == "BUY",
|
|
|
|
IsMaker: e.OrderTrade.IsMaker,
|
|
|
|
Time: types.Time(tt),
|
2022-02-03 04:55:25 +00:00
|
|
|
Fee: e.OrderTrade.CommissionAmount,
|
2022-01-13 11:28:12 +00:00
|
|
|
FeeCurrency: e.OrderTrade.CommissionAsset,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-12-27 22:26:27 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:08:50 +00:00
|
|
|
type EventBase struct {
|
|
|
|
Event string `json:"e"` // event
|
|
|
|
Time int64 `json:"E"`
|
|
|
|
}
|
2021-12-22 13:01:11 +00:00
|
|
|
|
|
|
|
type BookTickerEvent struct {
|
|
|
|
EventBase
|
|
|
|
Symbol string `json:"s"`
|
|
|
|
Buy fixedpoint.Value `json:"b"`
|
|
|
|
BuySize fixedpoint.Value `json:"B"`
|
|
|
|
Sell fixedpoint.Value `json:"a"`
|
|
|
|
SellSize fixedpoint.Value `json:"A"`
|
2022-01-10 17:41:33 +00:00
|
|
|
// "u":400900217, // order book updateId
|
|
|
|
// "s":"BNBUSDT", // symbol
|
|
|
|
// "b":"25.35190000", // best bid price
|
|
|
|
// "B":"31.21000000", // best bid qty
|
|
|
|
// "a":"25.36520000", // best ask price
|
|
|
|
// "A":"40.66000000" // best ask qty
|
2021-12-22 13:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *BookTickerEvent) BookTicker() types.BookTicker {
|
|
|
|
return types.BookTicker{
|
|
|
|
Symbol: k.Symbol,
|
|
|
|
Buy: k.Buy,
|
|
|
|
BuySize: k.BuySize,
|
|
|
|
Sell: k.Sell,
|
|
|
|
SellSize: k.SellSize,
|
|
|
|
}
|
|
|
|
}
|