binance-exchange / binance-java-api

binance-java-api is a lightweight Java library for the Binance API, supporting synchronous and asynchronous requests, as well as event streaming using WebSockets.
MIT License
830 stars 622 forks source link

New Market Order - Buy using QuoteOrderQuantity instead of Quantity #322

Closed ndrb closed 3 years ago

ndrb commented 3 years ago

Hello, I saw the code used to make a new Market Order:

NewOrderResponse newOrderResponse = client.newOrder(marketBuy("LINKETH","1000").orderRespType(OrderResponseType.FULL));
List<Trade> fills = newOrderResponse.getFills();
System.out.println(newOrderResponse.getClientOrderId());

This takes the quantity of the crypto that I want to buy. I was hoping to do market orders using a quote order quantity so I can give it the amount of USDT I want to buy with and for it to return the total final value that it bought. It is easy to do this calculation myself but I worry about market volatility and by diving USDT by BTC/ETH/BNB I will end up with an outdated value.

Thank you kindly for your time!

ndrb commented 3 years ago

I finally managed to figure it out. In the NewOrder.java class, there is the marketBuy method, I overwrote the method to use the quantity value as quoteOrderQuantity since NewOrder object already has a field for quoteOrderQuantity. This is the new method:

  public static NewOrder marketBuy(String symbol, String quantity) {
    //return new NewOrder(symbol, OrderSide.BUY, OrderType.MARKET, null, quantity);
    return new NewOrder(symbol, OrderSide.BUY, OrderType.MARKET, null, null,null, quantity);
  }
Angus-Less commented 3 years ago

I finally managed to figure it out. In the NewOrder.java class, there is the marketBuy method, I overwrote the method to use the quantity value as quoteOrderQuantity since NewOrder object already has a field for quoteOrderQuantity. This is the new method:

  public static NewOrder marketBuy(String symbol, String quantity) {
    //return new NewOrder(symbol, OrderSide.BUY, OrderType.MARKET, null, quantity);
    return new NewOrder(symbol, OrderSide.BUY, OrderType.MARKET, null, null,null, quantity);
  }

Thanks for actually providing a response and not just claiming you fixed it. There are some ambiguities about how Binance states its order system. Had to learn it the hard way that you have to specify the quantity of the asset, not just the flat USDT you'd like to buy. Would be nicer if they reverted it to the USDT equivalent (or provided another method of making orders this way).

ndrb commented 3 years ago

@Angus-Less Yeah it would be a lot clearer with different methods with descriptive names that way the people using the API have options to chose from. I also learned it the hard way by hitting the API and reading the output in my order history. Their test-net also has some differing functionality which I found really hard, it was a while ago so I can't recall but certain tests where failing on test-net but passing on the real API