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);
}
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");
}
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);
}
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);
}
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();
}
Aggregations