Search in sources :

Example 1 with KrakenTrade

use of org.knowm.xchange.kraken.dto.trade.KrakenTrade in project XChange by knowm.

the class KrakenTradeService method getTradeHistory.

/**
 * @param params Can optionally implement {@link TradeHistoryParamOffset} and {@link
 *     TradeHistoryParamsTimeSpan} and {@link TradeHistoryParamsIdSpan} All other
 *     TradeHistoryParams types will be ignored.
 */
@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws ExchangeException, IOException {
    String start = null;
    String end = null;
    Long offset = null;
    CurrencyPair currencyPair = null;
    if (params instanceof TradeHistoryParamOffset) {
        offset = ((TradeHistoryParamOffset) params).getOffset();
    }
    if (params instanceof TradeHistoryParamsIdSpan) {
        TradeHistoryParamsIdSpan idSpan = (TradeHistoryParamsIdSpan) params;
        start = idSpan.getStartId();
        end = idSpan.getEndId();
    }
    if (params instanceof TradeHistoryParamsTimeSpan) {
        TradeHistoryParamsTimeSpan timeSpan = (TradeHistoryParamsTimeSpan) params;
        start = DateUtils.toUnixTimeOptional(timeSpan.getStartTime()).map(Object::toString).orElse(start);
        end = DateUtils.toUnixTimeOptional(timeSpan.getEndTime()).map(Object::toString).orElse(end);
    }
    Map<String, KrakenTrade> krakenTradeHistory = getKrakenTradeHistory(null, false, start, end, offset).getTrades();
    if (params instanceof TradeHistoryParamCurrencyPair && ((TradeHistoryParamCurrencyPair) params).getCurrencyPair() != null) {
        krakenTradeHistory = KrakenUtils.filterTradeHistoryByCurrencyPair(krakenTradeHistory, ((TradeHistoryParamCurrencyPair) params).getCurrencyPair());
    }
    return KrakenAdapters.adaptTradesHistory(krakenTradeHistory);
}
Also used : TradeHistoryParamOffset(org.knowm.xchange.service.trade.params.TradeHistoryParamOffset) DefaultTradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.DefaultTradeHistoryParamsTimeSpan) TradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan) KrakenTrade(org.knowm.xchange.kraken.dto.trade.KrakenTrade) TradeHistoryParamsIdSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsIdSpan) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) DefaultOpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 2 with KrakenTrade

use of org.knowm.xchange.kraken.dto.trade.KrakenTrade in project XChange by knowm.

the class KrakenUtilsTest method testAdaptTradeHistoryByCurrencyPair.

