Search in sources :

Example 6 with OrderManager

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

the class OrderManagerTest method testPlaceOrder.

/**
 * Test the placement of an order
 * @throws InterruptedException
 * @throws APIException
 */
@Test(timeout = 60000)
public void testPlaceOrder() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    Mockito.when(bitfinexApiBroker.isAuthenticated()).thenReturn(true);
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.MARKET, 1).build();
    final Runnable r = () -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            return;
        }
        final ExchangeOrder exchangeOrder = new ExchangeOrder();
        exchangeOrder.setCid(order.getCid());
        exchangeOrder.setState(ExchangeOrderState.STATE_ACTIVE);
        orderManager.updateOrder(exchangeOrder);
    };
    // Cancel event
    (new Thread(r)).start();
    orderManager.placeOrderAndWaitUntilActive(order);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) Test(org.junit.Test)

Example 7 with OrderManager

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

the class OrderManagerTest method testCancelOrder.

/**
 * Test the cancelation of an order
 * @throws InterruptedException
 * @throws APIException
 */
@Test(timeout = 60000)
public void testCancelOrder() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    final Runnable r = () -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            return;
        }
        final ExchangeOrder exchangeOrder = new ExchangeOrder();
        exchangeOrder.setOrderId(12);
        exchangeOrder.setState(ExchangeOrderState.STATE_CANCELED);
        orderManager.updateOrder(exchangeOrder);
    };
    // Cancel event
    (new Thread(r)).start();
    orderManager.cancelOrderAndWaitForCompletion(12);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) Test(org.junit.Test)

Example 8 with OrderManager

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

the class OrderManagerTest method testOrderChannelHandler1.

/**
 * Test the order channel handler - single order
 * @throws APIException
 */
@Test
public void testOrderChannelHandler1() throws APIException {
    final String jsonString = "[0,\"on\",[6784335053,null,1514956504945000,\"tIOTUSD\",1514956505134,1514956505164,-24.175121,-24.175121,\"EXCHANGE STOP\",null,null,null,0,\"ACTIVE\",null,null,3.84,0,null,null,null,null,null,0,0,0]]";
    final JSONArray jsonArray = new JSONArray(jsonString);
    final OrderHandler orderHandler = new OrderHandler();
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    Assert.assertTrue(orderManager.getOrders().isEmpty());
    orderHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    Assert.assertEquals(1, orderManager.getOrders().size());
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) OrderHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.OrderHandler) Test(org.junit.Test)

Example 9 with OrderManager

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

the class OrderManagerTest 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 OrderManager orderManager = new OrderManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getOrderManager()).thenReturn(orderManager);
    return bitfinexApiBroker;
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) ExecutorService(java.util.concurrent.ExecutorService) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)9 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)8 Test (org.junit.Test)8 OrderHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.OrderHandler)3 JSONArray (org.json.JSONArray)3 BitfinexOrder (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)2 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)2 PortfolioOrderManager (com.github.jnidzwetzki.cryptobot.PortfolioOrderManager)1 Trade (com.github.jnidzwetzki.cryptobot.entity.Trade)1 ExecutorService (java.util.concurrent.ExecutorService)1 Session (org.hibernate.Session)1