cutechicken commited on
Commit
adf35bc
Β·
verified Β·
1 Parent(s): 917cf8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -21
app.py CHANGED
@@ -277,7 +277,7 @@ def create_stock_chart(ticker, period="1mo"):
277
  else:
278
  raise dl_error
279
 
280
- if stock_data.empty:
281
  logging.warning(f"No stock data found for ticker: {ticker}")
282
  return None
283
 
@@ -288,27 +288,59 @@ def create_stock_chart(ticker, period="1mo"):
288
  # κ·Έλž˜ν”„ μž‘μ„±
289
  fig, ax = plt.subplots(figsize=(10, 6))
290
 
291
- # μ’…κ°€ κ·Έλž˜ν”„
292
- ax.plot(stock_data.index, stock_data['Close'], label='Close Price', color='blue')
293
-
294
- # 이동평균선 μΆ”κ°€ (20일)
295
- if len(stock_data) > 20:
296
- stock_data['MA20'] = stock_data['Close'].rolling(window=20).mean()
297
- ax.plot(stock_data.index, stock_data['MA20'], label='20-day MA', color='orange')
298
-
299
- # κ±°λž˜λŸ‰ μ„œλΈŒν”Œλ‘― μΆ”κ°€ (κ±°λž˜λŸ‰μ΄ μžˆλŠ” 경우만)
300
- if 'Volume' in stock_data.columns and not stock_data['Volume'].isna().all():
301
- ax2 = ax.twinx()
302
- ax2.bar(stock_data.index, stock_data['Volume'], alpha=0.3, color='gray', label='Volume')
303
- ax2.set_ylabel('Volume')
304
-
305
- # λ²”λ‘€ μΆ”κ°€ (κ±°λž˜λŸ‰ μžˆλŠ” 경우)
306
- lines, labels = ax.get_legend_handles_labels()
307
- lines2, labels2 = ax2.get_legend_handles_labels()
308
- ax.legend(lines + lines2, labels + labels2, loc='upper left')
 
 
 
 
 
 
 
 
 
 
309
  else:
310
- # κ±°λž˜λŸ‰ μ—†λŠ” 경우 μ’…κ°€λ§Œ ν‘œμ‹œ
311
- ax.legend(loc='upper left')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
 
313
  # 차트 μŠ€νƒ€μΌλ§
314
  ax.set_title(f"{ticker} Stock Price")
 
277
  else:
278
  raise dl_error
279
 
280
+ if len(stock_data) == 0:
281
  logging.warning(f"No stock data found for ticker: {ticker}")
282
  return None
283
 
 
288
  # κ·Έλž˜ν”„ μž‘μ„±
289
  fig, ax = plt.subplots(figsize=(10, 6))
290
 
291
+ # μ’…κ°€ κ·Έλž˜ν”„ - λ©€ν‹°μΈλ±μŠ€ 처리
292
+ if isinstance(stock_data.columns, pd.MultiIndex):
293
+ # λ©€ν‹°μΈλ±μŠ€μΈ 경우 ('Close', ticker) ν˜•νƒœ
294
+ close_col = ('Close', ticker)
295
+ if close_col in stock_data.columns:
296
+ ax.plot(stock_data.index, stock_data[close_col], label='Close Price', color='blue')
297
+
298
+ # 이동평균선 μΆ”κ°€ (20일)
299
+ if len(stock_data) > 20:
300
+ stock_data['MA20'] = stock_data[close_col].rolling(window=20).mean()
301
+ ax.plot(stock_data.index, stock_data['MA20'], label='20-day MA', color='orange')
302
+
303
+ # κ±°λž˜λŸ‰ μ„œλΈŒν”Œλ‘― μΆ”κ°€ (κ±°λž˜λŸ‰μ΄ μžˆλŠ” 경우만)
304
+ volume_col = ('Volume', ticker)
305
+ if volume_col in stock_data.columns and not stock_data[volume_col].isna().all():
306
+ ax2 = ax.twinx()
307
+ ax2.bar(stock_data.index, stock_data[volume_col], alpha=0.3, color='gray', label='Volume')
308
+ ax2.set_ylabel('Volume')
309
+
310
+ # λ²”λ‘€ μΆ”κ°€ (κ±°λž˜λŸ‰ μžˆλŠ” 경우)
311
+ lines, labels = ax.get_legend_handles_labels()
312
+ lines2, labels2 = ax2.get_legend_handles_labels()
313
+ ax.legend(lines + lines2, labels + labels2, loc='upper left')
314
+ else:
315
+ # κ±°λž˜λŸ‰ μ—†λŠ” 경우 μ’…κ°€λ§Œ ν‘œμ‹œ
316
+ ax.legend(loc='upper left')
317
+ else:
318
+ raise ValueError(f"Close column not found in data columns: {stock_data.columns}")
319
  else:
320
+ # 일반 인덱슀인 경우
321
+ if 'Close' in stock_data.columns:
322
+ ax.plot(stock_data.index, stock_data['Close'], label='Close Price', color='blue')
323
+
324
+ # 이동평균선 μΆ”κ°€ (20일)
325
+ if len(stock_data) > 20:
326
+ stock_data['MA20'] = stock_data['Close'].rolling(window=20).mean()
327
+ ax.plot(stock_data.index, stock_data['MA20'], label='20-day MA', color='orange')
328
+
329
+ # κ±°λž˜λŸ‰ μ„œλΈŒν”Œλ‘― μΆ”κ°€ (κ±°λž˜λŸ‰μ΄ μžˆλŠ” 경우만)
330
+ if 'Volume' in stock_data.columns and not stock_data['Volume'].isna().all():
331
+ ax2 = ax.twinx()
332
+ ax2.bar(stock_data.index, stock_data['Volume'], alpha=0.3, color='gray', label='Volume')
333
+ ax2.set_ylabel('Volume')
334
+
335
+ # λ²”λ‘€ μΆ”κ°€ (κ±°λž˜λŸ‰ μžˆλŠ” 경우)
336
+ lines, labels = ax.get_legend_handles_labels()
337
+ lines2, labels2 = ax2.get_legend_handles_labels()
338
+ ax.legend(lines + lines2, labels + labels2, loc='upper left')
339
+ else:
340
+ # κ±°λž˜λŸ‰ μ—†λŠ” 경우 μ’…κ°€λ§Œ ν‘œμ‹œ
341
+ ax.legend(loc='upper left')
342
+ else:
343
+ raise ValueError(f"Close column not found in data columns: {stock_data.columns}")
344
 
345
  # 차트 μŠ€νƒ€μΌλ§
346
  ax.set_title(f"{ticker} Stock Price")