Added mod_concurrent_safety_orders

This commit is contained in:
Nicolás Sánchez 2025-09-05 16:36:55 -03:00
parent ab82f95a03
commit 5983fa244e
7 changed files with 93 additions and 1 deletions

View File

@ -4,7 +4,7 @@
<selectionStates> <selectionStates>
<SelectionState runConfigName="app"> <SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" /> <option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2025-06-16T13:28:36.173409509Z"> <DropdownSelection timestamp="2025-09-05T19:18:31.537268933Z">
<Target type="DEFAULT_BOOT"> <Target type="DEFAULT_BOOT">
<handle> <handle>
<DeviceId pluginId="LocalEmulator" identifier="path=/home/nicolas/.android/avd/Pixel_6_Pro_API_34.avd" /> <DeviceId pluginId="LocalEmulator" identifier="path=/home/nicolas/.android/avd/Pixel_6_Pro_API_34.avd" />

View File

@ -648,6 +648,49 @@ public class WorkerInterface {
} }
} }
public static JsonObject modConcurrentSafetyOrders(String exchange, String pair, int amount, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/");
String base = pairBaseAndQuote[0];
String quote = pairBaseAndQuote[1];
Gson gson = new Gson();
JsonObject jsonPayload = new JsonObject();
jsonPayload.addProperty("base", base);
jsonPayload.addProperty("quote", quote);
jsonPayload.addProperty("amount", String.valueOf(amount));
String jsonPayloadString = gson.toJson(jsonPayload);
RequestBody requestBody = RequestBody.create(jsonPayloadString, MediaType.get("application/json; charset=utf-8"));
Request modConcurrentSafetyOrdersRequest = new Request.Builder()
.url(API_BASE_URL + "/" + exchange + "/mod_concurrent_safety_orders")
.header("X-API-KEY", API_KEY)
.post(requestBody)
.build();
try (Response modConcurrentSafetyOrdersResponse = httpClient.newCall(modConcurrentSafetyOrdersRequest).execute()) {
if (!modConcurrentSafetyOrdersResponse.isSuccessful()) {
if (modConcurrentSafetyOrdersResponse.code() == 503 && retry) {
return modConcurrentSafetyOrders(exchange, pair, amount,false);
}
throw new IOException("Unexpected code " + modConcurrentSafetyOrdersResponse);
}
String modConcurrentSafetyOrdersResponseBody = modConcurrentSafetyOrdersResponse.body().string();
JsonElement jsonElement = JsonParser.parseString(modConcurrentSafetyOrdersResponseBody);
if (!jsonElement.isJsonObject()) {
System.err.println("The parsed JSON response is not a JsonObject.");
return null;
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("Error")) {
System.err.println("The parsed JSON response contains Error");
return jsonObject;
}
//If no error, the response is {"Success": "Success. The change will take effect as new safety orders are sent or filled"}
return jsonObject;
}
}
public static JsonObject addSafetyOrders(String exchange, String pair, int amount, boolean retry) throws IOException { public static JsonObject addSafetyOrders(String exchange, String pair, int amount, boolean retry) throws IOException {
String[] pairBaseAndQuote = pair.split("/"); String[] pairBaseAndQuote = pair.split("/");
@ -1032,6 +1075,41 @@ public class WorkerInterface {
builder.show(); builder.show();
} }
public static void sendModifyConcurrentSafetyOrdersCall(String exchange, String pair, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Modify concurrent safety orders of "+ pair);
final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
builder.setView(input);
builder.setPositiveButton("Modify concurrent safety orders", (dialog, which) -> {
final int newAmount = Integer.parseInt(input.getText().toString());
new Thread(() -> {
try {
JsonObject response = WorkerInterface.modConcurrentSafetyOrders(exchange, pair, newAmount, true);
new Handler(Looper.getMainLooper()).post(() -> {
showToggleDialog(response, context);
});
} catch (IOException e) {
e.printStackTrace();
// Show an error dialog on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
AlertDialog.Builder errorBuilder = new AlertDialog.Builder(context);
errorBuilder.setTitle("Error");
errorBuilder.setMessage("Failed to modify concurrent safety orders: " + e.getMessage());
errorBuilder.setPositiveButton("OK", null);
errorBuilder.show();
});
}
}).start();
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
public static void sendAddSafetyOrdersCall(String exchange, String pair, Context context) { public static void sendAddSafetyOrdersCall(String exchange, String pair, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);

View File

@ -100,6 +100,9 @@ public class BinanceFragment extends Fragment implements WorkerCardAdapter.OnCar
} else if (item.getItemId() == R.id.switchQuoteCurrency) { } else if (item.getItemId() == R.id.switchQuoteCurrency) {
WorkerInterface.sendSwitchQuoteCurrencyCall("binance", pair, getContext()); WorkerInterface.sendSwitchQuoteCurrencyCall("binance", pair, getContext());
return true; return true;
} else if (item.getItemId() == R.id.modConcurrentSafetyOrders) {
WorkerInterface.sendModifyConcurrentSafetyOrdersCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addSafetyOrders) { } else if (item.getItemId() == R.id.addSafetyOrders) {
WorkerInterface.sendAddSafetyOrdersCall("binance", pair, getContext()); WorkerInterface.sendAddSafetyOrdersCall("binance", pair, getContext());
return true; return true;

View File

@ -99,6 +99,9 @@ public class GateioFragment extends Fragment implements WorkerCardAdapter.OnCard
} else if (item.getItemId() == R.id.switchQuoteCurrency) { } else if (item.getItemId() == R.id.switchQuoteCurrency) {
WorkerInterface.sendSwitchQuoteCurrencyCall("gateio", pair, getContext()); WorkerInterface.sendSwitchQuoteCurrencyCall("gateio", pair, getContext());
return true; return true;
} else if (item.getItemId() == R.id.modConcurrentSafetyOrders) {
WorkerInterface.sendModifyConcurrentSafetyOrdersCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addSafetyOrders) { } else if (item.getItemId() == R.id.addSafetyOrders) {
WorkerInterface.sendAddSafetyOrdersCall("gateio", pair, getContext()); WorkerInterface.sendAddSafetyOrdersCall("gateio", pair, getContext());
return true; return true;

View File

@ -99,6 +99,9 @@ public class KucoinFragment extends Fragment implements WorkerCardAdapter.OnCard
} else if (item.getItemId() == R.id.switchQuoteCurrency) { } else if (item.getItemId() == R.id.switchQuoteCurrency) {
WorkerInterface.sendSwitchQuoteCurrencyCall("kucoin", pair, getContext()); WorkerInterface.sendSwitchQuoteCurrencyCall("kucoin", pair, getContext());
return true; return true;
} else if (item.getItemId() == R.id.modConcurrentSafetyOrders) {
WorkerInterface.sendModifyConcurrentSafetyOrdersCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addSafetyOrders) { } else if (item.getItemId() == R.id.addSafetyOrders) {
WorkerInterface.sendAddSafetyOrdersCall("kucoin", pair, getContext()); WorkerInterface.sendAddSafetyOrdersCall("kucoin", pair, getContext());
return true; return true;

View File

@ -99,6 +99,9 @@ public class OkxFragment extends Fragment implements WorkerCardAdapter.OnCardLon
} else if (item.getItemId() == R.id.switchQuoteCurrency) { } else if (item.getItemId() == R.id.switchQuoteCurrency) {
WorkerInterface.sendSwitchQuoteCurrencyCall("okex", pair, getContext()); WorkerInterface.sendSwitchQuoteCurrencyCall("okex", pair, getContext());
return true; return true;
} else if (item.getItemId() == R.id.modConcurrentSafetyOrders) {
WorkerInterface.sendModifyConcurrentSafetyOrdersCall("binance", pair, getContext());
return true;
} else if (item.getItemId() == R.id.addSafetyOrders) { } else if (item.getItemId() == R.id.addSafetyOrders) {
WorkerInterface.sendAddSafetyOrdersCall("okex", pair, getContext()); WorkerInterface.sendAddSafetyOrdersCall("okex", pair, getContext());
return true; return true;

View File

@ -42,6 +42,8 @@
<item android:id="@+id/workerMenuAdd" <item android:id="@+id/workerMenuAdd"
android:title="Add..."> android:title="Add...">
<menu> <menu>
<item android:id="@+id/modConcurrentSafetyOrders"
android:title="Modify concurrent safety orders"/>
<item android:id="@+id/addSafetyOrders" <item android:id="@+id/addSafetyOrders"
android:title="Add safety orders"/> android:title="Add safety orders"/>
<item android:id="@+id/addQuote" <item android:id="@+id/addQuote"