I happen to have the following df call df_trading_pair
which contains recent trading pair data BTC/USDT
:
Start Date Open Price High Price Low Price Close Price End Date
0 2022-08-11 11:25:00 24512.25 24548.24 24507.78 24542.74 2022-08-11 11:29:59.999
1 2022-08-11 11:30:00 24543.54 24576.21 24524.36 24545.57 2022-08-11 11:34:59.999
2 2022-08-11 11:35:00 24545.08 24598.75 24541.00 24582.48 2022-08-11 11:39:59.999
3 2022-08-11 11:40:00 24583.40 24587.86 24552.52 24585.19 2022-08-11 11:44:59.999
4 2022-08-11 11:45:00 24586.40 24629.78 24578.02 24605.81 2022-08-11 11:49:59.999
5 2022-08-11 11:50:00 24605.81 24685.20 24597.07 24605.73 2022-08-11 11:54:59.999
6 2022-08-11 11:55:00 24605.73 24627.63 24533.40 24559.37 2022-08-11 11:59:59.999
7 2022-08-11 12:00:00 24559.37 24629.27 24546.00 24605.93 2022-08-11 12:04:59.999
8 2022-08-11 12:05:00 24606.71 24629.72 24592.89 24608.49 2022-08-11 12:09:59.999
9 2022-08-11 12:10:00 24608.49 24690.00 24582.14 24643.84 2022-08-11 12:14:59.999
10 2022-08-11 12:15:00 24644.79 24734.49 24602.37 24653.81 2022-08-11 12:19:59.999
11 2022-08-11 12:20:00 24653.81 24711.33 24635.80 24658.57 2022-08-11 12:24:59.999
12 2022-08-11 12:25:00 24658.57 24686.70 24593.81 24612.47 2022-08-11 12:29:59.999
13 2022-08-11 12:30:00 24613.83 24918.54 24531.05 24614.16 2022-08-11 12:34:59.999
14 2022-08-11 12:35:00 24617.40 24770.00 24567.41 24738.36 2022-08-11 12:39:59.999
15 2022-08-11 12:40:00 24737.14 24766.82 24640.00 24687.93 2022-08-11 12:44:59.999
16 2022-08-11 12:45:00 24690.17 24780.00 24687.21 24729.00 2022-08-11 12:49:59.999
17 2022-08-11 12:50:00 24730.52 24779.32 24698.54 24768.73 2022-08-11 12:54:59.999
When executing df_trading_pair.dtypes
, the following output is returned:
Start Date datetime64[ns]
Open Price float64
High Price float64
Low Price float64
Close Price float64
End Date datetime64[ns]
dtype: object
I am interested in plotting the above data to make it look like the following image:
So, I decided to write the following lines of code for it:
import matplotlib.pyplot as plt
import numpy as np
def candlestick(t, o, h, l, c):
plt.figure(figsize=(16, 12), dpi=80)
color = ["green" if close_price >= open_price else "red" for close_price, open_price in zip(c, o)]
plt.bar(x=t, height=np.abs(o-c), bottom=np.min((o,c), axis=0), width=0.6, color=color)
plt.bar(x=t, height=h-l, bottom=l, width=0.1, color=color)
candlestick(df_trading_pair["Start Date"],
df_trading_pair["Open Price"],
df_trading_pair["High Price"],
df_trading_pair["Low Price"],
df_trading_pair["Close Price"])
plt.grid(alpha=0.2)
plt.show()
In my head, that worked very well, but after executing said function, the reality could not be more embarrassing:
I have come to kindly request a feedback and a possible alternative/correction to said custom function to get the desired output.
I think the statement yaxis.set_label_position("right")
could be added at the beginning of the function candlestick
to set the y-axis values to the right of the chart, but I don't know how I could make the x-axis values display only the time format HH:MM:00
(eg: 11:25:00
) of the column Start Date
.
Can you help me?
I decided to go for an alternative:
Departure: