Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -65,6 +65,7 @@ def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
|
|
65 |
profit_loss = final_value - initial_budget
|
66 |
percentage_return = (profit_loss / initial_budget) * 100
|
67 |
|
|
|
68 |
results = f"""
|
69 |
Ticker: {ticker}
|
70 |
Trading Period: {start_date} to {end_date}
|
@@ -73,8 +74,13 @@ def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
|
|
73 |
Total Profit/Loss: ${profit_loss:.2f}
|
74 |
Percentage Return: {percentage_return:.2f}%
|
75 |
"""
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# Define Gradio interface components
|
80 |
with gr.Blocks() as app:
|
@@ -96,6 +102,7 @@ with gr.Blocks() as app:
|
|
96 |
with gr.Row():
|
97 |
portfolio_graph = gr.Image(label="Portfolio Value Over Time")
|
98 |
summary_text = gr.Textbox(label="Simulation Summary", lines=8)
|
|
|
99 |
|
100 |
# Tab for Instructions
|
101 |
with gr.Tab("Instructions"):
|
@@ -105,6 +112,7 @@ with gr.Blocks() as app:
|
|
105 |
2. Specify the trading period (start and end dates).
|
106 |
3. Select a stock ticker symbol (e.g., SPY, TSLA, GOOGL).
|
107 |
4. Click "Run Simulation" to visualize the portfolio value over time and view a summary of results.
|
|
|
108 |
|
109 |
### Notes:
|
110 |
- The 50-day and 150-day SMAs are used for buy and sell signals.
|
@@ -115,7 +123,7 @@ with gr.Blocks() as app:
|
|
115 |
run_button.click(
|
116 |
sma_crossover_strategy,
|
117 |
inputs=[initial_budget, start_date, end_date, ticker],
|
118 |
-
outputs=[portfolio_graph, summary_text],
|
119 |
)
|
120 |
|
121 |
# Launch the app
|
|
|
65 |
profit_loss = final_value - initial_budget
|
66 |
percentage_return = (profit_loss / initial_budget) * 100
|
67 |
|
68 |
+
# Create summary text
|
69 |
results = f"""
|
70 |
Ticker: {ticker}
|
71 |
Trading Period: {start_date} to {end_date}
|
|
|
74 |
Total Profit/Loss: ${profit_loss:.2f}
|
75 |
Percentage Return: {percentage_return:.2f}%
|
76 |
"""
|
77 |
+
|
78 |
+
# Save results to a text file
|
79 |
+
results_file = "simulation_results.txt"
|
80 |
+
with open(results_file, "w") as f:
|
81 |
+
f.write(results)
|
82 |
+
|
83 |
+
return plot_file, results, results_file
|
84 |
|
85 |
# Define Gradio interface components
|
86 |
with gr.Blocks() as app:
|
|
|
102 |
with gr.Row():
|
103 |
portfolio_graph = gr.Image(label="Portfolio Value Over Time")
|
104 |
summary_text = gr.Textbox(label="Simulation Summary", lines=8)
|
105 |
+
download_button = gr.File(label="Download Results (.txt)")
|
106 |
|
107 |
# Tab for Instructions
|
108 |
with gr.Tab("Instructions"):
|
|
|
112 |
2. Specify the trading period (start and end dates).
|
113 |
3. Select a stock ticker symbol (e.g., SPY, TSLA, GOOGL).
|
114 |
4. Click "Run Simulation" to visualize the portfolio value over time and view a summary of results.
|
115 |
+
5. Download the results as a `.txt` file using the download button.
|
116 |
|
117 |
### Notes:
|
118 |
- The 50-day and 150-day SMAs are used for buy and sell signals.
|
|
|
123 |
run_button.click(
|
124 |
sma_crossover_strategy,
|
125 |
inputs=[initial_budget, start_date, end_date, ticker],
|
126 |
+
outputs=[portfolio_graph, summary_text, download_button],
|
127 |
)
|
128 |
|
129 |
# Launch the app
|