Last trades done

This commit is contained in:
Nicolás Sánchez 2024-12-13 19:37:15 -03:00
parent 308fd87731
commit e046b778f6
2 changed files with 129 additions and 22 deletions

View File

@ -2,6 +2,10 @@ package com.example.dcav2gui;
import static com.example.dcav2gui.MainActivity.globalSettings; import static com.example.dcav2gui.MainActivity.globalSettings;
import android.os.Build;
import androidx.annotation.RequiresApi;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -9,8 +13,15 @@ import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import java.io.IOException; 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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
@ -63,14 +74,76 @@ public class InstanceInterface {
} }
public static List<DealData> getAllTrades() throws IOException {
CompletableFuture<List<DealData>> binanceFuture = CompletableFuture.supplyAsync(() -> {
try {
return getLastTradesFromExchange("binance");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
CompletableFuture<List<DealData>> gateioFuture = CompletableFuture.supplyAsync(() -> {
try {
return getLastTradesFromExchange("gateio");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
CompletableFuture<List<DealData>> kucoinFuture = CompletableFuture.supplyAsync(() -> {
try {
return getLastTradesFromExchange("kucoin");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
CompletableFuture<List<DealData>> okxFuture = CompletableFuture.supplyAsync(() -> {
try {
return getLastTradesFromExchange("okex");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
CompletableFuture<Void> allFutures = CompletableFuture.allOf(binanceFuture, gateioFuture, kucoinFuture, okxFuture);
List<DealData> binanceDeals = binanceFuture.join();
List<DealData> gateioDeals = gateioFuture.join();
List<DealData> kucoinDeals = kucoinFuture.join();
List<DealData> okxDeals = okxFuture.join();
// List<DealData>binanceDeals = getLastTradesFromExchange("binance");
// List<DealData>gateioDeals = getLastTradesFromExchange("gateio");
// List<DealData>kucoinDeals = getLastTradesFromExchange("kucoin");
// List<DealData>okxDeals = getLastTradesFromExchange("okex");
List<DealData> 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<DealData> getLastTradesFromExchange(String exchange) throws IOException { public static List<DealData> getLastTradesFromExchange(String exchange) throws IOException {
Request dealsRequest = new Request.Builder() Request dealsRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/get_deals_cache") .url(API_BASE_URL + "/" + exchange + "/get_deals_cache")
.header("X-API-KEY", API_KEY) .header("X-API-KEY", API_KEY)
.build(); .build();
try (Response statsResponse = httpClient.newCall(dealsRequest).execute()) { try (Response statsResponse = httpClient.newCall(dealsRequest).execute()) {
if (!statsResponse.isSuccessful()) { if (!statsResponse.isSuccessful()) {
System.err.println("Unexpected code " + statsResponse);
throw new IOException("Unexpected code " + statsResponse); throw new IOException("Unexpected code " + statsResponse);
} }
String dealsResponseBody = statsResponse.body().string(); String dealsResponseBody = statsResponse.body().string();
@ -91,18 +164,15 @@ public class InstanceInterface {
//It can be safely ignored //It can be safely ignored
JsonArray dealsArray = jsonObject.getAsJsonArray("Deals"); JsonArray dealsArray = jsonObject.getAsJsonArray("Deals");
List<DealData> dealDataList = new ArrayList<>(); List<DealData> dealDataList = new ArrayList<>();
for (int i=0; i<dealsArray.size(); i++){ for (int i=0; i<dealsArray.size(); i++){
JsonObject dealObject = dealsArray.get(i).getAsJsonObject(); JsonElement stringToMince = dealsArray.get(i);
dealDataList.add(new DealData(
long timestamp = dealObject.get("timestamp").getAsLong(); stringToMince.getAsJsonArray().get(0).getAsDouble(),
String pair = dealObject.get("pair").getAsString(); stringToMince.getAsJsonArray().get(1).getAsString(),
double amount = dealObject.get("amount").getAsDouble(); stringToMince.getAsJsonArray().get(2).getAsDouble(),
String exchangeName = dealObject.get("exchange_name").getAsString(); stringToMince.getAsJsonArray().get(3).getAsString(),
String orderId = dealObject.get("order_id").getAsString(); stringToMince.getAsJsonArray().get(4).getAsString(),
""));
DealData dealData = new DealData(timestamp, pair, amount, exchangeName, orderId, "");
dealDataList.add(dealData);
} }
return dealDataList; return dealDataList;
} }
@ -323,7 +393,6 @@ public class InstanceInterface {
public String getExchangeName() { return exchangeName; } public String getExchangeName() { return exchangeName; }
public String getOrderId() { return orderId; } public String getOrderId() { return orderId; }
public String getOrderHistory() { return orderHistory; } public String getOrderHistory() { return orderHistory; }
} }
public static class InstanceGlobalStatsData { public static class InstanceGlobalStatsData {
@ -445,4 +514,5 @@ public class InstanceInterface {
public int getLongWorkers() { return longWorkers; } public int getLongWorkers() { return longWorkers; }
public int getShortWorkers() { return shortWorkers; } public int getShortWorkers() { return shortWorkers; }
} }
} }

View File

@ -1,5 +1,6 @@
package com.example.dcav2gui.ui.home; package com.example.dcav2gui.ui.home;
import android.annotation.SuppressLint;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
@ -19,7 +20,9 @@ import com.example.dcav2gui.MainActivity;
import com.example.dcav2gui.R; import com.example.dcav2gui.R;
import com.example.dcav2gui.TickerTracker; import com.example.dcav2gui.TickerTracker;
import java.lang.reflect.Array; import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -28,7 +31,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.Future;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
@ -71,11 +73,6 @@ public class HomeFragment extends Fragment {
private TextView exchange3Funds; private TextView exchange3Funds;
private TextView exchange4Funds; private TextView exchange4Funds;
private TextView exchange1FundsNeeded;
private TextView exchange2FundsNeeded;
private TextView exchange3FundsNeeded;
private TextView exchange4FundsNeeded;
private TextView exchange1FundsPercentage; private TextView exchange1FundsPercentage;
private TextView exchange2FundsPercentage; private TextView exchange2FundsPercentage;
private TextView exchange3FundsPercentage; private TextView exchange3FundsPercentage;
@ -381,14 +378,22 @@ public class HomeFragment extends Fragment {
} }
}); });
CompletableFuture<List<InstanceInterface.DealData>> future9 = CompletableFuture.supplyAsync(() -> {
try {
return InstanceInterface.getAllTrades();
} catch (IOException e) {
System.err.print(e.toString());
return null;
}
});
// Wait for all futures to complete // Wait for all futures to complete
CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2, future3, future4, future5, future6, future7, future8); CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2, future3, future4, future5, future6, future7, future8, future9);
// Update UI // Update UI
allFutures.thenAccept(voidResult -> { allFutures.thenAccept(voidResult -> {
try { try {
requireActivity().runOnUiThread(() -> { requireActivity().runOnUiThread(() -> {
TickerTracker.PriceChangeData priceData = null; TickerTracker.PriceChangeData priceData = null;
TickerTracker.PriceChangeData priceData2 = null; TickerTracker.PriceChangeData priceData2 = null;
TickerTracker.PriceChangeData priceData3 = null; TickerTracker.PriceChangeData priceData3 = null;
@ -448,6 +453,32 @@ public class HomeFragment extends Fragment {
profitsThisMonth.setText(String.format(Locale.ROOT, "%.2f", profitsData.getProfitsThisMonth())); profitsThisMonth.setText(String.format(Locale.ROOT, "%.2f", profitsData.getProfitsThisMonth()));
//Populate deals page
try {
List<InstanceInterface.DealData> 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 //Populate logs
log1Content.setText(logs1); log1Content.setText(logs1);
log2Content.setText(logs2); 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 @Override
public void onDestroyView() { public void onDestroyView() {