base_add_calculation_endpoint

This commit is contained in:
Nicolás Sánchez 2025-06-04 18:01:45 -03:00
parent 1a7de2f5a0
commit 25ee4bd106
3 changed files with 64 additions and 4 deletions

50
main.py
View File

@ -16,7 +16,7 @@ import exchange_wrapper
import trader
version = "2025.05.31"
version = "2025.06.04"
'''
Color definitions. If you want to change them, check the reference at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
@ -547,6 +547,28 @@ def return_switch_price():
return jsonify({'Error': 'Halp'})
return jsonify({'Error': 'API key invalid'}), 401
@base_api.route("/base_add_so_calculation", methods=["GET"])
def return_base_add_so_calculation():
'''
GET request
Parameters:
base: str
quote: str
'''
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys:
try:
base = request.args.get("base")
quote = request.args.get("quote")
return unwrapped_base_add_so_calculation(base,quote)
except Exception as e:
print(e)
return jsonify({'Error': 'Halp'})
return jsonify({'Error': 'API key invalid'}), 401
@base_api.route("/get_all_worker_status", methods=['GET'])
def return_all_worker_status():
'''
@ -1642,6 +1664,32 @@ def unwrapped_add_safety_orders(base,quote,amount):
return jsonify({"Error": "Error adding safety orders"})
def unwrapped_base_add_so_calculation(base,quote):
'''
Calculates the amount of safety order that can be added to a short trader with the current available base currency.
Parameters:
base (str): The base currency of the pair.
quote (str): The quote currency of the pair.
Returns:
jsonify: A jsonified dictionary with the amount of orders that can be added.
'''
try:
for x in running_instances:
if f"{base}/{quote}"==x.config.get_pair():
free_base = x.fetch_free_base()
if free_base is None:
return jsonify({"Error": "Can't fetch amount of free base on the exchange"})
amount_of_orders = x.base_add_calculation(free_base)
return jsonify({"Amount": amount_of_orders, "Free base on exchange": free_base})
return jsonify({"Error": "Can't find the pair in the running instances"})
except Exception as e:
broker.logger.log_this(f"{e}",2,f"{base}/{quote}")
return jsonify({"Error": "Error in unwrapped_base_add_so_calculation"})
def unwrapped_mod_tp_level(base,quote,amount):
'''
Modifies the take profit percentage of a pair. It applies the new percentage only after a new TP order is sent.

View File

@ -335,11 +335,10 @@ class trader:
if not self.config.get_is_short(): # Only works for short traders.
return 0
safety_price_table = self.calculate_safety_prices(self.status.get_start_price(),max_so,self.config.get_safety_order_deviance())
amount_accumulated = 0
so_count = 0
for i in range(self.status.get_so_amount()+1,max_so+1):
amount_accumulated+= self.gib_so_size(self.status.get_start_price(),i,self.config.get_safety_order_scale())/safety_price_table[i]
amount_accumulated+= self.gib_so_size(self.status.get_order_size(),i,self.config.get_safety_order_scale())
if amount_accumulated >= base_currency_amount:
return so_count
so_count+=1

View File

@ -57,6 +57,7 @@ TRADERS
68) toggle_check_old_long_price 69) switch_quote_currency
70) reload_safety_order 71) view_old_long 72) switch_price
73) reload_trader_config 74) toggle_liquidate_after_switch
75) base_add_calculation
98) Change broker 99) Exit
'''
@ -818,3 +819,15 @@ if __name__=="__main__":
"quote": quote}
print(json.loads(requests.post(url, headers=headers, json=parameters).content))
input("Press ENTER to continue ")
elif command==75:
print("Returns the amount of safety orders that can be added to a short trader with the available funds")
trading_pair = input("Input trader in the format BASE/QUOTE: ").upper()
if not validate_pair(trading_pair):
print("The input is invalid")
break
if input("Proceed? (Y/n) ") in ["Y","y",""]:
base,quote = trading_pair.split("/")
url = f"{base_url}{port}/base_add_so_calculation?base={base}&quote={quote}"
print(json.loads(requests.get(url,headers=headers).content))
input("Press ENTER to continue ")