Java Binance API

This project is designed to help you make your own projects that interact with the Binance API. You can stream candlestick chart data, market depth, or use other advanced features such as setting stop losses and iceberg orders.

Getting started

import com.binance.BinanceAPI;
import com.binance.dto.BinanceAccountDTO;
// Establish the BinanceAccount DTO
BinanceAccountDTO account = new BinanceAccountDTO("<APIKey>", "<secretKey>");
// Create instance of the api
BinanceAPI api = new BinanceAPI(account);

Getting latest price of a symbol

BinanceAPI.getLatestPrice("ETHBTC");
View Response
{"priceChange":"-0.00296400","priceChangePercent":"-4.839","weightedAvgPrice":"0.05985067","prevClosePrice":"0.06139300","lastPrice":"0.05828500","lastQty":"0.15400000","bidPrice":"0.05810000","bidQty":"1.05000000","askPrice":"0.05827800","askQty":"9.29000000","openPrice":"0.06124900","highPrice":"0.06216600","lowPrice":"0.05772100","volume":"31420.17400000","quoteVolume":"1880.51860017","openTime":1508124032499,"closeTime":1508210432499,"firstId":2112707,"lastId":2141239,"count":28529}

Getting depth of a symbol

BinanceAPI.getDepth("ETHBTC");
View Response
{"lastUpdateId":16734876,"bids":[["0.05819800","11.77000000",[]],["0.05811900","15.00000000",[]],["0.05799900","2.60100000",[]],["0.05797600","2.72900000",[]],["0.05790100","5.15600000",[]],["0.05790000","1.00000000",[]],["0.05788000","11.63000000",[]],["0.05787900","20.93200000",[]],["0.05787700","40.80700000",[]],["0.05787000","0.10000000",[]],["0.05779800","2.74200000",[]],["0.05761500","68.56800000",[]],["0.05761300","0.71800000",[]],["0.05758900","1.04200000",[]],["0.05758700","5.92700000",[]],["0.05750100","0.40000000",[]],["0.05750000","35.75500000",[]],["0.05741300","4.86500000",[]],["0.05735000","0.80200000",[]],["0.05732700","0.18700000",[]],["0.05725600","2.25000000",[]],["0.05716500","2.70100000",[]],["0.05710300","8.50000000",[]],["0.05700000","25.73700000",[]],["0.05689900","5.40100000",[]],["0.05686600","1.58300000",[]],["0.05670700","6.90100000",[]],["0.05667700","0.25000000",[]],["0.05663600","51.55600000",[]],["0.05661300","7.15100000",[]],["0.05655100","12.32000000",[]],["0.05650000","3.59700000",[]],["0.05648800","1.42300000",[]],["0.05636200","5.75000000",[]],["0.05626700","15.00000000",[]],["0.05619800","7.70100000",[]],["0.05609100","5.05000000",[]],["0.05605400","0.12000000",[]],["0.05600000","7.48200000",[]],["0.05586400","8.70000000",[]],["0.05565800","5.75000000",[]],["0.05552600","0.12000000",[]],["0.05551700","5.75000000",[]],["0.05550000","20.00000000",[]],["0.05533800","8.40100000",[]],["0.05524900","3.15000000",[]],["0.05500800","1.65000000",[]],["0.05500000","1.81800000",[]],["0.05482800","1.30100000",[]],["0.05475100","0.12000000",[]],["0.05473500","5.24900000",[]],["0.05462400","4.90100000",[]],["0.05459500","4.98900000",[]],["0.05444400","5.25000000",[]],["0.05426700","20.00000000",[]],["0.05422100","5.05000000",[]],["0.05403600","1.45000000",[]],["0.05400000","2.05100000",[]],["0.05383600","4.65100000",[]],["0.05375100","0.12000000",[]],["0.05362400","2.80000000",[]],["0.05349900","3.30000000",[]],["0.05336300","4.80000000",[]],["0.05319000","8.80100000",[]],["0.05310000","3.00000000",[]],["0.05306400","0.08600000",[]],["0.05302700","0.95000000",[]],["0.05300000","36.54300000",[]],["0.05283000","4.65100000",[]],["0.05268600","9.40100000",[]],["0.05251000","3.00000000",[]],["0.05250800","6.60000000",[]],["0.05234200","20.00000000",[]],["0.05200000","14.70100000",[]],["0.05154400","24.85500000",[]],["0.05152000","1.00000000",[]],["0.05151000","3.00000000",[]],["0.05150000","3.00000000",[]],["0.05127000","6.81500000",[]],["0.05111900","0.52200000",[]],["0.05108400","3.96200000",[]],["0.05108000","0.20000000",[]],["0.05100000","1.96000000",[]],["0.05030000","0.50000000",[]],["0.05000000","18.43100000",[]],["0.04900000","2.04000000",[]],["0.04851000","3.00000000",[]],["0.04651000","8.00000000",[]],["0.04451000","5.00000000",[]],["0.04251000","4.00000000",[]],["0.04014400","0.25200000",[]],["0.03800000","13.00000000",[]],["0.03720000","1.20000000",[]],["0.03161800","0.21000000",[]],["0.00499300","2737.95700000",[]],["0.00100000","10.00000000",[]],["0.00000100","21000.00000000",[]]],"asks":[["0.05833700","7.68000000",[]],["0.05833800","5.13000000",[]],["0.05834000","1.00000000",[]],["0.05835200","14.81000000",[]],["0.05836200","5.75000000",[]],["0.05836500","0.13000000",[]],["0.05847700","0.50000000",[]],["0.05849900","0.50000000",[]],["0.05854000","9.15100000",[]],["0.05857800","6.43900000",[]],["0.05858100","0.51200000",[]],["0.05858500","0.13400000",[]],["0.05864800","18.77700000",[]],["0.05867900","1.66600000",[]],["0.05870000","1.00000000",[]],["0.05873600","2.50000000",[]],["0.05881300","5.15500000",[]],["0.05881400","0.25000000",[]],["0.05881700","0.17000000",[]],["0.05891300","34.25700000",[]],["0.05892000","0.02500000",[]],["0.05892100","7.54800000",[]],["0.05900000","1.00000000",[]],["0.05911600","9.00000000",[]],["0.05918000","1.01400000",[]],["0.05929700","3.84700000",[]],["0.05948600","2.35100000",[]],["0.05959000","0.22100000",[]],["0.05961700","1.35100000",[]],["0.05970000","0.10000000",[]],["0.05971500","0.86300000",[]],["0.05972200","1.00000000",[]],["0.05978300","6.30000000",[]],["0.05980000","5.85900000",[]],["0.05992800","2.49600000",[]],["0.06000000","41.39400000",[]],["0.06009700","5.15100000",[]],["0.06010000","0.10000000",[]],["0.06012100","1.50000000",[]],["0.06012200","1.00000000",[]],["0.06021400","0.98000000",[]],["0.06030000","2.45100000",[]],["0.06030700","0.94600000",[]],["0.06035000","76.00000000",[]],["0.06041700","2.00000000",[]],["0.06050000","8.50000000",[]],["0.06050300","6.30000000",[]],["0.06055000","1.47600000",[]],["0.06060000","1.65700000",[]],["0.06069800","7.35000000",[]],["0.06078000","5.00000000",[]],["0.06083100","1.09800000",[]],["0.06088800","4.83700000",[]],["0.06091800","6.95100000",[]],["0.06100000","19.26300000",[]],["0.06100100","0.50000000",[]],["0.06100200","0.50000000",[]],["0.06100300","0.50000000",[]],["0.06100400","0.50000000",[]],["0.06100500","0.50000000",[]],["0.06100600","0.50000000",[]],["0.06100700","0.50000000",[]],["0.06100800","0.50000000",[]],["0.06100900","0.50000000",[]],["0.06101000","0.50000000",[]],["0.06101100","0.50000000",[]],["0.06101200","0.50000000",[]],["0.06101300","0.50000000",[]],["0.06101400","0.50000000",[]],["0.06101500","0.50000000",[]],["0.06101600","0.50000000",[]],["0.06101700","0.50000000",[]],["0.06101800","0.50000000",[]],["0.06101900","0.50000000",[]],["0.06102000","0.50000000",[]],["0.06102100","0.50000000",[]],["0.06102200","0.50000000",[]],["0.06102300","0.50000000",[]],["0.06102400","0.50000000",[]],["0.06102500","0.50000000",[]],["0.06102600","0.50000000",[]],["0.06102700","0.24000000",[]],["0.06110700","4.64100000",[]],["0.06120800","10.49000000",[]],["0.06121100","4.50000000",[]],["0.06123000","6.19300000",[]],["0.06131600","2.74900000",[]],["0.06145000","1.00000000",[]],["0.06150000","24.24000000",[]],["0.06152400","7.45100000",[]],["0.06155000","7.28100000",[]],["0.06160000","12.34000000",[]],["0.06166000","5.00000000",[]],["0.06167000","0.09900000",[]],["0.06170000","22.75100000",[]],["0.06176800","0.32500000",[]],["0.06180000","0.16000000",[]],["0.06181500","2.49000000",[]],["0.06188800","7.25000000",[]],["0.06189000","30.70000000",[]],["0.06190700","9.49800000",[]],["0.06192300","1.96900000",[]],["0.06193000","24.95800000",[]],["0.06193600","0.38500000",[]],["0.06197000","0.10000000",[]],["0.06199900","1.00000000",[]],["0.06200000","35.97700000",[]],["0.06210000","10.59100000",[]],["0.06218000","1.00000000",[]],["0.06219000","0.14600000",[]],["0.06224800","0.49800000",[]],["0.06240000","0.64000000",[]],["0.06246000","9.71700000",[]],["0.06248900","2.00000000",[]],["0.06250000","4.02000000",[]],["0.06257800","0.11600000",[]],["0.06260000","0.70300000",[]],["0.06269100","10.00000000",[]],["0.06270000","0.16000000",[]],["0.06300000","0.23000000",[]],["0.06307000","1.00000000",[]],["0.06310000","0.02000000",[]],["0.06320000","0.02000000",[]],["0.06330000","2.32500000",[]],["0.06339100","2.97700000",[]],["0.06340000","0.02000000",[]],["0.06341900","1.50100000",[]],["0.06349000","24.95700000",[]],["0.06350000","0.02000000",[]],["0.06360000","0.16000000",[]],["0.06365200","1.50100000",[]],["0.06390000","11.74700000",[]],["0.06396000","1.00000000",[]],["0.06399900","1.00000000",[]],["0.06400000","1.45300000",[]],["0.06420000","0.14000000",[]],["0.06446000","9.71700000",[]],["0.06484000","1.00000000",[]],["0.06500000","27.23100000",[]],["0.06526100","79.94900000",[]],["0.06566100","0.45900000",[]],["0.06569100","5.00000000",[]],["0.06573000","1.00000000",[]],["0.06646000","9.71700000",[]],["0.06661000","1.00000000",[]],["0.06668000","4.01800000",[]],["0.06690000","0.14000000",[]],["0.06695600","0.37500000",[]],["0.06699200","0.95100000",[]],["0.06720000","0.14000000",[]],["0.06750000","1.14000000",[]],["0.06754900","6.81000000",[]],["0.06780000","0.14000000",[]],["0.06800000","18.41400000",[]],["0.06839000","0.50000000",[]],["0.06846000","9.71800000",[]],["0.06882100","0.66200000",[]],["0.06887900","0.46200000",[]],["0.06900000","3.38100000",[]],["0.06930000","0.72200000",[]],["0.06960000","0.14000000",[]],["0.06970000","0.02000000",[]],["0.06980000","0.02000000",[]],["0.06990000","0.14000000",[]],["0.06999900","3.00000000",[]],["0.07000000","26.47900000",[]],["0.07010000","0.02000000",[]],["0.07020000","0.18000000",[]],["0.07030000","0.02000000",[]],["0.07037000","10.64900000",[]],["0.07040000","0.02000000",[]],["0.07043500","0.27200000",[]],["0.07050000","0.18000000",[]],["0.07070000","0.02000000",[]],["0.07080000","0.18000000",[]],["0.07100000","0.02000000",[]],["0.07110000","21.71500000",[]],["0.07111100","3.00000000",[]],["0.07111700","0.69500000",[]],["0.07112100","25.94400000",[]],["0.07112800","0.57800000",[]],["0.07120000","0.02000000",[]],["0.07130000","0.02000000",[]],["0.07137000","0.50400000",[]],["0.07140000","88.37900000",[]],["0.07150000","16.06500000",[]],["0.07197000","0.75700000",[]],["0.07199900","3.00000000",[]],["0.07200000","3.32700000",[]],["0.07210000","0.02000000",[]],["0.07218800","0.88800000",[]],["0.07220000","0.02000000",[]],["0.07230000","0.68700000",[]],["0.07297000","0.56900000",[]],["0.07300000","12.42500000",[]],["0.07337000","0.67200000",[]],["0.07410000","32.43900000",[]],["0.07417800","0.13000000",[]],["0.07499900","36.18000000",[]],["0.07500000","20.56600000",[]]]}

