Spaces:
Running
Running
Update charts.py
Browse files
charts.py
CHANGED
|
@@ -3,21 +3,28 @@ import matplotlib.dates as mdates
|
|
| 3 |
from io import BytesIO
|
| 4 |
|
| 5 |
def plot_candlestick(df):
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 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 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from io import BytesIO
|
| 4 |
|
| 5 |
def plot_candlestick(df):
|
| 6 |
+
try:
|
| 7 |
+
df = df.tail(50).copy()
|
| 8 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 9 |
|
| 10 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'])
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
for idx, row in df.iterrows():
|
| 13 |
+
color = 'green' if row['close'] >= row['open'] else 'red'
|
| 14 |
+
ax.plot([row['timestamp'], row['timestamp']], [row['low'], row['high']], color='black')
|
| 15 |
+
ax.plot([row['timestamp'], row['timestamp']], [row['open'], row['close']], color=color, linewidth=5)
|
| 16 |
|
| 17 |
+
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
|
| 18 |
+
ax.xaxis.set_major_locator(mdates.AutoDateLocator())
|
| 19 |
+
plt.xticks(rotation=45)
|
| 20 |
+
plt.title('Latest Candlestick Chart')
|
| 21 |
+
plt.tight_layout()
|
| 22 |
+
|
| 23 |
+
buf = BytesIO()
|
| 24 |
+
plt.savefig(buf, format='png')
|
| 25 |
+
buf.seek(0)
|
| 26 |
+
plt.close(fig)
|
| 27 |
+
return buf
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print("Chart error:", e)
|
| 30 |
+
raise e
|