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