use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.
the class TestCapitalAllocation method testCapitalAllocationExchange1.
@Test
public void testCapitalAllocationExchange1() throws APIException {
final PortfolioManager portfolioManager = buildPortfolioManager();
final Map<BitfinexCurrencyPair, CurrencyEntry> entries = new HashMap<>();
final CurrencyEntry entry1 = new CurrencyEntry(BitfinexCurrencyPair.BTC_USD, 1000, 990);
entries.put(BitfinexCurrencyPair.BTC_USD, entry1);
portfolioManager.calculatePositionSizes(entries);
// Max loss = 10, max capital allocation 50%
Assert.assertEquals(0.45, entry1.getPositionSize(), DELTA);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.
the class TestCapitalAllocation method testCapitalAllocationExchangeMaxPositionLoss1.
@Test
public void testCapitalAllocationExchangeMaxPositionLoss1() throws APIException {
final PortfolioManager portfolioManager = buildPortfolioManager();
final Map<BitfinexCurrencyPair, CurrencyEntry> entries = new HashMap<>();
final CurrencyEntry entry1 = new CurrencyEntry(BitfinexCurrencyPair.BTC_USD, 1000, 0);
entries.put(BitfinexCurrencyPair.BTC_USD, entry1);
portfolioManager.calculatePositionSizes(entries);
// Max loss = 10, max capital allocation 50%
Assert.assertEquals(0.045, entry1.getPositionSize(), DELTA);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.
the class TestCapitalAllocation method testCapitalAllocationExchange2.
@Test
public void testCapitalAllocationExchange2() throws APIException {
final PortfolioManager portfolioManager = buildPortfolioManager();
final Map<BitfinexCurrencyPair, CurrencyEntry> entries = new HashMap<>();
final CurrencyEntry entry1 = new CurrencyEntry(BitfinexCurrencyPair.BTC_USD, 1000, 990);
entries.put(BitfinexCurrencyPair.BTC_USD, entry1);
final CurrencyEntry entry2 = new CurrencyEntry(BitfinexCurrencyPair.IOT_USD, 1000, 990);
entries.put(BitfinexCurrencyPair.IOT_USD, entry2);
portfolioManager.calculatePositionSizes(entries);
// Max loss = 10, max capital allocation 50%
Assert.assertEquals(0.45, entry1.getPositionSize(), DELTA);
Assert.assertEquals(0.45, entry2.getPositionSize(), DELTA);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException 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.APIException in project crypto-bot by jnidzwetzki.
the class EMABot method registerTicker.
protected void registerTicker(final BitfinexApiBroker bitfinexApiBroker) throws InterruptedException, APIException {
logger.info("Register ticker");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
tickMerger.put(currency, new BarMerger(currency, TIMEFRAME, (s, t) -> barDoneCallback(s, t)));
final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currency);
bitfinexApiBroker.getQuoteManager().subscribeTicker(symbol);
logger.info("Wait for ticker");
while (!bitfinexApiBroker.isTickerActive(symbol)) {
Thread.sleep(100);
}
bitfinexApiBroker.getQuoteManager().registerTickCallback(symbol, (s, c) -> handleTickCallback(s, c));
}
}
Aggregations