mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-22 23:16:48 +00:00
committed by
Adrian Gallagher
parent
242b02c382
commit
63a9e9fcb1
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
// Please supply your own keys here for due diligence testing
|
||||
@@ -292,8 +293,8 @@ func TestFormatWithdrawPermissions(t *testing.T) {
|
||||
func TestGetActiveOrders(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var getOrdersRequest = exchange.GetOrdersRequest{
|
||||
OrderType: exchange.AnyOrderType,
|
||||
var getOrdersRequest = order.GetOrdersRequest{
|
||||
OrderType: order.AnyType,
|
||||
}
|
||||
_, err := b.GetActiveOrders(&getOrdersRequest)
|
||||
if err == nil {
|
||||
@@ -318,8 +319,8 @@ func TestGetActiveOrders(t *testing.T) {
|
||||
func TestGetOrderHistory(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var getOrdersRequest = exchange.GetOrdersRequest{
|
||||
OrderType: exchange.AnyOrderType,
|
||||
var getOrdersRequest = order.GetOrdersRequest{
|
||||
OrderType: order.AnyType,
|
||||
}
|
||||
|
||||
_, err := b.GetOrderHistory(&getOrdersRequest)
|
||||
@@ -352,14 +353,14 @@ func TestSubmitOrder(t *testing.T) {
|
||||
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
||||
}
|
||||
|
||||
var orderSubmission = &exchange.OrderSubmission{
|
||||
var orderSubmission = &order.Submit{
|
||||
Pair: currency.Pair{
|
||||
Delimiter: "_",
|
||||
Base: currency.LTC,
|
||||
Quote: currency.BTC,
|
||||
},
|
||||
OrderSide: exchange.BuyOrderSide,
|
||||
OrderType: exchange.MarketOrderType,
|
||||
OrderSide: order.Buy,
|
||||
OrderType: order.Market,
|
||||
Price: 1,
|
||||
Amount: 1000000000,
|
||||
ClientID: "meowOrder",
|
||||
@@ -383,7 +384,7 @@ func TestCancelExchangeOrder(t *testing.T) {
|
||||
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
||||
}
|
||||
|
||||
var orderCancellation = &exchange.OrderCancellation{
|
||||
var orderCancellation = &order.Cancel{
|
||||
OrderID: "1",
|
||||
WalletAddress: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
|
||||
AccountID: "1",
|
||||
@@ -408,7 +409,7 @@ func TestCancelAllExchangeOrders(t *testing.T) {
|
||||
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
|
||||
}
|
||||
|
||||
var orderCancellation = &exchange.OrderCancellation{
|
||||
var orderCancellation = &order.Cancel{
|
||||
OrderID: "1",
|
||||
WalletAddress: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
|
||||
AccountID: "1",
|
||||
@@ -443,7 +444,7 @@ func TestGetAccountInfo(t *testing.T) {
|
||||
func TestModifyOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := b.ModifyOrder(&exchange.ModifyOrder{})
|
||||
_, err := b.ModifyOrder(&order.Modify{})
|
||||
if err == nil {
|
||||
t.Error("ModifyOrder() error cannot be nil")
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
||||
@@ -182,7 +183,10 @@ func (b *Binance) Start(wg *sync.WaitGroup) {
|
||||
func (b *Binance) Run() {
|
||||
if b.Verbose {
|
||||
log.Debugf(log.ExchangeSys,
|
||||
"%s Websocket: %s. (url: %s).\n", b.GetName(), common.IsEnabled(b.Websocket.IsEnabled()), b.Websocket.GetWebsocketURL())
|
||||
"%s Websocket: %s. (url: %s).\n",
|
||||
b.GetName(),
|
||||
common.IsEnabled(b.Websocket.IsEnabled()),
|
||||
b.Websocket.GetWebsocketURL())
|
||||
b.PrintEnabledPairs()
|
||||
}
|
||||
|
||||
@@ -196,7 +200,9 @@ func (b *Binance) Run() {
|
||||
|
||||
err := b.UpdatePairs(enabledPairs, asset.Spot, true, true)
|
||||
if err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s failed to update currencies. Err: %s\n", b.Name, err)
|
||||
log.Errorf(log.ExchangeSys,
|
||||
"%s failed to update currencies. Err: %s\n",
|
||||
b.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,7 +212,10 @@ func (b *Binance) Run() {
|
||||
|
||||
err := b.UpdateTradablePairs(forceUpdate)
|
||||
if err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s failed to update tradable pairs. Err: %s", b.Name, err)
|
||||
log.Errorf(log.ExchangeSys,
|
||||
"%s failed to update tradable pairs. Err: %s",
|
||||
b.Name,
|
||||
err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,14 +312,20 @@ func (b *Binance) UpdateOrderbook(p currency.Pair, assetType asset.Item) (orderb
|
||||
return orderBook, err
|
||||
}
|
||||
|
||||
for _, bids := range orderbookNew.Bids {
|
||||
for x := range orderbookNew.Bids {
|
||||
orderBook.Bids = append(orderBook.Bids,
|
||||
orderbook.Item{Amount: bids.Quantity, Price: bids.Price})
|
||||
orderbook.Item{
|
||||
Amount: orderbookNew.Bids[x].Quantity,
|
||||
Price: orderbookNew.Bids[x].Price,
|
||||
})
|
||||
}
|
||||
|
||||
for _, asks := range orderbookNew.Asks {
|
||||
for x := range orderbookNew.Asks {
|
||||
orderBook.Asks = append(orderBook.Asks,
|
||||
orderbook.Item{Amount: asks.Quantity, Price: asks.Price})
|
||||
orderbook.Item{
|
||||
Amount: orderbookNew.Asks[x].Quantity,
|
||||
Price: orderbookNew.Asks[x].Price,
|
||||
})
|
||||
}
|
||||
|
||||
orderBook.Pair = p
|
||||
@@ -335,19 +350,19 @@ func (b *Binance) GetAccountInfo() (exchange.AccountInfo, error) {
|
||||
}
|
||||
|
||||
var currencyBalance []exchange.AccountCurrencyInfo
|
||||
for _, balance := range raw.Balances {
|
||||
freeCurrency, err := strconv.ParseFloat(balance.Free, 64)
|
||||
for i := range raw.Balances {
|
||||
freeCurrency, err := strconv.ParseFloat(raw.Balances[i].Free, 64)
|
||||
if err != nil {
|
||||
return info, err
|
||||
}
|
||||
|
||||
lockedCurrency, err := strconv.ParseFloat(balance.Locked, 64)
|
||||
lockedCurrency, err := strconv.ParseFloat(raw.Balances[i].Locked, 64)
|
||||
if err != nil {
|
||||
return info, err
|
||||
}
|
||||
|
||||
currencyBalance = append(currencyBalance, exchange.AccountCurrencyInfo{
|
||||
CurrencyName: currency.NewCode(balance.Asset),
|
||||
CurrencyName: currency.NewCode(raw.Balances[i].Asset),
|
||||
TotalValue: freeCurrency + lockedCurrency,
|
||||
Hold: freeCurrency,
|
||||
})
|
||||
@@ -374,28 +389,24 @@ func (b *Binance) GetExchangeHistory(p currency.Pair, assetType asset.Item) ([]e
|
||||
}
|
||||
|
||||
// SubmitOrder submits a new order
|
||||
func (b *Binance) SubmitOrder(order *exchange.OrderSubmission) (exchange.SubmitOrderResponse, error) {
|
||||
var submitOrderResponse exchange.SubmitOrderResponse
|
||||
if order == nil {
|
||||
return submitOrderResponse, exchange.ErrOrderSubmissionIsNil
|
||||
}
|
||||
|
||||
if err := order.Validate(); err != nil {
|
||||
func (b *Binance) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
|
||||
var submitOrderResponse order.SubmitResponse
|
||||
if err := s.Validate(); err != nil {
|
||||
return submitOrderResponse, err
|
||||
}
|
||||
|
||||
var sideType string
|
||||
if order.OrderSide == exchange.BuyOrderSide {
|
||||
sideType = exchange.BuyOrderSide.ToString()
|
||||
if s.OrderSide == order.Buy {
|
||||
sideType = order.Buy.String()
|
||||
} else {
|
||||
sideType = exchange.SellOrderSide.ToString()
|
||||
sideType = order.Sell.String()
|
||||
}
|
||||
|
||||
var requestParamsOrderType RequestParamsOrderType
|
||||
switch order.OrderType {
|
||||
case exchange.MarketOrderType:
|
||||
switch s.OrderType {
|
||||
case order.Market:
|
||||
requestParamsOrderType = BinanceRequestParamsOrderMarket
|
||||
case exchange.LimitOrderType:
|
||||
case order.Limit:
|
||||
requestParamsOrderType = BinanceRequestParamsOrderLimit
|
||||
default:
|
||||
submitOrderResponse.IsOrderPlaced = false
|
||||
@@ -403,10 +414,10 @@ func (b *Binance) SubmitOrder(order *exchange.OrderSubmission) (exchange.SubmitO
|
||||
}
|
||||
|
||||
var orderRequest = NewOrderRequest{
|
||||
Symbol: order.Pair.Base.String() + order.Pair.Quote.String(),
|
||||
Symbol: s.Pair.Base.String() + s.Pair.Quote.String(),
|
||||
Side: sideType,
|
||||
Price: order.Price,
|
||||
Quantity: order.Amount,
|
||||
Price: s.Price,
|
||||
Quantity: s.Amount,
|
||||
TradeType: requestParamsOrderType,
|
||||
TimeInForce: BinanceRequestParamsTimeGTC,
|
||||
}
|
||||
@@ -414,7 +425,7 @@ func (b *Binance) SubmitOrder(order *exchange.OrderSubmission) (exchange.SubmitO
|
||||
response, err := b.NewOrder(&orderRequest)
|
||||
|
||||
if response.OrderID > 0 {
|
||||
submitOrderResponse.OrderID = fmt.Sprintf("%v", response.OrderID)
|
||||
submitOrderResponse.OrderID = strconv.FormatInt(response.OrderID, 10)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
@@ -426,27 +437,28 @@ func (b *Binance) SubmitOrder(order *exchange.OrderSubmission) (exchange.SubmitO
|
||||
|
||||
// ModifyOrder will allow of changing orderbook placement and limit to
|
||||
// market conversion
|
||||
func (b *Binance) ModifyOrder(action *exchange.ModifyOrder) (string, error) {
|
||||
func (b *Binance) ModifyOrder(action *order.Modify) (string, error) {
|
||||
return "", common.ErrFunctionNotSupported
|
||||
}
|
||||
|
||||
// CancelOrder cancels an order by its corresponding ID number
|
||||
func (b *Binance) CancelOrder(order *exchange.OrderCancellation) error {
|
||||
func (b *Binance) CancelOrder(order *order.Cancel) error {
|
||||
orderIDInt, err := strconv.ParseInt(order.OrderID, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = b.CancelExistingOrder(b.FormatExchangeCurrency(order.CurrencyPair,
|
||||
order.AssetType).String(), orderIDInt, order.AccountID)
|
||||
|
||||
order.AssetType).String(),
|
||||
orderIDInt,
|
||||
order.AccountID)
|
||||
return err
|
||||
}
|
||||
|
||||
// CancelAllOrders cancels all orders associated with a currency pair
|
||||
func (b *Binance) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) {
|
||||
cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{
|
||||
OrderStatus: make(map[string]string),
|
||||
func (b *Binance) CancelAllOrders(_ *order.Cancel) (order.CancelAllResponse, error) {
|
||||
cancelAllOrdersResponse := order.CancelAllResponse{
|
||||
Status: make(map[string]string),
|
||||
}
|
||||
openOrders, err := b.OpenOrders("")
|
||||
if err != nil {
|
||||
@@ -454,9 +466,11 @@ func (b *Binance) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Cance
|
||||
}
|
||||
|
||||
for i := range openOrders {
|
||||
_, err = b.CancelExistingOrder(openOrders[i].Symbol, openOrders[i].OrderID, "")
|
||||
_, err = b.CancelExistingOrder(openOrders[i].Symbol,
|
||||
openOrders[i].OrderID,
|
||||
"")
|
||||
if err != nil {
|
||||
cancelAllOrdersResponse.OrderStatus[strconv.FormatInt(openOrders[i].OrderID, 10)] = err.Error()
|
||||
cancelAllOrdersResponse.Status[strconv.FormatInt(openOrders[i].OrderID, 10)] = err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -464,8 +478,8 @@ func (b *Binance) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.Cance
|
||||
}
|
||||
|
||||
// GetOrderInfo returns information on a current open order
|
||||
func (b *Binance) GetOrderInfo(orderID string) (exchange.OrderDetail, error) {
|
||||
var orderDetail exchange.OrderDetail
|
||||
func (b *Binance) GetOrderInfo(orderID string) (order.Detail, error) {
|
||||
var orderDetail order.Detail
|
||||
return orderDetail, common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
@@ -511,85 +525,87 @@ func (b *Binance) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error)
|
||||
}
|
||||
|
||||
// GetActiveOrders retrieves any orders that are active/open
|
||||
func (b *Binance) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
|
||||
if len(getOrdersRequest.Currencies) == 0 {
|
||||
func (b *Binance) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
if len(req.Currencies) == 0 {
|
||||
return nil, errors.New("at least one currency is required to fetch order history")
|
||||
}
|
||||
|
||||
var orders []exchange.OrderDetail
|
||||
for _, c := range getOrdersRequest.Currencies {
|
||||
resp, err := b.OpenOrders(b.FormatExchangeCurrency(c,
|
||||
var orders []order.Detail
|
||||
for x := range req.Currencies {
|
||||
resp, err := b.OpenOrders(b.FormatExchangeCurrency(req.Currencies[x],
|
||||
asset.Spot).String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range resp {
|
||||
orderSide := exchange.OrderSide(strings.ToUpper(resp[i].Side))
|
||||
orderType := exchange.OrderType(strings.ToUpper(resp[i].Type))
|
||||
orderSide := order.Side(strings.ToUpper(resp[i].Side))
|
||||
orderType := order.Type(strings.ToUpper(resp[i].Type))
|
||||
orderDate := time.Unix(0, int64(resp[i].Time)*int64(time.Millisecond))
|
||||
|
||||
orders = append(orders, exchange.OrderDetail{
|
||||
orders = append(orders, order.Detail{
|
||||
Amount: resp[i].OrigQty,
|
||||
OrderDate: orderDate,
|
||||
Exchange: b.Name,
|
||||
ID: fmt.Sprintf("%v", resp[i].OrderID),
|
||||
ID: strconv.FormatInt(resp[i].OrderID, 10),
|
||||
OrderSide: orderSide,
|
||||
OrderType: orderType,
|
||||
Price: resp[i].Price,
|
||||
Status: resp[i].Status,
|
||||
Status: order.Status(resp[i].Status),
|
||||
CurrencyPair: currency.NewPairFromString(resp[i].Symbol),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
exchange.FilterOrdersByType(&orders, getOrdersRequest.OrderType)
|
||||
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
|
||||
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
|
||||
order.FilterOrdersByType(&orders, req.OrderType)
|
||||
order.FilterOrdersBySide(&orders, req.OrderSide)
|
||||
order.FilterOrdersByTickRange(&orders, req.StartTicks, req.EndTicks)
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
// GetOrderHistory retrieves account order information
|
||||
// Can Limit response to specific order status
|
||||
func (b *Binance) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
|
||||
if len(getOrdersRequest.Currencies) == 0 {
|
||||
func (b *Binance) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
||||
if len(req.Currencies) == 0 {
|
||||
return nil, errors.New("at least one currency is required to fetch order history")
|
||||
}
|
||||
|
||||
var orders []exchange.OrderDetail
|
||||
for _, c := range getOrdersRequest.Currencies {
|
||||
resp, err := b.AllOrders(b.FormatExchangeCurrency(c,
|
||||
asset.Spot).String(), "", "1000")
|
||||
var orders []order.Detail
|
||||
for x := range req.Currencies {
|
||||
resp, err := b.AllOrders(b.FormatExchangeCurrency(req.Currencies[x],
|
||||
asset.Spot).String(),
|
||||
"",
|
||||
"1000")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range resp {
|
||||
orderSide := exchange.OrderSide(strings.ToUpper(resp[i].Side))
|
||||
orderType := exchange.OrderType(strings.ToUpper(resp[i].Type))
|
||||
orderSide := order.Side(strings.ToUpper(resp[i].Side))
|
||||
orderType := order.Type(strings.ToUpper(resp[i].Type))
|
||||
orderDate := time.Unix(0, int64(resp[i].Time)*int64(time.Millisecond))
|
||||
// New orders are covered in GetOpenOrders
|
||||
if resp[i].Status == "NEW" {
|
||||
continue
|
||||
}
|
||||
|
||||
orders = append(orders, exchange.OrderDetail{
|
||||
orders = append(orders, order.Detail{
|
||||
Amount: resp[i].OrigQty,
|
||||
OrderDate: orderDate,
|
||||
Exchange: b.Name,
|
||||
ID: fmt.Sprintf("%v", resp[i].OrderID),
|
||||
ID: strconv.FormatInt(resp[i].OrderID, 10),
|
||||
OrderSide: orderSide,
|
||||
OrderType: orderType,
|
||||
Price: resp[i].Price,
|
||||
CurrencyPair: currency.NewPairFromString(resp[i].Symbol),
|
||||
Status: resp[i].Status,
|
||||
Status: order.Status(resp[i].Status),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
exchange.FilterOrdersByType(&orders, getOrdersRequest.OrderType)
|
||||
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
|
||||
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
|
||||
order.FilterOrdersByType(&orders, req.OrderType)
|
||||
order.FilterOrdersBySide(&orders, req.OrderSide)
|
||||
order.FilterOrdersByTickRange(&orders, req.StartTicks, req.EndTicks)
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user