adshao / go-binance

A Go SDK for Binance API
MIT License
1.54k stars 677 forks source link

<APIError> code=-2010, msg=Account has insufficient balance for requested action. #474

Closed knkrth closed 1 year ago

knkrth commented 1 year ago

Hi, I'm unable to place a buy order using,

// Sell USDT to get BNB
    buy := func() {
        // Place a new market buy order with the maximum quantity of the quote asset
        order, err := client.NewCreateOrderService().Symbol(BNBUSDT).
            Side(binance.SideTypeBuy).Type(binance.OrderTypeMarket).
            Quantity("50").
            Do(context.Background())
        if err != nil {
            log.Println(err)
        }
        fmt.Printf("Order created: %+v\n", order)
    }

I have 100 USDT in my account but still I get <APIError> code=-2010, msg=Account has insufficient balance for requested action. While placing a buy order but sell order works fine.

Debug log

Binance-golang 2023/03/14 11:07:08 full url: https://api.binance.com/api/v3/order?timestamp=1678792028517&signature=aca2df6808141d0e659dc5e995c6e66485e233e5a027eb7815c45f3f16fca5b8, body: quantity=50&side=BUY&symbol=BNBUSDT&type=MARKET
Binance-golang 2023/03/14 11:07:08 request: &http.Request{Method:"POST", URL:(*url.URL)(0xc0000dc360), Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Content-Type":[]string{"application/x-www-form-urlencoded"}, "X-Mbx-Apikey":[]string{"zcoYITDm5nEpmxvNRpb6LYmsZZWFZCs2bukRBEoq5Ty16zVY3N6XlC9AM93eqqrL"}}, Body:io.nopCloser{Reader:(*bytes.Buffer)(0xc00043df50)}, GetBody:(func() (io.ReadCloser, error))(0x5fc460), ContentLength:47, TransferEncoding:[]string(nil), Close:false, Host:"api.binance.com", Form:url.Values(nil), PostForm:url.Values(nil), MultipartForm:(*multipart.Form)(nil), Trailer:http.Header(nil), RemoteAddr:"", RequestURI:"", TLS:(*tls.ConnectionState)(nil), Cancel:(<-chan struct {})(nil), Response:(*http.Response)(nil), ctx:(*context.emptyCtx)(0xc000014078)}
Binance-golang 2023/03/14 11:07:08 response: &http.Response{Status:"400 Bad Request", StatusCode:400, Proto:"HTTP/2.0", ProtoMajor:2, ProtoMinor:0, Header:http.Header{"Cache-Control":[]string{"no-cache, no-store, must-revalidate"}, "Content-Length":[]string{"77"}, "Content-Security-Policy":[]string{"default-src 'self'"}, "Content-Type":[]string{"application/json;charset=UTF-8"}, "Date":[]string{"Tue, 14 Mar 2023 11:07:08 GMT"}, "Expires":[]string{"0"}, "Pragma":[]string{"no-cache"}, "Server":[]string{"nginx"}, "Strict-Transport-Security":[]string{"max-age=31536000; includeSubdomains"}, "Via":[]string{"1.1 eeb2f3ca588ea4437f4b97ed276a6664.cloudfront.net (CloudFront)"}, "X-Amz-Cf-Id":[]string{"-xvwokCwPaLYHJfd-M20l5YnNEesiNIQQrahVQsrqYHNvTIT8x_DtA=="}, "X-Amz-Cf-Pop":[]string{"FRA56-P4"}, "X-Cache":[]string{"Error from cloudfront"}, "X-Content-Security-Policy":[]string{"default-src 'self'"}, "X-Content-Type-Options":[]string{"nosniff"}, "X-Frame-Options":[]string{"SAMEORIGIN"}, "X-Mbx-Order-Count-10s":[]string{"1"}, "X-Mbx-Order-Count-1d":[]string{"27"}, "X-Mbx-Used-Weight":[]string{"11"}, "X-Mbx-Used-Weight-1m":[]string{"11"}, "X-Mbx-Uuid":[]string{"7d691642-bc74-49b6-8ac9-694a45b3341b"}, "X-Webkit-Csp":[]string{"default-src 'self'"}, "X-Xss-Protection":[]string{"1; mode=block"}}, Body:http.http2transportResponseBody{cs:(*http.http2clientStream)(0xc0000e2900)}, ContentLength:77, TransferEncoding:[]string(nil), Close:false, Uncompressed:false, Trailer:http.Header(nil), Request:(*http.Request)(0xc0000de500), TLS:(*tls.ConnectionState)(0xc0000b0420)}
Binance-golang 2023/03/14 11:07:08 response body: {"code":-2010,"msg":"Account has insufficient balance for requested action."}
Binance-golang 2023/03/14 11:07:08 response status code: 400
2023/03/14 11:07:08 <APIError> code=-2010, msg=Account has insufficient balance for requested action.
DaniilSokolyuk commented 1 year ago

you are trying to buy 50 bnb

knkrth commented 1 year ago

Fixed it by placing QuoteOrderQty(fmt.Sprintf("%f", maxUSDTQuantity)). or QuoteOrderQty("50"). instead of Quantity("50").