akshare-one 0.1.3__py3-none-any.whl → 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. akshare_one/__init__.py +2 -3
  2. akshare_one/financial.py +7 -4
  3. akshare_one/insider.py +5 -9
  4. akshare_one/modules/financial/base.py +22 -0
  5. akshare_one/modules/financial/factory.py +44 -0
  6. akshare_one/{adapters → modules/financial}/sina.py +19 -203
  7. akshare_one/modules/historical/base.py +39 -0
  8. akshare_one/modules/historical/eastmoney.py +241 -0
  9. akshare_one/modules/historical/factory.py +46 -0
  10. akshare_one/modules/historical/sina.py +218 -0
  11. akshare_one/modules/insider/base.py +28 -0
  12. akshare_one/modules/insider/factory.py +44 -0
  13. akshare_one/{adapters → modules/insider}/xueqiu.py +16 -76
  14. akshare_one/modules/news/base.py +22 -0
  15. akshare_one/modules/news/eastmoney.py +47 -0
  16. akshare_one/modules/news/factory.py +44 -0
  17. akshare_one/modules/realtime/base.py +27 -0
  18. akshare_one/modules/realtime/eastmoney.py +57 -0
  19. akshare_one/modules/realtime/factory.py +46 -0
  20. akshare_one/modules/realtime/xueqiu.py +60 -0
  21. akshare_one/modules/utils.py +10 -0
  22. akshare_one/news.py +3 -4
  23. akshare_one/stock.py +17 -31
  24. {akshare_one-0.1.3.dist-info → akshare_one-0.2.1.dist-info}/METADATA +9 -9
  25. akshare_one-0.2.1.dist-info/RECORD +29 -0
  26. {akshare_one-0.1.3.dist-info → akshare_one-0.2.1.dist-info}/WHEEL +1 -1
  27. akshare_one/adapters/__init__.py +0 -7
  28. akshare_one/adapters/eastmoney.py +0 -353
  29. akshare_one-0.1.3.dist-info/RECORD +0 -15
  30. /akshare_one/{adapters/cache → modules}/cache.py +0 -0
  31. {akshare_one-0.1.3.dist-info → akshare_one-0.2.1.dist-info}/licenses/LICENSE +0 -0
  32. {akshare_one-0.1.3.dist-info → akshare_one-0.2.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,241 @@
1
+ from cachetools import cached
2
+ from .base import HistoricalDataProvider
3
+ import akshare as ak
4
+ import pandas as pd
5
+ from ..cache import CACHE_CONFIG
6
+
7
+
8
+ class EastMoneyHistorical(HistoricalDataProvider):
9
+ """Adapter for EastMoney historical stock data API"""
10
+
11
+ @cached(
12
+ cache=CACHE_CONFIG["hist_data_cache"],
13
+ key=lambda self: f"eastmoney_hist_{self.symbol}_{self.interval}_{self.interval_multiplier}_{self.adjust}",
14
+ )
15
+ def get_hist_data(self) -> pd.DataFrame:
16
+ """Fetches EastMoney historical market data
17
+
18
+ Returns:
19
+ pd.DataFrame:
20
+ - timestamp
21
+ - open
22
+ - high
23
+ - low
24
+ - close
25
+ - volume
26
+ """
27
+ self.interval = self.interval.lower()
28
+ self._validate_interval_params(self.interval, self.interval_multiplier)
29
+
30
+ try:
31
+ if self.interval in ["minute", "hour"]:
32
+ df = self._get_intraday_data()
33
+ else:
34
+ df = self._get_daily_plus_data()
35
+
36
+ return df
37
+ except Exception as e:
38
+ raise ValueError(f"Failed to fetch historical data: {str(e)}")
39
+
40
+ def _get_intraday_data(self) -> pd.DataFrame:
41
+ """Fetches intraday data at minute or hour intervals"""
42
+ # Set trading hours
43
+ start_date = self._ensure_time_format(self.start_date, "09:30:00")
44
+ end_date = self._ensure_time_format(self.end_date, "15:00:00")
45
+
46
+ # Get raw data
47
+ period = "1" if self.interval == "minute" else "60"
48
+ raw_df = ak.stock_zh_a_hist_min_em(
49
+ symbol=self.symbol,
50
+ period=period,
51
+ start_date=start_date,
52
+ end_date=end_date,
53
+ adjust=self._map_adjust_param(self.adjust),
54
+ )
55
+
56
+ # Process data
57
+ resampled = self._resample_intraday_data(
58
+ raw_df,
59
+ f"{self.interval_multiplier}min"
60
+ if self.interval == "minute"
61
+ else f"{self.interval_multiplier}h",
62
+ )
63
+ return self._clean_minute_data(resampled, str(self.interval_multiplier))
64
+
65
+ def _get_daily_plus_data(self) -> pd.DataFrame:
66
+ """Fetches daily and higher-level data (day/week/month/year)"""
67
+ start_date = self._convert_date_format(self.start_date)
68
+ end_date = self._convert_date_format(self.end_date)
69
+
70
+ period_map = {
71
+ "day": "daily",
72
+ "week": "weekly",
73
+ "month": "monthly",
74
+ "year": "monthly",
75
+ }
76
+ period = period_map[self.interval]
77
+
78
+ raw_df = ak.stock_zh_a_hist(
79
+ symbol=self.symbol,
80
+ period=period,
81
+ start_date=start_date,
82
+ end_date=end_date,
83
+ adjust=self._map_adjust_param(self.adjust),
84
+ )
85
+
86
+ if self.interval == "year":
87
+ self.interval_multiplier *= 12
88
+
89
+ if self.interval_multiplier > 1:
90
+ raw_df = self._resample_data(
91
+ raw_df, self.interval, self.interval_multiplier
92
+ )
93
+
94
+ return self._clean_data(raw_df)
95
+
96
+ def _validate_interval_params(self, interval: str, multiplier: int) -> None:
97
+ """Validates the validity of interval and multiplier"""
98
+ if interval not in self.get_supported_intervals():
99
+ raise ValueError(f"Unsupported interval parameter: {interval}")
100
+
101
+ if interval in ["minute", "hour"] and multiplier < 1:
102
+ raise ValueError(f"interval_multiplier for {interval} level must be ≥ 1")
103
+
104
+ def _ensure_time_format(self, date_str: str, default_time: str) -> str:
105
+ """Ensures the date string includes the time part"""
106
+ if " " not in date_str:
107
+ return f"{date_str} {default_time}"
108
+ return date_str
109
+
110
+ def _convert_date_format(self, date_str: str) -> str:
111
+ """Converts date format from YYYY-MM-DD to YYYYMMDD"""
112
+ return date_str.replace("-", "") if "-" in date_str else date_str
113
+
114
+ def _map_adjust_param(self, adjust: str) -> str:
115
+ """Maps adjustment parameters to the required format"""
116
+ return adjust if adjust != "none" else ""
117
+
118
+ def _resample_intraday_data(self, df: pd.DataFrame, freq: str) -> pd.DataFrame:
119
+ """Resamples intraday data to the specified frequency"""
120
+ df["时间"] = pd.to_datetime(df["时间"])
121
+ df = df.set_index("时间")
122
+ resampled = df.resample(freq).agg(
123
+ {
124
+ "开盘": "first",
125
+ "最高": "max",
126
+ "最低": "min",
127
+ "收盘": "last",
128
+ "成交量": "sum",
129
+ "成交额": "sum",
130
+ }
131
+ )
132
+ return resampled.reset_index()
133
+
134
+ def _resample_data(
135
+ self, df: pd.DataFrame, interval: str, multiplier: int
136
+ ) -> pd.DataFrame:
137
+ """Resamples daily and higher-level data to the specified interval"""
138
+ freq_map = {
139
+ "day": f"{multiplier}D",
140
+ "week": f"{multiplier}W-MON",
141
+ "month": f"{multiplier}MS",
142
+ "year": f"{multiplier}AS-JAN",
143
+ }
144
+ freq = freq_map[interval]
145
+
146
+ df["日期"] = pd.to_datetime(df["日期"])
147
+ df = df.set_index("日期")
148
+ resampled = df.resample(freq).agg(
149
+ {
150
+ "开盘": "first",
151
+ "最高": "max",
152
+ "最低": "min",
153
+ "收盘": "last",
154
+ "成交量": "sum",
155
+ }
156
+ )
157
+ return resampled.reset_index()
158
+
159
+ def _clean_minute_data(self, raw_df: pd.DataFrame, period: str) -> pd.DataFrame:
160
+ """Cleans and standardizes minute/hour level data, converting timestamps to UTC"""
161
+ column_map = {
162
+ "1": {
163
+ "时间": "timestamp",
164
+ "开盘": "open",
165
+ "收盘": "close",
166
+ "最高": "high",
167
+ "最低": "low",
168
+ "成交量": "volume",
169
+ "成交额": "amount",
170
+ "均价": "vwap",
171
+ },
172
+ "default": {
173
+ "时间": "timestamp",
174
+ "开盘": "open",
175
+ "收盘": "close",
176
+ "最高": "high",
177
+ "最低": "low",
178
+ "涨跌幅": "pct_change",
179
+ "涨跌额": "change",
180
+ "成交量": "volume",
181
+ "成交额": "amount",
182
+ "振幅": "amplitude",
183
+ "换手率": "turnover",
184
+ },
185
+ }
186
+
187
+ mapping = column_map["1"] if period == "1" else column_map["default"]
188
+ df = raw_df.rename(columns=mapping)
189
+
190
+ if "timestamp" in df.columns:
191
+ df["timestamp"] = (
192
+ pd.to_datetime(df["timestamp"])
193
+ .dt.tz_localize("Asia/Shanghai")
194
+ .dt.tz_convert("UTC")
195
+ )
196
+
197
+ return self._select_standard_columns(df)
198
+
199
+ def _clean_data(self, raw_df: pd.DataFrame) -> pd.DataFrame:
200
+ """Cleans and standardizes daily and higher-level data, converting timestamps to UTC"""
201
+ column_map = {
202
+ "日期": "timestamp",
203
+ "开盘": "open",
204
+ "收盘": "close",
205
+ "最高": "high",
206
+ "最低": "low",
207
+ "成交量": "volume",
208
+ }
209
+
210
+ available_columns = {
211
+ src: target for src, target in column_map.items() if src in raw_df.columns
212
+ }
213
+
214
+ if not available_columns:
215
+ raise ValueError("Expected columns not found in raw data")
216
+
217
+ df = raw_df.rename(columns=available_columns)
218
+
219
+ if "timestamp" in df.columns:
220
+ df["timestamp"] = (
221
+ pd.to_datetime(df["timestamp"])
222
+ .dt.tz_localize("Asia/Shanghai")
223
+ .dt.tz_convert("UTC")
224
+ )
225
+
226
+ if "volume" in df.columns:
227
+ df["volume"] = df["volume"].astype("int64")
228
+
229
+ return self._select_standard_columns(df)
230
+
231
+ def _select_standard_columns(self, df: pd.DataFrame) -> pd.DataFrame:
232
+ """Selects and orders the standard output columns"""
233
+ standard_columns = [
234
+ "timestamp",
235
+ "open",
236
+ "high",
237
+ "low",
238
+ "close",
239
+ "volume",
240
+ ]
241
+ return df[[col for col in standard_columns if col in df.columns]]
@@ -0,0 +1,46 @@
1
+ from .base import HistoricalDataProvider
2
+ from .eastmoney import EastMoneyHistorical
3
+ from .sina import SinaHistorical
4
+
5
+
6
+ class HistoricalDataFactory:
7
+ """
8
+ Factory class for creating historical data providers
9
+ """
10
+
11
+ _providers = {
12
+ "eastmoney": EastMoneyHistorical,
13
+ "sina": SinaHistorical,
14
+ }
15
+
16
+ @classmethod
17
+ def get_provider(cls, provider_name: str, **kwargs) -> HistoricalDataProvider:
18
+ """
19
+ Get a historical data provider by name
20
+
21
+ Args:
22
+ provider_name: Name of the provider (e.g., 'eastmoney')
23
+ **kwargs: Additional arguments to pass to the provider's constructor
24
+
25
+ Returns:
26
+ HistoricalDataProvider: An instance of the requested provider
27
+
28
+ Raises:
29
+ ValueError: If the requested provider is not found
30
+ """
31
+ provider_class = cls._providers.get(provider_name.lower())
32
+ if not provider_class:
33
+ raise ValueError(f"Unknown historical data provider: {provider_name}")
34
+
35
+ return provider_class(**kwargs)
36
+
37
+ @classmethod
38
+ def register_provider(cls, name: str, provider_class: type):
39
+ """
40
+ Register a new historical data provider
41
+
42
+ Args:
43
+ name: Name to associate with this provider
44
+ provider_class: The provider class to register
45
+ """
46
+ cls._providers[name.lower()] = provider_class
@@ -0,0 +1,218 @@
1
+ from cachetools import cached
2
+ from .base import HistoricalDataProvider
3
+ import akshare as ak
4
+ import pandas as pd
5
+ from ..cache import CACHE_CONFIG
6
+
7
+
8
+ class SinaHistorical(HistoricalDataProvider):
9
+ """Adapter for Sina historical stock data API"""
10
+
11
+ @cached(
12
+ cache=CACHE_CONFIG["hist_data_cache"],
13
+ key=lambda self: f"sina_hist_{self.symbol}_{self.interval}_{self.interval_multiplier}_{self.adjust}",
14
+ )
15
+ def get_hist_data(self) -> pd.DataFrame:
16
+ """Fetches Sina historical market data
17
+
18
+ Returns:
19
+ pd.DataFrame:
20
+ - timestamp
21
+ - open
22
+ - high
23
+ - low
24
+ - close
25
+ - volume
26
+ """
27
+ self.interval = self.interval.lower()
28
+ self._validate_interval_params(self.interval, self.interval_multiplier)
29
+
30
+ try:
31
+ stock = (
32
+ f"sh{self.symbol}"
33
+ if not self.symbol.startswith(("sh", "sz", "bj"))
34
+ else self.symbol
35
+ )
36
+
37
+ if self.interval == "minute":
38
+ df = self._get_minute_data(stock)
39
+ elif self.interval == "hour":
40
+ df = self._get_hour_data(stock)
41
+ else:
42
+ df = self._get_daily_plus_data(stock)
43
+
44
+ return df
45
+ except Exception as e:
46
+ raise ValueError(f"Failed to fetch historical data: {str(e)}")
47
+
48
+ def _get_minute_data(self, stock: str) -> pd.DataFrame:
49
+ """Fetches minute level data"""
50
+ raw_df = ak.stock_zh_a_minute(
51
+ symbol=stock,
52
+ period="1",
53
+ adjust=self._map_adjust_param(self.adjust),
54
+ )
55
+ raw_df = raw_df.rename(columns={"day": "date"})
56
+ raw_df["date"] = pd.to_datetime(raw_df["date"])
57
+ raw_df = raw_df.set_index("date")
58
+ raw_df = (
59
+ raw_df.resample(f"{self.interval_multiplier}min")
60
+ .agg(
61
+ {
62
+ "open": "first",
63
+ "high": "max",
64
+ "low": "min",
65
+ "close": "last",
66
+ "volume": "sum",
67
+ }
68
+ )
69
+ .reset_index()
70
+ )
71
+ return self._clean_minute_data(raw_df)
72
+
73
+ def _get_hour_data(self, stock: str) -> pd.DataFrame:
74
+ """Fetches hour level data"""
75
+ if self.interval_multiplier < 1:
76
+ raise ValueError("Hour interval multiplier must be >= 1")
77
+
78
+ raw_df = ak.stock_zh_a_minute(
79
+ symbol=stock,
80
+ period="60",
81
+ adjust=self._map_adjust_param(self.adjust),
82
+ )
83
+ raw_df = raw_df.rename(columns={"day": "date"})
84
+ raw_df["date"] = pd.to_datetime(raw_df["date"])
85
+ raw_df = raw_df.set_index("date")
86
+ raw_df = (
87
+ raw_df.resample(f"{self.interval_multiplier}h")
88
+ .agg(
89
+ {
90
+ "open": "first",
91
+ "high": "max",
92
+ "low": "min",
93
+ "close": "last",
94
+ "volume": "sum",
95
+ }
96
+ )
97
+ .reset_index()
98
+ )
99
+ return self._clean_minute_data(raw_df)
100
+
101
+ def _get_daily_plus_data(self, stock: str) -> pd.DataFrame:
102
+ """Fetches daily and higher-level data (day/week/month/year)"""
103
+ start_date = self._convert_date_format(self.start_date)
104
+ end_date = self._convert_date_format(self.end_date)
105
+
106
+ raw_df = ak.stock_zh_a_daily(
107
+ symbol=stock,
108
+ start_date=start_date,
109
+ end_date=end_date,
110
+ adjust=self._map_adjust_param(self.adjust),
111
+ )
112
+
113
+ if self.interval_multiplier > 1:
114
+ raw_df = self._resample_data(
115
+ raw_df, self.interval, self.interval_multiplier
116
+ )
117
+
118
+ return self._clean_data(raw_df)
119
+
120
+ def _validate_interval_params(self, interval: str, multiplier: int) -> None:
121
+ """Validates the validity of interval and multiplier"""
122
+ if interval not in self.get_supported_intervals():
123
+ raise ValueError(f"Unsupported interval parameter: {interval}")
124
+
125
+ if interval in ["minute", "hour"] and multiplier < 1:
126
+ raise ValueError(f"interval_multiplier for {interval} level must be ≥ 1")
127
+
128
+ def _convert_date_format(self, date_str: str) -> str:
129
+ """Converts date format from YYYY-MM-DD to YYYYMMDD"""
130
+ return date_str.replace("-", "") if "-" in date_str else date_str
131
+
132
+ def _map_adjust_param(self, adjust: str) -> str:
133
+ """Maps adjustment parameters to the required format"""
134
+ return adjust if adjust != "none" else ""
135
+
136
+ def _resample_data(
137
+ self, df: pd.DataFrame, interval: str, multiplier: int
138
+ ) -> pd.DataFrame:
139
+ """Resamples daily and higher-level data to the specified interval"""
140
+ freq_map = {
141
+ "day": f"{multiplier}D",
142
+ "week": f"{multiplier}W-MON",
143
+ "month": f"{multiplier}MS",
144
+ "year": f"{multiplier}AS-JAN",
145
+ }
146
+ freq = freq_map[interval]
147
+
148
+ df["date"] = pd.to_datetime(df["date"])
149
+ df = df.set_index("date")
150
+ resampled = df.resample(freq).agg(
151
+ {
152
+ "open": "first",
153
+ "high": "max",
154
+ "low": "min",
155
+ "close": "last",
156
+ "volume": "sum",
157
+ }
158
+ )
159
+ return resampled.reset_index()
160
+
161
+ def _clean_minute_data(self, raw_df: pd.DataFrame) -> pd.DataFrame:
162
+ """Cleans and standardizes minute/hour level data, converting timestamps to UTC"""
163
+ column_map = {
164
+ "date": "timestamp",
165
+ "open": "open",
166
+ "high": "high",
167
+ "low": "low",
168
+ "close": "close",
169
+ "volume": "volume",
170
+ }
171
+
172
+ df = raw_df.rename(columns=column_map)
173
+
174
+ if "timestamp" in df.columns:
175
+ df["timestamp"] = (
176
+ pd.to_datetime(df["timestamp"])
177
+ .dt.tz_localize("Asia/Shanghai")
178
+ .dt.tz_convert("UTC")
179
+ )
180
+
181
+ return self._select_standard_columns(df)
182
+
183
+ def _clean_data(self, raw_df: pd.DataFrame) -> pd.DataFrame:
184
+ """Cleans and standardizes daily and higher-level data, converting timestamps to UTC"""
185
+ column_map = {
186
+ "date": "timestamp",
187
+ "open": "open",
188
+ "high": "high",
189
+ "low": "low",
190
+ "close": "close",
191
+ "volume": "volume",
192
+ }
193
+
194
+ df = raw_df.rename(columns=column_map)
195
+
196
+ if "timestamp" in df.columns:
197
+ df["timestamp"] = (
198
+ pd.to_datetime(df["timestamp"])
199
+ .dt.tz_localize("Asia/Shanghai")
200
+ .dt.tz_convert("UTC")
201
+ )
202
+
203
+ if "volume" in df.columns:
204
+ df["volume"] = df["volume"].astype("int64")
205
+
206
+ return self._select_standard_columns(df)
207
+
208
+ def _select_standard_columns(self, df: pd.DataFrame) -> pd.DataFrame:
209
+ """Selects and orders the standard output columns"""
210
+ standard_columns = [
211
+ "timestamp",
212
+ "open",
213
+ "high",
214
+ "low",
215
+ "close",
216
+ "volume",
217
+ ]
218
+ return df[[col for col in standard_columns if col in df.columns]]
@@ -0,0 +1,28 @@
1
+ from abc import ABC, abstractmethod
2
+ import pandas as pd
3
+
4
+
5
+ class InsiderDataProvider(ABC):
6
+ def __init__(self, symbol: str) -> None:
7
+ self.symbol = symbol
8
+
9
+ @abstractmethod
10
+ def get_inner_trade_data(self) -> pd.DataFrame:
11
+ """Fetches insider trade data
12
+
13
+ Returns:
14
+ pd.DataFrame:
15
+ - symbol: 股票代码
16
+ - issuer: 股票名称
17
+ - name: 变动人
18
+ - title: 董监高职务
19
+ - transaction_date: 变动日期(UTC时区)
20
+ - transaction_shares: 变动股数
21
+ - transaction_price_per_share: 成交均价
22
+ - shares_owned_after_transaction: 变动后持股数
23
+ - relationship: 与董监高关系
24
+ - is_board_director: 是否为董事会成员
25
+ - transaction_value: 交易金额(变动股数*成交均价)
26
+ - shares_owned_before_transaction: 变动前持股数
27
+ """
28
+ pass
@@ -0,0 +1,44 @@
1
+ from .xueqiu import XueQiuInsider
2
+ from .base import InsiderDataProvider
3
+
4
+
5
+ class InsiderDataFactory:
6
+ """
7
+ Factory class for creating insider data providers
8
+ """
9
+
10
+ _providers = {
11
+ "xueqiu": XueQiuInsider,
12
+ }
13
+
14
+ @classmethod
15
+ def get_provider(cls, provider_name: str, **kwargs) -> InsiderDataProvider:
16
+ """
17
+ Get an insider data provider by name
18
+
19
+ Args:
20
+ provider_name: Name of the provider (e.g., 'xueqiu')
21
+ **kwargs: Additional arguments to pass to the provider's constructor
22
+
23
+ Returns:
24
+ InsiderDataProvider: An instance of the requested provider
25
+
26
+ Raises:
27
+ ValueError: If the requested provider is not found
28
+ """
29
+ provider_class = cls._providers.get(provider_name.lower())
30
+ if not provider_class:
31
+ raise ValueError(f"Unknown insider data provider: {provider_name}")
32
+
33
+ return provider_class(**kwargs)
34
+
35
+ @classmethod
36
+ def register_provider(cls, name: str, provider_class: type):
37
+ """
38
+ Register a new insider data provider
39
+
40
+ Args:
41
+ name: Name to associate with this provider
42
+ provider_class: The provider class to register
43
+ """
44
+ cls._providers[name.lower()] = provider_class