minor refactor

This commit is contained in:
Nicolás Sánchez 2024-11-28 17:27:31 -03:00
parent 8224a139f7
commit 6a0eedf7d6
3 changed files with 30 additions and 17 deletions

34
main.py
View File

@ -361,28 +361,30 @@ def main_loop():
top = 0 top = 0
for x in running_instances: for x in running_instances:
if not x.is_short: if not x.is_short:
curr += int(x.status_dict["so_amount"]) # For the safety order occupancy percentage calculation curr += int(x.get_status_dict()["so_amount"]) # For the safety order occupancy percentage calculation
top += int(x.config_dict["no_of_safety_orders"]) # It shows the percentage of safety orders not filled top += int(x.config_dict["no_of_safety_orders"]) # It shows the percentage of safety orders not filled
if not x.quit: #Why? Maybe to protect return_status() from weird errors if the trader errored out? if not x.quit: #Why? Maybe to protect return_status() from weird errors if the trader errored out?
try: try:
if x.pair in price_list and price_list[x.pair] is not None: if x.pair in price_list and price_list[x.pair] is not None:
x.status_dict["price"] = price_list[x.pair] x.get_status_dict()["price"] = price_list[x.pair]
except Exception as e: except Exception as e:
broker.logger.log_this(f"Exception while querying for pair price, key not present on price_list dictionary: {e}",1,x.pair) broker.logger.log_this(f"Exception while querying for pair price, key not present on price_list dictionary: {e}",1,x.pair)
#if "status_string" in x.status_dict: #if "status_string" in x.status_dict:
# screen_buffer.append(x.status_dict["status_string"]) # screen_buffer.append(x.status_dict["status_string"])
worker_status[x.pair] = x.status_dict worker_status[x.pair] = x.get_status_dict()
#Clear the screen buffer #Clear the screen buffer
screen_buffer.clear() screen_buffer.clear()
#Append worker data to screen buffer, shorts first. #Append worker data to screen buffer, shorts first.
for x in running_instances: for x in running_instances:
if x.is_short and "status_string" in x.status_dict: if x.is_short and "status_string" in x.get_status_dict():
screen_buffer.append(x.status_dict["status_string"]) #screen_buffer.append(x.status_dict["status_string"])
screen_buffer.append(str(x))
for x in running_instances: for x in running_instances:
if not x.is_short and "status_string" in x.status_dict: if not x.is_short and "status_string" in x.get_status_dict():
screen_buffer.append(x.status_dict["status_string"]) #screen_buffer.append(x.status_dict["status_string"])
screen_buffer.append(str(x))
#Updates some global status variables prior to deletion of those #Updates some global status variables prior to deletion of those
global_status["online_workers"] = online_pairs.copy() global_status["online_workers"] = online_pairs.copy()
@ -395,7 +397,7 @@ def main_loop():
#Check for paused pairs #Check for paused pairs
for x in running_instances: for x in running_instances:
if x.pause: if x.pause:
screen_buffer.append(f"{x.pair} paused: {x.status_dict['pause_reason']}") screen_buffer.append(f"{x.pair} paused: {x.get_status_dict()['pause_reason']}")
#Prints general info #Prints general info
instance_uptime = int(time.time()) - instance_start_time instance_uptime = int(time.time()) - instance_start_time
@ -1501,7 +1503,7 @@ def unwrapped_load_old_long(base,quote):
#Creates (or modifies) a key in the status dictionary and assigns the contents of the file to that same key. #Creates (or modifies) a key in the status dictionary and assigns the contents of the file to that same key.
for x in running_instances: for x in running_instances:
if x.pair==f"{base}/{quote}": if x.pair==f"{base}/{quote}":
x.status_dict["old_long"]=old_long x.get_status_dict()["old_long"]=old_long
x.update_status(True) x.update_status(True)
return jsonify({"Success": "old_long file loaded to status_dict"}) return jsonify({"Success": "old_long file loaded to status_dict"})
return jsonify({"Error": "Pair not found"}) return jsonify({"Error": "Pair not found"})
@ -1527,8 +1529,8 @@ def unwrapped_view_old_long(base,quote,from_file):
return jsonify(old_long) return jsonify(old_long)
for x in running_instances: for x in running_instances:
if f"{base}/{quote}"==x.pair: if f"{base}/{quote}"==x.pair:
if "old_long" in x.status_dict: if "old_long" in x.get_status_dict():
return jsonify(x.status_dict["old_long"]) return jsonify(x.get_status_dict()["old_long"])
return jsonify({"Error": "No old_long info found"}) return jsonify({"Error": "No old_long info found"})
return jsonify({"Error": "Pair not found"}) return jsonify({"Error": "Pair not found"})
except Exception as e: except Exception as e:
@ -1552,11 +1554,11 @@ def unwrapped_switch_to_long_price(base,quote):
try: try:
for x in running_instances: for x in running_instances:
if f"{base}/{quote}"==x.pair: if f"{base}/{quote}"==x.pair:
if "old_long" in x.status_dict: if "old_long" in x.get_status_dict():
#minimum_switch_price = (old_target - quote_already_in)/base_left #minimum_switch_price = (old_target - quote_already_in)/base_left
old_target = x.status_dict["old_long"]["tp_price"]*x.status_dict["old_long"]["tp_amount"] old_target = x.get_status_dict()["old_long"]["tp_price"]*x.get_status_dict()["old_long"]["tp_amount"]
base_left = x.status_dict["old_long"]["tp_amount"]-x.status_dict["base_bought"] base_left = x.get_status_dict()["old_long"]["tp_amount"]-x.get_status_dict()["base_bought"]
minimum_switch_price = (old_target - x.status_dict["quote_spent"])/base_left minimum_switch_price = (old_target - x.get_status_dict()["quote_spent"])/base_left
return jsonify({"switch_price": minimum_switch_price}) return jsonify({"switch_price": minimum_switch_price})
return jsonify({"Error": "No old_long info found"}) return jsonify({"Error": "No old_long info found"})
return jsonify({"Error": "Pair not found"}) return jsonify({"Error": "Pair not found"})
@ -1766,7 +1768,7 @@ def unwrapped_add_quote(base,quote,amount):
if x.is_short: if x.is_short:
return jsonify({"Error": "Quote can't be added to short bots"}) return jsonify({"Error": "Quote can't be added to short bots"})
x.pause = True x.pause = True
new_average_price = (x.total_amount_of_quote+float(amount))/(x.total_amount_of_base+(float(amount)/x.status_dict["price"])) new_average_price = (x.total_amount_of_quote+float(amount))/(x.total_amount_of_base+(float(amount)/x.get_status_dict()["price"]))
broker.logger.log_this(f"Your new average buy price will be {new_average_price} {x.quote}",2,f"{base}/{quote}") broker.logger.log_this(f"Your new average buy price will be {new_average_price} {x.quote}",2,f"{base}/{quote}")
broker.logger.log_this(f"Your new take profit price price will be {new_average_price*x.get_tp_level()} {x.quote}",2,f"{base}/{quote}") broker.logger.log_this(f"Your new take profit price price will be {new_average_price*x.get_tp_level()} {x.quote}",2,f"{base}/{quote}")
new_order = broker.new_market_order(x.pair,float(amount),"buy") new_order = broker.new_market_order(x.pair,float(amount),"buy")

View File

@ -8,6 +8,9 @@ Mandatory:
4. Base add for short traders. 4. Base add for short traders.
5. Proper handling of order price too high/low in OKX (rare, it happens when under heavy volatility). 5. Proper handling of order price too high/low in OKX (rare, it happens when under heavy volatility).
6. Optimize database code. 6. Optimize database code.
7. Things that should be objects (it's not 1994):
* Orders.
* A lot more.
Would be nice to have: Would be nice to have:

View File

@ -107,6 +107,14 @@ class trader:
self.restart = True self.restart = True
def __str__(self):
return self.status_dict["status_string"]
def get_status_dict(self):
return self.status_dict
def set_market_load_time(self, period: float) -> int: def set_market_load_time(self, period: float) -> int:
self.market_load_time = period self.market_load_time = period
return 0 return 0