Profit basic statistics working

This commit is contained in:
Nicolás Sánchez 2024-12-13 09:51:04 -03:00
parent f0b114bfbb
commit 090bf03a68
3 changed files with 175 additions and 49 deletions

View File

@ -0,0 +1,105 @@
package com.example.dcav2gui;
import static com.example.dcav2gui.MainActivity.globalSettings;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.util.Locale;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class InstanceInterface {
private static final String API_BASE_URL = globalSettings.apiUrl;
private static final String API_KEY = globalSettings.apiKey;
private static final OkHttpClient httpClient = new OkHttpClient();
public static ProfitStatsData getProfitStatsData() throws IOException {
// Construct the API request URL for stock quote
Request stockRequest = new Request.Builder()
.url(API_BASE_URL + "/statistics_server/combined_totals")
.header("X-API-KEY", API_KEY)
.build();
try (Response statsResponse = httpClient.newCall(stockRequest).execute()) {
if (!statsResponse.isSuccessful()) {
throw new IOException("Unexpected code " + statsResponse);
}
String stockResponseBody = statsResponse.body().string();
double todaysProfit = getTodaysProfit(stockResponseBody);
double thisMonthProfit = getThisMonthsProfit(stockResponseBody);
// Return the stock quote data
return new ProfitStatsData(
todaysProfit,
thisMonthProfit
);
}
}
private static double getTodaysProfit(String jsonResponse) {
try {
// Parse the JSON response
JsonElement jsonElement = JsonParser.parseString(jsonResponse);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return 0.0;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("combined")) {
JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray();
return combinedArray.get(0).getAsDouble();
} else {
System.err.println("The parsed JSON response does not contain a 'combined' array.");
return 0.0;
}
} catch (Exception e) {
System.err.println("Error processing combined profit data: " + e.getMessage());
return 0.0;
}
}
private static double getThisMonthsProfit(String jsonResponse) {
try {
// Parse the JSON response
JsonElement jsonElement = JsonParser.parseString(jsonResponse);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return 0.0;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("combined")) {
JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray();
return combinedArray.get(1).getAsDouble();
} else {
System.err.println("The parsed JSON response does not contain a 'combined' array.");
return 0.0;
}
} catch (Exception e) {
System.err.println("Error processing combined profit data: " + e.getMessage());
return 0.0;
}
}
// Class to hold profits stats data
public static class ProfitStatsData {
private final double profitsToday;
private final double profitsThisMonth;
public ProfitStatsData(double profitsToday, double profitsThisMonth) {
this.profitsToday = profitsToday;
this.profitsThisMonth = profitsThisMonth;
}
// Getters
public double getProfitsToday() { return profitsToday; }
public double getProfitsThisMonth() { return profitsThisMonth; }
}
}

View File

