Search in sources :

Example 46 with BitfinexApiBroker

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

the class PositionTest method testPositionHandlerUpdate.

/**
 * Test the position handler
 * @throws APIException
 */
@Test
public void testPositionHandlerUpdate() throws APIException {
    final String jsonString = "[0,\"pu\",[\"tETHUSD\",\"ACTIVE\",0.14,713.78,-0.00330012,0,null,null,null,null]]";
    final JSONArray jsonArray = new JSONArray(jsonString);
    final PositionHandler positionHandler = new PositionHandler();
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    Assert.assertTrue(bitfinexApiBroker.getPositionManager().getPositions().isEmpty());
    positionHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    Assert.assertEquals(1, bitfinexApiBroker.getPositionManager().getPositions().size());
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) PositionHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.PositionHandler) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 47 with BitfinexApiBroker

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

the class PositionTest method testPositionHandlerSnapshot.

/**
 * Test the position handler
 * @throws APIException
 */
@Test
public void testPositionHandlerSnapshot() throws APIException {
    final String jsonString = "[0,\"ps\",[[\"tETHUSD\",\"ACTIVE\",0.14,713.78,-0.00330012,0,null,null,null,null], [\"tBTCUSD\",\"ACTIVE\",0.14,713.78,-0.00330012,0,null,null,null,null]]]";
    final JSONArray jsonArray = new JSONArray(jsonString);
    final PositionHandler positionHandler = new PositionHandler();
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    Assert.assertTrue(bitfinexApiBroker.getPositionManager().getPositions().isEmpty());
    positionHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    Assert.assertEquals(2, bitfinexApiBroker.getPositionManager().getPositions().size());
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) PositionHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.PositionHandler) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 48 with BitfinexApiBroker

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

the class PositionTest method buildMockedBitfinexConnection.

/**
 * Build a mocked bitfinex connection
 * @return
 */
private BitfinexApiBroker buildMockedBitfinexConnection() {
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    final PositionManager positionManager = new PositionManager(executorService);
    Mockito.when(bitfinexApiBroker.getPositionManager()).thenReturn(positionManager);
    return bitfinexApiBroker;
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) PositionManager(com.github.jnidzwetzki.bitfinex.v2.manager.PositionManager) ExecutorService(java.util.concurrent.ExecutorService)

Example 49 with BitfinexApiBroker

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

the class TradeManagerTest method testTradeChannelHandler2.

/**
 * Test the trade channel handler
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testTradeChannelHandler2() throws APIException, InterruptedException {
    final String jsonString = "[0,\"tu\",[106655593,\"tBTCUSD\",1512247319827,5691690918,-0.002,10894,\"EXCHANGE MARKET\",10894,-1,-0.0392184,\"USD\"]]";
    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.assertFalse(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.assertEquals(BitfinexOrderType.EXCHANGE_MARKET, t.getOrderType());
            Assert.assertEquals(10894, t.getOrderPrice().doubleValue(), DELTA);
            Assert.assertFalse(t.isMaker());
            Assert.assertEquals(-0.0392184, t.getFee().doubleValue(), DELTA);
            Assert.assertEquals("USD", t.getFeeCurrency());
            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 50 with BitfinexApiBroker

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

the class TradeManagerTest method buildMockedBitfinexConnection.

/**
 * Build a mocked bitfinex connection
 * @return
 */
private BitfinexApiBroker buildMockedBitfinexConnection() {
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    Mockito.when(bitfinexApiBroker.getApiKey()).thenReturn(API_KEY);
    Mockito.when(bitfinexApiBroker.isAuthenticated()).thenReturn(true);
    Mockito.when(bitfinexApiBroker.getCapabilities()).thenReturn(ConnectionCapabilities.ALL_CAPABILITIES);
    final TradeManager tradeManager = new TradeManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getTradeManager()).thenReturn(tradeManager);
    return bitfinexApiBroker;
}
Also used : TradeManager(com.github.jnidzwetzki.bitfinex.v2.manager.TradeManager) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) ExecutorService(java.util.concurrent.ExecutorService)

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