use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class OrderManager method placeOrder.
/**
* Place a new order
* @throws APIException
*/
public void placeOrder(final BitfinexOrder order) throws APIException {
final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
if (!capabilities.isHavingOrdersWriteCapability()) {
throw new APIException("Unable to place order " + order + " connection has not enough capabilities: " + capabilities);
}
logger.info("Executing new order {}", order);
final OrderCommand orderCommand = new OrderCommand(order);
bitfinexApiBroker.sendCommand(orderCommand);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder 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.BitfinexOrder 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.BitfinexOrder in project crypto-bot by jnidzwetzki.
the class PortfolioManager method placeNewEntryOrders.
/**
* Place the new entry orders
* @param entries
* @throws APIException
* @throws InterruptedException
*/
private void placeNewEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
// Check current limits and position sizes
for (final BitfinexCurrencyPair currency : entries.keySet()) {
final ExchangeOrder order = getOpenOrderForSymbol(currency.toBitfinexString());
final CurrencyEntry entry = entries.get(currency);
final double entryPrice = entry.getEntryPrice();
final double positionSize = entry.getPositionSize();
if (positionSize < currency.getMinimumOrderSize()) {
logger.info("Not placing order for {}, position size is too small {}", currency, positionSize);
continue;
}
// Old order present
if (order != null) {
if (hasEntryOrderChanged(order, entryPrice, positionSize)) {
logger.info("Entry order for {}, values changed (amount: {} / {}} (price: {} / {})", currency, order.getAmount(), positionSize, order.getPrice(), entryPrice);
cancelOrder(order);
} else {
logger.info("Not placing a new order for {}, old order still active", currency);
continue;
}
}
final BitfinexOrder newOrder = BitfinexOrderBuilder.create(currency, getOrderType(), positionSize).withPrice(entryPrice).setPostOnly().build();
placeOrder(newOrder);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder in project crypto-bot by jnidzwetzki.
the class TestPersistence method testStoreAndFetchOrder.
/**
* Test order persistence
*/
@Test
public void testStoreAndFetchOrder() {
final BitfinexOrder order = new BitfinexOrder(BitfinexCurrencyPair.BTC_USD, BitfinexOrderType.EXCHANGE_LIMIT, 0, 0, 0, 0, false, false, -1);
final Session session = sessionFactory.openSession();
@SuppressWarnings("unchecked") final List<BitfinexOrder> result1 = session.createQuery("from BitfinexOrder").list();
// Assert.assertTrue(result1.isEmpty());
for (final BitfinexOrder orderFetched : result1) {
System.out.println("Order " + orderFetched);
}
session.beginTransaction();
session.save(order);
session.getTransaction().commit();
@SuppressWarnings("unchecked") final List<BitfinexOrder> result2 = session.createQuery("from BitfinexOrder").list();
Assert.assertEquals(1, result2.size());
session.close();
}
Aggregations