markytools commited on
Commit
082f7e8
·
1 Parent(s): 8405d25

working locally

Browse files
Files changed (1) hide show
  1. app.py +16 -23
app.py CHANGED
@@ -115,29 +115,22 @@ else:
115
  st.sidebar.markdown("---")
116
  st.sidebar.caption("Tip: Choose a wide date range for smoother moving averages and richer AI insights.")
117
 
118
- def load_stock_data(symbol: str, start_dt, end_dt):
119
- """Download data and flatten any MultiIndex columns from yfinance."""
120
- try:
121
- data = yf.download(symbol, start=start_dt, end=end_dt, auto_adjust=False, progress=False)
122
- except Exception:
123
- st.error("Error fetching stock data. Please check the ticker symbol and date range.")
124
- st.stop()
125
-
126
- if data.empty:
127
- st.error("No data returned. Try widening the date range or verifying the ticker.")
128
- st.stop()
129
-
130
- if isinstance(data.columns, pd.MultiIndex):
131
- price_like = {"Open", "High", "Low", "Close", "Adj Close", "Volume"}
132
- level0_has_price = any(col in price_like for col in data.columns.get_level_values(0))
133
- columns = data.columns.get_level_values(0 if level0_has_price else 1)
134
- data = data.copy()
135
- data.columns = columns
136
-
137
- data.reset_index(inplace=True) # Reset index to ensure 'Date' becomes a column
138
- if "Date" in data.columns:
139
- data["Date"] = pd.to_datetime(data["Date"]).dt.tz_localize(None)
140
- return data
141
 
142
  # Fetch stock data from Yahoo Finance
143
  df = load_stock_data(ticker_symbol, start_date, end_date)
 
115
  st.sidebar.markdown("---")
116
  st.sidebar.caption("Tip: Choose a wide date range for smoother moving averages and richer AI insights.")
117
 
118
+ def load_stock_data(symbol: str, start_dt, end_dt):
119
+ """Download data with a flat schema to avoid Plotly/MultiIndex issues on some runtimes."""
120
+ try:
121
+ ticker = yf.Ticker(symbol)
122
+ data = ticker.history(start=start_dt, end=end_dt, actions=False, auto_adjust=False)
123
+ except Exception:
124
+ st.error("Error fetching stock data. Please check the ticker symbol and date range.")
125
+ st.stop()
126
+
127
+ if data.empty:
128
+ st.error("No data returned. Try widening the date range or verifying the ticker.")
129
+ st.stop()
130
+
131
+ data = data.reset_index() # Ensure Date is a column
132
+ data["Date"] = pd.to_datetime(data["Date"]).dt.tz_localize(None)
133
+ return data
 
 
 
 
 
 
 
134
 
135
  # Fetch stock data from Yahoo Finance
136
  df = load_stock_data(ticker_symbol, start_date, end_date)