import yfinance as yf import pandas as pd import plotly.graph_objects as go import gradio as gr from datetime import date, timedelta from dateutil.relativedelta import relativedelta # ... (keep the existing imports and global variables) def get_company_info(ticker): stock = yf.Ticker(ticker) info = stock.info # Select relevant fundamental information fundamentals = { "Company Name": info.get("longName", "N/A"), "Sector": info.get("sector", "N/A"), "Industry": info.get("industry", "N/A"), "Market Cap": f"${info.get('marketCap', 'N/A'):,}", "P/E Ratio": round(info.get("trailingPE", 0), 2), "EPS": round(info.get("trailingEps", 0), 2), "52 Week High": f"${info.get('fiftyTwoWeekHigh', 'N/A'):,}", "52 Week Low": f"${info.get('fiftyTwoWeekLow', 'N/A'):,}", "Dividend Yield": f"{info.get('dividendYield', 0) * 100:.2f}%", "Beta": round(info.get("beta", 0), 2), } return pd.DataFrame(list(fundamentals.items()), columns=['Metric', 'Value']) def get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method): stock_name, ticker_name = stock.split(":") if ticker_dict[idx] == 'FTSE 100': ticker_name += '.L' if ticker_name[-1] != '.' else 'L' elif ticker_dict[idx] == 'CAC 40': ticker_name += '.PA' # Get stock price data series = yf.download(tickers=ticker_name, start=START_DATE, end=END_DATE, interval=interval) series = series.reset_index() # Generate forecast predictions = forecast_series(series, model=forecast_method) # ... (keep the existing forecast date generation code) # Create graph if graph_type == 'Line Graph': fig = go.Figure() fig.add_trace(go.Scatter(x=series['Date'], y=series['Close'], mode='lines', name='Historical')) fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast')) else: # Candlestick Graph fig = go.Figure(data=[go.Candlestick(x=series['Date'], open=series['Open'], high=series['High'], low=series['Low'], close=series['Close'], name='Historical')]) fig.add_trace(go.Scatter(x=forecast['Date'], y=forecast['Forecast'], mode='lines', name='Forecast')) fig.update_layout(title=f"Stock Price of {stock_name}", xaxis_title="Date", yaxis_title="Price") # Get fundamental information fundamentals = get_company_info(ticker_name) return fig, fundamentals # Update the Gradio interface with demo: # ... (keep the existing input components) out_graph = gr.Plot() out_fundamentals = gr.DataFrame() inputs = [d1, d2, d3, d4, d5] outputs = [out_graph, out_fundamentals] d2.input(get_stock_graph_and_info, inputs, outputs) d3.input(get_stock_graph_and_info, inputs, outputs) d4.input(get_stock_graph_and_info, inputs, outputs) d5.input(get_stock_graph_and_info, inputs, outputs) demo.launch()