diff --git a/src/pl/robotix/cinx/Config.java b/src/pl/robotix/cinx/Config.java index 5f8f806..40021e8 100644 --- a/src/pl/robotix/cinx/Config.java +++ b/src/pl/robotix/cinx/Config.java @@ -40,6 +40,7 @@ public void saveToDisk() throws IOException { public List getSubscribedCurrencies() { return Arrays.asList(data.getProperty(SUBSCRIBED_CURRENCIES_KEY).split(",")).stream() + .filter((symbol) -> !symbol.isEmpty()) .map((symbol) -> new Currency(symbol)).collect(toList()); } diff --git a/src/pl/robotix/cinx/CurrencySelector.java b/src/pl/robotix/cinx/CurrencySelector.java index da6a0cb..27937d2 100644 --- a/src/pl/robotix/cinx/CurrencySelector.java +++ b/src/pl/robotix/cinx/CurrencySelector.java @@ -1,6 +1,5 @@ package pl.robotix.cinx; -import static pl.robotix.cinx.Currency.USDT; import static pl.robotix.cinx.TimeRange.MONTH; import static pl.robotix.cinx.TimeRange.WEEK; import static pl.robotix.cinx.TimeRange.DAY; @@ -33,10 +32,8 @@ public void addDisplayListener(Api api, Graph graph) { chartCurrencies.addListener((Change change) -> { if (change.wasAdded()) { Currency added = change.getElementAdded(); - if (!added.equals(USDT)) { - List priceHistory = api.retrieveUSDPriceHistory(added, WEEK); - graph.display(priceHistory, added); - } + List priceHistory = api.retrieveUSDPriceHistory(added, DAY); + graph.display(priceHistory, added); } if (change.wasRemoved()) { graph.remove(change.getElementRemoved()); diff --git a/src/pl/robotix/cinx/Pair.java b/src/pl/robotix/cinx/Pair.java index 21ad1d7..152c1c1 100644 --- a/src/pl/robotix/cinx/Pair.java +++ b/src/pl/robotix/cinx/Pair.java @@ -34,6 +34,11 @@ public Pair(Currency quote, Currency base) { public Pair reverse() { return new Pair(base, quote); } + + public boolean isReverse() { + return base.equals(USDT) + || base.equals(BTC) && !quote.equals(USDT); + } @Override public String toString() { diff --git a/src/pl/robotix/cinx/Prices.java b/src/pl/robotix/cinx/Prices.java index 9162c7a..a9cb33d 100644 --- a/src/pl/robotix/cinx/Prices.java +++ b/src/pl/robotix/cinx/Prices.java @@ -58,29 +58,48 @@ public BigDecimal getRate(Pair pair) { } public List pairsToComputeUSDFor(final Currency currency) { - if (currency.equals(USDT)) { +// if (currency.equals(USDT)) { +// return Collections.emptyList(); +// } +// +// Pair currToUsd = new Pair(USDT, currency); +// BigDecimal foundPrice = prices.get(currToUsd); +// if (foundPrice != null) { +// return Arrays.asList(new Pair[]{ currToUsd }); +// } +// +// final LinkedList pairsHolder = new LinkedList<>(); +// prices.forEach((pair, price) -> { +// if (pairsHolder.isEmpty() && pair.base.equals(currency)) { +// Currency secondCurrency = pair.quote; +// List leftPairs = pairsToComputeUSDFor(secondCurrency); +// if (leftPairs != null) { +// pairsHolder.add(new Pair(secondCurrency, currency)); +// pairsHolder.addAll(leftPairs); +// } +// } +// }); +// +// return pairsHolder; + return pairsToComputePrice(USDT, currency); + } + + public List pairsToComputeBTCFor(final Currency currency) { + return pairsToComputePrice(BTC, currency); + } + + private List pairsToComputePrice(final Currency quote, final Currency base) { + if (base.equals(quote)) { return Collections.emptyList(); } - Pair currToUsd = new Pair(USDT, currency); - BigDecimal foundPrice = prices.get(currToUsd); + Pair currToX = new Pair(quote, base); + BigDecimal foundPrice = prices.get(currToX); if (foundPrice != null) { - return Arrays.asList(new Pair[]{ currToUsd }); + return Arrays.asList(currToX); } - final LinkedList pairsHolder = new LinkedList<>(); - prices.forEach((pair, price) -> { - if (pairsHolder.isEmpty() && pair.base.equals(currency)) { - Currency secondCurrency = pair.quote; - List leftPairs = pairsToComputeUSDFor(secondCurrency); - if (leftPairs != null) { - pairsHolder.add(new Pair(secondCurrency, currency)); - pairsHolder.addAll(leftPairs); - } - } - }); - - return pairsHolder; + return Arrays.asList(new Pair(quote, BTC), new Pair(BTC, base)); } public Set getAllCurrencies() { diff --git a/src/pl/robotix/cinx/api/Api.java b/src/pl/robotix/cinx/api/Api.java index 5c5f057..5c4832b 100644 --- a/src/pl/robotix/cinx/api/Api.java +++ b/src/pl/robotix/cinx/api/Api.java @@ -11,8 +11,10 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.function.Function; import com.cf.client.poloniex.PoloniexExchangeService; +import com.cf.data.model.poloniex.PoloniexChartData; import com.cf.data.model.poloniex.PoloniexCompleteBalance; import com.cf.data.model.poloniex.PoloniexOrderResult; import com.cf.data.model.poloniex.PoloniexTicker; @@ -43,7 +45,8 @@ public Api(String poloniexApiKey, String poloniexSecret) { public List retrieveUSDPriceHistory(Currency currency, TimeRange range) { List usdPriceHistory = initWithOnes(range); - List pairs = prices.pairsToComputeUSDFor(currency); +// List pairs = prices.pairsToComputeUSDFor(currency); + List pairs = prices.pairsToComputeBTCFor(currency); pairs.forEach((intermediatePair) -> { List intermediateHistory = retrievePriceHistory(intermediatePair, range); @@ -127,11 +130,18 @@ private List initWithOnes(TimeRange range) { protected List retrievePriceHistory(Pair pair, TimeRange range) { throttleControl(); + Function pointCreator; + if (pair.isReverse()) { + pointCreator = (point) -> new Point(point.date.toLocalDateTime() , 1.0 / point.weightedAverage.doubleValue()); + pair = pair.reverse(); + } else { + pointCreator = (point) -> new Point(point.date.toLocalDateTime() , point.weightedAverage.doubleValue()); + } + return service.returnChartData(pair.toString(), range.densitySeconds, range.getStart()) .stream() - .map((point) -> { - return new Point(point.date.toLocalDateTime() , point.weightedAverage.doubleValue()); - }).collect(toList()); + .map(pointCreator) + .collect(toList()); } private Prices retrievePrices() { diff --git a/src/pl/robotix/cinx/test/PricesTest.java b/src/pl/robotix/cinx/test/PricesTest.java index 5d7e807..80bc7b2 100644 --- a/src/pl/robotix/cinx/test/PricesTest.java +++ b/src/pl/robotix/cinx/test/PricesTest.java @@ -1,7 +1,5 @@ package pl.robotix.cinx.test; -import static java.math.BigDecimal.valueOf; - import java.math.BigDecimal; import java.util.Arrays; import java.util.HashMap; @@ -18,30 +16,48 @@ public class PricesTest { public static final Pair USDT_ETH = new Pair("USDT_ETH"); public static final Pair ETH_LTC = new Pair("ETH_LTC"); + public static final Pair USDT_BTC = new Pair("USDT_BTC"); + public static final Pair BTC_LTC = new Pair("BTC_LTC"); public static final Currency LTC = new Currency("LTC"); public static final Currency USDT = new Currency("USDT"); + public static final Currency BTC = new Currency("BTC"); @Test public void findsPriceChain() { Map prices = new HashMap<>(); - prices.put(USDT_ETH, valueOf(1.2)); - prices.put(ETH_LTC, valueOf(1.1)); + prices.put(USDT_ETH, new BigDecimal("1.1")); + prices.put(ETH_LTC, new BigDecimal("1.2")); + prices.put(USDT_BTC, new BigDecimal("1.32")); + prices.put(BTC_LTC, new BigDecimal("1.0")); Map volumes = new HashMap<>(); - volumes.put(USDT_ETH, valueOf(100)); - volumes.put(ETH_LTC, valueOf(0.200)); + volumes.put(USDT_ETH, new BigDecimal("100")); + volumes.put(ETH_LTC, new BigDecimal("0.200")); + volumes.put(USDT_BTC, new BigDecimal("1.0")); + volumes.put(BTC_LTC, new BigDecimal("10.0")); - Assert.assertEquals(valueOf(1.32), - new Prices(prices, volumes).getUSDFor(LTC)); + Assert.assertEquals(null, new BigDecimal("1.32").doubleValue(), + new Prices(prices, volumes).getUSDFor(LTC).doubleValue(), 0.0001); - Assert.assertEquals(Arrays.asList(ETH_LTC, USDT_ETH), +// Assert.assertEquals(Arrays.asList(ETH_LTC, USDT_ETH), +// new Prices(prices, volumes).pairsToComputeUSDFor(LTC)); + Assert.assertEquals(Arrays.asList(USDT_BTC, BTC_LTC), new Prices(prices, volumes).pairsToComputeUSDFor(LTC)); Assert.assertEquals(Arrays.asList(new Pair[] {}), new Prices(prices, volumes).pairsToComputeUSDFor(USDT)); + Assert.assertEquals(Arrays.asList(new Pair[] {}), + new Prices(prices, volumes).pairsToComputeBTCFor(BTC)); + + Assert.assertEquals(Arrays.asList(USDT_BTC.reverse()), + new Prices(prices, volumes).pairsToComputeBTCFor(USDT)); + + Assert.assertEquals(Arrays.asList(BTC_LTC), + new Prices(prices, volumes).pairsToComputeBTCFor(LTC)); + } }