mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
okex: refactor trade service and fix order details api
This commit is contained in:
parent
5f8108f93e
commit
365b4c3837
|
@ -95,7 +95,7 @@ var rootCmd = &cobra.Command{
|
||||||
log.Infof("%T%+v", ticker, ticker)
|
log.Infof("%T%+v", ticker, ticker)
|
||||||
|
|
||||||
log.Infof("PLACING ORDER:")
|
log.Infof("PLACING ORDER:")
|
||||||
placeResponse, err := client.NewPlaceOrderRequest().
|
placeResponse, err := client.TradeService.NewPlaceOrderRequest().
|
||||||
InstrumentID("LTC-USDT").
|
InstrumentID("LTC-USDT").
|
||||||
OrderType(okexapi.OrderTypeLimit).
|
OrderType(okexapi.OrderTypeLimit).
|
||||||
Side(okexapi.SideTypeBuy).
|
Side(okexapi.SideTypeBuy).
|
||||||
|
@ -109,9 +109,18 @@ var rootCmd = &cobra.Command{
|
||||||
log.Infof("place order response: %+v", placeResponse)
|
log.Infof("place order response: %+v", placeResponse)
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
client.NewGetOrderDetailsRequest()
|
log.Infof("getting order detail...")
|
||||||
|
orderDetail, err := client.TradeService.NewGetOrderDetailsRequest().
|
||||||
|
InstrumentID("LTC-USDT").
|
||||||
|
OrderID(placeResponse.OrderID).
|
||||||
|
Do(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
cancelResponse, err := client.NewCancelOrderRequest().
|
log.Infof("order detail: %+v", orderDetail)
|
||||||
|
|
||||||
|
cancelResponse, err := client.TradeService.NewCancelOrderRequest().
|
||||||
InstrumentID("LTC-USDT").
|
InstrumentID("LTC-USDT").
|
||||||
OrderID(placeResponse.OrderID).
|
OrderID(placeResponse.OrderID).
|
||||||
Do(ctx)
|
Do(ctx)
|
||||||
|
@ -123,15 +132,15 @@ var rootCmd = &cobra.Command{
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
log.Infof("BATCH PLACE ORDER:")
|
log.Infof("BATCH PLACE ORDER:")
|
||||||
batchPlaceReq := client.NewBatchPlaceOrderRequest()
|
batchPlaceReq := client.TradeService.NewBatchPlaceOrderRequest()
|
||||||
batchPlaceReq.Add(client.NewPlaceOrderRequest().
|
batchPlaceReq.Add(client.TradeService.NewPlaceOrderRequest().
|
||||||
InstrumentID("LTC-USDT").
|
InstrumentID("LTC-USDT").
|
||||||
OrderType(okexapi.OrderTypeLimit).
|
OrderType(okexapi.OrderTypeLimit).
|
||||||
Side(okexapi.SideTypeBuy).
|
Side(okexapi.SideTypeBuy).
|
||||||
Price("50.0").
|
Price("50.0").
|
||||||
Quantity("0.5"))
|
Quantity("0.5"))
|
||||||
|
|
||||||
batchPlaceReq.Add(client.NewPlaceOrderRequest().
|
batchPlaceReq.Add(client.TradeService.NewPlaceOrderRequest().
|
||||||
InstrumentID("LTC-USDT").
|
InstrumentID("LTC-USDT").
|
||||||
OrderType(okexapi.OrderTypeLimit).
|
OrderType(okexapi.OrderTypeLimit).
|
||||||
Side(okexapi.SideTypeBuy).
|
Side(okexapi.SideTypeBuy).
|
||||||
|
@ -147,7 +156,7 @@ var rootCmd = &cobra.Command{
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
log.Infof("getting pending orders...")
|
log.Infof("getting pending orders...")
|
||||||
pendingOrders, err := client.NewGetPendingOrderRequest().Do(ctx)
|
pendingOrders, err := client.TradeService.NewGetPendingOrderRequest().Do(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -155,9 +164,9 @@ var rootCmd = &cobra.Command{
|
||||||
log.Infof("pending order: %+v", pendingOrder)
|
log.Infof("pending order: %+v", pendingOrder)
|
||||||
}
|
}
|
||||||
|
|
||||||
cancelReq := client.NewBatchCancelOrderRequest()
|
cancelReq := client.TradeService.NewBatchCancelOrderRequest()
|
||||||
for _, resp := range batchPlaceResponse {
|
for _, resp := range batchPlaceResponse {
|
||||||
cancelReq.Add(client.NewCancelOrderRequest().
|
cancelReq.Add(client.TradeService.NewCancelOrderRequest().
|
||||||
InstrumentID("LTC-USDT").
|
InstrumentID("LTC-USDT").
|
||||||
OrderID(resp.OrderID))
|
OrderID(resp.OrderID))
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,8 @@ type RestClient struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
|
||||||
Key, Secret, Passphrase string
|
Key, Secret, Passphrase string
|
||||||
|
|
||||||
|
TradeService *TradeService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient() *RestClient {
|
func NewClient() *RestClient {
|
||||||
|
@ -63,12 +65,15 @@ func NewClient() *RestClient {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RestClient{
|
client := &RestClient{
|
||||||
BaseURL: u,
|
BaseURL: u,
|
||||||
client: &http.Client{
|
client: &http.Client{
|
||||||
Timeout: defaultHTTPTimeout,
|
Timeout: defaultHTTPTimeout,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.TradeService = &TradeService{client: client}
|
||||||
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RestClient) Auth(key, secret, passphrase string) {
|
func (c *RestClient) Auth(key, secret, passphrase string) {
|
||||||
|
@ -140,6 +145,9 @@ func (c *RestClient) newAuthenticatedRequest(method, refURL string, params url.V
|
||||||
|
|
||||||
pathURL := c.BaseURL.ResolveReference(rel)
|
pathURL := c.BaseURL.ResolveReference(rel)
|
||||||
path := pathURL.Path
|
path := pathURL.Path
|
||||||
|
if rel.RawQuery != "" {
|
||||||
|
path += "?" + rel.RawQuery
|
||||||
|
}
|
||||||
|
|
||||||
// set location to UTC so that it outputs "2020-12-08T09:08:57.715Z"
|
// set location to UTC so that it outputs "2020-12-08T09:08:57.715Z"
|
||||||
t := time.Now().In(time.UTC)
|
t := time.Now().In(time.UTC)
|
||||||
|
@ -375,50 +383,6 @@ func (c *RestClient) MarketTickers(instType string) ([]MarketTicker, error) {
|
||||||
return tickerResponse.Data, nil
|
return tickerResponse.Data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type OrderResponse struct {
|
|
||||||
OrderID string `json:"ordId"`
|
|
||||||
ClientOrderID string `json:"clOrdId"`
|
|
||||||
Tag string `json:"tag"`
|
|
||||||
Code string `json:"sCode"`
|
|
||||||
Message string `json:"sMsg"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RestClient) NewPlaceOrderRequest() *PlaceOrderRequest {
|
|
||||||
return &PlaceOrderRequest{
|
|
||||||
client: c,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RestClient) NewBatchPlaceOrderRequest() *BatchPlaceOrderRequest {
|
|
||||||
return &BatchPlaceOrderRequest{
|
|
||||||
client: c,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RestClient) NewCancelOrderRequest() *CancelOrderRequest {
|
|
||||||
return &CancelOrderRequest{
|
|
||||||
client: c,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RestClient) NewBatchCancelOrderRequest() *BatchCancelOrderRequest {
|
|
||||||
return &BatchCancelOrderRequest{
|
|
||||||
client: c,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RestClient) NewGetOrderDetailsRequest() *GetOrderDetailsRequest {
|
|
||||||
return &GetOrderDetailsRequest{
|
|
||||||
client: c,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RestClient) NewGetPendingOrderRequest() *GetPendingOrderRequest {
|
|
||||||
return &GetPendingOrderRequest{
|
|
||||||
client: c,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func sign(payload string, secret string) string {
|
func sign(payload string, secret string) string {
|
||||||
var sig = hmac.New(sha256.New, []byte(secret))
|
var sig = hmac.New(sha256.New, []byte(secret))
|
||||||
_, err := sig.Write([]byte(payload))
|
_, err := sig.Write([]byte(payload))
|
||||||
|
|
1
pkg/exchange/okex/okexapi/public.go
Normal file
1
pkg/exchange/okex/okexapi/public.go
Normal file
|
@ -0,0 +1 @@
|
||||||
|
package okexapi
|
|
@ -2,6 +2,7 @@ package okexapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
|
@ -9,6 +10,60 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type TradeService struct {
|
||||||
|
client *RestClient
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderResponse struct {
|
||||||
|
OrderID string `json:"ordId"`
|
||||||
|
ClientOrderID string `json:"clOrdId"`
|
||||||
|
Tag string `json:"tag"`
|
||||||
|
Code string `json:"sCode"`
|
||||||
|
Message string `json:"sMsg"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TradeService) NewPlaceOrderRequest() *PlaceOrderRequest {
|
||||||
|
return &PlaceOrderRequest{
|
||||||
|
client: c.client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TradeService) NewBatchPlaceOrderRequest() *BatchPlaceOrderRequest {
|
||||||
|
return &BatchPlaceOrderRequest{
|
||||||
|
client: c.client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TradeService) NewCancelOrderRequest() *CancelOrderRequest {
|
||||||
|
return &CancelOrderRequest{
|
||||||
|
client: c.client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TradeService) NewBatchCancelOrderRequest() *BatchCancelOrderRequest {
|
||||||
|
return &BatchCancelOrderRequest{
|
||||||
|
client: c.client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TradeService) NewGetOrderDetailsRequest() *GetOrderDetailsRequest {
|
||||||
|
return &GetOrderDetailsRequest{
|
||||||
|
client: c.client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TradeService) NewGetPendingOrderRequest() *GetPendingOrderRequest {
|
||||||
|
return &GetPendingOrderRequest{
|
||||||
|
client: c.client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TradeService) NewGetTransactionDetailsRequest() *GetTransactionDetailsRequest {
|
||||||
|
return &GetTransactionDetailsRequest{
|
||||||
|
client: c.client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type PlaceOrderRequest struct {
|
type PlaceOrderRequest struct {
|
||||||
client *RestClient
|
client *RestClient
|
||||||
|
|
||||||
|
@ -324,20 +379,6 @@ type GetOrderDetailsRequest struct {
|
||||||
clOrdId *string
|
clOrdId *string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *GetOrderDetailsRequest) Parameters() map[string]interface{} {
|
|
||||||
var payload = map[string]interface{}{
|
|
||||||
"instId": r.instId,
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.ordId != nil {
|
|
||||||
payload["ordId"] = r.ordId
|
|
||||||
} else if r.clOrdId != nil {
|
|
||||||
payload["clOrdId"] = r.clOrdId
|
|
||||||
}
|
|
||||||
|
|
||||||
return payload
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *GetOrderDetailsRequest) InstrumentID(instId string) *GetOrderDetailsRequest {
|
func (r *GetOrderDetailsRequest) InstrumentID(instId string) *GetOrderDetailsRequest {
|
||||||
r.instId = instId
|
r.instId = instId
|
||||||
return r
|
return r
|
||||||
|
@ -353,9 +394,23 @@ func (r *GetOrderDetailsRequest) ClientOrderID(clientOrderID string) *GetOrderDe
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *GetOrderDetailsRequest) QueryParameters() url.Values {
|
||||||
|
var values = url.Values{}
|
||||||
|
|
||||||
|
values.Add("instId", r.instId)
|
||||||
|
|
||||||
|
if r.ordId != nil {
|
||||||
|
values.Add("ordId", *r.ordId)
|
||||||
|
} else if r.clOrdId != nil {
|
||||||
|
values.Add("clOrdId", *r.clOrdId)
|
||||||
|
}
|
||||||
|
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
func (r *GetOrderDetailsRequest) Do(ctx context.Context) (*OrderDetails, error) {
|
func (r *GetOrderDetailsRequest) Do(ctx context.Context) (*OrderDetails, error) {
|
||||||
payload := r.Parameters()
|
params := r.QueryParameters()
|
||||||
req, err := r.client.newAuthenticatedRequest("GET", "/api/v5/trade/order", nil, payload)
|
req, err := r.client.newAuthenticatedRequest("GET", "/api/v5/trade/order", params, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -452,3 +507,70 @@ func (r *GetPendingOrderRequest) Do(ctx context.Context) ([]OrderDetails, error)
|
||||||
|
|
||||||
return orderResponse.Data, nil
|
return orderResponse.Data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetTransactionDetailsRequest struct {
|
||||||
|
client *RestClient
|
||||||
|
|
||||||
|
instType *string
|
||||||
|
|
||||||
|
instId *string
|
||||||
|
|
||||||
|
ordId *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *GetTransactionDetailsRequest) InstrumentType(instType string) *GetTransactionDetailsRequest {
|
||||||
|
r.instType = &instType
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *GetTransactionDetailsRequest) InstrumentID(instId string) *GetTransactionDetailsRequest {
|
||||||
|
r.instId = &instId
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *GetTransactionDetailsRequest) OrderID(orderID string) *GetTransactionDetailsRequest {
|
||||||
|
r.ordId = &orderID
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *GetTransactionDetailsRequest) Parameters() map[string]interface{} {
|
||||||
|
var payload = map[string]interface{}{}
|
||||||
|
|
||||||
|
if r.instType != nil {
|
||||||
|
payload["instType"] = r.instType
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.instId != nil {
|
||||||
|
payload["instId"] = r.instId
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.ordId != nil {
|
||||||
|
payload["ordId"] = r.ordId
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *GetTransactionDetailsRequest) Do(ctx context.Context) ([]OrderDetails, error) {
|
||||||
|
payload := r.Parameters()
|
||||||
|
req, err := r.client.newAuthenticatedRequest("GET", "/api/v5/trade/fills", nil, payload)
|
||||||
|
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 []OrderDetails `json:"data"`
|
||||||
|
}
|
||||||
|
if err := response.DecodeJSON(&orderResponse); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return orderResponse.Data, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user