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