use of org.springframework.boot.ApplicationArguments in project spring-native by spring-projects-experimental.
the class BuildTimeTestSpringApplication method run.
@Override
public GenericApplicationContext run(String... args) {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
GenericApplicationContext context = createContext();
context.setEnvironment(prepareEnvironment());
prepareContext(context, applicationArguments);
return context;
}
use of org.springframework.boot.ApplicationArguments in project bitcoin-spring-boot-starter by theborakompanioni.
the class KrakenBalanceCommandRunner method doRun.
@Override
@SneakyThrows
protected void doRun(ApplicationArguments args) {
log.debug("Fetch balance on exchange {}", exchange);
KrakenAccountService accountService = (KrakenAccountService) exchange.getAccountService();
Map<String, BigDecimal> krakenBalance = accountService.getKrakenBalance();
Map<String, BigDecimal> krakenPositiveBalances = krakenBalance.entrySet().stream().filter(it -> it.getValue().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (krakenPositiveBalances.isEmpty()) {
System.out.println("❌ There is no currency pair with a positive balance.");
} else {
krakenPositiveBalances.forEach((currencyCode, balance) -> {
System.out.printf("💰 %5s:\t%s%n", currencyCode, balance.toPlainString());
});
}
}
use of org.springframework.boot.ApplicationArguments in project bitcoin-spring-boot-starter by theborakompanioni.
the class KrakenRebalanceCommandRunner method doRun.
@Override
@SneakyThrows({ IllegalStateException.class, IOException.class })
protected void doRun(ApplicationArguments args) {
log.debug("Calculate rebalance on exchange {}", exchange);
if (!dryRun.isEnabled()) {
throw new IllegalStateException("Currently only implemented with '--dry-run' option");
}
BigDecimal bitcoinTargetPercentage = Optional.ofNullable(args.getOptionValues("target")).flatMap(it -> it.stream().findFirst()).map(BigDecimal::new).orElse(defaultBitcoinTargetPercentage);
// target percentage must be between 0 and 1
boolean targetParamIsWithinBounds = bitcoinTargetPercentage.compareTo(BigDecimal.ZERO) >= 0 && bitcoinTargetPercentage.compareTo(BigDecimal.ONE) <= 0;
if (!targetParamIsWithinBounds) {
throw new IllegalStateException("Invalid target percentage param: " + bitcoinTargetPercentage);
}
Currency bitcoin = Currency.BTC;
Currency fiatCurrency = Currency.getInstance(properties.getFiatCurrency());
CurrencyPair currencyPair = new CurrencyPair(bitcoin, fiatCurrency);
boolean supportedCurrencyPair = exchange.getExchangeSymbols().contains(currencyPair);
if (!supportedCurrencyPair) {
throw new IllegalStateException("Currency pair is not supported: " + currencyPair);
}
// ---------------------------------------------- balance
KrakenAccountService accountService = (KrakenAccountService) exchange.getAccountService();
Map<String, BigDecimal> krakenBalance = accountService.getKrakenBalance();
Map<String, BigDecimal> krakenPositiveBalances = krakenBalance.entrySet().stream().filter(it -> it.getValue().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (krakenPositiveBalances.isEmpty()) {
System.out.println("❌ There is no currency pair with a positive balance.");
return;
}
BigDecimal bitcoinBalanceValue = bitcoin.getCurrencyCodes().stream().filter(it -> krakenBalance.containsKey("X" + it)).map(it -> krakenBalance.get("X" + it)).findFirst().or(() -> Optional.ofNullable(krakenBalance.get(bitcoin.getCurrencyCode()))).orElse(BigDecimal.ZERO);
Money bitcoinBalance = Money.of(bitcoinBalanceValue, bitcoinCurrencyUnit);
BigDecimal fiatBalanceValue = Optional.ofNullable(krakenBalance.get("Z" + fiatCurrency.getCurrencyCode())).or(() -> Optional.ofNullable(krakenBalance.get(fiatCurrency.getCurrencyCode()))).orElse(BigDecimal.ZERO);
Money fiatBalance = Money.of(fiatBalanceValue, fiatCurrency.getCurrencyCode());
// ---------------------------------------------- balance - end
// ---------------------------------------------- ask/bid
KrakenMarketDataService marketDataService = (KrakenMarketDataService) exchange.getMarketDataService();
Ticker ticker = marketDataService.getTicker(currencyPair);
// ---------------------------------------------- ask/bid - end
Money oneBitcoin = Money.of(BigDecimal.ONE, "BTC");
Money oneBitcoinInFiat = Money.of(ticker.getAsk(), fiatCurrency.getCurrencyCode());
System.out.printf("📎 %s = %s%n", moneyFormat.format(oneBitcoin), moneyFormat.format(oneBitcoinInFiat));
Money bitcoinInFiat = oneBitcoinInFiat.multiply(bitcoinBalance.getNumber());
Money totalInFiat = fiatBalance.add(bitcoinInFiat);
Money fiatInBitcoin = Money.of(fiatBalance.divide(oneBitcoinInFiat.getNumber()).getNumber(), bitcoinCurrencyUnit);
Money totalInBitcoin = bitcoinBalance.add(fiatInBitcoin);
BigDecimal percentageOfBalanceInBtc = bitcoinBalance.getNumberStripped().divide(totalInBitcoin.getNumberStripped(), RoundingMode.HALF_UP).setScale(4, RoundingMode.HALF_UP).movePointRight(2);
System.out.printf("💰 Balance BTC (%s%%): %s (%s)%n", percentageOfBalanceInBtc, moneyFormat.format(bitcoinBalance), moneyFormat.format(bitcoinInFiat));
BigDecimal percentageOfBalanceInFiat = fiatInBitcoin.getNumberStripped().divide(totalInBitcoin.getNumberStripped(), RoundingMode.HALF_UP).setScale(4, RoundingMode.HALF_UP).movePointRight(2);
System.out.printf("💰 Balance FIAT (%s%%): %s (%s)%n", percentageOfBalanceInFiat, moneyFormat.format(fiatBalance), moneyFormat.format(fiatInBitcoin));
System.out.printf(" (_Hypothetical_ Total Balances: %s or %s)%n", moneyFormat.format(totalInBitcoin), moneyFormat.format(totalInFiat));
Money bitcoinTargetBalance = totalInBitcoin.multiply(bitcoinTargetPercentage);
System.out.printf("📎 Target Balance BTC (%s%%): %s%n", bitcoinTargetPercentage.multiply(BigDecimal.valueOf(100)), moneyFormat.format(bitcoinTargetBalance));
Money missingBitcoin = bitcoinTargetBalance.subtract(bitcoinBalance);
Money missingBitcoinInFiat = oneBitcoinInFiat.multiply(missingBitcoin.getNumber());
MonetaryAmount source = missingBitcoinInFiat;
MonetaryAmount target = missingBitcoin;
boolean buyBitcoin = missingBitcoin.isPositive();
if (!buyBitcoin) {
MonetaryAmount tmp = source.abs();
source = target.abs();
target = tmp;
}
System.out.printf("📈 You should use %s to get %s%n", moneyFormat.format(source), moneyFormat.format(target));
}
use of org.springframework.boot.ApplicationArguments in project bookmark by FleyX.
the class MqConfiguration method run.
@Override
public void run(ApplicationArguments args) {
Map<String, Object> map = context.getBeansWithAnnotation(MqConsumer.class);
map.values().forEach(item -> {
if (!(item instanceof RedisConsumer)) {
log.warn("注意检测到被@EsConsumer注解的类{}未实现RedisConsumer接口", item.getClass().getCanonicalName());
return;
}
MqConsumer[] annotations = item.getClass().getAnnotationsByType(MqConsumer.class);
MqConsumer annotation = annotations[0];
topicMap.computeIfAbsent(annotation.value(), k -> new ArrayList<>()).add((RedisConsumer) item);
});
log.info("redis订阅信息汇总完毕!!!!!!");
// 由一个线程始终循环获取es队列数据
threadPoolExecutor.execute(loop());
}
use of org.springframework.boot.ApplicationArguments in project j-jdbc by jingshouyan.
the class DataInitAutoConfiguration method run.
@Override
public void run(ApplicationArguments args) {
Map<String, VersionHandler> map = ctx.getBeansOfType(VersionHandler.class);
List<VersionHandler> handlers = Lists.newArrayList(map.values());
Collections.sort(handlers);
String latest = versionDao.latestVersion().map(DataInitVersion::getVersion).orElse("");
handlers.stream().filter(h -> isNew(h.version(), latest)).forEach(h -> {
DataInitVersion version = new DataInitVersion();
version.setVersion(h.version());
version.setClazz(h.getClass().getName());
versionDao.insert(version);
try {
h.action();
version.setSuccess(true);
versionDao.update(version);
} catch (Throwable e) {
version.setSuccess(false);
version.setMessage(e.getMessage());
version.forDelete();
versionDao.update(version);
throw e;
}
});
}
Aggregations