@Test
public void testAdaptTradeHistoryByCurrencyPair() throws JsonParseException, JsonMappingException, IOException {
    Map<String, KrakenTrade> krakenTradeMap = loadUserTrades("/org/knowm/xchange/kraken/dto/trading/example-tradehistory-data.json");
    Map<String, KrakenTrade> filteredKrakenTradeMap = KrakenUtils.filterTradeHistoryByCurrencyPair(krakenTradeMap, CurrencyPair.BTC_USD);
    assertThat(filteredKrakenTradeMap.size()).isEqualTo(2);
    UserTrades userTrades = KrakenAdapters.adaptTradesHistory(filteredKrakenTradeMap);
    UserTrade trade0 = userTrades.getUserTrades().get(0);
    assertThat(trade0).isInstanceOf(KrakenUserTrade.class);
    assertThat(trade0.getId()).isEqualTo("TY5BYV-WJUQF-XPYEYD-2");
    assertThat(trade0.getPrice()).isEqualTo("32.07562");
    assertThat(trade0.getOriginalAmount()).isEqualTo("0.50000000");
    assertThat(trade0.getCurrencyPair().base).isEqualTo(Currency.BTC);
    assertThat(trade0.getCurrencyPair().counter).isEqualTo(Currency.USD);
    assertThat(trade0.getType()).isEqualTo(OrderType.ASK);
    assertThat(trade0.getFeeAmount()).isEqualTo("0.03208");
    assertThat(trade0.getFeeCurrency()).isEqualTo(Currency.USD);
    assertThat(((KrakenUserTrade) trade0).getCost()).isEqualTo("16.03781");
    UserTrade trade1 = userTrades.getUserTrades().get(1);
    assertThat(trade1).isInstanceOf(KrakenUserTrade.class);
    assertThat(trade1.getId()).isEqualTo("TY5BYV-WJUQF-XPYEYD-3");
    assertThat(trade1.getPrice()).isEqualTo("32.07562");
    assertThat(trade1.getOriginalAmount()).isEqualTo("0.50000000");
    assertThat(trade1.getCurrencyPair().base).isEqualTo(Currency.BTC);
    assertThat(trade1.getCurrencyPair().counter).isEqualTo(Currency.USD);
    assertThat(trade1.getType()).isEqualTo(OrderType.ASK);
    assertThat(trade1.getFeeAmount()).isEqualTo("0.03208");
    assertThat(trade1.getFeeCurrency()).isEqualTo(Currency.USD);
    assertThat(((KrakenUserTrade) trade1).getCost()).isEqualTo("16.03781");
}
Also used : UserTrades(org.knowm.xchange.dto.trade.UserTrades) KrakenUserTrade(org.knowm.xchange.kraken.dto.trade.KrakenUserTrade) KrakenTrade(org.knowm.xchange.kraken.dto.trade.KrakenTrade) UserTrade(org.knowm.xchange.dto.trade.UserTrade) KrakenUserTrade(org.knowm.xchange.kraken.dto.trade.KrakenUserTrade) Test(org.junit.Test)

Example 3 with KrakenTrade

use of org.knowm.xchange.kraken.dto.trade.KrakenTrade in project XChange by knowm.

the class KrakenTradeRawSpecificDemo method main.

public static void main(String[] args) throws IOException {
    Exchange krakenExchange = KrakenExampleUtils.createTestExchange();
    // Interested in the private trading functionality (authentication)
    KrakenTradeServiceRaw tradeService = (KrakenTradeServiceRaw) krakenExchange.getTradeService();
    // stop at -5% loss, take profit at +$10 price increase.
    KrakenStandardOrder order = KrakenStandardOrder.getLimitOrderBuilder(CurrencyPair.BTC_USD, KrakenType.BUY, "100.00", new BigDecimal("2.12345678")).withCloseOrder(KrakenOrderType.STOP_LOSS_PROFIT, "#5%", "#10").withValidateOnly(// validate only for demo purposes
    true).buildOrder();
    KrakenOrderResponse orderResponse = tradeService.placeKrakenOrder(order);
    System.out.println(orderResponse);
    Map<String, KrakenOrder> openOrders = tradeService.getKrakenOpenOrders();
    System.out.println(openOrders);
    limitRate();
    Map<String, KrakenOrder> closedOrders = tradeService.getKrakenClosedOrders();
    System.out.println(closedOrders);
    Set<String> closedOrderIds = closedOrders.keySet();
    System.out.println(tradeService.queryKrakenOrders(closedOrderIds.toArray(new String[closedOrderIds.size()])));
    Map<String, KrakenTrade> trades = tradeService.getKrakenTradeHistory().getTrades();
    System.out.println(trades);
    Set<String> tradeIds = trades.keySet();
    System.out.println(tradeService.queryKrakenTrades(tradeIds.toArray(new String[tradeIds.size()])));
    Map<String, KrakenOpenPosition> openPositions = tradeService.getKrakenOpenPositions();
    System.out.println(openPositions);
}
Also used : KrakenTrade(org.knowm.xchange.kraken.dto.trade.KrakenTrade) KrakenOrderResponse(org.knowm.xchange.kraken.dto.trade.KrakenOrderResponse) KrakenOrder(org.knowm.xchange.kraken.dto.trade.KrakenOrder) BigDecimal(java.math.BigDecimal) Exchange(org.knowm.xchange.Exchange) KrakenOpenPosition(org.knowm.xchange.kraken.dto.trade.KrakenOpenPosition) KrakenTradeServiceRaw(org.knowm.xchange.kraken.service.KrakenTradeServiceRaw)

