File size: 10,801 Bytes
ad64b5a
c14c75e
 
 
ad64b5a
c14c75e
ad64b5a
 
 
c14c75e
ad64b5a
 
c14c75e
 
ad64b5a
c14c75e
 
ad64b5a
c14c75e
 
ad64b5a
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
c14c75e
ad64b5a
 
c14c75e
ad64b5a
 
c14c75e
ad64b5a
 
c14c75e
 
ad64b5a
 
 
c14c75e
ad64b5a
 
 
 
 
 
 
 
 
 
 
 
 
 
c14c75e
 
ad64b5a
 
 
c14c75e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad64b5a
 
 
 
c14c75e
 
 
 
ad64b5a
c14c75e
 
ad64b5a
 
 
c14c75e
 
 
ad64b5a
c14c75e
 
 
 
 
 
 
ad64b5a
 
c14c75e
 
ad64b5a
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
3e6331d
ad64b5a
c14c75e
ad64b5a
c14c75e
 
 
 
 
ad64b5a
 
c14c75e
 
 
 
ad64b5a
c14c75e
 
 
 
ad64b5a
 
 
 
c14c75e
 
 
ad64b5a
c14c75e
ad64b5a
 
 
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
c14c75e
ad64b5a
 
c14c75e
 
ad64b5a
 
c14c75e
 
 
 
ad64b5a
c14c75e
ad64b5a
 
 
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
c14c75e
 
ad64b5a
 
 
 
c14c75e
 
 
 
 
ad64b5a
c14c75e
ad64b5a
 
 
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
c14c75e
 
ad64b5a
 
 
 
 
c14c75e
 
 
 
ad64b5a
c14c75e
ad64b5a
 
 
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
c14c75e
 
ad64b5a
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
 
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
c14c75e
 
ad64b5a
 
 
 
 
c14c75e
 
 
 
ad64b5a
c14c75e
ad64b5a
 
 
c14c75e
 
ad64b5a
c14c75e
ad64b5a
 
c14c75e
 
ad64b5a
 
 
 
c14c75e
 
 
ad64b5a
c14c75e
 
 
ad64b5a
c14c75e
ad64b5a
 
 
c14c75e
 
 
ad64b5a
 
c14c75e
 
ad64b5a
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# stock.py  — compact, merged, single-file

import yfinance as yf
import pandas as pd
import traceback

# ================================================================
#                    BASIC YFINANCE FETCHERS
# ================================================================

def yfinfo(symbol):
    return yf.Ticker(symbol + ".NS").info

def qresult(symbol):
    return yf.Ticker(symbol + ".NS").quarterly_financials

def result(symbol):
    return yf.Ticker(symbol + ".NS").financials

def balance(symbol):
    return yf.Ticker(symbol + ".NS").balance_sheet

def cashflow(symbol):
    return yf.Ticker(symbol + ".NS").cashflow

def dividend(symbol):
    return yf.Ticker(symbol + ".NS").dividends.to_frame("Dividend")

def split(symbol):
    return yf.Ticker(symbol + ".NS").splits.to_frame("Split")

def intraday(symbol):
    return yf.download(symbol + ".NS", period="1d", interval="5m").round(2)

def daily(symbol):
    return yf.download(symbol + ".NS", period="1y", interval="1d").round(2)


# ================================================================
#              FETCH INFO  (USES COMMON.PY HELPERS)
# ================================================================

from common import (
    format_number,
    format_large_number,
    make_table,
    html_card,
    html_section,
    html_error,
    clean_df,
    safe_get,
    wrap_html
)

from chart_builder import build_chart
from ta_indi_pat import talib_df


# -------------------------- INFO ------------------------------

