File size: 2,040 Bytes
45a77a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import pandas as pd
from .indicators import rsi, macd

@dataclass
class DataBundle:
    df_sentiment: pd.DataFrame
    df_transcript: pd.DataFrame
    df_bn_hourly: pd.DataFrame
    df_news: pd.DataFrame
    df_bn_1m: pd.DataFrame
    df_nifty_daily: pd.DataFrame

    def copy(self) -> "DataBundle":
        return DataBundle(
            self.df_sentiment.copy(),
            self.df_transcript.copy(),
            self.df_bn_hourly.copy(),
            self.df_news.copy(),
            self.df_bn_1m.copy(),
            self.df_nifty_daily.copy(),
        )

def _read_excel(path: Path) -> pd.DataFrame:
    if not path.exists():
        raise FileNotFoundError(path)
    return pd.read_excel(path)

def load_data(paths) -> DataBundle:
    df_sent = _read_excel(paths.sentiment_pred)
    df_tx = _read_excel(paths.zerodha_tx)
    df_bn_hourly = _read_excel(paths.banknifty_hourly)
    df_news = _read_excel(paths.bank_news)
    df_bn_1m = _read_excel(paths.banknifty_1m)
    df_nifty = _read_excel(paths.nifty_daily)

    # Parse dates
    if "predicted_for" in df_sent.columns:
        df_sent["predicted_for"] = pd.to_datetime(df_sent["predicted_for"])
    if "Prediction_for_date" in df_tx.columns:
        df_tx["Prediction_for_date"] = pd.to_datetime(df_tx["Prediction_for_date"], dayfirst=True)
    for df in (df_bn_hourly, df_bn_1m, df_news, df_nifty):
        for c in df.columns:
            if "date" in c.lower() or c.lower() == "datetime":
                try:
                    df[c] = pd.to_datetime(df[c])
                except Exception:
                    pass

    # Indicators
    df_bn_hourly = rsi(df_bn_hourly)
    df_bn_hourly = macd(df_bn_hourly)
    df_nifty = rsi(df_nifty)
    df_nifty = macd(df_nifty)

    return DataBundle(
        df_sentiment=df_sent,
        df_transcript=df_tx,
        df_bn_hourly=df_bn_hourly,
        df_news=df_news,
        df_bn_1m=df_bn_1m,
        df_nifty_daily=df_nifty,
    )