Skip to content

Commit

Permalink
Update with v3 APIs
Browse files Browse the repository at this point in the history
Signed-off-by: adshao <tjusgj@gmail.com>
  • Loading branch information
adshao committed Jul 29, 2019
1 parent 4076847 commit d46b2b9
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 219 deletions.
65 changes: 43 additions & 22 deletions client.go
Expand Up @@ -23,35 +23,60 @@ type SideType string
// OrderType define order type
type OrderType string

// TimeInForce define time in force type of order
type TimeInForce string
// TimeInForceType define time in force type of order
type TimeInForceType string

// NewOrderRespType define response JSON verbosity
type NewOrderRespType string

// OrderStatusType define order status type
type OrderStatusType string

// SymbolType define symbol type
type SymbolType string

// SymbolStatusType define symbol status type
type SymbolStatusType string

// Global enums
const (
SideTypeBuy SideType = "BUY"
SideTypeSell SideType = "SELL"

OrderTypeLimit OrderType = "LIMIT"
OrderTypeMarket OrderType = "MARKET"
OrderTypeLimitMaker OrderType = "LIMIT_MAKER"

OrderTypeStopLoss OrderType = "STOP_LOSS"
OrderTypeStopLossLimit OrderType = "STOP_LOSS_LIMIT"

OrderTypeLimit OrderType = "LIMIT"
OrderTypeMarket OrderType = "MARKET"
OrderTypeLimitMaker OrderType = "LIMIT_MAKER"
OrderTypeStopLoss OrderType = "STOP_LOSS"
OrderTypeStopLossLimit OrderType = "STOP_LOSS_LIMIT"
OrderTypeTakeProfit OrderType = "TAKE_PROFIT"
OrderTypeTakeProfitLimit OrderType = "TAKE_PROFIT_LIMIT"

TimeInForceGTC TimeInForce = "GTC"
TimeInForceIOC TimeInForce = "IOC"
TimeInForceFOK TimeInForce = "FOK"
TimeInForceTypeGTC TimeInForceType = "GTC"
TimeInForceTypeIOC TimeInForceType = "IOC"
TimeInForceTypeFOK TimeInForceType = "FOK"

NewOrderRespTypeACK NewOrderRespType = "ACK"
NewOrderRespTypeRESULT NewOrderRespType = "RESULT"
NewOrderRespTypeFULL NewOrderRespType = "FULL"

OrderStatusTypeNew OrderStatusType = "NEW"
OrderStatusTypePartiallyFilled OrderStatusType = "PARTIALLY_FILLED"
OrderStatusTypeFilled OrderStatusType = "FILLED"
OrderStatusTypeCanceled OrderStatusType = "CANCELED"
OrderStatusTypePendingCancel OrderStatusType = "PENDING_CANCEL"
OrderStatusTypeRejected OrderStatusType = "REJECTED"
OrderStatusTypeExpired OrderStatusType = "EXPIRED"

SymbolTypeSpot SymbolType = "SPOT"

SymbolStatusTypePreTrading SymbolStatusType = "PRE_TRADING"
SymbolStatusTypeTrading SymbolStatusType = "TRADING"
SymbolStatusTypePostTrading SymbolStatusType = "POST_TRADING"
SymbolStatusTypeEndOfDay SymbolStatusType = "END_OF_DAY"
SymbolStatusTypeHalt SymbolStatusType = "HALT"
SymbolStatusTypeAuctionMatch SymbolStatusType = "AUCTION_MATCH"
SymbolStatusTypeBreak SymbolStatusType = "BREAK"

timestampKey = "timestamp"
signatureKey = "signature"
recvWindowKey = "recvWindow"
Expand Down Expand Up @@ -192,6 +217,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
}()
c.debug("response: %#v", res)
c.debug("response body: %s", string(data))
c.debug("response status code: %d", res.StatusCode)

