Search in sources :

Example 1 with KrakenTradeService

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);
}
Also used : KrakenMarketDataService(org.knowm.xchange.kraken.service.KrakenMarketDataService) KrakenTradeService(org.knowm.xchange.kraken.service.KrakenTradeService) KrakenAccountService(org.knowm.xchange.kraken.service.KrakenAccountService)

Example 2 with KrakenTradeService

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());
    });
}
Also used : KrakenTradeService(org.knowm.xchange.kraken.service.KrakenTradeService) KrakenOrder(org.knowm.xchange.kraken.dto.trade.KrakenOrder) SneakyThrows(lombok.SneakyThrows)

Example 3 with KrakenTradeService

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
}
Also used : Monetary(javax.money.Monetary) Arrays(java.util.Arrays) Ticker(org.knowm.xchange.dto.marketdata.Ticker) BitcoinAutoDcaExampleProperties(org.tbk.bitcoin.autodca.example.BitcoinAutoDcaExampleProperties) ApplicationArguments(org.springframework.boot.ApplicationArguments) DryRunOption(org.tbk.bitcoin.autodca.example.BitcoinAutoDcaExampleApplicationConfig.DryRunOption) SneakyThrows(lombok.SneakyThrows) KrakenExchange(org.knowm.xchange.kraken.KrakenExchange) KrakenMarketDataService(org.knowm.xchange.kraken.service.KrakenMarketDataService) KrakenStandardOrder(org.knowm.xchange.kraken.dto.trade.KrakenStandardOrder) KrakenAccountService(org.knowm.xchange.kraken.service.KrakenAccountService) KrakenType(org.knowm.xchange.kraken.dto.trade.KrakenType) BigDecimal(java.math.BigDecimal) Slf4j(lombok.extern.slf4j.Slf4j) Currency(org.knowm.xchange.currency.Currency) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) KrakenTradeService(org.knowm.xchange.kraken.service.KrakenTradeService) KrakenOrderResponse(org.knowm.xchange.kraken.dto.trade.KrakenOrderResponse) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) KrakenOrderBuilder(org.knowm.xchange.kraken.dto.trade.KrakenStandardOrder.KrakenOrderBuilder) RoundingMode(java.math.RoundingMode) KrakenOrderBuilder(org.knowm.xchange.kraken.dto.trade.KrakenStandardOrder.KrakenOrderBuilder) KrakenMarketDataService(org.knowm.xchange.kraken.service.KrakenMarketDataService) KrakenTradeService(org.knowm.xchange.kraken.service.KrakenTradeService) Ticker(org.knowm.xchange.dto.marketdata.Ticker) KrakenOrderResponse(org.knowm.xchange.kraken.dto.trade.KrakenOrderResponse) BigDecimal(java.math.BigDecimal) Currency(org.knowm.xchange.currency.Currency) KrakenAccountService(org.knowm.xchange.kraken.service.KrakenAccountService) KrakenStandardOrder(org.knowm.xchange.kraken.dto.trade.KrakenStandardOrder) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) SneakyThrows(lombok.SneakyThrows)

Aggregations

KrakenTradeService (org.knowm.xchange.kraken.service.KrakenTradeService)3 SneakyThrows (lombok.SneakyThrows)2 KrakenAccountService (org.knowm.xchange.kraken.service.KrakenAccountService)2 KrakenMarketDataService (org.knowm.xchange.kraken.service.KrakenMarketDataService)2 BigDecimal (java.math.BigDecimal)1 RoundingMode (java.math.RoundingMode)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Monetary (javax.money.Monetary)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Currency (org.knowm.xchange.currency.Currency)1 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)1 Ticker (org.knowm.xchange.dto.marketdata.Ticker)1 KrakenExchange (org.knowm.xchange.kraken.KrakenExchange)1 KrakenOrder (org.knowm.xchange.kraken.dto.trade.KrakenOrder)1 KrakenOrderResponse (org.knowm.xchange.kraken.dto.trade.KrakenOrderResponse)1 KrakenStandardOrder (org.knowm.xchange.kraken.dto.trade.KrakenStandardOrder)1 KrakenOrderBuilder (org.knowm.xchange.kraken.dto.trade.KrakenStandardOrder.KrakenOrderBuilder)1