neurostats-API 0.0.16__py3-none-any.whl → 0.0.17__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -53,6 +53,23 @@ class StatsFetcher:
53
53
  season = (month - 1) // 3 + 1
54
54
 
55
55
  return StatsDateTime(date, year, month, day, season)
56
+
57
+ def has_required_columns(self, df:pd.DataFrame, required_cols=None):
58
+ """
59
+ Check if the required columns are present in the DataFrame.
60
+
61
+ Args:
62
+ df (pd.DataFrame): The DataFrame to check.
63
+ required_cols (list, optional): List of required column names.
64
+ Defaults to ['date', 'open', 'high', 'low', 'close', 'volume'].
65
+
66
+ Returns:
67
+ bool: True if all required columns are present, False otherwise.
68
+ """
69
+ if required_cols is None:
70
+ required_cols = ['date', 'open', 'high', 'low', 'close', 'volume']
71
+
72
+ return all(col in df.columns for col in required_cols)
56
73
 
57
74
 
58
75
  class BaseTEJFetcher(abc.ABC):
@@ -47,40 +47,46 @@ class TechFetcher(StatsFetcher):
47
47
  )
48
48
 
49
49
  def _get_ohlcv(self):
50
-
51
- if self.ticker in ['GSPC', 'IXIC', 'DJI', 'TWII']:
52
-
53
- full_tick = f'^{self.ticker}'
54
- yf_ticker = yf.Ticker(full_tick)
55
- origin_df = yf_ticker.history(period="10y")
56
- origin_df = origin_df.reset_index()
57
- origin_df["Date"] = pd.to_datetime(origin_df["Date"]).dt.date
58
- df = origin_df.rename(
59
- columns={
60
- "Date": "date",
61
- "Open": "open",
62
- "High": "high",
63
- "Low": "low",
64
- "Close": "close",
65
- "Volume": "volume"
66
- }
67
- )
68
- else:
69
50
 
51
+ required_cols = ['date', 'open', 'high', 'low', 'close', 'volume']
52
+
53
+ try:
70
54
  query = {'ticker': self.ticker}
71
- ticker_full = list(self.collection.find(query))
55
+ ticker_full = self.collection.find_one(query)
72
56
 
73
57
  if not ticker_full:
74
58
  raise ValueError(f"No data found for ticker: {self.ticker}")
75
59
 
76
- if 'daily_data' not in ticker_full[0] or ticker_full[0]['daily_data'] is None:
77
- raise KeyError("Missing 'daily_data' in the retrieved data")
60
+ daily_data = ticker_full.get("daily_data", [])
61
+ if not isinstance(daily_data, list):
62
+ raise TypeError("Expected 'daily_data' to be a list.")
63
+
64
+ df = pd.DataFrame(daily_data)
78
65
 
79
- df = pd.DataFrame(ticker_full[0]['daily_data'])
66
+ if not self.has_required_columns(df, required_cols):
67
+ raise KeyError(f"Missing required columns")
80
68
 
81
- selected_cols = ['date','open','high','low','close','volume']
69
+ except (KeyError, ValueError, TypeError) as e:
70
+
71
+ print(f"Conduct yf searching")
72
+
73
+ if self.ticker in ['GSPC', 'IXIC', 'DJI', 'TWII']:
74
+ full_tick = f'^{self.ticker}'
75
+ else:
76
+ full_tick = f'{self.ticker}.tw'
77
+
78
+ df = self.conduct_yf_search(full_tick)
79
+
80
+ if not self.has_required_columns(df, required_cols):
81
+
82
+ print(f".tw failed, try .two")
83
+
84
+ full_tick = f'{self.ticker}.two'
85
+
86
+ df = self.conduct_yf_search(full_tick)
87
+
88
+ return df[required_cols]
82
89
 
83
- return df[selected_cols]
84
90
 
85
91
  def get_daily(self):
86
92
 
@@ -101,6 +107,29 @@ class TechFetcher(StatsFetcher):
101
107
  def get_yearly(self):
102
108
 
103
109
  return self.yearly_index
110
+
111
+ def conduct_yf_search(self, ticker:str):
112
+
113
+ yf_ticker = yf.Ticker(ticker)
114
+ origin_df = yf_ticker.history(period="10y")
115
+
116
+ if origin_df.empty:
117
+ return origin_df
118
+
119
+ origin_df = origin_df.reset_index()
120
+ origin_df["Date"] = pd.to_datetime(origin_df["Date"])
121
+ df = origin_df.rename(
122
+ columns={
123
+ "Date": "date",
124
+ "Open": "open",
125
+ "High": "high",
126
+ "Low": "low",
127
+ "Close": "close",
128
+ "Volume": "volume"
129
+ }
130
+ )
131
+
132
+ return df
104
133
 
105
134
  class TechProcessor:
106
135
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: neurostats_API
3
- Version: 0.0.16
3
+ Version: 0.0.17
4
4
  Summary: The service of NeuroStats website
5
5
  Home-page: https://github.com/NeurowattStats/NeuroStats_API.git
6
6
  Author: JasonWang@Neurowatt
