From 6a0eedf7d6a6490970fd49da750dad946e1fba18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Thu, 28 Nov 2024 17:27:31 -0300 Subject: [PATCH] minor refactor --- main.py | 34 ++++++++++++++++++---------------- todo.txt | 3 +++ trader.py | 10 +++++++++- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index e947082..3caac0a 100644 --- a/main.py +++ b/main.py @@ -361,28 +361,30 @@ def main_loop(): top = 0 for x in running_instances: 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 if not x.quit: #Why? Maybe to protect return_status() from weird errors if the trader errored out? try: 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: 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: # 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 screen_buffer.clear() #Append worker data to screen buffer, shorts first. for x in running_instances: - if x.is_short and "status_string" in x.status_dict: - screen_buffer.append(x.status_dict["status_string"]) + if x.is_short and "status_string" in x.get_status_dict(): + #screen_buffer.append(x.status_dict["status_string"]) + screen_buffer.append(str(x)) for x in running_instances: - if not x.is_short and "status_string" in x.status_dict: - screen_buffer.append(x.status_dict["status_string"]) + if not x.is_short and "status_string" in x.get_status_dict(): + #screen_buffer.append(x.status_dict["status_string"]) + screen_buffer.append(str(x)) #Updates some global status variables prior to deletion of those global_status["online_workers"] = online_pairs.copy() @@ -395,7 +397,7 @@ def main_loop(): #Check for paused pairs for x in running_instances: 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 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. for x in running_instances: 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) return jsonify({"Success": "old_long file loaded to status_dict"}) return jsonify({"Error": "Pair not found"}) @@ -1527,8 +1529,8 @@ def unwrapped_view_old_long(base,quote,from_file): return jsonify(old_long) for x in running_instances: if f"{base}/{quote}"==x.pair: - if "old_long" in x.status_dict: - return jsonify(x.status_dict["old_long"]) + if "old_long" in x.get_status_dict(): + return jsonify(x.get_status_dict()["old_long"]) return jsonify({"Error": "No old_long info found"}) return jsonify({"Error": "Pair not found"}) except Exception as e: @@ -1552,11 +1554,11 @@ def unwrapped_switch_to_long_price(base,quote): try: for x in running_instances: 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 - old_target = x.status_dict["old_long"]["tp_price"]*x.status_dict["old_long"]["tp_amount"] - base_left = x.status_dict["old_long"]["tp_amount"]-x.status_dict["base_bought"] - minimum_switch_price = (old_target - x.status_dict["quote_spent"])/base_left + old_target = x.get_status_dict()["old_long"]["tp_price"]*x.get_status_dict()["old_long"]["tp_amount"] + base_left = x.get_status_dict()["old_long"]["tp_amount"]-x.get_status_dict()["base_bought"] + minimum_switch_price = (old_target - x.get_status_dict()["quote_spent"])/base_left return jsonify({"switch_price": minimum_switch_price}) return jsonify({"Error": "No old_long info found"}) return jsonify({"Error": "Pair not found"}) @@ -1766,7 +1768,7 @@ def unwrapped_add_quote(base,quote,amount): if x.is_short: return jsonify({"Error": "Quote can't be added to short bots"}) 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 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") diff --git a/todo.txt b/todo.txt index fd0045e..129e5e6 100755 --- a/todo.txt +++ b/todo.txt @@ -8,6 +8,9 @@ Mandatory: 4. Base add for short traders. 5. Proper handling of order price too high/low in OKX (rare, it happens when under heavy volatility). 6. Optimize database code. +7. Things that should be objects (it's not 1994): + * Orders. + * A lot more. Would be nice to have: diff --git a/trader.py b/trader.py index 45e0257..a270afe 100755 --- a/trader.py +++ b/trader.py @@ -106,7 +106,15 @@ class trader: self.pause = False 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: self.market_load_time = period return 0