File size: 3,231 Bytes
2e2dfb4
2e79a3d
2e2dfb4
2e79a3d
2e2dfb4
 
 
2e79a3d
2e2dfb4
2e79a3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2dfb4
2e79a3d
 
 
 
 
 
 
2e2dfb4
2e79a3d
 
 
2e2dfb4
2e79a3d
 
2e2dfb4
2e79a3d
2e2dfb4
2e79a3d
 
 
 
 
 
 
 
 
 
 
 
 
2e2dfb4
2e79a3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2dfb4
6a50720
2e79a3d
 
 
 
 
 
2e2dfb4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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()