use of com.github.jnidzwetzki.cryptobot.entity.Trade in project crypto-bot by jnidzwetzki.
the class TestPersistence method testGetOpenTrades.
/**
* Test get open trades from order manager
*/
@Test
public void testGetOpenTrades() {
final BitfinexApiBroker apiBroker = Mockito.mock(BitfinexApiBroker.class);
final PortfolioOrderManager ordermanager = new PortfolioOrderManager(apiBroker);
Assert.assertTrue(ordermanager.getAllOpenTrades().isEmpty());
final Trade trade = new Trade("ABC", TradeDirection.LONG, BitfinexCurrencyPair.BTC_USD, 1);
trade.setTradeState(TradeState.CREATED);
final Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(trade);
session.getTransaction().commit();
Assert.assertTrue(ordermanager.getAllOpenTrades().isEmpty());
session.beginTransaction();
trade.setTradeState(TradeState.OPEN);
session.saveOrUpdate(trade);
session.getTransaction().commit();
session.beginTransaction();
Assert.assertEquals(1, ordermanager.getAllOpenTrades().size());
session.getTransaction().commit();
final Trade trade2 = new Trade("ABC", TradeDirection.LONG, BitfinexCurrencyPair.BTC_USD, 1);
trade2.setTradeState(TradeState.OPEN);
session.beginTransaction();
session.save(trade2);
session.getTransaction().commit();
session.beginTransaction();
Assert.assertEquals(2, ordermanager.getAllOpenTrades().size());
session.getTransaction().commit();
session.close();
}
use of com.github.jnidzwetzki.cryptobot.entity.Trade 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.cryptobot.entity.Trade in project crypto-bot by jnidzwetzki.
the class EMABot method openOrder.
/**
* Execute a new open position order
* @param symbol
* @param endIndex
* @throws APIException
*/
private void openOrder(final BitfinexCurrencyPair symbol, final int endIndex) throws APIException {
final Decimal lastClosePrice = timeSeries.get(symbol).getLastBar().getClosePrice();
final Trade openTrade = getOpenTrade(symbol);
if (openTrade != null) {
logger.debug("Unable to open new trade, there is already one active {}", openTrade);
return;
}
final double amount = PositionSizeManager.getPositionSize(symbol, OrderType.BUY, bitfinexApiBroker.getWalletManager().getWallets());
final Trade trade = new Trade("EMA Strategy", TradeDirection.LONG, symbol, amount);
trade.setExpectedPriceOpen(lastClosePrice.doubleValue());
addTradeToOpenTradeList(trade);
orderManager.openTrade(trade);
}
use of com.github.jnidzwetzki.cryptobot.entity.Trade in project crypto-bot by jnidzwetzki.
the class EMABot method closeOrder.
/**
* Execute a new close order
* @param symbol
* @param endIndex
* @throws APIException
*/
private void closeOrder(final BitfinexCurrencyPair symbol, final int endIndex) {
final Decimal lastClosePrice = timeSeries.get(symbol).getLastBar().getClosePrice();
final Trade openTrade = getOpenTrade(symbol);
if (openTrade == null) {
logger.error("Unable to close a trade, there is no trade open");
return;
}
openTrade.setExpectedPriceClose(lastClosePrice.doubleValue());
orderManager.closeTrade(openTrade);
}
use of com.github.jnidzwetzki.cryptobot.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());
}
Aggregations