Placing a LIMIT order

api.buy("ETHBTC", BigDecimal.valueOf(10.5), BigDecimal.valueOf(0.0578), OrderType.LIMIT);
View Response
{
  "symbol":"LTCBTC",
  "orderId": 1,
  "clientOrderId": "myOrder1" // Will be newClientOrderId
  "transactTime": 1499827319559
}

Placing a MARKET order

api.buy("ETHBTC", BigDecimal.valueOf(10.5), OrderType.MARKET);
View Response
{
  "symbol":"LTCBTC",
  "orderId": 1,
  "clientOrderId": "myOrder1" // Will be newClientOrderId
  "transactTime": 1499827319559
}

Checking an order’s status (By Symbol)

api.checkOrderStat("ETHBTC");
View Response
{
  "symbol": "LTCBTC",
  "orderId": 1,
  "clientOrderId": "myOrder1",
  "price": "0.1",
  "origQty": "1.0",
  "executedQty": "0.0",
  "status": "NEW",
  "timeInForce": "GTC",
  "type": "LIMIT",
  "side": "BUY",
  "stopPrice": "0.0",
  "icebergQty": "0.0",
  "time": 1499827319559
}

Checking an order’s status (By Symbol and orderID)

long orderid = 7610385;
api.checkOrderStat("ETHBTC", orderid);
View Response
{
  "symbol": "LTCBTC",
  "orderId": 1,
  "clientOrderId": "myOrder1",
  "price": "0.1",
  "origQty": "1.0",
  "executedQty": "0.0",
  "status": "NEW",
  "timeInForce": "GTC",
  "type": "LIMIT",
  "side": "BUY",
  "stopPrice": "0.0",
  "icebergQty": "0.0",
  "time": 1499827319559
}