@ -14,6 +14,7 @@ import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import com.example.dcav2gui.InstanceInterface;
import com.example.dcav2gui.MainActivity; 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;
@ -24,6 +25,7 @@ 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 {
@ -176,12 +178,16 @@ public class HomeFragment extends Fragment {
log4Content = root.findViewById(R.id.log4CardContent); log4Content = root.findViewById(R.id.log4CardContent);
/*
*
* LOAD CACHED VALUES
*
*/
// Profits today and this month // Profits today and this month
profitsToday.setText(R.string.profits_today_example); profitsToday.setText(R.string.profits_today_example);
profitsThisMonth.setText(R.string.profits_this_month_example); profitsThisMonth.setText(R.string.profits_this_month_example);
// LOAD CACHED VALUES
//Prices //Prices
pricePair1.setText(R.string.default_price_ticker_1); pricePair1.setText(R.string.default_price_ticker_1);
pricePair2.setText(R.string.default_price_ticker_2); pricePair2.setText(R.string.default_price_ticker_2);
@ -334,9 +340,17 @@ public class HomeFragment extends Fragment {
} }
}); });
// Wait for all futures to complete CompletableFuture<InstanceInterface.ProfitStatsData> future4 = CompletableFuture.supplyAsync(() -> {
CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2, future3); try {
return InstanceInterface.getProfitStatsData();
} catch (IOException e) {
e.printStackTrace();
return null;
}
});
// Wait for all futures to complete
CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2, future3, future4);
// Update UI // Update UI
try { try {
@ -344,6 +358,9 @@ public class HomeFragment extends Fragment {
TickerTracker.PriceChangeData priceData2 = future2.get(); TickerTracker.PriceChangeData priceData2 = future2.get();
TickerTracker.PriceChangeData priceData3 = future3.get();; TickerTracker.PriceChangeData priceData3 = future3.get();;
InstanceInterface.ProfitStatsData profitsData = future4.get();
pricePair1.setText(String.format(Locale.ROOT,"%.2f", priceData.getCurrentPrice())); pricePair1.setText(String.format(Locale.ROOT,"%.2f", priceData.getCurrentPrice()));
pricePair124hPercentage.setText(formatPercentage(priceData.getPriceChangePercent24h())); pricePair124hPercentage.setText(formatPercentage(priceData.getPriceChangePercent24h()));
pricePair17dPercentage.setText(formatPercentage(priceData.getPriceChangePercent7d())); pricePair17dPercentage.setText(formatPercentage(priceData.getPriceChangePercent7d()));
@ -368,6 +385,10 @@ public class HomeFragment extends Fragment {
setPercentageColor(pricePair37dPercentage,priceData3.getPriceChangePercent7d()); setPercentageColor(pricePair37dPercentage,priceData3.getPriceChangePercent7d());
setPercentageColor(pricePair330dPercentage,priceData3.getPriceChangePercent30d()); setPercentageColor(pricePair330dPercentage,priceData3.getPriceChangePercent30d());
//Toast.makeText(getContext(), String.format(Locale.ROOT,"%.2f", profitsData.getProfitsToday()), Toast.LENGTH_SHORT).show();
profitsToday.setText(String.format(Locale.ROOT,"%.2f", profitsData.getProfitsToday()));
profitsThisMonth.setText(String.format(Locale.ROOT,"%.2f", profitsData.getProfitsThisMonth()));
} catch (Exception e) { } catch (Exception e) {
Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show();
} }

View File

@ -1,17 +1,17 @@
<resources> <resources>
<string name="app_name">DCAv2GUI</string> <string name="app_name" translatable="false">DCAv2GUI</string>
<string name="navigation_drawer_open">Open navigation drawer</string> <string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string> <string name="navigation_drawer_close">Close navigation drawer</string>
<string name="nav_header_title">DCAv2</string> <string name="nav_header_title" translatable="false">DCAv2</string>
<string name="nav_header_subtitle">nicolassanchez@tutanota.com</string> <string name="nav_header_subtitle">nicolassanchez@tutanota.com</string>
<string name="nav_header_desc">Navigation header</string> <string name="nav_header_desc">Navigation header</string>
<string name="action_settings">Settings</string> <string name="action_settings">Settings</string>
<string name="menu_home">Home</string> <string name="menu_home">Home</string>
<string name="menu_binance">Binance</string> <string name="menu_binance" translatable="false">Binance</string>
<string name="menu_gateio">Gate.io</string> <string name="menu_gateio" translatable="false">Gate.io</string>
<string name="menu_kucoin">KuCoin</string> <string name="menu_kucoin" translatable="false">KuCoin</string>
<string name="menu_okx">OKX</string> <string name="menu_okx" translatable="false">OKX</string>
<string name="profile_name_hint">Enter the profile name</string> <string name="profile_name_hint">Enter the profile name</string>
<string name="api_url_hint">Enter the API URL</string> <string name="api_url_hint">Enter the API URL</string>
@ -27,52 +27,52 @@
<string name="time_between_queries_title">Time between queries</string> <string name="time_between_queries_title">Time between queries</string>
<string name="log_lines_to_display_title">Log lines to display</string> <string name="log_lines_to_display_title">Log lines to display</string>
<string name="last_trades_to_display_title">Last trades to display</string> <string name="last_trades_to_display_title">Last trades to display</string>
<string name="api_url_title">API URL</string> <string name="api_url_title" translatable="false">API URL</string>
<string name="api_key_title">API key</string> <string name="api_key_title" translatable="false">API key</string>
<string name="bot_token_title">Bot token</string> <string name="bot_token_title" translatable="false">Bot token</string>
<string name="chat_id_title">Chat ID</string> <string name="chat_id_title" translatable="false">Chat ID</string>
<string name="profile_name_title">Profile name</string> <string name="profile_name_title">Profile name</string>
<string name="general_settings_title">General settings</string> <string name="general_settings_title">General settings</string>
<string name="ticker_1">BTCUSDT</string> <string name="ticker_1" translatable="false">BTCUSDT</string>
<string name="ticker_2">ETHUSDT</string> <string name="ticker_2" translatable="false">ETHUSDT</string>
<string name="ticker_3">USDTARS</string> <string name="ticker_3" translatable="false">USDTARS</string>
<string name="base_price_ticker_1">BTC</string> <string name="base_price_ticker_1" translatable="false">BTC</string>
<string name="default_price_ticker_1"> </string> <string name="default_price_ticker_1" translatable="false"> </string>
<string name="base_price_ticker_2">ETH</string> <string name="base_price_ticker_2" translatable="false">ETH</string>
<string name="default_price_ticker_2"> </string> <string name="default_price_ticker_2" translatable="false"> </string>
<string name="base_price_ticker_3">USDT</string> <string name="base_price_ticker_3" translatable="false">USDT</string>
<string name="default_price_ticker_3"> </string> <string name="default_price_ticker_3" translatable="false"> </string>
<string name="quote_price_ticker_1">USDT</string> <string name="quote_price_ticker_1" translatable="false">USDT</string>
<string name="quote_price_ticker_2">USDT</string> <string name="quote_price_ticker_2" translatable="false">USDT</string>
<string name="quote_price_ticker_3">ARS</string> <string name="quote_price_ticker_3" translatable="false">ARS</string>
<string name="h_24_title">24h</string> <string name="h_24_title" translatable="false">24h</string>
<string name="d_7_title">7d</string> <string name="d_7_title" translatable="false">7d</string>
<string name="d_30_title">30d</string> <string name="d_30_title" translatable="false">30d</string>
<string name="percentage_example"> </string> <string name="percentage_example" translatable="false"> </string>
<string name="profits_today_title">Today:</string> <string name="profits_today_title">Today:</string>
<string name="profits_today_example">420.69</string> <string name="profits_today_example" translatable="false">420.69</string>
<string name="profits_this_month_title">This month:</string> <string name="profits_this_month_title">This month:</string>
<string name="profits_this_month_example">8392.39</string> <string name="profits_this_month_example" translatable="false">8392.39</string>
<string name="profits_quote_currency">USDT</string> <string name="profits_quote_currency" translatable="false">USDT</string>
<string name="semaphore_description">Exchange semaphore</string> <string name="semaphore_description">Exchange semaphore</string>
<string name="exchange_1_name">Binance</string> <string name="exchange_1_name" translatable="false">Binance</string>
<string name="exchange_2_name">Gate.io</string> <string name="exchange_2_name" translatable="false">Gate.io</string>
<string name="exchange_3_name">KuCoin</string> <string name="exchange_3_name" translatable="false">KuCoin</string>
<string name="exchange_4_name">OKX</string> <string name="exchange_4_name" translatable="false">OKX</string>
<string name="exchange_funds_example">15203.20</string> <string name="exchange_funds_example" translatable="false">15203.20</string>
<string name="exchange_funds_needed_example">59393.39</string> <string name="exchange_funds_needed_example" translatable="false">59393.39</string>
<string name="exchange_funds_percentage_example">169%</string> <string name="exchange_funds_percentage_example" translatable="false">169%</string>
<string name="exchange_workers_online_example">20</string> <string name="exchange_workers_online_example" translatable="false">20</string>
<string name="exchange_workers_long_short_example">20/20</string> <string name="exchange_workers_long_short_example" translatable="false">20/20</string>
<string name="exchange_separator">/</string> <string name="exchange_separator" translatable="false">/</string>
<string name="exchange_online_label">online</string> <string name="exchange_online_label">online</string>
<string name="last_trades_label">Last trades</string> <string name="last_trades_label">Last trades</string>
<string name="last_trades_example">[2024/12/11 10:41:14] LUMIA/USDT | 2.29 USDT | Binance <string name="last_trades_example" translatable="false">[2024/12/11 10:41:14] LUMIA/USDT | 2.29 USDT | Binance
[2024/12/11 10:10:18] HARD/USDT | 1.4 USDT | Binance [2024/12/11 10:10:18] HARD/USDT | 1.4 USDT | Binance
[2024/12/11 09:26:51] LUMIA/USDT | 1.66 USDT | Binance [2024/12/11 09:26:51] LUMIA/USDT | 1.66 USDT | Binance
[2024/12/11 09:17:42] TROY/USDT | 0.83 USDT | Binance [2024/12/11 09:17:42] TROY/USDT | 0.83 USDT | Binance
@ -83,9 +83,9 @@
[2024/12/11 07:44:02] NEIRO/USDT | 0.27 USDT | Binance [2024/12/11 07:44:02] NEIRO/USDT | 0.27 USDT | Binance
[2024/12/11 07:09:51] TROY/USDT | 1.71 USDT | Binance</string> [2024/12/11 07:09:51] TROY/USDT | 1.71 USDT | Binance</string>
<string name="log_example">[2024/12/11 10:41:34] Everything is horrible\n[2024/12/11 10:41:34] Things are going south very rapidly\n[2024/12/11 10:41:34] Can\'t imagine how to even try to fix this\n[2024/12/11 10:41:34] Oh lord, take me now\n[2024/12/11 10:41:34] What the hell is that?!?!\n[2024/12/11 10:41:34] Oh dear\n[2024/12/11 10:41:34] What is that small red light?</string> <string name="log_example" translatable="false">[2024/12/11 10:41:34] Everything is horrible\n[2024/12/11 10:41:34] Things are going south very rapidly\n[2024/12/11 10:41:34] Can\'t imagine how to even try to fix this\n[2024/12/11 10:41:34] Oh lord, take me now\n[2024/12/11 10:41:34] What the hell is that?!?!\n[2024/12/11 10:41:34] Oh dear\n[2024/12/11 10:41:34] What is that small red light?</string>
<string name="exchange_1_log_title">Binance log</string> <string name="exchange_1_log_title" translatable="false">Binance log</string>
<string name="exchange_2_log_title">Gate.io log</string> <string name="exchange_2_log_title" translatable="false">Gate.io log</string>
<string name="exchange_3_log_title">Kucoin log</string> <string name="exchange_3_log_title" translatable="false">Kucoin log</string>
<string name="exchange_4_log_title">OKX log</string> <string name="exchange_4_log_title" translatable="false">OKX log</string>
</resources> </resources>