Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import pandas as pd
|
2 |
import yfinance as yf
|
3 |
-
import numpy as np
|
4 |
import matplotlib.pyplot as plt
|
5 |
import io
|
6 |
import gradio as gr
|
7 |
|
|
|
8 |
def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
|
9 |
try:
|
10 |
df = yf.download(ticker, start=start_date, end=end_date, progress=False)
|
@@ -26,7 +26,7 @@ def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
|
|
26 |
shares = 0
|
27 |
portfolio_values = []
|
28 |
|
29 |
-
for
|
30 |
if pd.isna(row['Close']):
|
31 |
continue
|
32 |
if row['Position'] == 1 and cash > 0:
|
@@ -45,7 +45,7 @@ def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
|
|
45 |
plt.plot(df['Portfolio Value'], label='Portfolio Value', color='purple')
|
46 |
plt.xlabel('Date')
|
47 |
plt.ylabel('Portfolio Value ($)')
|
48 |
-
plt.title(f'Portfolio Value Over Time
|
49 |
plt.legend()
|
50 |
plt.grid()
|
51 |
plt.tight_layout()
|
@@ -64,15 +64,15 @@ def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
|
|
64 |
Trading Period: {start_date} to {end_date}
|
65 |
Initial Investment: ${initial_budget}
|
66 |
Final Portfolio Value: ${final_value:.2f}
|
67 |
-
|
68 |
Percentage Return: {percentage_return:.2f}%
|
69 |
"""
|
70 |
|
71 |
return plot_file, results
|
72 |
|
73 |
-
|
74 |
with gr.Blocks() as app:
|
75 |
-
gr.Markdown("
|
76 |
|
77 |
with gr.Row():
|
78 |
initial_budget = gr.Number(label="Initial Investment ($)", value=100)
|
@@ -93,7 +93,5 @@ with gr.Blocks() as app:
|
|
93 |
outputs=[portfolio_graph, summary_text],
|
94 |
)
|
95 |
|
96 |
-
|
97 |
-
app.launch()
|
98 |
-
|
99 |
|
|
|
1 |
import pandas as pd
|
2 |
import yfinance as yf
|
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
import io
|
5 |
import gradio as gr
|
6 |
|
7 |
+
|
8 |
def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
|
9 |
try:
|
10 |
df = yf.download(ticker, start=start_date, end=end_date, progress=False)
|
|
|
26 |
shares = 0
|
27 |
portfolio_values = []
|
28 |
|
29 |
+
for _, row in df.iterrows():
|
30 |
if pd.isna(row['Close']):
|
31 |
continue
|
32 |
if row['Position'] == 1 and cash > 0:
|
|
|
45 |
plt.plot(df['Portfolio Value'], label='Portfolio Value', color='purple')
|
46 |
plt.xlabel('Date')
|
47 |
plt.ylabel('Portfolio Value ($)')
|
48 |
+
plt.title(f'Portfolio Value Over Time ({ticker})')
|
49 |
plt.legend()
|
50 |
plt.grid()
|
51 |
plt.tight_layout()
|
|
|
64 |
Trading Period: {start_date} to {end_date}
|
65 |
Initial Investment: ${initial_budget}
|
66 |
Final Portfolio Value: ${final_value:.2f}
|
67 |
+
Profit/Loss: ${profit_loss:.2f}
|
68 |
Percentage Return: {percentage_return:.2f}%
|
69 |
"""
|
70 |
|
71 |
return plot_file, results
|
72 |
|
73 |
+
|
74 |
with gr.Blocks() as app:
|
75 |
+
gr.Markdown("## SMA Crossover Trading Strategy Simulator")
|
76 |
|
77 |
with gr.Row():
|
78 |
initial_budget = gr.Number(label="Initial Investment ($)", value=100)
|
|
|
93 |
outputs=[portfolio_graph, summary_text],
|
94 |
)
|
95 |
|
96 |
+
app.launch()
|
|
|
|
|
97 |
|