Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- pages/1_High_Performing_Shares.py +115 -0
- utils/data_fetching.py +0 -0
- utils/helpers.py +0 -0
pages/1_High_Performing_Shares.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import pandas as pd
|
4 |
+
from datetime import datetime, timedelta
|
5 |
+
import plotly.graph_objects as go
|
6 |
+
|
7 |
+
# ---------------------------
|
8 |
+
# Configuration and Constants
|
9 |
+
# ---------------------------
|
10 |
+
|
11 |
+
# Define Indian sectors and their corresponding prominent stocks
|
12 |
+
SECTORS = {
|
13 |
+
'Information Technology': ['TCS.NS', 'INFY.NS', 'WIPRO.NS', 'HCLTECH.NS', 'TECHM.NS'],
|
14 |
+
'Pharmaceuticals': ['SUNPHARMA.NS', 'DRREDDY.NS', 'CIPLA.NS', 'BIOCON.NS', 'DIVISLAB.NS'],
|
15 |
+
'Banking': ['HDFCBANK.NS', 'ICICIBANK.NS', 'KOTAKBANK.NS', 'AXISBANK.NS', 'SBIN.NS'],
|
16 |
+
'Energy': ['RELIANCE.NS', 'ONGC.NS', 'IOC.NS', 'BPCL.NS', 'TATAMOTORS.NS'],
|
17 |
+
'Automobiles': ['MARUTI.NS', 'TATAMOTORS.NS', 'BAJAJ-AUTO.NS', 'M&M.NS', 'HEROMOTOCO.NS'],
|
18 |
+
'Consumer Goods': ['HINDUNILVR.NS', 'ITC.NS', 'NESTLEIND.NS', 'TATACONSUM.NS', 'DABUR.NS'],
|
19 |
+
'Financial Services': ['BAJAJFINSV.NS', 'HDFC.NS', 'AXISBANK.NS', 'ICICIPRULI.NS', 'INDUSINDBK.NS'],
|
20 |
+
'Metals': ['TATASTEEL.NS', 'JSWSTEEL.NS', 'HINDALCO.NS', 'SAIL.NS', 'JINDALSTEL.NS'],
|
21 |
+
'Telecommunication Services': ['BHARTIARTL.NS', 'RELIANCE.NS', 'TATACOMM.NS', 'VODAFONE-I.NS', 'IDEA.NS'],
|
22 |
+
'Real Estate': ['DLF.NS', 'PROPTIA.NS', 'OIL.NS', 'BRIGADE.NS', 'ANSALHSG.NS']
|
23 |
+
}
|
24 |
+
|
25 |
+
COLORS = [
|
26 |
+
'#2E7D32', '#1976D2', '#C62828', '#F57C00',
|
27 |
+
'#6A1B9A', '#283593', '#00838F', '#558B2F',
|
28 |
+
'#D84315', '#4527A0', '#00695C'
|
29 |
+
]
|
30 |
+
|
31 |
+
# ---------------------------
|
32 |
+
# Utility Functions
|
33 |
+
# ---------------------------
|
34 |
+
|
35 |
+
def get_high_performing_stocks(sector, top_n=5, period='1mo'):
|
36 |
+
"""
|
37 |
+
Fetches and returns the top N high performing stocks in the specified sector over a given period.
|
38 |
+
|
39 |
+
Parameters:
|
40 |
+
sector (str): The sector name.
|
41 |
+
top_n (int): Number of top performers to return.
|
42 |
+
period (str): Time period for performance calculation (e.g., '1mo').
|
43 |
+
|
44 |
+
Returns:
|
45 |
+
pd.DataFrame: DataFrame containing top N performing stocks.
|
46 |
+
"""
|
47 |
+
if sector not in SECTORS:
|
48 |
+
st.error(f"No data available for sector: {sector}")
|
49 |
+
return pd.DataFrame()
|
50 |
+
|
51 |
+
tickers = SECTORS[sector]
|
52 |
+
data = {}
|
53 |
+
company_names = []
|
54 |
+
for ticker in tickers:
|
55 |
+
stock = yf.Ticker(ticker)
|
56 |
+
hist = stock.history(period=period)
|
57 |
+
if not hist.empty and len(hist['Close']) >= 2:
|
58 |
+
hist = hist.sort_index()
|
59 |
+
latest_close = hist['Close'][-1]
|
60 |
+
oldest_close = hist['Close'][0]
|
61 |
+
performance = ((latest_close - oldest_close) / oldest_close) * 100
|
62 |
+
data[ticker] = performance
|
63 |
+
info = stock.info
|
64 |
+
company_names.append(info.get('shortName', 'N/A'))
|
65 |
+
else:
|
66 |
+
data[ticker] = 0.0 # Assign 0.0% if insufficient data
|
67 |
+
company_names.append('N/A')
|
68 |
+
|
69 |
+
# Create DataFrame
|
70 |
+
df = pd.DataFrame(list(data.items()), columns=['Ticker', 'Performance (%)'])
|
71 |
+
df['Company'] = company_names
|
72 |
+
# Sort by performance descending
|
73 |
+
df = df.sort_values(by='Performance (%)', ascending=False)
|
74 |
+
# Return top N
|
75 |
+
return df.head(top_n)
|
76 |
+
|
77 |
+
# ---------------------------
|
78 |
+
# Streamlit UI
|
79 |
+
# ---------------------------
|
80 |
+
|
81 |
+
def main():
|
82 |
+
st.title("📈 High Performing Shares by Sector")
|
83 |
+
|
84 |
+
# Sector selection
|
85 |
+
sector = st.selectbox("Select a Sector", list(SECTORS.keys()))
|
86 |
+
|
87 |
+
# Performance period selection
|
88 |
+
period = st.selectbox("Select Performance Period", ['1mo', '3mo', '6mo', '1y'], index=0)
|
89 |
+
|
90 |
+
if st.button("Get Top Performers"):
|
91 |
+
if sector:
|
92 |
+
with st.spinner(f'Fetching top performers for {sector} sector over {period}...'):
|
93 |
+
top_stocks = get_high_performing_stocks(sector, top_n=5, period=period)
|
94 |
+
if not top_stocks.empty:
|
95 |
+
st.subheader(f"Top 5 High Performing Stocks in {sector} Sector over {period}")
|
96 |
+
st.dataframe(top_stocks[['Ticker', 'Company', 'Performance (%)']])
|
97 |
+
|
98 |
+
# Bar Chart
|
99 |
+
fig = go.Figure(data=[
|
100 |
+
go.Bar(
|
101 |
+
x=top_stocks['Ticker'],
|
102 |
+
y=top_stocks['Performance (%)'],
|
103 |
+
marker_color=COLORS[:len(top_stocks)]
|
104 |
+
)
|
105 |
+
])
|
106 |
+
fig.update_layout(title="Top Performing Stocks", xaxis_title="Ticker", yaxis_title="Performance (%)")
|
107 |
+
st.plotly_chart(fig)
|
108 |
+
else:
|
109 |
+
st.warning("No high performing stocks found.")
|
110 |
+
|
111 |
+
st.markdown("---")
|
112 |
+
st.markdown("**Note:** This is a demonstration. In a production application, consider integrating a dynamic data source for sector stocks.")
|
113 |
+
|
114 |
+
if __name__ == "__main__":
|
115 |
+
main()
|
utils/data_fetching.py
ADDED
File without changes
|
utils/helpers.py
ADDED
File without changes
|