Cancelling an order (By Symbol)

api.cancelOrder("ETHBTC");
View Response
{
  "symbol": "LTCBTC",
  "origClientOrderId": "myOrder1",
  "orderId": 1,
  "clientOrderId": "cancelMyOrder1"
}

Cancelling an order (By Symbol and orderID)

orderid = 7610385;
api.cancelOrder("ETHBTC", orderid);
View Response
{
  "symbol": "LTCBTC",
  "origClientOrderId": "myOrder1",
  "orderId": 1,
  "clientOrderId": "cancelMyOrder1"
}

Getting list of open orders

api.getOpenOrders("ETHBTC");
View Response
[
  {
    "symbol": "LTCBTC",
    "orderId": 1,
    "clientOrderId": "myOrder1",
    "price": "0.1",
    "origQty": "1.0",
    "executedQty": "0.0",
    "status": "NEW",
    "timeInForce": "GTC",
    "type": "LIMIT",
    "side": "BUY",
    "stopPrice": "0.0",
    "icebergQty": "0.0",
    "time": 1499827319559
  }
]

Getting list of current position

api.getCurrentPositions();
View Response
{
  "makerCommission": 15,
  "takerCommission": 15,
  "buyerCommission": 0,
  "sellerCommission": 0,
  "canTrade": true,
  "canWithdraw": true,
  "canDeposit": true,
  "balances": [
    {
      "asset": "BTC",
      "free": "4723846.89208129",
      "locked": "0.00000000"
    },
    {
      "asset": "LTC",
      "free": "4763368.68006011",
      "locked": "0.00000000"
    }
  ]
}