File size: 7,914 Bytes
42c2fbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import yfinance as yf
import pandas as pd
import streamlit as st
from datetime import datetime, timedelta

# Fetch Nifty 50 tickers
def fetch_nifty50_tickers():
    return [
        "TATAMOTORS.NS", "RELIANCE.NS", "INFY.NS", "HDFCBANK.NS", "ICICIBANK.NS",
        "SBIN.NS", "ITC.NS", "AXISBANK.NS", "MARUTI.NS", "TATASTEEL.NS",
        "WIPRO.NS", "SUNPHARMA.NS", "HINDALCO.NS", "HCLTECH.NS", "NTPC.NS",
        "L&T.NS", "M&M.NS", "ONGC.NS", "HDFCLIFE.NS", "ULTRACEMCO.NS",
        "ADANIGREEN.NS", "BHARTIARTL.NS", "BAJAJFINSV.NS", "JSWSTEEL.NS", "DIVISLAB.NS",
        "POWERGRID.NS", "KOTAKBANK.NS", "HINDUNILVR.NS", "TCS.NS", "CIPLA.NS",
        "ASIANPAINT.NS", "GRASIM.NS", "BRITANNIA.NS", "SHREECEM.NS",
        "TECHM.NS", "INDUSINDBK.NS", "EICHERMOT.NS", "COALINDIA.NS", "GAIL.NS",
        "BOSCHLTD.NS", "M&MFIN.NS", "IDFCFIRSTB.NS", "HAVELLS.NS"
    ]

# Fetch large cap tickers
def fetch_large_cap_tickers():
    return fetch_nifty50_tickers()  # Assuming large caps are the same as Nifty 50

# Fetch small cap tickers
def fetch_small_cap_tickers():
    return [
        "ALOKINDS.NS", "ADANIENT.NS", "AARTIIND.NS", "AVANTIFEED.NS", "BLS.IN",
        "BHEL.NS", "BIRLACORP.NS", "CARBORUNIV.NS", "CENTRALBANK.NS", "EMAMILTD.NS",
        "FDC.NS", "GLAXO.NS", "GODFRYPHLP.NS", "GSKCONS.NS", "HAVELLS.NS",
        "HEMIPAPER.NS", "HIL.NS", "JINDALSAW.NS", "JUBLFOOD.NS", "KOTAKMAH.NS",
        "MSTCLAS.NS", "NCC.NS", "PAGEIND.NS", "PIIND.NS", "SBI.CN",
        "SISL.NS", "SOMANYCERA.NS", "STAR.NS", "SUNDARAM.NS", "TATAINVEST.NS",
        "VSTIND.NS", "WABCOINDIA.NS", "WELCORP.NS", "ZEELEARN.NS", "ZOMATO.NS"
    ]

# Get top movers
def get_top_movers(tickers, days=1):
    end_date = datetime.now()
    start_date = end_date - timedelta(days=days)
    
    data = {}
    for ticker in tickers:
        try:
            df = yf.download(ticker, start=start_date, end=end_date)
            if not df.empty and 'Close' in df.columns:
                df['Ticker'] = ticker
                data[ticker] = df['Close'].pct_change().iloc[-1]  # Percentage change
        except Exception as e:
            st.error(f"Error fetching data for {ticker}: {e}")
    
    sorted_data = sorted(data.items(), key=lambda x: x[1], reverse=True)
    top_gainers = sorted_data[:10]
    top_losers = sorted_data[-10:]
    
    return top_gainers, top_losers

# Format DataFrame with color
def format_df(df):
    if not df.empty:
        df['Percentage Change'] = pd.to_numeric(df['Percentage Change'], errors='coerce')
        return df.style.applymap(lambda x: 'color: green' if x > 0 else 'color: red', subset=['Percentage Change'])
    return df

