pm6six commited on
Commit
5b2ae49
·
verified ·
1 Parent(s): de0e5cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -9,17 +9,17 @@ 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)
11
  if df.empty:
12
- return None, "No data available for the specified ticker and date range.", None
13
  except Exception as e:
14
- return None, f"Error fetching data: {str(e)}", None
15
 
16
  df = df[['Close']]
17
  df['SMA_50'] = df['Close'].rolling(window=50).mean()
18
  df['SMA_150'] = df['Close'].rolling(window=150).mean()
19
 
20
  df['Signal'] = 0
21
- df['Signal'][df['SMA_50'] > df['SMA_150']] = 1
22
- df['Signal'][df['SMA_50'] < df['SMA_150']] = -1
23
  df['Position'] = df['Signal'].diff()
24
 
25
  cash = initial_budget
@@ -38,7 +38,7 @@ def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
38
  portfolio_value = cash + (shares * row['Close'])
39
  portfolio_values.append(portfolio_value)
40
 
41
- df = df.iloc[149:]
42
  df['Portfolio Value'] = portfolio_values[149:]
43
 
44
  plt.figure(figsize=(14, 8))
@@ -68,9 +68,9 @@ def sma_crossover_strategy(initial_budget, start_date, end_date, ticker):
68
  Percentage Return: {percentage_return:.2f}%
69
  """
70
 
71
- return plot_file, results, None
72
 
73
- # Gradio Blocks Context
74
  with gr.Blocks() as app:
75
  gr.Markdown("# SMA Crossover Trading Strategy Simulator")
76
 
@@ -87,13 +87,11 @@ with gr.Blocks() as app:
87
  portfolio_graph = gr.Image(label="Portfolio Value Over Time")
88
  summary_text = gr.Textbox(label="Simulation Summary", lines=8)
89
 
90
- # Ensure run_button.click() is defined here
91
  run_button.click(
92
  sma_crossover_strategy,
93
  inputs=[initial_budget, start_date, end_date, ticker],
94
  outputs=[portfolio_graph, summary_text],
95
  )
96
 
97
- app.launch(share=True)
98
-
99
 
 
9
  try:
10
  df = yf.download(ticker, start=start_date, end=end_date, progress=False)
11
  if df.empty:
12
+ return None, "No data available for the specified ticker and date range."
13
  except Exception as e:
14
+ return None, f"Error fetching data: {str(e)}"
15
 
16
  df = df[['Close']]
17
  df['SMA_50'] = df['Close'].rolling(window=50).mean()
18
  df['SMA_150'] = df['Close'].rolling(window=150).mean()
19
 
20
  df['Signal'] = 0
21
+ df.loc[df['SMA_50'] > df['SMA_150'], 'Signal'] = 1
22
+ df.loc[df['SMA_50'] < df['SMA_150'], 'Signal'] = -1
23
  df['Position'] = df['Signal'].diff()
24
 
25
  cash = initial_budget
 
38
  portfolio_value = cash + (shares * row['Close'])
39
  portfolio_values.append(portfolio_value)
40
 
41
+ df = df.iloc[149:] # Skip rows without SMA values
42
  df['Portfolio Value'] = portfolio_values[149:]
43
 
44
  plt.figure(figsize=(14, 8))
 
68
  Percentage Return: {percentage_return:.2f}%
69
  """
70
 
71
+ return plot_file, results
72
 
73
+ # Define Gradio App
74
  with gr.Blocks() as app:
75
  gr.Markdown("# SMA Crossover Trading Strategy Simulator")
76
 
 
87
  portfolio_graph = gr.Image(label="Portfolio Value Over Time")
88
  summary_text = gr.Textbox(label="Simulation Summary", lines=8)
89
 
 
90
  run_button.click(
91
  sma_crossover_strategy,
92
  inputs=[initial_budget, start_date, end_date, ticker],
93
  outputs=[portfolio_graph, summary_text],
94
  )
95
 
96
+ app.launch() # No share=True
 
97