forked from KillerInk/binance-java-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTotalAccountBalanceExample.java
executable file
·61 lines (47 loc) · 2.42 KB
/
TotalAccountBalanceExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.binance.api.examples;
import com.binance.api.client.api.sync.BinanceApiSpotRestClient;
import com.binance.api.client.constant.Util;
import com.binance.api.client.domain.account.Account;
import com.binance.api.client.domain.account.AssetBalance;
import com.binance.api.client.factory.BinanceAbstractFactory;
import com.binance.api.client.factory.BinanceSpotApiClientFactory;
/**
* Example how to get total of balances on your account
*/
public class TotalAccountBalanceExample {
public static void main(String[] args) {
BinanceSpotApiClientFactory factory = BinanceAbstractFactory.createSpotFactory("YOUR_API_KEY", "YOUR_SECRET");
BinanceApiSpotRestClient client = factory.newRestClient();
// Get account balances
Account account = client.getAccount(60_000L, System.currentTimeMillis());
// Get total account balance in BTC (spot only)
TotalAccountBalanceExample accountBalance = new TotalAccountBalanceExample();
double totalBalanceInBTC = accountBalance.getTotalAccountBalance(client, account);
System.out.println(totalBalanceInBTC);
// Get total account balance in USDT (spot only)
double totalBalanceInUSDT = totalBalanceInBTC * Double.parseDouble(client.getPrice("BTCUSDT").getPrice());
System.out.println(totalBalanceInUSDT);
}
// Get total account balance in BTC (spot only)
public double getTotalAccountBalance(BinanceApiSpotRestClient client, Account account) {
double totalBalance = 0;
for (AssetBalance balance : account.getBalances()) {
double free = Double.parseDouble(balance.getFree());
double locked = Double.parseDouble(balance.getLocked());
String ticker = balance.getAsset() + Util.BTC_TICKER;
String tickerReverse = Util.BTC_TICKER + balance.getAsset();
if (free + locked != 0) {
if (Util.isFiatCurrency(balance.getAsset())) {
double price = Double.parseDouble(client.getPrice(tickerReverse).getPrice());
double amount = (free + locked) / price;
totalBalance += amount;
} else {
double price = Double.parseDouble(client.getPrice(ticker).getPrice());
double amount = price * (free + locked);
totalBalance += amount;
}
}
}
return totalBalance;
}
}