okex: refactor trade service and fix order details api

This commit is contained in:
c9s 2021-05-25 01:15:46 +08:00
parent 5f8108f93e
commit 365b4c3837
4 changed files with 166 additions and 70 deletions

View File

@ -95,7 +95,7 @@ var rootCmd = &cobra.Command{
log.Infof("%T%+v", ticker, ticker)
log.Infof("PLACING ORDER:")
placeResponse, err := client.NewPlaceOrderRequest().
placeResponse, err := client.TradeService.NewPlaceOrderRequest().
InstrumentID("LTC-USDT").
OrderType(okexapi.OrderTypeLimit).
Side(okexapi.SideTypeBuy).
@ -109,9 +109,18 @@ var rootCmd = &cobra.Command{
log.Infof("place order response: %+v", placeResponse)
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").
OrderID(placeResponse.OrderID).
Do(ctx)
@ -123,15 +132,15 @@ var rootCmd = &cobra.Command{
time.Sleep(time.Second)
log.Infof("BATCH PLACE ORDER:")
batchPlaceReq := client.NewBatchPlaceOrderRequest()
batchPlaceReq.Add(client.NewPlaceOrderRequest().
batchPlaceReq := client.TradeService.NewBatchPlaceOrderRequest()
batchPlaceReq.Add(client.TradeService.NewPlaceOrderRequest().
InstrumentID("LTC-USDT").
OrderType(okexapi.OrderTypeLimit).
Side(okexapi.SideTypeBuy).
Price("50.0").
Quantity("0.5"))
batchPlaceReq.Add(client.NewPlaceOrderRequest().
batchPlaceReq.Add(client.TradeService.NewPlaceOrderRequest().
InstrumentID("LTC-USDT").
OrderType(okexapi.OrderTypeLimit).
Side(okexapi.SideTypeBuy).
@ -147,7 +156,7 @@ var rootCmd = &cobra.Command{
time.Sleep(time.Second)
log.Infof("getting pending orders...")
pendingOrders, err := client.NewGetPendingOrderRequest().Do(ctx)
pendingOrders, err := client.TradeService.NewGetPendingOrderRequest().Do(ctx)
if err != nil {
return err
}
@ -155,9 +164,9 @@ var rootCmd = &cobra.Command{
log.Infof("pending order: %+v", pendingOrder)
}
cancelReq := client.NewBatchCancelOrderRequest()
cancelReq := client.TradeService.NewBatchCancelOrderRequest()
for _, resp := range batchPlaceResponse {
cancelReq.Add(client.NewCancelOrderRequest().
cancelReq.Add(client.TradeService.NewCancelOrderRequest().
InstrumentID("LTC-USDT").
OrderID(resp.OrderID))
}

View File

@ -55,6 +55,8 @@ type RestClient struct {
client *http.Client
Key, Secret, Passphrase string
TradeService *TradeService
}
func NewClient() *RestClient {
@ -63,12 +65,15 @@ func NewClient() *RestClient {
panic(err)
}
return &RestClient{
client := &RestClient{
BaseURL: u,
client: &http.Client{
Timeout: defaultHTTPTimeout,
},
}
client.TradeService = &TradeService{client: client}
return client
}
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)
path := pathURL.Path
if rel.RawQuery != "" {
path += "?" + rel.RawQuery
}
// set location to UTC so that it outputs "2020-12-08T09:08:57.715Z"
t := time.Now().In(time.UTC)
@ -375,50 +383,6 @@ func (c *RestClient) MarketTickers(instType string) ([]MarketTicker, error) {
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 {
var sig = hmac.New(sha256.New, []byte(secret))
_, err := sig.Write([]byte(payload))

View File

@ -0,0 +1 @@
package okexapi

View File

@ -2,6 +2,7 @@ package okexapi
import (
"context"
"net/url"
"strings"
"github.com/c9s/bbgo/pkg/fixedpoint"
@ -9,6 +10,60 @@ import (
"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 {
client *RestClient
@ -324,20 +379,6 @@ type GetOrderDetailsRequest struct {
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 {
r.instId = instId
return r
@ -353,9 +394,23 @@ func (r *GetOrderDetailsRequest) ClientOrderID(clientOrderID string) *GetOrderDe
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) {
payload := r.Parameters()
req, err := r.client.newAuthenticatedRequest("GET", "/api/v5/trade/order", nil, payload)
params := r.QueryParameters()
req, err := r.client.newAuthenticatedRequest("GET", "/api/v5/trade/order", params, nil)
if err != nil {
return nil, err
}
@ -452,3 +507,70 @@ func (r *GetPendingOrderRequest) Do(ctx context.Context) ([]OrderDetails, error)
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
}