From e046b778f6ee58d262d2ccfd36a2fdca94c7b66e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Fri, 13 Dec 2024 19:37:15 -0300 Subject: [PATCH] Last trades done --- .../example/dcav2gui/InstanceInterface.java | 96 ++++++++++++++++--- .../dcav2gui/ui/home/HomeFragment.java | 55 +++++++++-- 2 files changed, 129 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/example/dcav2gui/InstanceInterface.java b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java index 39b99ba..9af665a 100644 --- a/app/src/main/java/com/example/dcav2gui/InstanceInterface.java +++ b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java @@ -2,6 +2,10 @@ package com.example.dcav2gui; import static com.example.dcav2gui.MainActivity.globalSettings; +import android.os.Build; + +import androidx.annotation.RequiresApi; + import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -9,8 +13,15 @@ import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import java.io.IOException; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; +import java.util.concurrent.CompletableFuture; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -63,14 +74,76 @@ public class InstanceInterface { } + public static List getAllTrades() throws IOException { + + CompletableFuture> binanceFuture = CompletableFuture.supplyAsync(() -> { + try { + return getLastTradesFromExchange("binance"); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + + CompletableFuture> gateioFuture = CompletableFuture.supplyAsync(() -> { + try { + return getLastTradesFromExchange("gateio"); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + + CompletableFuture> kucoinFuture = CompletableFuture.supplyAsync(() -> { + try { + return getLastTradesFromExchange("kucoin"); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + + CompletableFuture> okxFuture = CompletableFuture.supplyAsync(() -> { + try { + return getLastTradesFromExchange("okex"); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + + CompletableFuture allFutures = CompletableFuture.allOf(binanceFuture, gateioFuture, kucoinFuture, okxFuture); + + List binanceDeals = binanceFuture.join(); + List gateioDeals = gateioFuture.join(); + List kucoinDeals = kucoinFuture.join(); + List okxDeals = okxFuture.join(); + +// ListbinanceDeals = getLastTradesFromExchange("binance"); +// ListgateioDeals = getLastTradesFromExchange("gateio"); +// ListkucoinDeals = getLastTradesFromExchange("kucoin"); +// ListokxDeals = getLastTradesFromExchange("okex"); + + List allDeals = new ArrayList<>(); + allDeals.addAll(binanceDeals); + allDeals.addAll(gateioDeals); + allDeals.addAll(kucoinDeals); + allDeals.addAll(okxDeals); + + //Sort deals by timestamp + Collections.sort(allDeals, Comparator.comparingDouble(DealData::getTimestamp)); + Collections.reverse(allDeals); + + return allDeals.subList(0,globalSettings.amountOfLastTrades); + } + public static List getLastTradesFromExchange(String exchange) throws IOException { + Request dealsRequest = new Request.Builder() .url(API_BASE_URL + "/" + exchange + "/get_deals_cache") .header("X-API-KEY", API_KEY) .build(); + try (Response statsResponse = httpClient.newCall(dealsRequest).execute()) { if (!statsResponse.isSuccessful()) { + System.err.println("Unexpected code " + statsResponse); throw new IOException("Unexpected code " + statsResponse); } String dealsResponseBody = statsResponse.body().string(); @@ -91,18 +164,15 @@ public class InstanceInterface { //It can be safely ignored JsonArray dealsArray = jsonObject.getAsJsonArray("Deals"); List dealDataList = new ArrayList<>(); - - for (int i = 0; i < dealsArray.size(); i++) { - JsonObject dealObject = dealsArray.get(i).getAsJsonObject(); - - long timestamp = dealObject.get("timestamp").getAsLong(); - String pair = dealObject.get("pair").getAsString(); - double amount = dealObject.get("amount").getAsDouble(); - String exchangeName = dealObject.get("exchange_name").getAsString(); - String orderId = dealObject.get("order_id").getAsString(); - - DealData dealData = new DealData(timestamp, pair, amount, exchangeName, orderId, ""); - dealDataList.add(dealData); + for (int i=0; i> future9 = CompletableFuture.supplyAsync(() -> { + try { + return InstanceInterface.getAllTrades(); + } catch (IOException e) { + System.err.print(e.toString()); + return null; + } + }); + // Wait for all futures to complete - CompletableFuture allFutures = CompletableFuture.allOf(future1, future2, future3, future4, future5, future6, future7, future8); + CompletableFuture allFutures = CompletableFuture.allOf(future1, future2, future3, future4, future5, future6, future7, future8, future9); // Update UI allFutures.thenAccept(voidResult -> { try { requireActivity().runOnUiThread(() -> { - TickerTracker.PriceChangeData priceData = null; TickerTracker.PriceChangeData priceData2 = null; TickerTracker.PriceChangeData priceData3 = null; @@ -448,6 +453,32 @@ public class HomeFragment extends Fragment { profitsThisMonth.setText(String.format(Locale.ROOT, "%.2f", profitsData.getProfitsThisMonth())); + //Populate deals page + try { + List deals = future9.get(); + if (deals != null) { + StringBuilder dealsList = new StringBuilder(); + for (InstanceInterface.DealData deal : deals) { + String timestamp = timeStampConverter(deal.getTimestamp()); + String pair = deal.getPair(); + String amount = String.format(Locale.ROOT,"%.2f", deal.getAmount()); + String exchange = Character.toUpperCase(deal.getExchangeName().charAt(0)) + deal.getExchangeName().substring(1); + dealsList.append(timestamp) + .append(" | ") + .append(pair) + .append(" | ") + .append(amount) + .append(" USDT | ") + .append(exchange).append("\n"); + } + lastTrades.setText(dealsList); + } + } catch (ExecutionException e) { + throw new RuntimeException(e); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + //Populate logs log1Content.setText(logs1); log2Content.setText(logs2); @@ -468,6 +499,12 @@ public class HomeFragment extends Fragment { }); } +public static String timeStampConverter(double timestamp) { + long linuxTimestamp = (long) timestamp; // Replace with your timestamp + Date date = new Date(linuxTimestamp * 1000); // Multiply by 1000 to convert to milliseconds + @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return formatter.format(date); +} @Override public void onDestroyView() {