I'm trying to have a dynamic display for a continuously changing positive number in a range that is also changing. For concrete example, lets imagine its representing the dynamic price of a stock where the range is its current daily range.
Say the symbol is X, which opened at $100 today, went unto $125 intra-day and is now trading at $112.5. So ideally I'd like to display it as a dynamic progress bar:
X [#### ] 112.5 [100, 125]
If the stock then goes to 130 in the same day, the same progress bar should now look like
X [########] 130 [100, 130]
(Note: I'm placing no restriction on whether the length of the progress bar should change with increase in range - all i'm asking is that the current price should be accurately represented as a percentage of the range visually.)
Any ideas what packages I should look at (I really would prefer not having to implement it from scratch!!)
Please suggest a python 3.x approach if possible as well.
This is a pretty simple example of how to make a status bar on the console:
import sys
import time
def progress(count, total, status=''):
bar_len = 60 # the lenght of the status bar
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '#' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
sys.stdout.flush()
a=0
while a < 50:
progress(a, 100)
a = a+5
time.sleep(2)
So, in the example above, the progress function is recalled in that while loop
until variable a
reaches 50. So, the important thing is the progress
function.
I hope I helped you!
You can create it yourself using the print statement:
The basestring is f"X [{variable}:>{length}] {value:.0f} [{value_min}, {value_max}]"
. What you have to find are the variables and set the length:
length = 20 # 20 characters
value = 112.5
value_min = 100
value_max = 125
percentage = ((value - value_min) / (value_max - value_min)) * 100
variable = '#' * int(percentage / 100 * length) # int is a floor function, you can deal with it how you like but we need an integer value.
string = f"X [{variable:<{length}}] {value:.0f} [{value_min}, {value_max}]"
print(string)
>>> X [########## ] 112 [100, 125]
When you put this in a for loop you can also overwrite the previous line:
import time
for x in range(10):
print(x, end='\r', flush=True)
time.sleep(0.5)
For example:
length = 20 # 20 characters
value = 112.5
value_min = 100
value_max = 125
for value in range(value_min, value_max):
percentage = ((value - value_min) / (value_max - value_min)) * 100
variable = '#' * int(percentage / 100 * length)
string = f"X [{variable:<{length}}] {value:.0f} [{value_min}, {value_max}]"
print(string)
Output: (I did not overwrite the line to show the output).
X [ ] 100 [100, 125]
X [ ] 101 [100, 125]
X [# ] 102 [100, 125]
X [## ] 103 [100, 125]
X [### ] 104 [100, 125]
X [#### ] 105 [100, 125]
X [#### ] 106 [100, 125]
X [##### ] 107 [100, 125]
X [###### ] 108 [100, 125]
X [####### ] 109 [100, 125]
X [######## ] 110 [100, 125]
X [######## ] 111 [100, 125]
X [######### ] 112 [100, 125]
X [########## ] 113 [100, 125]
X [########### ] 114 [100, 125]
X [############ ] 115 [100, 125]
X [############ ] 116 [100, 125]
X [############# ] 117 [100, 125]
X [############## ] 118 [100, 125]
X [############### ] 119 [100, 125]
X [################ ] 120 [100, 125]
X [################ ] 121 [100, 125]
X [################# ] 122 [100, 125]
X [################## ] 123 [100, 125]
X [################### ] 124 [100, 125]
When the maximum value is changed:
length = 20 # 20 characters
value_min = 100
value_max = 125
values = [112.5, 125, 130, 110]
for value in values:
if value > value_max:
value_max = value
percentage = ((value - value_min) / (value_max - value_min)) * 100
variable = '#' * int(percentage / 100 * length)
string = f"X [{variable:<{length}}] {value:.0f} [{value_min}, {value_max}]"
print(string)
Output:
X [########## ] 112 [100, 125]
X [####################] 125 [100, 125]
X [####################] 130 [100, 130]
X [###### ] 110 [100, 130]
def progress_string(symbol, start, end, actual, bar_len=10):
fill = int(bar_len*(actual-start)/(end-start))
string = "#"*fill + " "*(bar_len-fill)
if actual<start:
string = " "*bar_len
if actual>end:
string = "#"*bar_len
return "%s [%s] %.2f [%i, %i]"%(symbol, string, actual, start, end)
### TEST ###
START = 100
END = 125
X = 100
while X<=END:
X+=.001
time.sleep(.001)
sys.stdout.flush()
print(progress_string("X", START, END, X, bar_len=25), end="\r")
This will also replace the progress bar each time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.