Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
from datetime import datetime, timedelta
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
st.set_page_config(layout="wide", page_title="Stock Analysis Dashboard")
|
8 |
+
|
9 |
+
def format_number(num):
|
10 |
+
"""Format large numbers to readable format with B/M suffix"""
|
11 |
+
if num >= 1e9:
|
12 |
+
return f"₹{num/1e9:.2f}B"
|
13 |
+
elif num >= 1e6:
|
14 |
+
return f"₹{num/1e6:.2f}M"
|
15 |
+
else:
|
16 |
+
return f"₹{num:,.2f}"
|
17 |
+
|
18 |
+
def create_price_chart(ticker_data):
|
19 |
+
"""Create interactive price chart using Plotly"""
|
20 |
+
fig = go.Figure(data=[go.Candlestick(x=ticker_data.index,
|
21 |
+
open=ticker_data['Open'],
|
22 |
+
high=ticker_data['High'],
|
23 |
+
low=ticker_data['Low'],
|
24 |
+
close=ticker_data['Close'])])
|
25 |
+
|
26 |
+
fig.update_layout(
|
27 |
+
title="Stock Price Movement",
|
28 |
+
yaxis_title="Price",
|
29 |
+
xaxis_title="Date",
|
30 |
+
template="plotly_dark"
|
31 |
+
)
|
32 |
+
return fig
|
33 |
+
|
34 |
+
def create_volume_chart(ticker_data):
|
35 |
+
"""Create volume chart using Plotly"""
|
36 |
+
fig = go.Figure(data=[go.Bar(x=ticker_data.index, y=ticker_data['Volume'])])
|
37 |
+
fig.update_layout(
|
38 |
+
title="Trading Volume",
|
39 |
+
yaxis_title="Volume",
|
40 |
+
xaxis_title="Date",
|
41 |
+
template="plotly_dark"
|
42 |
+
)
|
43 |
+
return fig
|
44 |
+
|
45 |
+
def main():
|
46 |
+
st.title("📊 Stock Analysis Dashboard")
|
47 |
+
|
48 |
+
# Input section
|
49 |
+
col1, col2 = st.columns([2,2])
|
50 |
+
with col1:
|
51 |
+
ticker_symbol = st.text_input("Enter Stock Ticker (e.g., RELIANCE.NS for Indian stocks):")
|
52 |
+
with col2:
|
53 |
+
period = st.selectbox("Select Time Period:",
|
54 |
+
["1mo", "3mo", "6mo", "1y", "2y", "5y", "max"],
|
55 |
+
index=2)
|
56 |
+
|
57 |
+
if ticker_symbol:
|
58 |
+
try:
|
59 |
+
# Fetch stock data
|
60 |
+
stock = yf.Ticker(ticker_symbol)
|
61 |
+
info = stock.info
|
62 |
+
hist_data = stock.history(period=period)
|
63 |
+
|
64 |
+
# Company Overview Section
|
65 |
+
st.header("Company Overview")
|
66 |
+
col1, col2, col3 = st.columns([1,1,1])
|
67 |
+
|
68 |
+
with col1:
|
69 |
+
st.metric("Current Price", format_number(info.get('currentPrice', 0)))
|
70 |
+
st.metric("Market Cap", format_number(info.get('marketCap', 0)))
|
71 |
+
|
72 |
+
with col2:
|
73 |
+
day_change = info.get('regularMarketChangePercent', 0)
|
74 |
+
st.metric("Day Change", f"{day_change:.2f}%")
|
75 |
+
st.metric("P/E Ratio", f"{info.get('forwardPE', 0):.2f}")
|
76 |
+
|
77 |
+
with col3:
|
78 |
+
st.metric("52 Week High", format_number(info.get('fiftyTwoWeekHigh', 0)))
|
79 |
+
st.metric("52 Week Low", format_number(info.get('fiftyTwoWeekLow', 0)))
|
80 |
+
|
81 |
+
# Price Charts
|
82 |
+
st.header("Price Analysis")
|
83 |
+
tab1, tab2 = st.tabs(["Price Chart", "Volume Chart"])
|
84 |
+
|
85 |
+
with tab1:
|
86 |
+
st.plotly_chart(create_price_chart(hist_data), use_container_width=True)
|
87 |
+
with tab2:
|
88 |
+
st.plotly_chart(create_volume_chart(hist_data), use_container_width=True)
|
89 |
+
|
90 |
+
# Detailed Information
|
91 |
+
st.header("Detailed Information")
|
92 |
+
col1, col2 = st.columns([1,1])
|
93 |
+
|
94 |
+
with col1:
|
95 |
+
st.subheader("Company Profile")
|
96 |
+
st.write("**Sector:**", info.get('sector', 'N/A'))
|
97 |
+
st.write("**Industry:**", info.get('industry', 'N/A'))
|
98 |
+
st.write("**Country:**", info.get('country', 'N/A'))
|
99 |
+
st.write("**Website:**", info.get('website', 'N/A'))
|
100 |
+
|
101 |
+
st.subheader("Key Statistics")
|
102 |
+
metrics_df = pd.DataFrame({
|
103 |
+
'Metric': [
|
104 |
+
'Beta',
|
105 |
+
'Dividend Yield (%)',
|
106 |
+
'Trailing P/E',
|
107 |
+
'Forward P/E',
|
108 |
+
'PEG Ratio',
|
109 |
+
'Price to Book'
|
110 |
+
],
|
111 |
+
'Value': [
|
112 |
+
f"{info.get('beta', 0):.2f}",
|
113 |
+
f"{info.get('dividendYield', 0)*100:.2f}",
|
114 |
+
f"{info.get('trailingPE', 0):.2f}",
|
115 |
+
f"{info.get('forwardPE', 0):.2f}",
|
116 |
+
f"{info.get('pegRatio', 0):.2f}",
|
117 |
+
f"{info.get('priceToBook', 0):.2f}"
|
118 |
+
]
|
119 |
+
})
|
120 |
+
st.dataframe(metrics_df, hide_index=True)
|
121 |
+
|
122 |
+
with col2:
|
123 |
+
st.subheader("Business Summary")
|
124 |
+
st.write(info.get('longBusinessSummary', 'No summary available'))
|
125 |
+
|
126 |
+
st.subheader("Financial Metrics")
|
127 |
+
financial_df = pd.DataFrame({
|
128 |
+
'Metric': [
|
129 |
+
'Revenue Growth',
|
130 |
+
'Gross Margins',
|
131 |
+
'Operating Margins',
|
132 |
+
'Profit Margins',
|
133 |
+
'Return on Equity',
|
134 |
+
'Return on Assets'
|
135 |
+
],
|
136 |
+
'Value': [
|
137 |
+
f"{info.get('revenueGrowth', 0)*100:.2f}%",
|
138 |
+
f"{info.get('grossMargins', 0)*100:.2f}%",
|
139 |
+
f"{info.get('operatingMargins', 0)*100:.2f}%",
|
140 |
+
f"{info.get('profitMargins', 0)*100:.2f}%",
|
141 |
+
f"{info.get('returnOnEquity', 0)*100:.2f}%",
|
142 |
+
f"{info.get('returnOnAssets', 0)*100:.2f}%"
|
143 |
+
]
|
144 |
+
})
|
145 |
+
st.dataframe(financial_df, hide_index=True)
|
146 |
+
|
147 |
+
# Historical Data Download Section
|
148 |
+
st.header("Download Historical Data")
|
149 |
+
csv = hist_data.to_csv()
|
150 |
+
st.download_button(
|
151 |
+
label="📥 Download Historical Data",
|
152 |
+
data=csv,
|
153 |
+
file_name=f"{ticker_symbol}_historical_data.csv",
|
154 |
+
mime="text/csv"
|
155 |
+
)
|
156 |
+
|
157 |
+
except Exception as e:
|
158 |
+
st.error(f"Error fetching data for {ticker_symbol}. Please check the ticker symbol and try again.")
|
159 |
+
st.error(f"Error details: {str(e)}")
|
160 |
+
|
161 |
+
if __name__ == "__main__":
|
162 |
+
main()
|