Search in sources :

Example 96 with Balance

use of org.knowm.xchange.dto.account.Balance in project XChange by knowm.

the class PoloniexAdapters method adaptPoloniexBalances.

public static List<Balance> adaptPoloniexBalances(HashMap<String, PoloniexBalance> poloniexBalances) {
    List<Balance> balances = new ArrayList<>();
    for (Map.Entry<String, PoloniexBalance> item : poloniexBalances.entrySet()) {
        Currency currency = Currency.getInstance(item.getKey());
        balances.add(new Balance(currency, null, item.getValue().getAvailable(), item.getValue().getOnOrders()));
    }
    return balances;
}
Also used : PoloniexBalance(org.knowm.xchange.poloniex.dto.account.PoloniexBalance) Currency(org.knowm.xchange.currency.Currency) ArrayList(java.util.ArrayList) Balance(org.knowm.xchange.dto.account.Balance) PoloniexBalance(org.knowm.xchange.poloniex.dto.account.PoloniexBalance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 97 with Balance

use of org.knowm.xchange.dto.account.Balance in project XChange by knowm.

the class PaymiumAdapters method adaptWallet.

public static Wallet adaptWallet(PaymiumBalance paymiumBalances) {
    List<Balance> wallets = new ArrayList<>();
    wallets.add(new Balance(Currency.BTC, paymiumBalances.getBalanceBtc(), paymiumBalances.getBalanceBtc().subtract(paymiumBalances.getLockedBtc()), paymiumBalances.getLockedBtc()));
    wallets.add(new Balance(Currency.EUR, paymiumBalances.getBalanceEur(), paymiumBalances.getBalanceEur().subtract(paymiumBalances.getLockedEur()), paymiumBalances.getLockedEur()));
    return Wallet.Builder.from(wallets).build();
}
Also used : ArrayList(java.util.ArrayList) PaymiumBalance(org.knowm.xchange.paymium.dto.account.PaymiumBalance) Balance(org.knowm.xchange.dto.account.Balance)

Example 98 with Balance

use of org.knowm.xchange.dto.account.Balance in project XChange by knowm.

the class QuoineAdapters method adaptWallet.

public static Wallet adaptWallet(QuoineAccountInfo quoineWallet) {
    List<Balance> balances = new ArrayList<>();
    // Adapt to XChange DTOs
    Balance btcBalance = new Balance(Currency.getInstance(quoineWallet.getBitcoinAccount().getCurrency()), quoineWallet.getBitcoinAccount().getBalance(), quoineWallet.getBitcoinAccount().getFreeBalance());
    balances.add(btcBalance);
    for (FiatAccount fiatAccount : quoineWallet.getFiatAccounts()) {
        Balance fiatBalance = new Balance(Currency.getInstance(fiatAccount.getCurrency()), fiatAccount.getBalance(), fiatAccount.getBalance());
        balances.add(fiatBalance);
    }
    return Wallet.Builder.from(balances).build();
}
Also used : ArrayList(java.util.ArrayList) Balance(org.knowm.xchange.dto.account.Balance) FiatAccount(org.knowm.xchange.quoine.dto.account.FiatAccount)

Example 99 with Balance

use of org.knowm.xchange.dto.account.Balance in project cassandre-trading-bot by cassandre-tech.

the class UserServiceDryModeAOP method addToBalance.

/**
 * Update balance of trade account (method called by trade service).
 *
 * @param strategy strategy
 * @param currency currency
 * @param amount   amount
 */
public void addToBalance(final GenericCassandreStrategy strategy, final Currency currency, final BigDecimal amount) {
    final Optional<AccountDTO> tradeAccount = strategy.getTradeAccount();
    if (tradeAccount.isEmpty()) {
        logger.error("Trading account not found!");
    } else {
        // We build a new account information from what we saved.
        Collection<Wallet> wallets = new LinkedHashSet<>();
        // We retreat all the wallets we have.
        accountInfo.getWallets().forEach((name, wallet) -> {
            HashMap<Currency, Balance> balances = new LinkedHashMap<>();
            // For each balance, we add it if nothing changed or, if on trading account, and we need to change the amount,
            // Then we do it.
            wallet.getBalances().forEach((balanceCurrency, balance) -> {
                if (name.equals(tradeAccount.get().getName()) && balanceCurrency.equals(currency)) {
                    // If we are on the account and currency to update, we calculate the new value.
                    balances.put(balanceCurrency, new Balance(balanceCurrency, balance.getTotal().add(amount)));
                } else {
                    // Else we keep the same value.
                    balances.put(balanceCurrency, balance);
                }
            });
            // amounts, then we create a new balance.
            if (name.equals(tradeAccount.get().getName()) && balances.get(currency) == null) {
                balances.put(currency, new Balance(currency, amount));
            }
            // We add the wallet.
            wallets.add(new Wallet(name, name, balances.values(), Collections.emptySet(), ZERO, ZERO));
        });
        // Creates the account info.
        accountInfo = new AccountInfo(USER_ID, wallets);
        // Updates all strategies.
        final UserDTO userDTO = ACCOUNT_MAPPER.mapToUserDTO(accountInfo);
        applicationContext.getBeansWithAnnotation(CassandreStrategy.class).values().stream().map(o -> (CassandreStrategyInterface) o).forEach(cassandreStrategyInterface -> cassandreStrategyInterface.initializeAccounts(userDTO.getAccounts()));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Arrays(java.util.Arrays) Scanner(java.util.Scanner) HashMap(java.util.HashMap) Balance(org.knowm.xchange.dto.account.Balance) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) LinkedHashMap(java.util.LinkedHashMap) BigDecimal(java.math.BigDecimal) ConditionalOnExpression(org.springframework.boot.autoconfigure.condition.ConditionalOnExpression) Aspect(org.aspectj.lang.annotation.Aspect) LinkedHashSet(java.util.LinkedHashSet) Resource(org.springframework.core.io.Resource) AccountInfo(org.knowm.xchange.dto.account.AccountInfo) Wallet(org.knowm.xchange.dto.account.Wallet) AccountDTO(tech.cassandre.trading.bot.dto.user.AccountDTO) Collection(java.util.Collection) ZERO(java.math.BigDecimal.ZERO) IOException(java.io.IOException) ApplicationContext(org.springframework.context.ApplicationContext) Around(org.aspectj.lang.annotation.Around) FileNotFoundException(java.io.FileNotFoundException) UserDTO(tech.cassandre.trading.bot.dto.user.UserDTO) Currency(org.knowm.xchange.currency.Currency) Component(org.springframework.stereotype.Component) List(java.util.List) GenericCassandreStrategy(tech.cassandre.trading.bot.strategy.GenericCassandreStrategy) BaseService(tech.cassandre.trading.bot.util.base.service.BaseService) CassandreStrategy(tech.cassandre.trading.bot.strategy.CassandreStrategy) Optional(java.util.Optional) CassandreStrategyInterface(tech.cassandre.trading.bot.strategy.CassandreStrategyInterface) Collections(java.util.Collections) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Wallet(org.knowm.xchange.dto.account.Wallet) UserDTO(tech.cassandre.trading.bot.dto.user.UserDTO) CassandreStrategyInterface(tech.cassandre.trading.bot.strategy.CassandreStrategyInterface) LinkedHashMap(java.util.LinkedHashMap) Currency(org.knowm.xchange.currency.Currency) AccountDTO(tech.cassandre.trading.bot.dto.user.AccountDTO) Balance(org.knowm.xchange.dto.account.Balance) AccountInfo(org.knowm.xchange.dto.account.AccountInfo)

Aggregations

Balance (org.knowm.xchange.dto.account.Balance)99 BigDecimal (java.math.BigDecimal)51 ArrayList (java.util.ArrayList)50 AccountInfo (org.knowm.xchange.dto.account.AccountInfo)50 Wallet (org.knowm.xchange.dto.account.Wallet)38 Currency (org.knowm.xchange.currency.Currency)37 Test (org.junit.Test)23 Map (java.util.Map)14 Date (java.util.Date)13 HashMap (java.util.HashMap)10 List (java.util.List)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)5 Ticker (org.knowm.xchange.dto.marketdata.Ticker)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Arrays (java.util.Arrays)4 HashSet (java.util.HashSet)4 Collection (java.util.Collection)3