pm6six commited on
Commit
da6bd62
·
verified ·
1 Parent(s): 24b8531

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -9
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 index, row in df.iterrows():
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 with 50/150 SMA Crossover Strategy ({ticker})')
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
- Total Profit/Loss: ${profit_loss:.2f}
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
 
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
- if __name__ == "__main__":
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