os1187 commited on
Commit
c0c59ce
1 Parent(s): d71b1cd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import yfinance as yf
3
+ import pandas as pd
4
+ import numpy as np
5
+
6
+ def fetch_stock_data(ticker_symbol):
7
+ ticker = yf.Ticker(ticker_symbol)
8
+ hist = ticker.history(period="1mo") # Fetch historical data for the last month
9
+ info = ticker.info
10
+
11
+ # Calculating some financial ratios
12
+ try:
13
+ pe_ratio = info['forwardPE']
14
+ pb_ratio = info['priceToBook']
15
+ ps_ratio = info.get('priceToSalesTrailing12Months', np.nan)
16
+ debt_to_equity = info.get('debtToEquity', np.nan)
17
+ roe = info.get('returnOnEquity', np.nan)
18
+ earnings_yield = 1 / pe_ratio if pe_ratio else np.nan
19
+ book_to_market_ratio = 1 / pb_ratio if pb_ratio else np.nan
20
+
21
+ ratios = {
22
+ 'P/E Ratio': pe_ratio,
23
+ 'P/B Ratio': pb_ratio,
24
+ 'P/S Ratio': ps_ratio,
25
+ 'Debt to Equity': debt_to_equity,
26
+ 'Return on Equity (ROE)': roe,
27
+ 'Earnings Yield': earnings_yield,
28
+ 'Book-to-Market Ratio': book_to_market_ratio,
29
+ }
30
+ except Exception as e:
31
+ st.error(f"Failed to fetch data for {ticker_symbol}: {e}")
32
+ ratios = {}
33
+
34
+ return ratios
35
+
36
+ # Streamlit UI
37
+ st.title('Stock Financial Ratios Analysis')
38
+
39
+ ticker_symbol = st.text_input('Enter Stock Ticker Symbol (e.g., AAPL, MSFT):').upper()
40
+
41
+ if ticker_symbol:
42
+ ratios = fetch_stock_data(ticker_symbol)
43
+
44
+ if ratios:
45
+ st.write(f"Financial Ratios for {ticker_symbol}:")
46
+ for ratio, value in ratios.items():
47
+ st.write(f"{ratio}: {value}")
48
+ else:
49
+ st.write("No data available for the given ticker symbol.")