diff --git a/changelog.txt b/changelog.txt index 767b2d5..2d044b2 100755 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,6 @@ 2026.06.04: . Proper tp_mode 2 fix +. Minor fixes to string handling. 2026.06.03: . Fixed tp mode 2 non-functional diff --git a/main.py b/main.py index 735d523..35baeee 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import time import logging import signal -from sys import argv +from sys import argv, stdout from os import _exit as os_exit from json import load from datetime import date @@ -410,8 +410,10 @@ def main_routine(): screen_buffer.append(separator_line) #Print screen buffer - for line in screen_buffer: - print(line) + stdout.write('\n'.join(screen_buffer)+'\n') + stdout.flush() + #for line in screen_buffer: + # print(line) #Clear the screen buffer screen_buffer.clear() diff --git a/trader.py b/trader.py index a0bd74d..b47ba70 100755 --- a/trader.py +++ b/trader.py @@ -15,6 +15,15 @@ class trader: 2: "start_trader returned error #2: Initial order never got filled. Trader will be restarted", 3: "start_trader returned error #3: Slippage threshold exceeded. Trader will be restarted"} + self.COLORS = {"yellow": "\033[0;33;40m", + "green": "\033[0;32;40m", + "red": "\033[0;31;40m", + "blue": "\033[0;34;40m", + "cyan": "\033[0;36;40m", + "bright_white": "\033[0;97;40m", + "bright_green": "\033[0;92;40m", + "white": "\033[0;37;40m"} + #Status string caches self.low_price_cache = None self.mid_price_cache = None @@ -83,21 +92,6 @@ class trader: return self.status.get_status_string() - def get_color(self, color): - ''' - Returns white if color does not exist - ''' - colors = {"yellow": "\033[0;33;40m", - "green": "\033[0;32;40m", - "red": "\033[0;31;40m", - "blue": "\033[0;34;40m", - "cyan": "\033[0;36;40m", - "bright_white": "\033[0;97;40m", - "bright_green": "\033[0;92;40m", - "white": "\033[0;37;40m"} - return colors[color] if color in colors else "\033[0;37;40m" - - def get_status_dict(self): return self.status.get_status() @@ -1583,6 +1577,24 @@ class trader: #Done return 0 + def draw_line(self,price,min_value,max_value,break_even): + ''' + It draws the progress bar according to the inputs: + * If the price is bigger or equal to the break even price, the line's color is green + * If it's lower, the line's color is red + * All the way to the left, new safety order + * All the way to the right, profit! + ''' + try: + value = int(((price-min_value)/(max_value-min_value))*80) + except Exception as e: + self.broker.logger.log_this(f"{e}") + value = 1 + if min_value=break_even else self.COLORS["red"] + else: + color = self.COLORS["red"] if price>=break_even else self.COLORS["green"] + return f"{color}{'='*value}{self.COLORS['white']}{'='*max(0,(80-value))}"[:100] def generate_status_strings(self) -> str: ''' @@ -1593,25 +1605,6 @@ class trader: low_percentage = 1 mid_percentage = 10 high_percentage = 20 - - def draw_line(price,min_value,max_value,break_even): - ''' - It draws the progress bar according to the inputs: - * If the price is bigger or equal to the break even price, the line's color is green - * If it's lower, the line's color is red - * All the way to the left, new safety order - * All the way to the right, profit! - ''' - try: - value = int(((price-min_value)/(max_value-min_value))*80) - except Exception as e: - self.broker.logger.log_this(f"{e}") - value = 1 - if min_value=break_even else self.get_color("red") - else: - color = self.get_color("red") if price>=break_even else self.get_color("green") - return f"{color}{'='*value}{self.get_color('white')}{'='*max(0,(80-value))}"[:100] low_price = self.status.get_next_so_price() if self.status.get_next_so_price() is not None else 0 mid_price = self.status.get_price() if self.status.get_price() is not None else 0 @@ -1631,9 +1624,9 @@ class trader: self.concurrent_so_amount_cache = concurrent_so_amount #Formatting - low_boundary = '{:.20f}'.format(low_price)[:decimals].center(decimals) - mid_boundary = '{:.20f}'.format(mid_price)[:decimals].center(decimals) - high_boundary = '{:.20f}'.format(high_price)[:decimals].center(decimals) + low_boundary = f"{low_price:.{decimals-2}f}"[:decimals].center(decimals) + mid_boundary = f"{mid_price:.{decimals-2}f}"[:decimals].center(decimals) + high_boundary = f"{high_price:.{decimals-2}f}"[:decimals].center(decimals) percentage_to_profit = 100 pct_to_profit_str = "XX.XX" @@ -1650,28 +1643,28 @@ class trader: line3 = "" if self.status.get_base_bought()!=0: - line3 = draw_line(mid_price,low_price,high_price,self.status.get_quote_spent()/self.status.get_base_bought()) - low_boundary_color = self.get_color("red") - price_color = self.get_color("white") - target_price_color = self.get_color("green") - pair_color = self.get_color("cyan") + line3 = self.draw_line(mid_price,low_price,high_price,self.status.get_quote_spent()/self.status.get_base_bought()) + low_boundary_color = self.COLORS["red"] + price_color = self.COLORS["white"] + target_price_color = self.COLORS["green"] + pair_color = self.COLORS["cyan"] if self.config.get_is_short(): - price_color = self.get_color("white") - pair_color = self.get_color("yellow") + price_color = self.COLORS["white"] + pair_color = self.COLORS["yellow"] if self.status.get_old_long()!={}: if mid_price>self.status.get_old_long()["tp_price"]: - price_color = self.get_color("bright_green") + price_color = self.COLORS["bright_green"] if high_price>self.status.get_old_long()["tp_price"]: - target_price_color = self.get_color("bright_green") + target_price_color = self.COLORS["bright_green"] #Set percentage's color - pct_color = self.get_color("white") + pct_color = self.COLORS["white"] if percentage_to_profitmid_percentage: - pct_color = self.get_color("yellow") + pct_color = self.COLORS["yellow"] if percentage_to_profit>high_percentage: - pct_color = self.get_color("red") + pct_color = self.COLORS["red"] multiplier = 0 if self.config.get_is_short() and self.status.get_old_long()!={}: @@ -1681,32 +1674,32 @@ class trader: base_left = self.status.get_old_long()["tp_amount"]-self.status.get_base_bought() minimum_switch_price = (old_target - self.status.get_quote_spent())/base_left if old_target-self.status.get_quote_spent()>0 and base_left>0 and minimum_switch_price1: #Only displays the multiplier if autoswitch is enabled. - line1 = f"{line1}{auto_color}x{multiplier}{self.get_color('white')}" + line1 = f"{line1}{auto_color}x{multiplier}{self.COLORS['white']}" if self.config.get_programmed_stop() and time.time()<=self.config.get_programmed_stop_time(): line1 = f"{line1} | PROGRAMMED LAST DEAL" if self.status.get_stop_when_profit(): line1 = f"{line1} | LAST DEAL" - status_string = f"{self.get_color('white')}{line1}\n{line3}{self.get_color('white')}" + status_string = f"{self.COLORS['white']}{line1}\n{line3}{self.COLORS['white']}" return status_string