minor fixes to string handling

This commit is contained in:
Nicolás Sánchez 2026-06-04 09:06:16 -03:00
parent d667371694
commit 5246d69063
3 changed files with 58 additions and 62 deletions

View File

@ -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

View File

@ -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()

111
trader.py
View File

@ -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<max_value:
color = self.COLORS["green"] if price>=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<max_value:
color = self.get_color("green") if price>=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_profit<low_percentage:
pct_color = self.get_color("green")
pct_color = self.COLORS["green"]
if percentage_to_profit>mid_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_price<low_price:
low_boundary_color = self.get_color("bright_green")
low_boundary_color = self.COLORS["bright_green"]
low_boundary = '{:.20f}'.format(minimum_switch_price)[:decimals].center(decimals)
if mid_price!=0:
multiplier = int(self.status.get_old_long()["tp_price"]/self.status.get_price())
except Exception as e:
print(e)
safety_order_string = f"{self.status.get_safety_orders_filled()}/{self.get_color('cyan')}{concurrent_so_amount}{self.get_color('white')}/{self.status.get_no_of_safety_orders()}".rjust(27)
prices = f"{low_boundary_color}{low_boundary}{self.get_color('white')}|{price_color}{mid_boundary}{self.get_color('white')}|{target_price_color}{high_boundary}{self.get_color('white')}|{pct_color}{pct_to_profit_str}%{self.get_color('white')}"
line1 = f"{pair_color}{self.status.get_pair().center(13)}{self.get_color('white')}| {safety_order_string} |{prices}| Uptime: {self.seconds_to_time(self.status.get_deal_uptime())}"
safety_order_string = f"{self.status.get_safety_orders_filled()}/{self.COLORS['cyan']}{concurrent_so_amount}{self.COLORS['white']}/{self.status.get_no_of_safety_orders()}".rjust(27)
prices = f"{low_boundary_color}{low_boundary}{self.COLORS['white']}|{price_color}{mid_boundary}{self.COLORS['white']}|{target_price_color}{high_boundary}{self.COLORS['white']}|{pct_color}{pct_to_profit_str}%{self.COLORS['white']}"
line1 = f"{pair_color}{self.status.get_pair().center(13)}{self.COLORS['white']}| {safety_order_string} |{prices}| Uptime: {self.seconds_to_time(self.status.get_deal_uptime())}"
if self.status.get_is_boosted():
line1 = f"{line1} | BOOSTED"
if self.config.get_autoswitch():
auto_color = self.get_color("white")
auto_color = self.COLORS['white']
if self.config.get_liquidate_after_switch():
auto_color = self.get_color("red")
line1 = f"{line1} | {auto_color}AUTO{self.get_color('white')}"
auto_color = self.COLORS['red']
line1 = f"{line1} | {auto_color}AUTO{self.COLORS['white']}"
if multiplier>1:
#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