Method314 commited on
Commit
44fe375
·
verified ·
1 Parent(s): a779d49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -46
app.py CHANGED
@@ -63,13 +63,13 @@ def download_stock_data(ticker, start_date, end_date):
63
  df = stock.history(start=start_date, end=end_date)
64
  return df
65
 
66
- def plot_chart(ticker, start_date, end_date, chart_type):
67
  try:
68
  stock = yf.Ticker(ticker)
69
  data = stock.history(start=start_date, end=end_date)
70
 
71
  if data.empty:
72
- return "No data available for the specified date range.", None, None, None
73
 
74
  if chart_type == "Log":
75
  return plot_logarithmic_chart(data, ticker)
@@ -77,7 +77,7 @@ def plot_chart(ticker, start_date, end_date, chart_type):
77
  return plot_candlestick_chart(data, ticker)
78
 
79
  except Exception as e:
80
- return f"An error occurred: {str(e)}", None, None, None
81
 
82
  def plot_logarithmic_chart(data, ticker):
83
  x = (data.index - data.index[0]).days
@@ -136,11 +136,7 @@ def plot_logarithmic_chart(data, ticker):
136
  )
137
  )
138
 
139
- current_price = data['Close'].iloc[-1]
140
- log_price = log_trend[-len(data):]
141
- percent_diff = ((current_price - log_price.iloc[-1]) / log_price.iloc[-1]) * 100
142
-
143
- return fig, current_price, log_price.iloc[-1], percent_diff
144
 
145
  def plot_candlestick_chart(data, ticker):
146
  fig = go.Figure(data=[go.Candlestick(x=data.index,
@@ -180,33 +176,7 @@ def plot_candlestick_chart(data, ticker):
180
  )
181
  )
182
 
183
- current_price = data['Close'].iloc[-1]
184
- return fig, current_price, None, None
185
-
186
- def format_price_info(current_price, log_price, percent_diff):
187
- if current_price is None:
188
- return "Unable to retrieve price information."
189
-
190
- if log_price is None or percent_diff is None:
191
- return f"Current Price: ${current_price:.2f}"
192
-
193
- color = "green" if percent_diff > 0 else "red"
194
- intensity = min(abs(percent_diff) / 100, 1) # Normalize to 0-1
195
- bg_color = f"rgba({255 if color == 'red' else 0}, {255 if color == 'green' else 0}, 0, {intensity})"
196
-
197
- return f"""
198
- <div style="display: flex; justify-content: space-between; padding: 10px; background-color: {bg_color}; border-radius: 10px;">
199
- <div>
200
- <p>Current Price: ${current_price:.2f}</p>
201
- <p>Log Price: ${log_price:.2f}</p>
202
- </div>
203
- <div>
204
- <p style="font-size: 1.2em; font-weight: bold; color: {color};">
205
- {percent_diff:.2f}% {'above' if percent_diff > 0 else 'below'} log price
206
- </p>
207
- </div>
208
- </div>
209
- """
210
 
211
  # Get the current date
212
  current_date = datetime.now().strftime("%Y-%m-%d")
@@ -222,7 +192,7 @@ custom_css = """
222
  }
223
  """
224
 
225
- # Update the Gradio interface section
226
  with gr.Blocks(theme=seafoam, title="Stock Charts", css=custom_css) as iface:
227
  gr.Markdown("# Stock Charts")
228
  gr.Markdown("Enter a stock ticker and date range to generate a chart.")
@@ -238,19 +208,12 @@ with gr.Blocks(theme=seafoam, title="Stock Charts", css=custom_css) as iface:
238
  submit_button = gr.Button("Generate Chart", elem_id="generate-button")
239
 
240
  with gr.Row():
241
- chart = gr.Plot(label="Stock Chart")
242
 
243
- price_info = gr.HTML(label="Price Information")
244
-
245
- def update_chart_and_info(ticker, start_date, end_date, chart_type):
246
- chart_data, current_price, log_price, percent_diff = plot_chart(ticker, start_date, end_date, chart_type)
247
- price_info_html = format_price_info(current_price, log_price, percent_diff)
248
- return chart_data, price_info_html
249
-
250
  submit_button.click(
251
- update_chart_and_info,
252
  inputs=[ticker, start_date, end_date, chart_type],
253
- outputs=[chart, price_info]
254
  )
255
 
256
  # Launch the app
 
63
  df = stock.history(start=start_date, end=end_date)
64
  return df
65
 
66
+ def plot_interactive_stock_chart(ticker, start_date, end_date, chart_type):
67
  try:
68
  stock = yf.Ticker(ticker)
69
  data = stock.history(start=start_date, end=end_date)
70
 
71
  if data.empty:
72
+ return "No data available for the specified date range."
73
 
74
  if chart_type == "Log":
75
  return plot_logarithmic_chart(data, ticker)
 
77
  return plot_candlestick_chart(data, ticker)
78
 
79
  except Exception as e:
80
+ return f"An error occurred: {str(e)}"
81
 
82
  def plot_logarithmic_chart(data, ticker):
83
  x = (data.index - data.index[0]).days
 
136
  )
137
  )
138
 
139
+ return fig
 
 
 
 
140
 
141
  def plot_candlestick_chart(data, ticker):
142
  fig = go.Figure(data=[go.Candlestick(x=data.index,
 
176
  )
177
  )
178
 
179
+ return fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  # Get the current date
182
  current_date = datetime.now().strftime("%Y-%m-%d")
 
192
  }
193
  """
194
 
195
+ # Create Gradio interface with custom Seafoam theme
196
  with gr.Blocks(theme=seafoam, title="Stock Charts", css=custom_css) as iface:
197
  gr.Markdown("# Stock Charts")
198
  gr.Markdown("Enter a stock ticker and date range to generate a chart.")
 
208
  submit_button = gr.Button("Generate Chart", elem_id="generate-button")
209
 
210
  with gr.Row():
211
+ stock_chart = gr.Plot(label="Stock Chart")
212
 
 
 
 
 
 
 
 
213
  submit_button.click(
214
+ plot_interactive_stock_chart,
215
  inputs=[ticker, start_date, end_date, chart_type],
216
+ outputs=[stock_chart]
217
  )
218
 
219
  # Launch the app