Search in sources :

Example 1 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class BitsoAdapters method adaptTrades.

/**
 * Adapts a Transaction[] to a Trades Object
 *
 * @param transactions The Bitso transactions
 * @param currencyPair (e.g. BTC/MXN)
 * @return The XChange Trades
 */
public static Trades adaptTrades(BitsoTransaction[] transactions, CurrencyPair currencyPair) {
    List<Trade> trades = new ArrayList<>();
    long lastTradeId = 0;
    for (BitsoTransaction tx : transactions) {
        Order.OrderType type;
        switch(tx.getSide()) {
            case "buy":
                type = Order.OrderType.ASK;
                break;
            case "sell":
                type = Order.OrderType.BID;
                break;
            default:
                type = null;
        }
        final long tradeId = tx.getTid();
        if (tradeId > lastTradeId) {
            lastTradeId = tradeId;
        }
        trades.add(new Trade.Builder().type(type).originalAmount(tx.getAmount()).currencyPair(currencyPair).price(tx.getPrice()).timestamp(DateUtils.fromMillisUtc(tx.getDate() * 1000L)).id(String.valueOf(tradeId)).build());
    }
    return new Trades(trades, lastTradeId, TradeSortType.SortByID);
}
Also used : Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Trade(org.knowm.xchange.dto.marketdata.Trade) BitsoTransaction(org.knowm.xchange.bitso.dto.marketdata.BitsoTransaction) UserTrades(org.knowm.xchange.dto.trade.UserTrades) Trades(org.knowm.xchange.dto.marketdata.Trades) ArrayList(java.util.ArrayList)

Example 2 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class BitstampAdapters method adaptTrades.

/**
 * Adapts a Transaction[] to a Trades Object
 *
 * @param transactions The Bitstamp transactions
 * @param currencyPair (e.g. BTC/USD)
 * @return The XChange Trades
 */
public static Trades adaptTrades(BitstampTransaction[] transactions, CurrencyPair currencyPair) {
    List<Trade> trades = new ArrayList<>();
    long lastTradeId = 0;
    for (BitstampTransaction tx : transactions) {
        final long tradeId = tx.getTid();
        if (tradeId > lastTradeId) {
            lastTradeId = tradeId;
        }
        trades.add(adaptTrade(tx, currencyPair, 1000));
    }
    return new Trades(trades, lastTradeId, TradeSortType.SortByID);
}
Also used : UserTrade(org.knowm.xchange.dto.trade.UserTrade) Trade(org.knowm.xchange.dto.marketdata.Trade) UserTrades(org.knowm.xchange.dto.trade.UserTrades) Trades(org.knowm.xchange.dto.marketdata.Trades) ArrayList(java.util.ArrayList) BitstampTransaction(org.knowm.xchange.bitstamp.dto.marketdata.BitstampTransaction)

Example 3 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class BitstampAdapterTest method testTradeAdapter.

@Test
public void testTradeAdapter() throws IOException {
    // Read in the JSON from the example resources
    InputStream is = BitstampAdapterTest.class.getResourceAsStream("/org/knowm/xchange/bitstamp/dto/marketdata/example-trades-data.json");
    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    BitstampTransaction[] transactions = mapper.readValue(is, BitstampTransaction[].class);
    Trade trade = BitstampAdapters.adaptTrade(transactions[3], CurrencyPair.BTC_USD, 1000);
    // verify all fields filled
    assertThat(trade.getPrice().toString()).isEqualTo("13.14");
    assertThat(trade.getType()).isEqualTo(OrderType.BID);
    assertThat(trade.getOriginalAmount()).isEqualTo(new BigDecimal("23.66362253"));
    assertThat(trade.getCurrencyPair()).isEqualTo(CurrencyPair.BTC_USD);
}
Also used : Trade(org.knowm.xchange.dto.marketdata.Trade) InputStream(java.io.InputStream) BitstampTransaction(org.knowm.xchange.bitstamp.dto.marketdata.BitstampTransaction) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 4 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class CoinfloorPublicTradesIntegration method fetchTransactionTest.

@Test
public void fetchTransactionTest() throws IOException {
    Exchange exchange = ExchangeFactory.INSTANCE.createExchange(CoinfloorExchange.class);
    MarketDataService service = exchange.getMarketDataService();
    Trades trades = service.getTrades(CurrencyPair.BTC_GBP, CoinfloorInterval.HOUR);
    assertThat(trades.getTrades()).isNotEmpty();
    int tradeCount = trades.getTrades().size();
    Trade mostRecentTrade = trades.getTrades().get(tradeCount - 1);
    assertThat(mostRecentTrade.getPrice()).isGreaterThan(BigDecimal.ZERO);
    assertThat(mostRecentTrade.getOriginalAmount()).isGreaterThan(BigDecimal.ZERO);
    assertThat(trades.getlastID()).isEqualTo(Long.parseLong(mostRecentTrade.getId()));
}
Also used : CoinfloorExchange(org.knowm.xchange.coinfloor.CoinfloorExchange) Exchange(org.knowm.xchange.Exchange) Trade(org.knowm.xchange.dto.marketdata.Trade) Trades(org.knowm.xchange.dto.marketdata.Trades) MarketDataService(org.knowm.xchange.service.marketdata.MarketDataService) Test(org.junit.Test)

Example 5 with Trade

use of org.knowm.xchange.dto.marketdata.Trade in project XChange by knowm.

the class CoingiAdapters method adaptTrade.

/**
 * Adapts a Transaction to a Trade Object
 *
 * @param tx The Coingi transaction
 * @param currencyPair (e.g. BTC/USD)
 * @param timeScale polled order books provide a timestamp in seconds, stream in ms
 * @return The XChange Trade
 */
public static Trade adaptTrade(CoingiUserTransaction tx, CurrencyPair currencyPair, int timeScale) {
    OrderType orderType = tx.getType() == 0 ? OrderType.BID : OrderType.ASK;
    final String tradeId = tx.getId();
    Date date = DateUtils.fromMillisUtc(tx.getTimestamp() * // polled order books provide a timestamp in seconds, stream in ms
    timeScale);
    return new Trade.Builder().type(orderType).originalAmount(tx.getBaseAmount()).currencyPair(currencyPair).price(tx.getPrice()).timestamp(date).id(tradeId).build();
}
Also used : UserTrade(org.knowm.xchange.dto.trade.UserTrade) Trade(org.knowm.xchange.dto.marketdata.Trade) OrderType(org.knowm.xchange.dto.Order.OrderType) Date(java.util.Date)

Aggregations

Trade (org.knowm.xchange.dto.marketdata.Trade)95 Trades (org.knowm.xchange.dto.marketdata.Trades)58 UserTrade (org.knowm.xchange.dto.trade.UserTrade)49 ArrayList (java.util.ArrayList)42 Date (java.util.Date)38 BigDecimal (java.math.BigDecimal)30 UserTrades (org.knowm.xchange.dto.trade.UserTrades)29 OrderType (org.knowm.xchange.dto.Order.OrderType)25 Test (org.junit.Test)21 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 Exchange (org.knowm.xchange.Exchange)8 InputStream (java.io.InputStream)7 Order (org.knowm.xchange.dto.Order)7 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)7 Ticker (org.knowm.xchange.dto.marketdata.Ticker)7 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 StreamingObjectMapperHelper (info.bitrich.xchangestream.service.netty.StreamingObjectMapperHelper)5 Observable (io.reactivex.Observable)5