Search in sources :

Example 6 with Trade

use of com.github.jnidzwetzki.bitfinex.v2.entity.Trade in project crypto-bot by jnidzwetzki.

the class EMABot method addTradeToOpenTradeList.

/**
 * Open the trade to the open trades list
 * @param currency
 * @param trade
 */
private void addTradeToOpenTradeList(final Trade trade) {
    final BitfinexCurrencyPair currency = trade.getSymbol();
    if (!trades.containsKey(currency)) {
        trades.put(currency, new ArrayList<>());
    }
    trades.get(currency).add(trade);
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)

Example 7 with Trade

use of com.github.jnidzwetzki.bitfinex.v2.entity.Trade in project crypto-bot by jnidzwetzki.

the class PortfolioOrderManager method closeTrade.

/**
 * Close a trade
 * @param trade
 */
public void closeTrade(final Trade trade) {
    if (!bitfinexApiBroker.isAuthenticated()) {
        logger.error("Unable to execute trade {} on marketplace, conecction is not authenticated", trade);
        return;
    }
    final double amount = trade.getAmount() * -1.0;
    final BitfinexOrder order = BitfinexOrderBuilder.create(trade.getSymbol(), BitfinexOrderType.EXCHANGE_MARKET, amount).build();
    try {
        trade.setTradeState(TradeState.CLOSING);
        trade.addCloseOrder(order);
        bitfinexApiBroker.getOrderManager().placeOrder(order);
        trade.setTradeState(TradeState.CLOSED);
    } catch (APIException e) {
        logger.error("Got an exception while closing trade {}", trade);
        trade.setTradeState(TradeState.ERROR);
    } finally {
        persistTrade(trade);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)

Example 8 with Trade

use of com.github.jnidzwetzki.bitfinex.v2.entity.Trade in project crypto-bot by jnidzwetzki.

the class PortfolioOrderManager method openTrade.

/**
 * Open a new trade
 * @param trade
 */
public void openTrade(final Trade trade) {
    if (!bitfinexApiBroker.isAuthenticated()) {
        logger.error("Unable to execute trade {} on marketplace, conecction is not authenticated", trade);
        return;
    }
    final double amount = trade.getAmount();
    final BitfinexOrder order = BitfinexOrderBuilder.create(trade.getSymbol(), BitfinexOrderType.EXCHANGE_MARKET, amount).build();
    try {
        trade.setTradeState(TradeState.OPENING);
        trade.addOpenOrder(order);
        bitfinexApiBroker.getOrderManager().placeOrder(order);
        trade.setTradeState(TradeState.OPEN);
    } catch (APIException e) {
        logger.error("Got an exception while opening trade {}", trade);
        trade.setTradeState(TradeState.ERROR);
    } finally {
        persistTrade(trade);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)

Example 9 with Trade

use of com.github.jnidzwetzki.bitfinex.v2.entity.Trade in project crypto-bot by jnidzwetzki.

the class TestPersistence method testTradePersistence.

/**
 * Test testade persistence
 */
@Test
public void testTradePersistence() {
    final BitfinexOrder order = new BitfinexOrder(BitfinexCurrencyPair.BTC_USD, BitfinexOrderType.EXCHANGE_LIMIT, 0, 0, 0, 0, false, false, -1);
    final Trade trade = new Trade("ABC", TradeDirection.LONG, BitfinexCurrencyPair.BTC_USD, 1);
    trade.getOrdersOpen().add(order);
    trade.setTradeState(TradeState.OPEN);
    final Session session = sessionFactory.openSession();
    session.beginTransaction();
    @SuppressWarnings("unchecked") final List<Trade> result1 = session.createQuery("from Trade t").list();
    Assert.assertTrue(result1.isEmpty());
    session.save(trade);
    session.getTransaction().commit();
    session.beginTransaction();
    @SuppressWarnings("unchecked") final List<Trade> result2 = session.createQuery("from Trade t").list();
    session.getTransaction().commit();
    session.close();
    Assert.assertEquals(1, result2.size());
}
Also used : Trade(com.github.jnidzwetzki.cryptobot.entity.Trade) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with Trade

use of com.github.jnidzwetzki.bitfinex.v2.entity.Trade 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)

Aggregations

Test (org.junit.Test)5 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)4 JSONArray (org.json.JSONArray)4 TradeHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.TradeHandler)3 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)3 BitfinexOrder (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)3 Trade (com.github.jnidzwetzki.cryptobot.entity.Trade)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutedTradeHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler)2 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)2 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)2 ArrayList (java.util.ArrayList)2 Session (org.hibernate.Session)2 DoNothingHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.DoNothingHandler)1 HeartbeatHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.HeartbeatHandler)1 NotificationHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.NotificationHandler)1 OrderHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.OrderHandler)1 PositionHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.PositionHandler)1 WalletHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler)1 SubscribeTradesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTradesCommand)1