def fetch_info(symbol):
    try:
        info = yfinfo(symbol)
        if not info:
            return html_error(f"No information found for {symbol}")

        basic = {
            "Symbol": symbol,
            "Name": safe_get(info, "longName"),
            "Sector": safe_get(info, "sector"),
            "Industry": safe_get(info, "industry"),
            "Website": safe_get(info, "website"),
            "Employee Count": format_large_number(safe_get(info, "fullTimeEmployees")),
        }
        df_basic = pd.DataFrame(basic.items(), columns=["Field", "Value"])

        price_info = {
            "Current Price": format_number(safe_get(info, "currentPrice")),
            "Previous Close": format_number(safe_get(info, "previousClose")),
            "Open": format_number(safe_get(info, "open")),
            "Day High": format_number(safe_get(info, "dayHigh")),
            "Day Low": format_number(safe_get(info, "dayLow")),
            "52W High": format_number(safe_get(info, "fiftyTwoWeekHigh")),
            "52W Low": format_number(safe_get(info, "fiftyTwoWeekLow")),
            "Volume": format_large_number(safe_get(info, "volume")),
            "Avg Volume": format_large_number(safe_get(info, "averageVolume")),
        }
        df_price = pd.DataFrame(price_info.items(), columns=["Field", "Value"])

        valuation = {
            "Market Cap": format_large_number(safe_get(info, "marketCap")),
            "PE Ratio": format_number(safe_get(info, "trailingPE")),
            "EPS": format_number(safe_get(info, "trailingEps")),
            "PB Ratio": format_number(safe_get(info, "priceToBook")),
            "Dividend Yield": format_number(safe_get(info, "dividendYield")),
            "ROE": format_number(safe_get(info, "returnOnEquity")),
            "ROA": format_number(safe_get(info, "returnOnAssets")),
        }
        df_val = pd.DataFrame(valuation.items(), columns=["Field", "Value"])

        extra = {
            "Beta": format_number(safe_get(info, "beta")),
            "Revenue": format_large_number(safe_get(info, "totalRevenue")),
            "Gross Margins": format_number(safe_get(info, "grossMargins")),
            "Operating Margins": format_number(safe_get(info, "operatingMargins")),
            "Profit Margins": format_number(safe_get(info, "profitMargins")),
            "Book Value": format_number(safe_get(info, "bookValue")),
        }
        df_extra = pd.DataFrame(extra.items(), columns=["Field", "Value"])

        final_html = (
            html_card("Basic Information", make_table(df_basic))
            + html_card("Price Details", make_table(df_price))
            + html_card("Valuation Metrics", make_table(df_val))
            + html_card("Additional Company Data", make_table(df_extra))
        )
        return final_html

    except Exception as e:
        return html_error(f"INFO ERROR: {e}<br><pre>{traceback.format_exc()}</pre>")


# -------------------------- INTRADAY ------------------------------

def fetch_intraday(symbol, indicators=None):
    try:
        df = intraday(symbol)
        if df.empty:
            return wrap_html(f"<h1>No intraday data for {symbol}</h1>")

        if isinstance(df.columns, pd.MultiIndex):
            df.columns = df.columns.get_level_values(0)

        chart_html = build_chart(df, indicators=indicators, volume=True)
        table_html = make_table(df.tail(50))

        return wrap_html(f"{chart_html}<h2>Last 50 Rows</h2>{table_html}",
                         title=f"{symbol} Intraday")

    except Exception as e:
        return wrap_html(f"<h1>Error:{e}</h1>")


# -------------------------- DAILY ------------------------------

def fetch_daily(symbol, source="yfinance", max_rows=200):
    try:
        df = daily(symbol)
        df_disp = df.head(max_rows)

        combined_df = talib_df(df_disp)
        table_html = combined_df.to_html(
            classes="table table-striped table-bordered",
            index=False
        )

        scroll = f"""
        <div style="overflow:auto; max-height:600px; border:1px solid #ccc;">
            {table_html}
        </div>
        """

        return wrap_html(f"<h2>{symbol} Daily</h2>" + html_card("TA-Lib", scroll))

    except Exception as e:
        return html_card("Error", str(e))


# -------------------------- QUARTERLY ------------------------------

