Spaces:
Sleeping
Sleeping
File size: 3,106 Bytes
97f6d3d 3c87c5c 640f727 8c28401 97f6d3d b9eb3a2 8c28401 97f6d3d a11b754 97f6d3d da6bd62 ae3466a 3c87c5c 97f6d3d a11b754 3c87c5c 97f6d3d a11b754 97f6d3d 10cb253 a11b754 97f6d3d 3098af2 97f6d3d ae3466a 97f6d3d 8c28401 97f6d3d da6bd62 8c28401 97f6d3d c51bf55 8c28401 97f6d3d 3098af2 3c87c5c 8c28401 97f6d3d 8c28401 97f6d3d 3098af2 7ba26f8 2963c08 ae3466a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
import io
import gradio as gr
def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
try:
df = yf.download(ticker, start=start_date, end=end_date, progress=False)
if df.empty:
return None, "No data available for the specified ticker and date range."
except Exception as e:
return None, f"Error fetching data: {str(e)}"
df = df[['Close']]
df['SMA_50'] = df['Close'].rolling(window=50).mean()
df['SMA_150'] = df['Close'].rolling(window=150).mean()
df['Signal'] = 0
df.loc[df['SMA_50'] > df['SMA_150'], 'Signal'] = 1
df.loc[df['SMA_50'] < df['SMA_150'], 'Signal'] = -1
df['Position'] = df['Signal'].diff()
cash = initial_budget
shares = 0
portfolio_values = []
for _, row in df.iterrows():
if pd.isna(row['Close']):
continue
if row['Position'] == 1 and cash > 0:
shares = cash / row['Close']
cash = 0
elif row['Position'] == -1 and shares > 0:
cash = shares * row['Close']
shares = 0
portfolio_value = cash + (shares * row['Close'])
portfolio_values.append(portfolio_value)
df = df.iloc[149:]
df['Portfolio Value'] = portfolio_values[149:]
plt.figure(figsize=(14, 8))
plt.plot(df['Portfolio Value'], label='Portfolio Value', color='purple')
plt.xlabel('Date')
plt.ylabel('Portfolio Value ($)')
plt.title(f'Portfolio Value Over Time with SMA Strategy ({ticker})')
plt.legend()
plt.grid()
plt.tight_layout()
plot_file = io.BytesIO()
plt.savefig(plot_file, format='png')
plot_file.seek(0)
plt.close()
final_value = portfolio_values[-1]
profit_loss = final_value - initial_budget
percentage_return = (profit_loss / initial_budget) * 100
results = f"""
Ticker: {ticker}
Trading Period: {start_date} to {end_date}
Initial Investment: ${initial_budget}
Final Portfolio Value: ${final_value:.2f}
Profit/Loss: ${profit_loss:.2f}
Percentage Return: {percentage_return:.2f}%
"""
return plot_file, results
with gr.Blocks() as app:
gr.Markdown("# SMA Crossover Strategy Simulator")
with gr.Row():
initial_budget = gr.Number(label="Initial Investment ($)", value=100)
start_date = gr.Text(label="Start Date (YYYY-MM-DD)", value="1993-01-01")
end_date = gr.Text(label="End Date (YYYY-MM-DD)", value="2023-12-31")
ticker = gr.Dropdown(
label="Stock Ticker Symbol",
choices=["SPY", "TSLA", "GOOGL", "AAPL", "MSFT"],
value="SPY",
)
run_button = gr.Button("Run Simulation")
portfolio_graph = gr.Image(label="Portfolio Value Over Time")
summary_text = gr.Textbox(label="Simulation Summary", lines=8)
run_button.click(
sma_crossover_strategy,
inputs=[initial_budget, start_date, end_date, ticker],
outputs=[portfolio_graph, summary_text],
)
if __name__ == "__main__":
app.launch(server_port=7861)
|