Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,10 +37,14 @@ with st.sidebar.expander("Asset Settings", expanded=True):
|
|
| 37 |
start_date = st.date_input("Start Date", value=datetime(2015, 1, 1), help="Select the start date for historical data.")
|
| 38 |
end_date = st.date_input("End Date", value=datetime.today() + timedelta(days=1), help="Select the end date for historical data.")
|
| 39 |
|
| 40 |
-
# Function to download data
|
| 41 |
@st.cache_data
|
| 42 |
def download_data(ticker, start, end):
|
| 43 |
-
data = yf.download(ticker, start=start, end=end)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
return data
|
| 45 |
|
| 46 |
# Function to calculate moving averages
|
|
@@ -91,7 +95,6 @@ def execute_strategy(data, short_window, medium_window, long_window, threshold,
|
|
| 91 |
data = calculate_equity_curve(data)
|
| 92 |
return data
|
| 93 |
|
| 94 |
-
# Function to plot results
|
| 95 |
# Function to plot results with subplots for better alignment
|
| 96 |
def plot_results(data, params, title_suffix):
|
| 97 |
# Create subplots: 2 rows (Price + MA, and Equity Curve), shared x-axis for alignment
|
|
|
|
| 37 |
start_date = st.date_input("Start Date", value=datetime(2015, 1, 1), help="Select the start date for historical data.")
|
| 38 |
end_date = st.date_input("End Date", value=datetime.today() + timedelta(days=1), help="Select the end date for historical data.")
|
| 39 |
|
| 40 |
+
# Function to download data with yfinance adjustments
|
| 41 |
@st.cache_data
|
| 42 |
def download_data(ticker, start, end):
|
| 43 |
+
data = yf.download(ticker, start=start, end=end, auto_adjust=False)
|
| 44 |
+
if isinstance(data.columns, pd.MultiIndex):
|
| 45 |
+
data.columns = data.columns.get_level_values(0)
|
| 46 |
+
if data.empty:
|
| 47 |
+
raise ValueError(f"No data fetched for {ticker} from {start} to {end}.")
|
| 48 |
return data
|
| 49 |
|
| 50 |
# Function to calculate moving averages
|
|
|
|
| 95 |
data = calculate_equity_curve(data)
|
| 96 |
return data
|
| 97 |
|
|
|
|
| 98 |
# Function to plot results with subplots for better alignment
|
| 99 |
def plot_results(data, params, title_suffix):
|
| 100 |
# Create subplots: 2 rows (Price + MA, and Equity Curve), shared x-axis for alignment
|