Skip to content

Commit

Permalink
graph relative to BTC instead USDT
Browse files Browse the repository at this point in the history
  • Loading branch information
loziniak committed Jan 17, 2018
1 parent e331b66 commit 1d91d22
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/pl/robotix/cinx/Config.java
Expand Up @@ -40,6 +40,7 @@ public void saveToDisk() throws IOException {

public List<Currency> getSubscribedCurrencies() {
return Arrays.asList(data.getProperty(SUBSCRIBED_CURRENCIES_KEY).split(",")).stream()
.filter((symbol) -> !symbol.isEmpty())
.map((symbol) -> new Currency(symbol)).collect(toList());
}

Expand Down
7 changes: 2 additions & 5 deletions 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;
Expand Down Expand Up @@ -33,10 +32,8 @@ public void addDisplayListener(Api api, Graph graph) {
chartCurrencies.addListener((Change<? extends Currency> change) -> {
if (change.wasAdded()) {
Currency added = change.getElementAdded();
if (!added.equals(USDT)) {
List<Point> priceHistory = api.retrieveUSDPriceHistory(added, WEEK);
graph.display(priceHistory, added);
}
List<Point> priceHistory = api.retrieveUSDPriceHistory(added, DAY);
graph.display(priceHistory, added);
}
if (change.wasRemoved()) {
graph.remove(change.getElementRemoved());
Expand Down
5 changes: 5 additions & 0 deletions src/pl/robotix/cinx/Pair.java
Expand Up @@ -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() {
Expand Down
53 changes: 36 additions & 17 deletions src/pl/robotix/cinx/Prices.java
Expand Up @@ -58,29 +58,48 @@ public BigDecimal getRate(Pair pair) {
}

public List<Pair> 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<Pair> pairsHolder = new LinkedList<>();
// prices.forEach((pair, price) -> {
// if (pairsHolder.isEmpty() && pair.base.equals(currency)) {
// Currency secondCurrency = pair.quote;
// List<Pair> leftPairs = pairsToComputeUSDFor(secondCurrency);
// if (leftPairs != null) {
// pairsHolder.add(new Pair(secondCurrency, currency));
// pairsHolder.addAll(leftPairs);
// }
// }
// });
//
// return pairsHolder;
return pairsToComputePrice(USDT, currency);
}

public List<Pair> pairsToComputeBTCFor(final Currency currency) {
return pairsToComputePrice(BTC, currency);
}

private List<Pair> 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<Pair> pairsHolder = new LinkedList<>();
prices.forEach((pair, price) -> {
if (pairsHolder.isEmpty() && pair.base.equals(currency)) {
Currency secondCurrency = pair.quote;
List<Pair> 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<Currency> getAllCurrencies() {
Expand Down
18 changes: 14 additions & 4 deletions src/pl/robotix/cinx/api/Api.java
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +45,8 @@ public Api(String poloniexApiKey, String poloniexSecret) {
public List<Point> retrieveUSDPriceHistory(Currency currency, TimeRange range) {
List<Point> usdPriceHistory = initWithOnes(range);

List<Pair> pairs = prices.pairsToComputeUSDFor(currency);
// List<Pair> pairs = prices.pairsToComputeUSDFor(currency);
List<Pair> pairs = prices.pairsToComputeBTCFor(currency);
pairs.forEach((intermediatePair) -> {
List<Point> intermediateHistory = retrievePriceHistory(intermediatePair, range);

Expand Down Expand Up @@ -127,11 +130,18 @@ private List<Point> initWithOnes(TimeRange range) {
protected List<Point> retrievePriceHistory(Pair pair, TimeRange range) {
throttleControl();

Function<PoloniexChartData, Point> 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() {
Expand Down
34 changes: 25 additions & 9 deletions 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;
Expand All @@ -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<Pair, BigDecimal> 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<Pair, BigDecimal> 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));

}

}

0 comments on commit 1d91d22

Please sign in to comment.