use of tech.cassandre.trading.bot.strategy.internal.CassandreStrategy 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 CassandreStrategy 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(tech.cassandre.trading.bot.strategy.CassandreStrategy.class).values().stream().map(o -> (CassandreStrategyInterface) o).forEach(cassandreStrategyInterface -> cassandreStrategyInterface.initializeAccounts(userDTO.getAccounts()));
}
}
Aggregations