import pandas as pd
import yfinance as yf
import datetime
import json
import plotly.graph_objs as go
def load_sepa_stocks():
try:
df = pd.read_csv('2024-11-18T12-28_export.csv')
df['criteria_details'] = df['criteria_details'].apply(json.loads)
return df
except Exception as e:
print(f"CSV 파일 로드 중 오류 발생: {str(e)}")
return None
def calculate_technical_indicators(df):
if len(df) < 200:
return None
try:
df["MA5"] = df["Close"].rolling(window=5).mean()
df["MA50"] = df["Close"].rolling(window=50).mean()
df["MA150"] = df["Close"].rolling(window=150).mean()
df["MA200"] = df["Close"].rolling(window=200).mean()
return df
except Exception as e:
print(f"지표 계산 중 오류 발생: {str(e)}")
return None
def create_stock_figure(ticker, company_name, stock_data):
fig = go.Figure()
# 캔들스틱 차트
fig.add_trace(
go.Candlestick(
x=stock_data.index,
open=stock_data['Open'],
high=stock_data['High'],
low=stock_data['Low'],
close=stock_data['Close'],
name='Price'
)
)
# 이동평균선
colors = {'MA5': 'purple', 'MA50': 'blue', 'MA150': 'green', 'MA200': 'red'}
for ma, color in colors.items():
fig.add_trace(
go.Scatter(
x=stock_data.index,
y=stock_data[ma],
name=ma,
line=dict(color=color)
)
)
fig.update_layout(
title=f"{ticker} - {company_name}",
yaxis_title="Price",
xaxis_title="Date",
height=600,
template="plotly_white",
showlegend=True
)
return fig
# 데이터 준비
sepa_df = load_sepa_stocks()
top_10_stocks = sepa_df.nlargest(10, '시가총액(M)')
stock_data = {}
for _, stock in top_10_stocks.iterrows():
ticker = stock['티커']
yf_ticker = yf.Ticker(ticker)
hist_data = yf_ticker.history(period="1y")
if not hist_data.empty:
hist_data = calculate_technical_indicators(hist_data)
if hist_data is not None:
stock_data[ticker] = {
'data': hist_data,
'company_name': stock['기업명']
}