@@ -3,14 +3,14 @@ neurostats_API/cli.py,sha256=UJSWLIw03P24p-gkBb6JSEI5dW5U12UvLf1L8HjQD-o,873
3
3
  neurostats_API/main.py,sha256=QcsfmWivg2Dnqw3MTJWiI0QvEiRs0VuH-BjwQHFCv00,677
4
4
  neurostats_API/fetchers/__init__.py,sha256=B4aBwVzf_X-YieEf3fZteU0qmBPVIB9VjrmkyWhLK18,489
5
5
  neurostats_API/fetchers/balance_sheet.py,sha256=sQv4Gk5uoKURLEdh57YknOQWiyVwaXJ2Mw75jxNqUS0,5804
6
- neurostats_API/fetchers/base.py,sha256=rcrKW2PTJYfBsxsiGpCYiVTK9pQW4aALYLixIjvNMUk,4890
6
+ neurostats_API/fetchers/base.py,sha256=Rl88Mhvi0uFpPupUvy0iyS7IA4B3fnn6ovMNzS7EU34,5594
7
7
  neurostats_API/fetchers/cash_flow.py,sha256=TY7VAWVXkj5-mzH5Iu0sIE-oV8MvGmmDy0URNotNV1E,7614
8
8
  neurostats_API/fetchers/finance_overview.py,sha256=PxUdWY0x030olYMLcCHDBn068JLmCE2RTOce1dxs5vM,27753
9
9
  neurostats_API/fetchers/institution.py,sha256=UrcBc6t7u7CnEwUsf6YmLbbJ8VncdWpq8bCz17q2dgs,11168
10
10
  neurostats_API/fetchers/margin_trading.py,sha256=lQImtNdvaBoSlKhJvQ3DkH3HjSSgKRJz4ZZpyR5-Z4I,10433
11
11
  neurostats_API/fetchers/month_revenue.py,sha256=nixX2llzjCFr2m2YVjxrSfkBusnZPrPb2dRDq1XLGhw,4251
12
12
  neurostats_API/fetchers/profit_lose.py,sha256=EN9Y0iamcAaHMZdjHXO6b_2buLnORssf8ZS7A0hi74s,5896
13
- neurostats_API/fetchers/tech.py,sha256=Hol1bcwJ_ERcnoTXNWlqqaWOuzdl7MeiAjCvzQMZDTg,12269
13
+ neurostats_API/fetchers/tech.py,sha256=8U6kn7cvWJsmKIMn_f2l6U9H_NBy_OwOXlS26XhFIv0,12926
14
14
  neurostats_API/fetchers/tej_finance_report.py,sha256=laXph2ca1LCFocZjjdvtzmm5fcUecHk2Gs5h6-XMSWY,12967
15
15
  neurostats_API/fetchers/value_invest.py,sha256=b_x2Dpgs8VBU5HdG8ocKtfIEkqhU-Q0S5n6RxuFuM2g,7467
16
16
  neurostats_API/tools/balance_sheet.yaml,sha256=6XygNG_Ybb1Xkk1e39LMLKr7ATvaCP3xxuwFbgNl6dA,673
@@ -24,7 +24,7 @@ neurostats_API/utils/data_process.py,sha256=A--dzOsu42jRxqqCD41gTtjE5rhEBYmhB6y-
24
24
  neurostats_API/utils/datetime.py,sha256=XJya4G8b_-ZOaBbMXgQjWh2MC4wc-o6goQ7EQJQMWrQ,773
25
25
  neurostats_API/utils/db_client.py,sha256=OYe6yazcR4Aa6jYmy47JrryUeh2NnKGqY2K_lSZe6i8,455
26
26
  neurostats_API/utils/fetcher.py,sha256=VbrUhjA-GG5AyjPX2SHtFIbZM4dm3jo0RgZzuCbb_Io,40927
27
- neurostats_API-0.0.16.dist-info/METADATA,sha256=9US1mdwWnOCAnwfsOj-ZLRCfo07p3yd0UfwMKS6989g,29848
28
- neurostats_API-0.0.16.dist-info/WHEEL,sha256=R06PA3UVYHThwHvxuRWMqaGcr-PuniXahwjmQRFMEkY,91
29
- neurostats_API-0.0.16.dist-info/top_level.txt,sha256=nSlQPMG0VtXivJyedp4Bkf86EOy2TpW10VGxolXrqnU,15
30
- neurostats_API-0.0.16.dist-info/RECORD,,
27
+ neurostats_API-0.0.17.dist-info/METADATA,sha256=_MqEN2Yi-tDE8i4UzX9WGUi25Z7SzyNgDR2kj0p2vhw,29848
28
+ neurostats_API-0.0.17.dist-info/WHEEL,sha256=R06PA3UVYHThwHvxuRWMqaGcr-PuniXahwjmQRFMEkY,91
29
+ neurostats_API-0.0.17.dist-info/top_level.txt,sha256=nSlQPMG0VtXivJyedp4Bkf86EOy2TpW10VGxolXrqnU,15
30
+ neurostats_API-0.0.17.dist-info/RECORD,,