mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
okex: implement batch place and batch cancel orders
This commit is contained in:
parent
1acbaefcd9
commit
7e97163207
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
|
"github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
@ -93,9 +94,8 @@ var rootCmd = &cobra.Command{
|
||||||
log.Infof("TICKER:")
|
log.Infof("TICKER:")
|
||||||
log.Infof("%T%+v", ticker, ticker)
|
log.Infof("%T%+v", ticker, ticker)
|
||||||
|
|
||||||
|
|
||||||
log.Infof("PLACING ORDER:")
|
log.Infof("PLACING ORDER:")
|
||||||
response, err := client.NewPlaceOrderRequest().
|
placeResponse, err := client.NewPlaceOrderRequest().
|
||||||
InstrumentID("LTC-USDT").
|
InstrumentID("LTC-USDT").
|
||||||
OrderType(okexapi.OrderTypeLimit).
|
OrderType(okexapi.OrderTypeLimit).
|
||||||
Side(okexapi.SideTypeBuy).
|
Side(okexapi.SideTypeBuy).
|
||||||
|
@ -106,16 +106,57 @@ var rootCmd = &cobra.Command{
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("place order response: %+v", response)
|
log.Infof("place order response: %+v", placeResponse)
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
response, err = client.NewCancelOrderRequest().
|
cancelResponse, err := client.NewCancelOrderRequest().
|
||||||
InstrumentID("LTC-USDT").
|
InstrumentID("LTC-USDT").
|
||||||
OrderID(response.OrderID).
|
OrderID(placeResponse.OrderID).
|
||||||
Do(ctx)
|
Do(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Infof("cancel order response: %+v", response)
|
log.Infof("cancel order response: %+v", cancelResponse)
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
log.Infof("BATCH PLACE ORDER:")
|
||||||
|
batchPlaceReq := client.NewBatchPlaceOrderRequest()
|
||||||
|
batchPlaceReq.Add(client.NewPlaceOrderRequest().
|
||||||
|
InstrumentID("LTC-USDT").
|
||||||
|
OrderType(okexapi.OrderTypeLimit).
|
||||||
|
Side(okexapi.SideTypeBuy).
|
||||||
|
Price("50.0").
|
||||||
|
Quantity("0.5"))
|
||||||
|
|
||||||
|
batchPlaceReq.Add(client.NewPlaceOrderRequest().
|
||||||
|
InstrumentID("LTC-USDT").
|
||||||
|
OrderType(okexapi.OrderTypeLimit).
|
||||||
|
Side(okexapi.SideTypeBuy).
|
||||||
|
Price("30.0").
|
||||||
|
Quantity("0.5"))
|
||||||
|
|
||||||
|
batchPlaceResponse, err := batchPlaceReq.Do(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("batch place order response: %+v", batchPlaceResponse)
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
cancelReq := client.NewBatchCancelOrderRequest()
|
||||||
|
for _, resp := range batchPlaceResponse {
|
||||||
|
cancelReq.Add(client.NewCancelOrderRequest().
|
||||||
|
InstrumentID("LTC-USDT").
|
||||||
|
OrderID(resp.OrderID))
|
||||||
|
}
|
||||||
|
|
||||||
|
batchCancelResponse, err := cancelReq.Do(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Infof("batch cancel order response: %+v", batchCancelResponse)
|
||||||
|
|
||||||
// cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
// cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -512,7 +512,7 @@ func (r *BatchPlaceOrderRequest) Add(reqs ...*PlaceOrderRequest) *BatchPlaceOrde
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *BatchPlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
|
func (r *BatchPlaceOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) {
|
||||||
var parameterList []map[string]interface{}
|
var parameterList []map[string]interface{}
|
||||||
|
|
||||||
for _, req := range r.reqs {
|
for _, req := range r.reqs {
|
||||||
|
@ -539,11 +539,7 @@ func (r *BatchPlaceOrderRequest) Do(ctx context.Context) (*OrderResponse, error)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(orderResponse.Data) == 0 {
|
return orderResponse.Data, nil
|
||||||
return nil, errors.New("order create error")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &orderResponse.Data[0], nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RestClient) NewBatchPlaceOrderRequest() *BatchPlaceOrderRequest {
|
func (c *RestClient) NewBatchPlaceOrderRequest() *BatchPlaceOrderRequest {
|
||||||
|
@ -575,7 +571,7 @@ func (r *CancelOrderRequest) ClientOrderID(clientOrderID string) *CancelOrderReq
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *CancelOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
|
func (r *CancelOrderRequest) Parameters() map[string]interface{} {
|
||||||
var payload = map[string]interface{}{
|
var payload = map[string]interface{}{
|
||||||
"instId": r.instId,
|
"instId": r.instId,
|
||||||
}
|
}
|
||||||
|
@ -584,7 +580,15 @@ func (r *CancelOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
|
||||||
payload["ordId"] = r.ordId
|
payload["ordId"] = r.ordId
|
||||||
} else if r.clOrdId != nil {
|
} else if r.clOrdId != nil {
|
||||||
payload["clOrdId"] = r.clOrdId
|
payload["clOrdId"] = r.clOrdId
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
return payload
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *CancelOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) {
|
||||||
|
var payload = r.Parameters()
|
||||||
|
|
||||||
|
if r.ordId == nil && r.clOrdId != nil {
|
||||||
return nil, errors.New("either orderID or clientOrderID is required for canceling order")
|
return nil, errors.New("either orderID or clientOrderID is required for canceling order")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -607,6 +611,53 @@ func (r *CancelOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return orderResponse.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RestClient) NewCancelOrderRequest() *CancelOrderRequest {
|
||||||
|
return &CancelOrderRequest{
|
||||||
|
client: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type BatchCancelOrderRequest struct {
|
||||||
|
client *RestClient
|
||||||
|
|
||||||
|
reqs []*CancelOrderRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *BatchCancelOrderRequest) Add(reqs ...*CancelOrderRequest) *BatchCancelOrderRequest {
|
||||||
|
r.reqs = append(r.reqs, reqs...)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *BatchCancelOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
|
||||||
|
var parameterList []map[string]interface{}
|
||||||
|
|
||||||
|
for _, req := range r.reqs {
|
||||||
|
params := req.Parameters()
|
||||||
|
parameterList = append(parameterList, params)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := r.client.newAuthenticatedRequest("POST", "/api/v5/trade/cancel-batch-orders", nil, parameterList)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := r.client.sendRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var orderResponse struct {
|
||||||
|
Code string `json:"code"`
|
||||||
|
Message string `json:"msg"`
|
||||||
|
Data []OrderResponse `json:"data"`
|
||||||
|
}
|
||||||
|
if err := response.DecodeJSON(&orderResponse); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if len(orderResponse.Data) == 0 {
|
if len(orderResponse.Data) == 0 {
|
||||||
return nil, errors.New("order create error")
|
return nil, errors.New("order create error")
|
||||||
}
|
}
|
||||||
|
@ -614,8 +665,8 @@ func (r *CancelOrderRequest) Do(ctx context.Context) (*OrderResponse, error) {
|
||||||
return &orderResponse.Data[0], nil
|
return &orderResponse.Data[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RestClient) NewCancelOrderRequest() *CancelOrderRequest {
|
func (c *RestClient) NewBatchCancelOrderRequest() *BatchCancelOrderRequest {
|
||||||
return &CancelOrderRequest{
|
return &BatchCancelOrderRequest{
|
||||||
client: c,
|
client: c,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user