use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project crypto-bot by jnidzwetzki.
the class PortfolioManager method cancelRemovedEntryOrders.
/**
* Cancel the removed entry orders
* Position is at the moment not interesting for an entry
*
* @param entries
* @throws APIException
* @throws InterruptedException
*/
private void cancelRemovedEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
final List<ExchangeOrder> entryOrders = getAllOpenEntryOrders();
for (final ExchangeOrder order : entryOrders) {
final String symbol = order.getSymbol();
final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.fromSymbolString(symbol);
if (!entries.containsKey(currencyPair)) {
logger.info("Entry order for {} is not contained, canceling", currencyPair);
cancelOrder(order);
}
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project crypto-bot by jnidzwetzki.
the class PortfolioManager method placeNewExitOrders.
/**
* Place the exit orders
* @param exits
* @throws APIException
* @throws InterruptedException
*/
private void placeNewExitOrders(final Map<BitfinexCurrencyPair, Double> exits) throws APIException, InterruptedException {
for (final BitfinexCurrencyPair currency : exits.keySet()) {
final ExchangeOrder order = getOpenOrderForSymbol(currency.toBitfinexString());
final double exitPrice = exits.get(currency);
// Check old orders
if (order != null) {
final double orderPrice = order.getPrice();
if (orderPrice >= exitPrice || MathHelper.almostEquals(orderPrice, exitPrice)) {
logger.info("Old order price for {} is fine (price: order {} model {})", currency, orderPrice, exitPrice);
continue;
}
logger.info("Exit price for {} has moved form {} to {}, canceling old order", currency, orderPrice, exitPrice);
cancelOrder(order);
}
final double positionSize = getOpenPositionSizeForCurrency(currency.getCurrency1());
// * -1.0 for sell order
final double positionSizeSell = positionSize * -1.0;
final BitfinexOrder newOrder = BitfinexOrderBuilder.create(currency, getOrderType(), positionSizeSell).withPrice(exitPrice).setPostOnly().build();
placeOrder(newOrder);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project crypto-bot by jnidzwetzki.
the class PortfolioManager method cleanupOldExitOrders.
/**
* Cleanup the old exit orders (remove duplicates, unknown orders)
* @param exits
* @throws APIException
* @throws InterruptedException
*/
private void cleanupOldExitOrders(final Map<BitfinexCurrencyPair, Double> exits) throws APIException, InterruptedException {
final List<ExchangeOrder> oldExitOrders = getAllOpenExitOrders();
// Remove unknown orders
for (final ExchangeOrder order : oldExitOrders) {
final BitfinexCurrencyPair symbol = BitfinexCurrencyPair.fromSymbolString(order.getSymbol());
if (!exits.containsKey(symbol)) {
logger.error("Found old and unknown order {}, canceling", order);
cancelOrder(order);
}
}
// Remove duplicates
final Map<String, List<ExchangeOrder>> oldOrders = oldExitOrders.stream().collect(Collectors.groupingBy(ExchangeOrder::getSymbol));
for (final String symbol : oldOrders.keySet()) {
final List<ExchangeOrder> symbolOrderList = oldOrders.get(symbol);
if (symbolOrderList.size() > 1) {
logger.error("Found duplicates {}", symbolOrderList);
for (final ExchangeOrder order : symbolOrderList) {
cancelOrder(order);
}
}
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project crypto-bot by jnidzwetzki.
the class EMABot method updateScreen.
public synchronized void updateScreen() {
if (!UPDATE_SCREEN) {
return;
}
final QuoteManager tickerManager = bitfinexApiBroker.getQuoteManager();
CliTools.clearScreen();
System.out.println("");
System.out.println("==========");
System.out.println("Last ticks");
System.out.println("==========");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
final String symbol = currency.toBitfinexString();
final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
System.out.println(symbol + " " + tickerManager.getLastTick(tickerSymbol));
}
System.out.println("");
System.out.println("==========");
System.out.println("Last bars");
System.out.println("==========");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
System.out.println(currency + " " + timeSeries.get(currency).getLastBar());
}
System.out.println("");
System.out.println("==========");
System.out.println("P/L");
System.out.println("==========");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
final String symbol = currency.toBitfinexString();
final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
final Trade trade = getOpenTrade(currency);
if (trade != null) {
final double priceIn = trade.getExpectedPriceOpen();
final double currentPrice = tickerManager.getLastTick(tickerSymbol).getClose();
System.out.println(symbol + ": price in " + priceIn + " / " + (currentPrice - priceIn));
}
}
System.out.println("");
System.out.println("==========");
System.out.println("Trades");
System.out.println("==========");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
final String symbol = currency.toBitfinexString();
final List<Trade> lastTrades = trades.get(currency);
if (lastTrades == null) {
continue;
}
lastTrades.sort((t1, t2) -> Long.compare(t2.getTid(), t1.getTid()));
final List<Trade> lastTwoTrades = lastTrades.subList(Math.max(lastTrades.size() - 2, 0), lastTrades.size());
for (final Trade trade : lastTwoTrades) {
System.out.println(symbol + " " + trade);
}
}
try {
System.out.println("");
System.out.println("==========");
System.out.println("Orders");
System.out.println("==========");
final List<ExchangeOrder> orders = new ArrayList<>(bitfinexApiBroker.getOrderManager().getOrders());
orders.sort((o1, o2) -> Long.compare(o2.getCid(), o1.getCid()));
final List<ExchangeOrder> lastOrders = orders.subList(Math.max(orders.size() - 3, 0), orders.size());
for (final ExchangeOrder order : lastOrders) {
System.out.println(order);
}
} catch (APIException e) {
logger.error("Got eception while reading wallets", e);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder 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);
}
Aggregations