I have a log file that has a trades summary, the log file looks like this
date time amt sell/buy type current_price
2020-08-03 01:20:06 1.5 0.00187130 SELL 0.00187030
2020-08-03 01:23:20 1.1 0.00187110 SELL 0.00187090
2020-08-03 01:26:36 1.8 0.00187190 SELL 0.00187070
2020-08-03 01:29:52 1.3 0.00187160 SELL 0.00186820
2020-08-03 01:36:24 1.4 0.00186580 BUY 0.00186780
2020-08-03 01:39:41 1.4 0.00186860 SELL 0.00186640
2020-08-03 01:42:54 1.3 0.00186710 BUY 0.00186880
2020-08-03 01:46:10 1.0 0.00186990 SELL 0.00186980
2020-08-03 01:49:25 1.2 0.00186840 BUY 0.00186850
my goal is to graph the data inside this log file the values I want to graph are the date_time
format the sell/buy
price and the current_price
i read the lines from the log file with readlines
and for each line, I append date_time
and current_price
to date_time_lst
and current_prices_lst
then for each trade I append the sell/buy
price and date_time
to other lists, this is obviously not efficient and time-consuming also when I get the graph with plt.show
I get the values that are out of order on the graph.
as you can see the y-axis is not in ascending or descending order, what I am doing wrong and how can I improve on this
date_time_lst = []
current_price_lst = []
BUYS_prices_lst = []
SELLS_prices_lst = []
BUYS_DATES =[]
SELLS_DATES = []
with open(file__name , "r") as r_handle:
log_lines = r_handle.readlines()
for line in log_lines:
line = line.split()
date_time_lst.append(str(line[0] + " " + line[1]))
current_price_lst.append(line[5])
if line[4] == "SELL":
## the amount of BNB multiplyed by the sell price returns the amount of bitcoin we get from the trade
amt = float(line[2])*float(line[3])
SELLS_prices_lst.append(line[3])
SELLS_DATES.append(str(line[0] + " " + line[1]))
## the amount above gets added to the qoute_balance (bitcoin_balance)
qoute_balance += amt
## the original BNB amount is subtracted from the base_balance (binance_balance)
base_balance -= float(line[2])
elif line[4] == "BUY":
amt_ = float(line[2])*float(line[3])
qoute_balance += amt_
base_balance -= float(line[2])
BUYS_prices_lst.append(line[3])
BUYS_DATES.append(str(line[0] + " " + line[1]))
plt.rcParams['axes.facecolor'] = 'grey'
plt.grid(color="black")
plt.scatter(date_time_lst , current_price_lst, color='green' , label='current_coin_prices')
plt.legend(loc='lower right')
plt.show()
the values that are out of order on the graph.
The numbers are read as strings. Turn them into floats before appending them to the lists.
price = float(line[3]) current = float(line[5])
Turn the date and time columns into a datetime object
dt = line[0] + ' ' + line[1] dt = datetime.datetime.strptime(dt,'%Y-%m-%d %H:%M:%S')
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.