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.")