# Display dashboard
def display_dashboard():
    st.header("Dashboard")
    
    # Fetch tickers
    nifty50_tickers = fetch_nifty50_tickers()
    large_cap_tickers = fetch_large_cap_tickers()
    small_cap_tickers = fetch_small_cap_tickers()

    # Get top gainers and losers
    top_gainers_nifty50, top_losers_nifty50 = get_top_movers(nifty50_tickers)
    top_gainers_large_cap, top_losers_large_cap = get_top_movers(large_cap_tickers)
    top_gainers_small_cap, top_losers_small_cap = get_top_movers(small_cap_tickers)
    
    # Create columns for tables
    col1, col2, col3, col4 = st.columns(4)

    with col1:
        st.write("### Nifty 50 Top Gainers")
        if top_gainers_nifty50:
            df_gainers_nifty50 = pd.DataFrame(top_gainers_nifty50, columns=['Ticker', 'Percentage Change'])
            st.dataframe(format_df(df_gainers_nifty50))

    with col2:
        st.write("### Nifty 50 Top Losers")
        if top_losers_nifty50:
            df_losers_nifty50 = pd.DataFrame(top_losers_nifty50, columns=['Ticker', 'Percentage Change'])
            st.dataframe(format_df(df_losers_nifty50))

    with col3:
        st.write("### Large Cap Top Gainers")
        if top_gainers_large_cap:
            df_gainers_large_cap = pd.DataFrame(top_gainers_large_cap, columns=['Ticker', 'Percentage Change'])
            st.dataframe(format_df(df_gainers_large_cap))

    with col4:
        st.write("### Large Cap Top Losers")
        if top_losers_large_cap:
            df_losers_large_cap = pd.DataFrame(top_losers_large_cap, columns=['Ticker', 'Percentage Change'])
            st.dataframe(format_df(df_losers_large_cap))

# Fetch and display stock profile
def fetch_stock_profile(ticker):
    try:
        stock = yf.Ticker(ticker)
        info = stock.info

        profile = {
            "Name": info.get('shortName', 'N/A'),
            "Current Price": f"₹ {info.get('currentPrice', 'N/A')}",
            "Market Cap": f"₹ {info.get('marketCap', 'N/A') / 1e7:.2f} Cr.",
            "P/E Ratio": info.get('forwardEps', 'N/A'),
            "Book Value": info.get('bookValue', 'N/A'),
            "Dividend Yield": info.get('dividendYield', 'N/A'),
            "ROCE": info.get('returnOnCapitalEmployed', 'N/A'),
            "ROE": info.get('returnOnEquity', 'N/A'),
            "Face Value": info.get('faceValue', 'N/A')
        }
        return profile
    except Exception as e:
        st.error(f"Error fetching profile for {ticker}: {e}")
        return {}




# Display stock profile as a table
def display_profile(profile):
    st.subheader("Stock Profile")
    profile_df = pd.DataFrame([profile])
    st.table(profile_df)

# Fetch and display quarterly results
def display_quarterly_results(ticker):
    st.subheader("Quarterly Results Summary")
    try:
        stock = yf.Ticker(ticker)
        financials = stock.quarterly_financials.T
        if not financials.empty:
            results = {
                'Sales': financials['Total Revenue'].iloc[-1] if 'Total Revenue' in financials.columns else 'N/A',
                'Operating Profit Margin': financials['Operating Income'].iloc[-1] if 'Operating Income' in financials.columns else 'N/A',
                'Net Profit': financials['Net Income'].iloc[-1] if 'Net Income' in financials.columns else 'N/A'
            }
            results_df = pd.DataFrame([results])
            st.table(results_df)
        else:
            st.write("No quarterly results available.")
    except Exception as e:
        st.write(f"Error fetching quarterly results: {e}")

# Fetch and display shareholding pattern
def display_shareholding_pattern(ticker):
    st.subheader("Shareholding Pattern")
    
    # Placeholder values; replace with actual data source or API call
    data = {
        'Category': ['Promoters', 'FIIs (Foreign Institutional Investors)', 'DIIs (Domestic Institutional Investors)', 'Public'],
        'Holding (%)': [45.0, 20.0, 15.0, 20.0]
    }
    
    df = pd.DataFrame(data)
    st.table(df)


def display_financial_ratios(ticker):
    st.subheader("Financial Ratios")
    stock = yf.Ticker(ticker)
    
    try:
        # Placeholder values, calculate actual values based on your requirements
        ratios = {
            'Debtor Days': 73,
            'Working Capital Days': 194,
            'Cash Conversion Cycle': 51
        }
        ratios_df = pd.DataFrame([ratios])
        st.table(ratios_df)
    except Exception as e:
        st.write("Error fetching financial ratios:", e)

















# Main application
# def main():
#     st.title("Stock Analysis Dashboard")

#     # Select ticker input
#     ticker = st.text_input("Enter Stock Ticker (e.g., TATAMOTORS.NS)")

#     if ticker:
#         profile = fetch_stock_profile(ticker)
#         if profile:
#             display_profile(profile)
        
#         display_quarterly_results(ticker)
#         display_shareholding_pattern(ticker)

#     # Show dashboard
#     if st.button("Show Dashboard"):
#         display_dashboard()

# if __name__ == "__main__":
#     main()