File size: 1,629 Bytes
c0c59ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import yfinance as yf
import pandas as pd
import numpy as np

def fetch_stock_data(ticker_symbol):
    ticker = yf.Ticker(ticker_symbol)
    hist = ticker.history(period="1mo")  # Fetch historical data for the last month
    info = ticker.info
    
    # Calculating some financial ratios
    try:
        pe_ratio = info['forwardPE']
        pb_ratio = info['priceToBook']
        ps_ratio = info.get('priceToSalesTrailing12Months', np.nan)
        debt_to_equity = info.get('debtToEquity', np.nan)
        roe = info.get('returnOnEquity', np.nan)
        earnings_yield = 1 / pe_ratio if pe_ratio else np.nan
        book_to_market_ratio = 1 / pb_ratio if pb_ratio else np.nan
        
        ratios = {
            'P/E Ratio': pe_ratio,
            'P/B Ratio': pb_ratio,
            'P/S Ratio': ps_ratio,
            'Debt to Equity': debt_to_equity,
            'Return on Equity (ROE)': roe,
            'Earnings Yield': earnings_yield,
            'Book-to-Market Ratio': book_to_market_ratio,
        }
    except Exception as e:
        st.error(f"Failed to fetch data for {ticker_symbol}: {e}")
        ratios = {}
    
    return ratios

# Streamlit UI
st.title('Stock Financial Ratios Analysis')

ticker_symbol = st.text_input('Enter Stock Ticker Symbol (e.g., AAPL, MSFT):').upper()

if ticker_symbol:
    ratios = fetch_stock_data(ticker_symbol)
    
    if ratios:
        st.write(f"Financial Ratios for {ticker_symbol}:")
        for ratio, value in ratios.items():
            st.write(f"{ratio}: {value}")
    else:
        st.write("No data available for the given ticker symbol.")