def fetch_qresult(symbol):
    try:
        df = qresult(symbol)
        if df.empty:
            return wrap_html(f"<h1>No quarterly results for {symbol}</h1>")

        df_fmt = df.copy()
        for col in df_fmt.columns:
            df_fmt[col] = df_fmt[col].apply(
                lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
            )
        df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]

        return wrap_html(make_table(df_fmt),
                         title=f"{symbol} Quarterly Results")

    except Exception as e:
        return wrap_html(html_error(f"Quarterly Error: {e}"))


# -------------------------- ANNUAL ------------------------------

def fetch_result(symbol):
    try:
        df = result(symbol)
        if df.empty:
            return wrap_html(f"<h1>No annual results for {symbol}</h1>")

        df_fmt = df.copy()
        for col in df_fmt.columns:
            df_fmt[col] = df_fmt[col].apply(
                lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
            )
        df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]

        return wrap_html(make_table(df_fmt),
                         title=f"{symbol} Annual Results")

    except Exception as e:
        return wrap_html(html_error(f"Annual Error: {e}"))


# -------------------------- BALANCE ------------------------------

def fetch_balance(symbol):
    try:
        df = balance(symbol)
        if df.empty:
            return wrap_html(f"<h1>No balance sheet for {symbol}</h1>")

        df_fmt = df.copy()
        for col in df_fmt.columns:
            df_fmt[col] = df_fmt[col].apply(
                lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
            )
        df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]

        return wrap_html(make_table(df_fmt),
                         title=f"{symbol} Balance Sheet")

    except Exception as e:
        return wrap_html(html_error(f"Balance Error: {e}"))


# -------------------------- CASHFLOW ------------------------------

def fetch_cashflow(symbol):
    try:
        df = cashflow(symbol)
        if df.empty:
            return wrap_html(f"<h1>No cashflow for {symbol}</h1>")

        df_fmt = df.copy()
        for col in df_fmt.columns:
            df_fmt[col] = df_fmt[col].apply(
                lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
            )
        df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]

        return wrap_html(make_table(df_fmt),
                         title=f"{symbol} Cash Flow")

    except Exception as e:
        return wrap_html(html_error(f"Cash Flow Error: {e}"))


# -------------------------- DIVIDEND ------------------------------

def fetch_dividend(symbol):
    try:
        df = dividend(symbol)
        if df.empty:
            return wrap_html(f"<h1>No dividend history for {symbol}</h1>")

        df_fmt = df.copy()
        for col in df_fmt.columns:
            df_fmt[col] = df_fmt[col].apply(
                lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
            )
        df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]

        return wrap_html(make_table(df_fmt),
                         title=f"{symbol} Dividends")

    except Exception as e:
        return wrap_html(html_error(f"Dividend Error: {e}"))


# -------------------------- SPLIT ------------------------------

def fetch_split(symbol):
    try:
        df = split(symbol)
        if df.empty:
            return wrap_html(f"<h1>No splits for {symbol}</h1>")

        df_fmt = df.copy()
        for col in df_fmt.columns:
            df_fmt[col] = df_fmt[col].apply(
                lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
            )
        df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]

        return wrap_html(make_table(df_fmt),
                         title=f"{symbol} Splits")

    except Exception as e:
        return wrap_html(html_error(f"Split Error: {e}"))


# -------------------------- OTHER (EARNINGS) ------------------------------

def fetch_other(symbol):
    try:
        ticker = yf.Ticker(symbol + ".NS")
        df = ticker.earnings

        if df.empty:
            return wrap_html(f"<h1>No earnings data for {symbol}</h1>")

        df_fmt = df.copy()
        for col in df_fmt.columns:
            df_fmt[col] = df_fmt[col].apply(
                lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
            )

        return wrap_html(make_table(df_fmt),
                         title=f"{symbol} Earnings")

    except Exception as e:
        return wrap_html(html_error(f"Earnings Error: {e}"))