Search in sources :

Example 21 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class TradeManagerTest method testTradeChannelHandler1.

/**
 * Test the trade channel handler
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testTradeChannelHandler1() throws APIException, InterruptedException {
    final String jsonString = "[0,\"te\",[106655593,\"tBTCUSD\",1512247319827,5691690918,-0.002,10894,null,null,-1]]";
    final JSONArray jsonArray = new JSONArray(jsonString);
    final TradeHandler tradeHandler = new TradeHandler();
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    final CountDownLatch latch = new CountDownLatch(1);
    bitfinexApiBroker.getTradeManager().registerCallback((t) -> {
        try {
            Assert.assertTrue(t.isExecuted());
            Assert.assertEquals(106655593, t.getId());
            Assert.assertEquals(BitfinexCurrencyPair.BTC_USD, t.getCurrency());
            Assert.assertEquals(1512247319827l, t.getMtsCreate());
            Assert.assertEquals(5691690918l, t.getOrderId());
            Assert.assertEquals(-0.002, t.getExecAmount().doubleValue(), DELTA);
            Assert.assertEquals(10894, t.getExecPrice().doubleValue(), DELTA);
            Assert.assertTrue(t.toString().length() > 0);
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
        latch.countDown();
    });
    tradeHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    latch.await();
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) CountDownLatch(java.util.concurrent.CountDownLatch) TradeHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.TradeHandler) Test(org.junit.Test)

Example 22 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class NotificationHandler method handleErrorNotification.

/**
 * Handle the error notification
 *
 * @param bitfinexApiBroker
 * @param order
 * @param state
 * @param stateValue
 */
private void handleErrorNotification(final BitfinexApiBroker bitfinexApiBroker, final JSONArray order, final String state, final String stateValue) {
    final ExchangeOrder exchangeOrder = new ExchangeOrder();
    exchangeOrder.setApikey(bitfinexApiBroker.getApiKey());
    exchangeOrder.setCid(order.getLong(2));
    exchangeOrder.setSymbol(order.getString(3));
    exchangeOrder.setState(ExchangeOrderState.STATE_ERROR);
    logger.error("State for order {} is {}, reason is {}", exchangeOrder.getOrderId(), state, stateValue);
    bitfinexApiBroker.getOrderManager().updateOrder(exchangeOrder);
}
Also used : ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 23 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class OrderHandler method handleOrderCallback.

/**
 * Handle a single order callback
 * @param bitfinexApiBroker
 * @param orderArray
 * @throws APIException
 */
private void handleOrderCallback(BitfinexApiBroker bitfinexApiBroker, final JSONArray order) throws APIException {
    final ExchangeOrder exchangeOrder = new ExchangeOrder();
    exchangeOrder.setApikey(bitfinexApiBroker.getApiKey());
    exchangeOrder.setOrderId(order.getLong(0));
    exchangeOrder.setGroupId(order.optInt(1, -1));
    exchangeOrder.setCid(order.optLong(2, -1));
    exchangeOrder.setSymbol(order.getString(3));
    exchangeOrder.setCreated(order.getLong(4));
    exchangeOrder.setUpdated(order.getLong(5));
    exchangeOrder.setAmount(order.getBigDecimal(6));
    exchangeOrder.setAmountAtCreation(order.getBigDecimal(7));
    exchangeOrder.setOrderType(BitfinexOrderType.fromString(order.getString(8)));
    final ExchangeOrderState orderState = ExchangeOrderState.fromString(order.getString(13));
    exchangeOrder.setState(orderState);
    exchangeOrder.setPrice(order.optBigDecimal(16, BigDecimal.valueOf(-1)));
    exchangeOrder.setPriceAvg(order.optBigDecimal(17, BigDecimal.valueOf(-1)));
    exchangeOrder.setPriceTrailing(order.optBigDecimal(18, BigDecimal.valueOf(-1)));
    exchangeOrder.setPriceAuxLimit(order.optBigDecimal(19, BigDecimal.valueOf(-1)));
    exchangeOrder.setNotify(order.getInt(23) == 1 ? true : false);
    exchangeOrder.setHidden(order.getInt(24) == 1 ? true : false);
    bitfinexApiBroker.getOrderManager().updateOrder(exchangeOrder);
}
Also used : ExchangeOrderState(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrderState) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 24 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class TradeHandler method handleTradeCallback.

/**
 * Handle a single trade callback
 * @param bitfinexApiBroker
 * @param orderArray
 * @throws APIException
 */
private void handleTradeCallback(final BitfinexApiBroker bitfinexApiBroker, final JSONArray jsonTrade, final boolean executed) throws APIException {
    final Trade trade = new Trade();
    trade.setExecuted(executed);
    trade.setApikey(bitfinexApiBroker.getApiKey());
    trade.setId(jsonTrade.getLong(0));
    trade.setCurrency(BitfinexCurrencyPair.fromSymbolString(jsonTrade.getString(1)));
    trade.setMtsCreate(jsonTrade.getLong(2));
    trade.setOrderId(jsonTrade.getLong(3));
    trade.setExecAmount(jsonTrade.getBigDecimal(4));
    trade.setExecPrice(jsonTrade.getBigDecimal(5));
    final String orderTypeString = jsonTrade.optString(6, null);
    if (orderTypeString != null) {
        trade.setOrderType(BitfinexOrderType.fromString(orderTypeString));
    }
    trade.setOrderPrice(jsonTrade.optBigDecimal(7, BigDecimal.valueOf(-1)));
    trade.setMaker(jsonTrade.getInt(8) == 1 ? true : false);
    trade.setFee(jsonTrade.optBigDecimal(9, BigDecimal.valueOf(-1)));
    trade.setFeeCurrency(jsonTrade.optString(10, ""));
    bitfinexApiBroker.getTradeManager().updateTrade(trade);
}
Also used : Trade(com.github.jnidzwetzki.bitfinex.v2.entity.Trade)

Example 25 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class OrderbookHandler method handleEntry.

/**
 * Handle a new orderbook entry
 * @param bitfinexApiBroker
 * @param configuration
 * @param jsonArray
 */
private void handleEntry(final BitfinexApiBroker bitfinexApiBroker, final OrderbookConfiguration configuration, final JSONArray jsonArray) {
    final BigDecimal price = jsonArray.getBigDecimal(0);
    final BigDecimal count = jsonArray.getBigDecimal(1);
    final BigDecimal amount = jsonArray.getBigDecimal(2);
    final OrderbookEntry orderbookEntry = new OrderbookEntry(price, count, amount);
    bitfinexApiBroker.getOrderbookManager().handleNewOrderbookEntry(configuration, orderbookEntry);
}
Also used : OrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry) BigDecimal(java.math.BigDecimal)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)46 Test (org.junit.Test)36 JSONArray (org.json.JSONArray)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)15 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)15 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)13 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)12 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)12 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)11 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)11 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)9 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)9 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)9 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)9 ExecutorService (java.util.concurrent.ExecutorService)9 ExecutedTrade (com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade)7 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)7 Assert (org.junit.Assert)7 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)6