Spaces:
Running
Running
Create charts.py
Browse files
charts.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt
|
| 2 |
+
import matplotlib.dates as mdates
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
|
| 5 |
+
def plot_candlestick(df):
|
| 6 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 7 |
+
df = df.tail(50)
|
| 8 |
+
|
| 9 |
+
for idx, row in df.iterrows():
|
| 10 |
+
color = 'green' if row['close'] >= row['open'] else 'red'
|
| 11 |
+
ax.plot([row['timestamp'], row['timestamp']], [row['low'], row['high']], color='black')
|
| 12 |
+
ax.plot([row['timestamp'], row['timestamp']], [row['open'], row['close']], color=color, linewidth=5)
|
| 13 |
+
|
| 14 |
+
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
|
| 15 |
+
plt.xticks(rotation=45)
|
| 16 |
+
plt.title('Latest Candlestick Chart')
|
| 17 |
+
plt.tight_layout()
|
| 18 |
+
|
| 19 |
+
buf = BytesIO()
|
| 20 |
+
plt.savefig(buf, format='png')
|
| 21 |
+
buf.seek(0)
|
| 22 |
+
plt.close(fig)
|
| 23 |
+
return buf
|