mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
ftx: add toGlobalOrderNew to convert new order structure
This commit is contained in:
parent
e9e1127d3e
commit
66700016e4
|
@ -37,6 +37,67 @@ func TrimLowerString(original string) string {
|
|||
|
||||
var errUnsupportedOrderStatus = fmt.Errorf("unsupported order status")
|
||||
|
||||
func toGlobalOrderNew(r ftxapi.Order) (types.Order, error) {
|
||||
// In exchange/max/convert.go, it only parses these fields.
|
||||
timeInForce := types.TimeInForceGTC
|
||||
if r.Ioc {
|
||||
timeInForce = types.TimeInForceIOC
|
||||
}
|
||||
|
||||
// order type definition: https://github.com/ftexchange/ftx/blob/master/rest/client.py#L122
|
||||
orderType := types.OrderType(TrimUpperString(string(r.Type)))
|
||||
if orderType == types.OrderTypeLimit && r.PostOnly {
|
||||
orderType = types.OrderTypeLimitMaker
|
||||
}
|
||||
|
||||
o := types.Order{
|
||||
SubmitOrder: types.SubmitOrder{
|
||||
ClientOrderID: r.ClientId,
|
||||
Symbol: toGlobalSymbol(r.Market),
|
||||
Side: types.SideType(TrimUpperString(string(r.Side))),
|
||||
Type: orderType,
|
||||
Quantity: r.Size,
|
||||
Price: r.Price,
|
||||
TimeInForce: timeInForce,
|
||||
},
|
||||
Exchange: types.ExchangeFTX,
|
||||
IsWorking: r.Status == "open",
|
||||
OrderID: uint64(r.Id),
|
||||
Status: "",
|
||||
ExecutedQuantity: r.FilledSize,
|
||||
CreationTime: types.Time(r.CreatedAt),
|
||||
UpdateTime: types.Time(r.CreatedAt),
|
||||
}
|
||||
|
||||
s, err := toGlobalOrderStatus(r, r.Status)
|
||||
o.Status = s
|
||||
return o, err
|
||||
}
|
||||
|
||||
func toGlobalOrderStatus(o ftxapi.Order, s ftxapi.OrderStatus) (types.OrderStatus, error) {
|
||||
switch s {
|
||||
case ftxapi.OrderStatusNew:
|
||||
return types.OrderStatusNew, nil
|
||||
|
||||
case ftxapi.OrderStatusOpen:
|
||||
if !o.FilledSize.IsZero() {
|
||||
return types.OrderStatusPartiallyFilled, nil
|
||||
} else {
|
||||
return types.OrderStatusNew, nil
|
||||
}
|
||||
case ftxapi.OrderStatusClosed:
|
||||
// filled or canceled
|
||||
if o.FilledSize == o.Size {
|
||||
return types.OrderStatusFilled, nil
|
||||
} else {
|
||||
// can't distinguish it's canceled or rejected from order response, so always set to canceled
|
||||
return types.OrderStatusCanceled, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("unsupported ftx order status %s: %w", s, errUnsupportedOrderStatus)
|
||||
}
|
||||
|
||||
func toGlobalOrder(r order) (types.Order, error) {
|
||||
// In exchange/max/convert.go, it only parses these fields.
|
||||
timeInForce := types.TimeInForceGTC
|
||||
|
|
|
@ -505,18 +505,19 @@ func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.O
|
|||
|
||||
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
|
||||
// TODO: invoke open trigger orders
|
||||
resp, err := e.newRest().OpenOrders(ctx, toLocalSymbol(symbol))
|
||||
|
||||
req := e.client.NewGetOpenOrdersRequest(toLocalSymbol(symbol))
|
||||
ftxOrders, err := req.Do(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !resp.Success {
|
||||
return nil, fmt.Errorf("ftx returns querying open orders failure")
|
||||
}
|
||||
for _, r := range resp.Result {
|
||||
o, err := toGlobalOrder(r)
|
||||
|
||||
for _, ftxOrder := range ftxOrders {
|
||||
o, err := toGlobalOrderNew(ftxOrder)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return orders, err
|
||||
}
|
||||
|
||||
orders = append(orders, o)
|
||||
}
|
||||
return orders, nil
|
||||
|
@ -538,7 +539,6 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
|
|||
s := since
|
||||
var lastOrder order
|
||||
for hasMoreData {
|
||||
|
||||
if err := requestLimit.Wait(ctx); err != nil {
|
||||
logrus.WithError(err).Error("rate limit error")
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ type Order struct {
|
|||
ReduceOnly bool `json:"reduceOnly"`
|
||||
Ioc bool `json:"ioc"`
|
||||
PostOnly bool `json:"postOnly"`
|
||||
ClientId *string `json:"clientId"`
|
||||
ClientId string `json:"clientId"`
|
||||
}
|
||||
|
||||
//go:generate GetRequest -url "/api/orders" -type GetOpenOrdersRequest -responseDataType []Order
|
||||
|
|
Loading…
Reference in New Issue
Block a user