Example 4 with KrakenTrade

use of org.knowm.xchange.kraken.dto.trade.KrakenTrade in project XChange by knowm.

the class KrakenTradeHistoryDemo method raw.

private static void raw(Exchange krakenExchange) throws IOException {
    // Interested in the private trading functionality (authentication)
    KrakenTradeServiceRaw tradeService = (KrakenTradeServiceRaw) krakenExchange.getTradeService();
    // Get the trade history
    Map<String, KrakenTrade> trades = tradeService.getKrakenTradeHistory().getTrades();
    System.out.println(trades);
}
Also used : KrakenTrade(org.knowm.xchange.kraken.dto.trade.KrakenTrade) KrakenTradeServiceRaw(org.knowm.xchange.kraken.service.KrakenTradeServiceRaw)

Example 5 with KrakenTrade

use of org.knowm.xchange.kraken.dto.trade.KrakenTrade in project XChange by knowm.

the class KrakenAdaptersTest method loadUserTrades.

private static List<UserTrade> loadUserTrades(String resourceName) throws IOException {
    // Read in the JSON from the example resources
    InputStream is = KrakenAdaptersTest.class.getResourceAsStream(resourceName);
    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    KrakenTradeHistoryResult krakenResult = mapper.readValue(is, KrakenTradeHistoryResult.class);
    KrakenTradeHistory krakenTradeHistory = krakenResult.getResult();
    Map<String, KrakenTrade> krakenTradeHistoryMap = krakenTradeHistory.getTrades();
    UserTrades trades = KrakenAdapters.adaptTradesHistory(krakenTradeHistoryMap);
    return trades.getUserTrades();
}
Also used : KrakenTradeHistory(org.knowm.xchange.kraken.dto.trade.results.KrakenTradeHistoryResult.KrakenTradeHistory) UserTrades(org.knowm.xchange.dto.trade.UserTrades) KrakenTradeHistoryResult(org.knowm.xchange.kraken.dto.trade.results.KrakenTradeHistoryResult) InputStream(java.io.InputStream) KrakenTrade(org.knowm.xchange.kraken.dto.trade.KrakenTrade) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

KrakenTrade (org.knowm.xchange.kraken.dto.trade.KrakenTrade)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 InputStream (java.io.InputStream)3 KrakenTradeHistoryResult (org.knowm.xchange.kraken.dto.trade.results.KrakenTradeHistoryResult)3 KrakenTradeHistory (org.knowm.xchange.kraken.dto.trade.results.KrakenTradeHistoryResult.KrakenTradeHistory)3 Test (org.junit.Test)2 UserTrades (org.knowm.xchange.dto.trade.UserTrades)2 KrakenTradeServiceRaw (org.knowm.xchange.kraken.service.KrakenTradeServiceRaw)2 BigDecimal (java.math.BigDecimal)1 Exchange (org.knowm.xchange.Exchange)1 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)1 UserTrade (org.knowm.xchange.dto.trade.UserTrade)1 KrakenOpenPosition (org.knowm.xchange.kraken.dto.trade.KrakenOpenPosition)1 KrakenOrder (org.knowm.xchange.kraken.dto.trade.KrakenOrder)1 KrakenOrderResponse (org.knowm.xchange.kraken.dto.trade.KrakenOrderResponse)1 KrakenUserTrade (org.knowm.xchange.kraken.dto.trade.KrakenUserTrade)1 DefaultTradeHistoryParamsTimeSpan (org.knowm.xchange.service.trade.params.DefaultTradeHistoryParamsTimeSpan)1 TradeHistoryParamCurrencyPair (org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)1 TradeHistoryParamOffset (org.knowm.xchange.service.trade.params.TradeHistoryParamOffset)1 TradeHistoryParamsIdSpan (org.knowm.xchange.service.trade.params.TradeHistoryParamsIdSpan)1