if res.StatusCode >= 400 {
apiErr := new(APIError)
Expand Down Expand Up @@ -234,11 +260,6 @@ func (c *Client) NewKlinesService() *KlinesService {
return &KlinesService{c: c}
}

// NewPriceChangeStatsService init prices change stats service
func (c *Client) NewPriceChangeStatsService() *PriceChangeStatsService {
return &PriceChangeStatsService{c: c}
}

// NewListPriceChangeStatsService init list prices change stats service
func (c *Client) NewListPriceChangeStatsService() *ListPriceChangeStatsService {
return &ListPriceChangeStatsService{c: c}
Expand All @@ -249,11 +270,6 @@ func (c *Client) NewListPricesService() *ListPricesService {
return &ListPricesService{c: c}
}

// NewBookTickerService init booking ticker service
func (c *Client) NewBookTickerService() *BookTickerService {
return &BookTickerService{c: c}
}

// NewListBookTickersService init listing booking tickers service
func (c *Client) NewListBookTickersService() *ListBookTickersService {
return &ListBookTickersService{c: c}
Expand Down Expand Up @@ -338,3 +354,8 @@ func (c *Client) NewExchangeInfoService() *ExchangeInfoService {
func (c *Client) NewGetWithdrawFeeService() *GetWithdrawFeeService {
return &GetWithdrawFeeService{c: c}
}

// NewAveragePriceService init average price service
func (c *Client) NewAveragePriceService() *AveragePriceService {
return &AveragePriceService{c: c}
}
13 changes: 13 additions & 0 deletions helpers.go
@@ -1,8 +1,21 @@
package binance

import "math"
import "bytes"

// AmountToLotSize converts an amount to a lot sized amount
func AmountToLotSize(lot float64, precision int, amount float64) float64 {
return math.Trunc(math.Floor(amount/lot)*lot*math.Pow10(precision)) / math.Pow10(precision)
}

// toJSONList convert v to json list if v is a map
func toJSONList(v []byte) []byte {
if len(v) > 0 && v[0] == '{' {
var b bytes.Buffer
b.Write([]byte("["))
b.Write(v)
b.Write([]byte("]"))
return b.Bytes()
}
return v
}
112 changes: 72 additions & 40 deletions order_service.go
Expand Up @@ -11,7 +11,7 @@ type CreateOrderService struct {
symbol string
side SideType
orderType OrderType
timeInForce *TimeInForce
timeInForce *TimeInForceType
newOrderRespType *NewOrderRespType
quantity string
price *string
Expand Down Expand Up @@ -39,7 +39,7 @@ func (s *CreateOrderService) Type(orderType OrderType) *CreateOrderService {
}

// TimeInForce set timeInForce
func (s *CreateOrderService) TimeInForce(timeInForce TimeInForce) *CreateOrderService {
func (s *CreateOrderService) TimeInForce(timeInForce TimeInForceType) *CreateOrderService {
s.timeInForce = &timeInForce
return s
}
Expand Down Expand Up @@ -135,24 +135,24 @@ func (s *CreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res
// Test send test api to check if the request is valid
func (s *CreateOrderService) Test(ctx context.Context, opts ...RequestOption) (err error) {
_, err = s.createOrder(ctx, "/api/v3/order/test", opts...)
return
return err
}

// CreateOrderResponse define create order response
type CreateOrderResponse struct {
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
TransactTime int64 `json:"transactTime"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CummulativeQuoteQty string `json:"cummulativeQuoteQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Side string `json:"side"`
Fills []*Fill `json:"fills"`
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
TransactTime int64 `json:"transactTime"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CummulativeQuoteQuantity string `json:"cummulativeQuoteQty"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
Fills []*Fill `json:"fills"`
}

// Fill may be returned in an array of fills in a CreateOrderResponse.
Expand Down Expand Up @@ -251,27 +251,32 @@ func (s *GetOrderService) Do(ctx context.Context, opts ...RequestOption) (res *O

// Order define order info
type Order struct {
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Side string `json:"side"`
StopPrice string `json:"stopPrice"`
IcebergQuantity string `json:"icebergQty"`
Time int64 `json:"time"`
}

// ListOrdersService list all orders
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CummulativeQuoteQuantity string `json:"cummulativeQuoteQty"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
StopPrice string `json:"stopPrice"`
IcebergQuantity string `json:"icebergQty"`
Time int64 `json:"time"`
UpdateTime int64 `json:"updateTime"`
IsWorking bool `json:"isWorking"`
}

// ListOrdersService all account orders; active, canceled, or filled
type ListOrdersService struct {
c *Client
symbol string
orderID *int64
limit *int
c *Client
symbol string
orderID *int64
startTime *int64
endTime *int64
limit *int
}

// Symbol set symbol
Expand All @@ -286,6 +291,18 @@ func (s *ListOrdersService) OrderID(orderID int64) *ListOrdersService {
return s
}

// StartTime set starttime
func (s *ListOrdersService) StartTime(startTime int64) *ListOrdersService {
s.startTime = &startTime
return s
}

// EndTime set endtime
func (s *ListOrdersService) EndTime(endTime int64) *ListOrdersService {
s.endTime = &endTime
return s
}

// Limit set limit
func (s *ListOrdersService) Limit(limit int) *ListOrdersService {
s.limit = &limit
Expand All @@ -303,6 +320,12 @@ func (s *ListOrdersService) Do(ctx context.Context, opts ...RequestOption) (res
if s.orderID != nil {
r.setParam("orderId", *s.orderID)
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
if s.limit != nil {
r.setParam("limit", *s.limit)
}
Expand Down Expand Up @@ -382,8 +405,17 @@ func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res

// CancelOrderResponse define response of canceling order
type CancelOrderResponse struct {
Symbol string `json:"symbol"`
OrigClientOrderID string `json:"origClientOrderId"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
Symbol string `json:"symbol"`
OrigClientOrderID string `json:"origClientOrderId"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
TransactTime int64 `json:"transactTime"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CummulativeQuoteQuantity string `json:"cummulativeQuoteQty"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
}

0 comments on commit d46b2b9

Please sign in to comment.