use of org.knowm.xchange.kraken.service.KrakenTradeService in project XChange by knowm.
the class KrakenExchange method initServices.
@Override
protected void initServices() {
this.marketDataService = new KrakenMarketDataService(this);
this.tradeService = new KrakenTradeService(this);
this.accountService = new KrakenAccountService(this);
}
use of org.knowm.xchange.kraken.service.KrakenTradeService in project bitcoin-spring-boot-starter by theborakompanioni.
the class KrakenHistoryCommandRunner method doRun.
@Override
@SneakyThrows
protected void doRun(ApplicationArguments args) {
log.info("Show history from exchange {}", exchange);
KrakenTradeService tradeService = (KrakenTradeService) exchange.getTradeService();
Map<String, KrakenOrder> openOrders = tradeService.getKrakenOpenOrders();
System.out.printf("======================================%n");
System.out.printf("🐇 Open Orders: %d %n", openOrders.size());
System.out.printf("--------------------------------------%n");
openOrders.forEach((key, val) -> {
System.out.printf("💸 %s: %-4s\t%12s\t(%5s%%)\t@ %-9s\t%8s\t%3s%n", key, val.getOrderDescription().getType(), val.getVolume().toPlainString(), val.getVolumeExecuted().multiply(BigDecimal.valueOf(100L)).divide(val.getVolume(), RoundingMode.HALF_DOWN).setScale(1, RoundingMode.HALF_DOWN).toPlainString(), val.getOrderDescription().getOrderType(), val.getOrderDescription().getPrice().toPlainString(), val.getOrderDescription().getAssetPair());
});
Map<String, KrakenOrder> closedOrders = tradeService.getKrakenClosedOrders();
System.out.printf("======================================%n");
System.out.printf("🦔 Closed Orders: %d %n", closedOrders.size());
System.out.printf("--------------------------------------%n");
closedOrders.forEach((key, val) -> {
System.out.printf("💸 %s: %-8s\t%-4s\t%12s\t(%5s%%)\t@ %-9s\t%8s\t%3s%n", key, val.getStatus(), val.getOrderDescription().getType(), val.getVolume().toPlainString(), val.getVolumeExecuted().multiply(BigDecimal.valueOf(100L)).divide(val.getVolume(), RoundingMode.HALF_DOWN).setScale(1, RoundingMode.HALF_DOWN).toPlainString(), val.getOrderDescription().getOrderType(), val.getPrice().toPlainString(), val.getOrderDescription().getAssetPair());
});
}
use of org.knowm.xchange.kraken.service.KrakenTradeService in project bitcoin-spring-boot-starter by theborakompanioni.
the class KrakenStackCommandRunner method doRun.
@Override
@SneakyThrows
protected void doRun(ApplicationArguments args) {
log.info("Would stack on exchange {}", exchange);
Currency bitcoin = Currency.BTC;
Currency fiatCurrency = Currency.getInstance(properties.getFiatCurrency());
BigDecimal fiatAmount = properties.getFiatAmount();
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();
BigDecimal fiatBalance = Optional.ofNullable(krakenBalance.get("Z" + fiatCurrency.getCurrencyCode())).or(() -> Optional.ofNullable(krakenBalance.get(fiatCurrency.getCurrencyCode()))).orElse(BigDecimal.ZERO);
BigDecimal bitcoinBalance = 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);
System.out.printf("💰 Balance: %s %s / %s %s%n", fiatBalance.toPlainString(), fiatCurrency, bitcoinBalance.toPlainString(), bitcoin);
if (fiatBalance.compareTo(fiatAmount) < 0) {
System.out.printf("❌ Balance is too low - balance < order price: %s < %s%n", fiatBalance.toPlainString(), fiatAmount.toPlainString());
return;
}
// ---------------------------------------------- balance - end
// ---------------------------------------------- ask/bid
KrakenMarketDataService marketDataService = (KrakenMarketDataService) exchange.getMarketDataService();
Ticker ticker = marketDataService.getTicker(currencyPair);
System.out.printf("📈 Ask: %s %s%n", ticker.getAsk(), fiatCurrency);
System.out.printf("📉 Bid: %s %s%n", ticker.getBid(), fiatCurrency);
// ---------------------------------------------- ask/bid - end
// run dry test with massively undervalued order! from the kraken docs:
// "[...] we recommend placing very small market orders (orders for the minimum order size),
// or limit orders that are priced far away from the current market price"
BigDecimal priceMultiplier = dryRun.isEnabled() ? new BigDecimal("0.1") : BigDecimal.ONE;
BigDecimal buyingPrice = ticker.getBid().multiply(priceMultiplier);
BigDecimal cryptoAmount = fiatAmount.setScale(BTC_FRACTION_DIGITS, RoundingMode.HALF_DOWN).divide(buyingPrice, RoundingMode.HALF_DOWN);
// ---------------------------------------------- place limit order
boolean validateOnly = dryRun.isEnabled();
KrakenOrderBuilder krakenLimitOrderBuilder = KrakenStandardOrder.getLimitOrderBuilder(currencyPair, KrakenType.BUY, buyingPrice.toPlainString(), cryptoAmount).withValidateOnly(validateOnly);
KrakenStandardOrder buyLimitOrder = krakenLimitOrderBuilder.buildOrder();
KrakenTradeService tradeService = (KrakenTradeService) exchange.getTradeService();
System.out.printf("💸 Order: %s %s %s @ limit %s %s%n", buyLimitOrder.getType(), buyLimitOrder.getVolume(), buyLimitOrder.getAssetPair().base, buyLimitOrder.getPrice(), buyLimitOrder.getAssetPair().counter);
if (dryRun.isEnabled()) {
System.out.printf("📎 Fake (dry-run) Transaction IDs: %s%n", Arrays.asList("42", "1337"));
} else {
KrakenOrderResponse krakenOrderResponse = tradeService.placeKrakenOrder(buyLimitOrder);
System.out.printf("📎 Transaction IDs: %s%n", krakenOrderResponse.getTransactionIds());
}
// ---------------------------------------------- place limit - end
}
Aggregations