API global bugfixes

This commit is contained in:
Nicolás Sánchez 2025-02-27 10:00:40 -03:00
parent 2f47b77e23
commit b5783838ff
3 changed files with 68 additions and 21 deletions

View File

@ -229,7 +229,7 @@ class ConfigHandler:
file_path = self.config_file_path file_path = self.config_file_path
try: try:
with open(file_path, "r") as f: with open(file_path, "r") as f:
self.set_config(json.load(f)) self.set_config({**self.default_config_dictionary, **json.load(f)})
return 0 return 0
except Exception as e: except Exception as e:
self.broker.logger.log_this(f"Error loading config to file: {file_path}: {e}",1,self.get_pair()) self.broker.logger.log_this(f"Error loading config to file: {file_path}: {e}",1,self.get_pair())

69
main.py
View File

@ -24,7 +24,7 @@ In case the permissions of the certificate changes, reset them this way:
# ll /etc/letsencrypt/ # ll /etc/letsencrypt/
''' '''
version = "2025.02.26" version = "2025.02.27"
''' '''
Color definitions. If you want to change them, check the reference at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors Color definitions. If you want to change them, check the reference at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
@ -526,17 +526,17 @@ def return_global_status():
return unwrapped_global_status() return unwrapped_global_status()
return jsonify({'Error': 'API key invalid'}), 401 return jsonify({'Error': 'API key invalid'}), 401
#@base_api.route("/paused_traders", methods=['GET']) @base_api.route("/paused_traders", methods=['GET'])
#def return_paused_status(): def return_paused_status():
# ''' '''
# GET request GET request
#
# Parameters: Parameters:
# None None
# ''' '''
# if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys: if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys:
# return unwrapped_paused_traders() return unwrapped_paused_traders()
# return jsonify({'Error': 'API key invalid'}), 401 return jsonify({'Error': 'API key invalid'}), 401
@base_api.route("/worker_status", methods=['GET']) @base_api.route("/worker_status", methods=['GET'])
def return_worker_status(): def return_worker_status():
@ -931,6 +931,19 @@ def global_last_call():
return unwrapped_global_last_call() return unwrapped_global_last_call()
return jsonify({'Error': 'API key invalid'}), 401 return jsonify({'Error': 'API key invalid'}), 401
@base_api.route("/cancel_global_last_call", methods=['POST'])
def cancel_global_last_call():
'''
POST request
Parameters:
None
'''
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys:
return unwrapped_cancel_global_last_call()
return jsonify({'Error': 'API key invalid'}), 401
@base_api.route("/add_quote", methods=['POST']) @base_api.route("/add_quote", methods=['POST'])
def add_quote(): def add_quote():
''' '''
@ -1266,7 +1279,7 @@ def reload_safety_order():
def run_API(): def run_API():
serve(base_api, host="0.0.0.0", port=broker.get_config()["port"], threads=32) serve(base_api, host="0.0.0.0", port=broker.get_config()["port"], threads=4)
#base_api.run(host="0.0.0.0", port=broker.get_config()["port"]) #base_api.run(host="0.0.0.0", port=broker.get_config()["port"])
@ -1282,6 +1295,14 @@ def unwrapped_global_status():
return jsonify(global_status) return jsonify(global_status)
def unwrapped_paused_traders():
'''
Returns the paused_traders field from the global status dictionary of the instance.
'''
return jsonify(global_status["paused_traders"])
def unwrapped_return_worker_status(base,quote): def unwrapped_return_worker_status(base,quote):
''' '''
Returns the individual status dictionary of the trader. Returns the individual status dictionary of the trader.
@ -1768,12 +1789,26 @@ def unwrapped_global_last_call():
x.stop_when_profit = True x.stop_when_profit = True
broker.logger.log_this("Modified flag",2,f"{x.base}/{x.quote}") broker.logger.log_this("Modified flag",2,f"{x.base}/{x.quote}")
return jsonify({"Success": "All traders scheduled to go offline when profit is reached"}) return jsonify({"Success": "All traders scheduled to go offline when profit is reached"})
else: return jsonify({"Error": "No traders running"})
except Exception:
return jsonify({"Error": "Halp"})
def unwrapped_cancel_global_last_call():
'''
Cancels the last call.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
try:
if broker.get_pairs!=[]:
#broker.clear_pairs()
for x in running_instances: for x in running_instances:
x.stop_when_profit = False x.stop_when_profit = False
broker.logger.log_this("Modified flag",2,f"{x.base}/{x.quote}") broker.logger.log_this("Modified flag",2,f"{x.base}/{x.quote}")
broker.add_pair_to_config(f"{x.base}{x.quote}")
return jsonify({"Success": "Last call canceled"}) return jsonify({"Success": "Last call canceled"})
return jsonify({"Error": "No traders running"})
except Exception: except Exception:
return jsonify({"Error": "Halp"}) return jsonify({"Error": "Halp"})
@ -1991,7 +2026,9 @@ def unwrapped_toggle_restart():
new_config = broker.get_config() new_config = broker.get_config()
new_config["attempt_to_restart"] = not new_config["attempt_to_restart"] new_config["attempt_to_restart"] = not new_config["attempt_to_restart"]
broker.set_config(new_config) broker.set_config(new_config)
return jsonify({"Success": "attempt_to_restart toggled successfully"}) if broker.get_config()["attempt_to_restart"]:
return jsonify({"Success": "attempt_to_restart enabled"})
return jsonify({"Success": "attempt_to_restart disabled"})
def unwrapped_toggle_telegram(): def unwrapped_toggle_telegram():

View File

@ -36,7 +36,7 @@ INSTANCE
7) mod_global_tp_level 8) global_last_call 9) edit_loop_wait_time 7) mod_global_tp_level 8) global_last_call 9) edit_loop_wait_time
10) edit_call_wait_time 11) reload_markets 12) fetch_full_log 10) edit_call_wait_time 11) reload_markets 12) fetch_full_log
13) paused_traders 14) fetch_log 15) edit_cooldown_multiplier 13) paused_traders 14) fetch_log 15) edit_cooldown_multiplier
16) get_balance 16) get_balance 17) cancel_global_last_call
EARN EARN
31) toggle_pause 32) get_step_size 33) set_step_size 31) toggle_pause 32) get_step_size 33) set_step_size
@ -226,6 +226,7 @@ if __name__=="__main__":
print("edit_loop_wait_time modifies the pause the instance takes after processing the open orders") print("edit_loop_wait_time modifies the pause the instance takes after processing the open orders")
print("instance fetch the orders -> instance sends the orders to the traders ->") print("instance fetch the orders -> instance sends the orders to the traders ->")
print("instance waits for the traders to complete their tasks -> instance waits <loop_wait_time> seconds") print("instance waits for the traders to complete their tasks -> instance waits <loop_wait_time> seconds")
print(f"Current value is {None}")
print("The input value can be an integer or a float") print("The input value can be an integer or a float")
new_wait_time = input("Desired wait time: ") new_wait_time = input("Desired wait time: ")
if not validate_float_or_int(new_wait_time): if not validate_float_or_int(new_wait_time):
@ -241,6 +242,7 @@ if __name__=="__main__":
print("edit_call_wait_time modifies the pause that the traders take between some API calls") print("edit_call_wait_time modifies the pause that the traders take between some API calls")
print("This aims to reduce the load on the API endpoints of the broker.") print("This aims to reduce the load on the API endpoints of the broker.")
print("The input value can be an integer or a float") print("The input value can be an integer or a float")
print(f"Current value is {None}")
new_wait_time = input("Desired call wait time: ") new_wait_time = input("Desired call wait time: ")
if not validate_float_or_int(new_wait_time): if not validate_float_or_int(new_wait_time):
print("The input is invalid") print("The input is invalid")
@ -287,7 +289,8 @@ if __name__=="__main__":
elif command==15: elif command==15:
print("edit_cooldown_multiplier modifies the pause's multiplier after it hits profit.") print("edit_cooldown_multiplier modifies the pause's multiplier after it hits profit.")
print("This aims to reduce the volatility when there are big orderbook movements.") print("This aims to reduce the volatility effect on the trader when there are big orderbook movements.")
print(f"Current value is {None}")
print("The input value can be an integer or a float") print("The input value can be an integer or a float")
new_multiplier = input("Desired multiplier: ") new_multiplier = input("Desired multiplier: ")
if not validate_float_or_int(new_multiplier): if not validate_float_or_int(new_multiplier):
@ -299,13 +302,20 @@ if __name__=="__main__":
print(json.loads(requests.post(url, headers=headers, json=parameters).content)) print(json.loads(requests.post(url, headers=headers, json=parameters).content))
input("Press ENTER to continue ") input("Press ENTER to continue ")
elif command==17: elif command==16:
print("Returns the free balance of a given coin") print("Returns the free balance of a given coin")
coin = input("Input currency: ").upper() coin = input("Input currency: ").upper()
if input("Proceed? (Y/n) ") in ["Y","y",""]: if input("Proceed? (Y/n) ") in ["Y","y",""]:
url = f"{base_url}{port}/get_balance?coin={coin}" url = f"{base_url}{port}/get_balance?coin={coin}"
print(json.loads(requests.get(url,headers=headers).content)) print(json.loads(requests.get(url,headers=headers).content))
input("Press ENTER to continue ") input("Press ENTER to continue ")
elif command==17:
print("cancel_global_last_call reverts global_last_call")
if input("Proceed? (Y/n) ") in ["Y","y",""]:
url = f"{base_url}{port}/cancel_global_last_call"
print(json.loads(requests.post(url, headers=headers).content))
input("Press ENTER to continue ")
###################### ######################