Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit 8aebad5

Browse files
authored
Merge pull request #246 from puzatin/TotalAccountBalanceExample
Add TotalAccountBalanceExample.
2 parents 9e6cfca + 4c3e521 commit 8aebad5

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.binance.api.client.constant;
2+
3+
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
/**
9+
* Utility class
10+
*/
11+
public final class Util {
12+
13+
/**
14+
* List of fiat currencies.
15+
*/
16+
public static final List<String> FIAT_CURRENCY = Collections.unmodifiableList(Arrays.asList("USDT", "BUSD", "PAX", "TUSD", "USDC", "NGN", "RUB", "USDS, TRY"));
17+
18+
public static final String BTC_TICKER = "BTC";
19+
20+
private Util() {
21+
22+
}
23+
24+
/**
25+
* Check if the ticker is fiat currency.
26+
*/
27+
public static boolean isFiatCurrency(String symbol) {
28+
for (String fiat : FIAT_CURRENCY) {
29+
if (symbol.equals(fiat)) return true;
30+
}
31+
return false;
32+
}
33+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.binance.api.examples;
2+
3+
import com.binance.api.client.BinanceApiClientFactory;
4+
import com.binance.api.client.BinanceApiRestClient;
5+
import com.binance.api.client.constant.Util;
6+
import com.binance.api.client.domain.account.Account;
7+
import com.binance.api.client.domain.account.AssetBalance;
8+
9+
/**
10+
* Example how to get total of balances on your account
11+
*/
12+
public class TotalAccountBalanceExample {
13+
14+
15+
public static void main(String[] args) {
16+
BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_SECRET");
17+
BinanceApiRestClient client = factory.newRestClient();
18+
19+
// Get account balances
20+
Account account = client.getAccount(60_000L, System.currentTimeMillis());
21+
22+
// Get total account balance in BTC (spot only)
23+
TotalAccountBalanceExample accountBalance = new TotalAccountBalanceExample();
24+
double totalBalanceInBTC = accountBalance.getTotalAccountBalance(client,account);
25+
System.out.println(totalBalanceInBTC);
26+
// Get total account balance in USDT (spot only)
27+
double totalBalanceInUSDT = totalBalanceInBTC * Double.parseDouble(client.getPrice("BTCUSDT").getPrice());
28+
System.out.println(totalBalanceInUSDT);
29+
30+
31+
32+
33+
}
34+
35+
// Get total account balance in BTC (spot only)
36+
public double getTotalAccountBalance(BinanceApiRestClient client, Account account) {
37+
double totalBalance = 0;
38+
for (AssetBalance balance : account.getBalances()) {
39+
double free = Double.parseDouble(balance.getFree());
40+
double locked = Double.parseDouble(balance.getLocked());
41+
String ticker = balance.getAsset() + Util.BTC_TICKER;
42+
String tickerReverse = Util.BTC_TICKER + balance.getAsset();
43+
if (free + locked != 0) {
44+
if (Util.isFiatCurrency(balance.getAsset())) {
45+
double price = Double.parseDouble(client.getPrice(tickerReverse).getPrice());
46+
double amount = (free + locked) / price;
47+
totalBalance += amount;
48+
} else {
49+
double price = Double.parseDouble(client.getPrice(ticker).getPrice());
50+
double amount = price * (free + locked);
51+
totalBalance += amount;
52+
}
53+
54+
}
55+
}
56+
57+
return totalBalance;
58+
59+
}
60+
61+
62+
63+
}

0 commit comments

Comments
 (0)