forked from ZhuLinsen/daily_stock_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtushare_fetcher.py
More file actions
1321 lines (1070 loc) · 49.7 KB
/
Copy pathtushare_fetcher.py
File metadata and controls
1321 lines (1070 loc) · 49.7 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
===================================
TushareFetcher - 备用数据源 1 (Priority 2)
===================================
数据来源:Tushare Pro API(挖地兔)
特点:需要 Token、有请求配额限制
优点:数据质量高、接口稳定
流控策略:
1. 实现"每分钟调用计数器"
2. 超过免费配额(80次/分)时,强制休眠到下一分钟
3. 使用 tenacity 实现指数退避重试
"""
import json as _json
import logging
import re
import time
from datetime import datetime, timedelta
from typing import Optional, Tuple, List, Dict, Any
import pandas as pd
import requests
from tenacity import (
retry,
stop_after_attempt,
wait_exponential,
retry_if_exception_type,
before_sleep_log,
)
from .base import BaseFetcher, DataFetchError, RateLimitError, STANDARD_COLUMNS,is_bse_code, is_st_stock, is_kc_cy_stock, normalize_stock_code, _is_hk_market
from .realtime_types import UnifiedRealtimeQuote, ChipDistribution
from src.config import get_config
import os
from zoneinfo import ZoneInfo
logger = logging.getLogger(__name__)
# ETF code prefixes by exchange
# Shanghai: 51xxxx, 52xxxx, 56xxxx, 58xxxx
# Shenzhen: 15xxxx, 16xxxx, 18xxxx
_ETF_SH_PREFIXES = ('51', '52', '56', '58')
_ETF_SZ_PREFIXES = ('15', '16', '18')
_ETF_ALL_PREFIXES = _ETF_SH_PREFIXES + _ETF_SZ_PREFIXES
def _is_etf_code(stock_code: str) -> bool:
"""
Check if the code is an ETF fund code.
ETF code ranges:
- Shanghai ETF: 51xxxx, 52xxxx, 56xxxx, 58xxxx
- Shenzhen ETF: 15xxxx, 16xxxx, 18xxxx
"""
code = stock_code.strip().split('.')[0]
return code.startswith(_ETF_ALL_PREFIXES) and len(code) == 6
def _is_us_code(stock_code: str) -> bool:
"""
判断代码是否为美股
美股代码规则:
- 1-5个大写字母,如 'AAPL', 'TSLA'
- 可能包含 '.',如 'BRK.B'
"""
code = stock_code.strip().upper()
return bool(re.match(r'^[A-Z]{1,5}(\.[A-Z])?$', code))
class _TushareHttpClient:
"""Lightweight Tushare Pro client that does not require the tushare SDK."""
def __init__(self, token: str, timeout: int = 30, api_url: str = "http://api.tushare.pro") -> None:
self._token = token
self._timeout = timeout
self._api_url = api_url
def query(self, api_name: str, fields: str = "", **kwargs) -> pd.DataFrame:
req_params = {
"api_name": api_name,
"token": self._token,
"params": kwargs,
"fields": fields,
}
res = requests.post(self._api_url, json=req_params, timeout=self._timeout)
if res.status_code != 200:
raise Exception(f"Tushare API HTTP {res.status_code}")
result = _json.loads(res.text)
if result.get("code") != 0:
raise Exception(result.get("msg") or f"Tushare API error code {result.get('code')}")
data = result.get("data") or {}
columns = data.get("fields") or []
items = data.get("items") or []
return pd.DataFrame(items, columns=columns)
def __getattr__(self, api_name: str):
if api_name.startswith("_"):
raise AttributeError(api_name)
def caller(**kwargs) -> pd.DataFrame:
return self.query(api_name, **kwargs)
return caller
class TushareFetcher(BaseFetcher):
"""
Tushare Pro 数据源实现
优先级:2
数据来源:Tushare Pro API
关键策略:
- 每分钟调用计数器,防止超出配额
- 超过 80 次/分钟时强制等待
- 失败后指数退避重试
配额说明(Tushare 免费用户):
- 每分钟最多 80 次请求
- 每天最多 500 次请求
"""
name = "TushareFetcher"
priority = int(os.getenv("TUSHARE_PRIORITY", "2")) # 默认优先级,会在 __init__ 中根据配置动态调整
def __init__(self, rate_limit_per_minute: int = 80):
"""
初始化 TushareFetcher
Args:
rate_limit_per_minute: 每分钟最大请求数(默认80,Tushare免费配额)
"""
self.rate_limit_per_minute = rate_limit_per_minute
self._call_count = 0 # 当前分钟内的调用次数
self._minute_start: Optional[float] = None # 当前计数周期开始时间
self._api: Optional[object] = None # Tushare API 实例
self.date_list: Optional[List[str]] = None # 交易日列表缓存(倒序,最新日期在前)
self._date_list_end: Optional[str] = None # 缓存对应的截止日期,用于跨日刷新
# 尝试初始化 API
self._init_api()
# 根据 API 初始化结果动态调整优先级
self.priority = self._determine_priority()
def _init_api(self) -> None:
"""
初始化 Tushare API
如果 Token 未配置,此数据源将不可用。
这里直接使用内置 HTTP client,避免运行时强依赖 tushare SDK,
从而减少 Docker / PyInstaller / 多虚拟环境场景下因缺包导致的初始化失败。
"""
config = get_config()
if not config.tushare_token:
logger.warning("Tushare Token 未配置,此数据源不可用")
return
try:
self._api = self._build_api_client(config.tushare_token)
logger.info("Tushare API 初始化成功")
except Exception as e:
logger.error(f"Tushare API 初始化失败: {e}")
self._api = None
def _build_api_client(self, token: str) -> _TushareHttpClient:
"""
Build a lightweight Tushare Pro client over direct HTTP requests.
The project already normalizes all Pro calls through the same request
contract, so we do not need the official tushare SDK during runtime.
"""
client = _TushareHttpClient(token=token)
logger.debug("Tushare API client configured for direct HTTP calls")
return client
def _determine_priority(self) -> int:
"""
根据 Token 配置和 API 初始化状态确定优先级
策略:
- Token 配置且 API 初始化成功:优先级 -1(绝对最高,优于 efinance)
- 其他情况:优先级 2(默认)
Returns:
优先级数字(0=最高,数字越大优先级越低)
"""
config = get_config()
if config.tushare_token and self._api is not None:
# Token 配置且 API 初始化成功,提升为最高优先级(与 efinance 同级,
# 通过 stable sort 顺序保证 Tushare 在前)
logger.info("✅ 检测到 TUSHARE_TOKEN 且 API 初始化成功,Tushare 数据源优先级提升为最高 (Priority 0)")
return 0
# Token 未配置或 API 初始化失败,保持默认优先级
return 2
def is_available(self) -> bool:
"""
检查数据源是否可用
Returns:
True 表示可用,False 表示不可用
"""
return self._api is not None
def _check_rate_limit(self) -> None:
"""
检查并执行速率限制
流控策略:
1. 检查是否进入新的一分钟
2. 如果是,重置计数器
3. 如果当前分钟调用次数超过限制,强制休眠
"""
current_time = time.time()
# 检查是否需要重置计数器(新的一分钟)
if self._minute_start is None:
self._minute_start = current_time
self._call_count = 0
elif current_time - self._minute_start >= 60:
# 已经过了一分钟,重置计数器
self._minute_start = current_time
self._call_count = 0
logger.debug("速率限制计数器已重置")
# 检查是否超过配额
if self._call_count >= self.rate_limit_per_minute:
# 计算需要等待的时间(到下一分钟)
elapsed = current_time - self._minute_start
sleep_time = max(0, 60 - elapsed) + 1 # +1 秒缓冲
logger.warning(
f"Tushare 达到速率限制 ({self._call_count}/{self.rate_limit_per_minute} 次/分钟),"
f"等待 {sleep_time:.1f} 秒..."
)
time.sleep(sleep_time)
# 重置计数器
self._minute_start = time.time()
self._call_count = 0
# 增加调用计数
self._call_count += 1
logger.debug(f"Tushare 当前分钟调用次数: {self._call_count}/{self.rate_limit_per_minute}")
def _call_api_with_rate_limit(self, method_name: str, **kwargs) -> pd.DataFrame:
"""统一通过速率限制包装 Tushare API 调用。"""
if self._api is None:
raise DataFetchError("Tushare API 未初始化,请检查 Token 配置")
self._check_rate_limit()
method = getattr(self._api, method_name)
return method(**kwargs)
def _get_china_now(self) -> datetime:
"""返回上海时区当前时间,方便测试覆盖跨日刷新逻辑。"""
return datetime.now(ZoneInfo("Asia/Shanghai"))
def _get_trade_dates(self, end_date: Optional[str] = None) -> List[str]:
"""按自然日刷新交易日历缓存,避免服务跨日后继续复用旧日历。"""
if self._api is None:
return []
china_now = self._get_china_now()
requested_end_date = end_date or china_now.strftime("%Y%m%d")
if self.date_list is not None and self._date_list_end == requested_end_date:
return self.date_list
start_date = (china_now - timedelta(days=20)).strftime("%Y%m%d")
df_cal = self._call_api_with_rate_limit(
"trade_cal",
exchange="SSE",
start_date=start_date,
end_date=requested_end_date,
)
if df_cal is None or df_cal.empty or "cal_date" not in df_cal.columns:
logger.warning("[Tushare] trade_cal 返回为空,无法更新交易日历缓存")
self.date_list = []
self._date_list_end = requested_end_date
return self.date_list
trade_dates = sorted(
df_cal[df_cal["is_open"] == 1]["cal_date"].astype(str).tolist(),
reverse=True,
)
self.date_list = trade_dates
self._date_list_end = requested_end_date
return trade_dates
@staticmethod
def _pick_trade_date(trade_dates: List[str], use_today: bool) -> Optional[str]:
"""根据可用交易日列表选择当天或前一交易日。"""
if not trade_dates:
return None
if use_today or len(trade_dates) == 1:
return trade_dates[0]
return trade_dates[1]
@staticmethod
def _detect_exchange_hint(stock_code: str) -> Optional[str]:
"""Return SH/SZ/BJ when the raw user input carries an explicit exchange hint."""
upper = (stock_code or "").strip().upper()
if upper.startswith(("SH", "SS")) or upper.endswith((".SH", ".SS")):
return "SH"
if upper.startswith("SZ") or upper.endswith(".SZ"):
return "SZ"
if upper.startswith("BJ") or upper.endswith(".BJ"):
return "BJ"
return None
@classmethod
def _get_legacy_realtime_symbol(cls, stock_code: str) -> str:
"""Build the legacy tushare symbol while preserving explicit SH/SZ hints."""
code = normalize_stock_code(stock_code)
exchange_hint = cls._detect_exchange_hint(stock_code)
if code == '000001' and exchange_hint == 'SH':
return 'sh000001'
if code == '399001':
return 'sz399001'
if code == '399006':
return 'sz399006'
if code == '000300':
return 'sh000300'
if is_bse_code(code):
return f"bj{code}"
return code
def _convert_stock_code(self, stock_code: str) -> str:
"""
转换 A 股 / ETF / 北交所等为 Tushare ts_code(不含港股逻辑)。
Tushare 要求的格式示例:
- 沪市股票:600519.SH
- 深市股票:000001.SZ
- 沪市 ETF:510050.SH
- 深市 ETF:159919.SZ
Args:
stock_code: 原始代码,如 '600519', '000001', '563230'
Returns:
Tushare 格式代码,如 '600519.SH', '000001.SZ'
"""
raw_code = stock_code.strip()
# Already has suffix
if '.' in raw_code:
ts_code = raw_code.upper()
if ts_code.endswith('.SS'):
return f"{ts_code[:-3]}.SH"
return ts_code
if _is_us_code(raw_code):
raise DataFetchError(f"TushareFetcher 不支持美股 {raw_code},请使用 AkshareFetcher 或 YfinanceFetcher")
if _is_hk_market(raw_code):
#raise DataFetchError(f"TushareFetcher 不支持港股 {raw_code},请使用 AkshareFetcher")
return normalize_stock_code(raw_code)
code = normalize_stock_code(raw_code)
exchange_hint = self._detect_exchange_hint(raw_code)
if exchange_hint == "SH":
return f"{code}.SH"
if exchange_hint == "SZ":
return f"{code}.SZ"
if exchange_hint == "BJ":
return f"{code}.BJ"
# ETF: determine exchange by prefix
if code.startswith(_ETF_SH_PREFIXES) and len(code) == 6:
return f"{code}.SH"
if code.startswith(_ETF_SZ_PREFIXES) and len(code) == 6:
return f"{code}.SZ"
# BSE (Beijing Stock Exchange): 8xxxxx, 4xxxxx, 920xxx
if is_bse_code(code):
return f"{code}.BJ"
# Regular stocks
# Shanghai: 600xxx, 601xxx, 603xxx, 688xxx (STAR Market)
# Shenzhen: 000xxx, 002xxx, 300xxx (ChiNext)
if code.startswith(('600', '601', '603', '688')):
return f"{code}.SH"
elif code.startswith(('000', '002', '300')):
return f"{code}.SZ"
else:
logger.warning(f"无法确定股票 {code} 的市场,默认使用深市")
return f"{code}.SZ"
def _convert_hk_stock_code_for_tushare(self, stock_code: str) -> str:
"""
将用户输入转为 Tushare Pro 接口所需的 ts_code(含港股 nnnnn.HK)。
- 非港股:委托 _convert_stock_code(A 股 / ETF / 北交所等)。
- 港股:从 HK00700、00700、00700.HK 等形式归一为 5 位数字 + .HK。
"""
raw_code = stock_code.strip()
if _is_hk_market(raw_code):
if "." in raw_code:
ts_code = raw_code.upper()
if ts_code.endswith(".SS"):
return f"{ts_code[:-3]}.SH"
if ts_code.endswith(".HK"):
return ts_code
digits = re.sub(r"\D", "", raw_code)
if not digits:
raise DataFetchError(f"无法识别港股代码 {raw_code}")
code = digits[-5:].rjust(5, "0")
return f"{code}.HK"
return self._convert_stock_code(stock_code)
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=2, max=30),
retry=retry_if_exception_type((ConnectionError, TimeoutError)),
before_sleep=before_sleep_log(logger, logging.WARNING),
)
def _fetch_raw_data(self, stock_code: str, start_date: str, end_date: str) -> pd.DataFrame:
"""
从 Tushare 获取原始数据
根据代码类型选择不同接口:
- 普通股票:daily()
- ETF 基金:fund_daily()
流程:
1. 检查 API 是否可用
2. 检查是否为美股(不支持)
3. 执行速率限制检查
4. 转换股票代码格式
5. 根据代码类型选择接口并调用
"""
if self._api is None:
raise DataFetchError("Tushare API 未初始化,请检查 Token 配置")
# US stocks not supported
if _is_us_code(stock_code):
raise DataFetchError(f"TushareFetcher 不支持美股 {stock_code},请使用 AkshareFetcher 或 YfinanceFetcher")
# Rate-limit check
self._check_rate_limit()
is_hk = _is_hk_market(stock_code)
# 判断是否为 ETF / 港股,以选择不同接口
is_etf = _is_etf_code(stock_code)
if is_hk:
ts_code = self._convert_hk_stock_code_for_tushare(stock_code)
api_name = "hk_daily"
else:
ts_code = self._convert_stock_code(stock_code)
api_name = "fund_daily" if is_etf else "daily"
# Convert date format (Tushare requires YYYYMMDD)
ts_start = start_date.replace('-', '')
ts_end = end_date.replace('-', '')
logger.debug(f"调用 Tushare {api_name}({ts_code}, {ts_start}, {ts_end})")
try:
if is_hk:
# 港股使用 hk_daily 接口
df = self._api.hk_daily(
ts_code=ts_code,
start_date=ts_start,
end_date=ts_end,
)
elif is_etf:
# ETF uses fund_daily interface
df = self._api.fund_daily(
ts_code=ts_code,
start_date=ts_start,
end_date=ts_end,
)
else:
# Regular A-share stocks use daily interface
df = self._api.daily(
ts_code=ts_code,
start_date=ts_start,
end_date=ts_end,
)
return df
except Exception as e:
error_msg = str(e).lower()
# 检测配额超限
if any(keyword in error_msg for keyword in ['quota', '配额', 'limit', '权限']):
logger.warning(f"Tushare 配额可能超限: {e}")
raise RateLimitError(f"Tushare 配额超限: {e}") from e
raise DataFetchError(f"Tushare 获取数据失败: {e}") from e
def _normalize_data(self, df: pd.DataFrame, stock_code: str) -> pd.DataFrame:
"""
标准化 Tushare 数据
Tushare daily / fund_daily 返回的列名:
ts_code, trade_date, open, high, low, close, pre_close, change, pct_chg, vol, amount
需要映射到标准列名:
date, open, high, low, close, volume, amount, pct_chg
单位缩放仅适用于 A 股(及 ETF 等使用同一套单位的接口):
- vol 按「手」计,乘以 100 转为「股」
- amount 按「千元」计,乘以 1000 转为「元」
港股 hk_daily 返回的 vol / amount 已是可直接使用的量级,不做上述缩放。
"""
df = df.copy()
is_hk = _is_hk_market(stock_code)
# 列名映射
column_mapping = {
'trade_date': 'date',
'vol': 'volume',
# open, high, low, close, amount, pct_chg 列名相同
}
df = df.rename(columns=column_mapping)
# 转换日期格式(YYYYMMDD -> YYYY-MM-DD)
if 'date' in df.columns:
df['date'] = pd.to_datetime(df['date'], format='%Y%m%d')
# 成交量 / 成交额:仅 A 股类接口做单位换算(港股 hk_daily 不换算)
if 'volume' in df.columns and not is_hk:
df['volume'] = df['volume'] * 100
if 'amount' in df.columns and not is_hk:
df['amount'] = df['amount'] * 1000
# 添加股票代码列
df['code'] = stock_code
# 只保留需要的列
keep_cols = ['code'] + STANDARD_COLUMNS
existing_cols = [col for col in keep_cols if col in df.columns]
df = df[existing_cols]
return df
def get_stock_name(self, stock_code: str) -> Optional[str]:
"""
获取股票名称
使用 Tushare 的 stock_basic 接口获取股票基本信息
Args:
stock_code: 股票代码
Returns:
股票名称,失败返回 None
"""
if self._api is None:
logger.warning("Tushare API 未初始化,无法获取股票名称")
return None
# 检查缓存
if hasattr(self, '_stock_name_cache') and stock_code in self._stock_name_cache:
return self._stock_name_cache[stock_code]
# 初始化缓存
if not hasattr(self, '_stock_name_cache'):
self._stock_name_cache = {}
try:
# 速率限制检查
self._check_rate_limit()
# 根据市场/类型选择基础信息接口
if _is_hk_market(stock_code):
ts_code = self._convert_hk_stock_code_for_tushare(stock_code)
# 港股:使用 hk_basic
df = self._api.hk_basic(
ts_code=ts_code,
fields='ts_code,name'
)
elif _is_etf_code(stock_code):
ts_code = self._convert_stock_code(stock_code)
# ETF:使用 fund_basic
df = self._api.fund_basic(
ts_code=ts_code,
fields='ts_code,name'
)
else:
ts_code = self._convert_stock_code(stock_code)
# A 股股票:使用 stock_basic
df = self._api.stock_basic(
ts_code=ts_code,
fields='ts_code,name'
)
if df is not None and not df.empty:
name = df.iloc[0]['name']
self._stock_name_cache[stock_code] = name
logger.debug(f"Tushare 获取股票名称成功: {stock_code} -> {name}")
return name
except Exception as e:
logger.warning(f"Tushare 获取股票名称失败 {stock_code}: {e}")
return None
def get_stock_list(self) -> Optional[pd.DataFrame]:
"""
获取股票列表
使用 Tushare 的 stock_basic 接口获取 A 股列表(不含港股)。
Returns:
包含 code, name, industry, area, market 列的 DataFrame,失败返回 None
"""
if self._api is None:
logger.warning("Tushare API 未初始化,无法获取股票列表")
return None
try:
self._check_rate_limit()
df = self._api.stock_basic(
exchange='',
list_status='L',
fields='ts_code,name,industry,area,market'
)
if df is None or df.empty:
return None
df = df.copy()
df['code'] = df['ts_code'].astype(str).str.split('.').str[0]
if not hasattr(self, '_stock_name_cache'):
self._stock_name_cache = {}
for _, row in df.iterrows():
self._stock_name_cache[row['code']] = row['name']
logger.info(f"Tushare 获取股票列表成功: {len(df)} 条")
return df[['code', 'name', 'industry', 'area', 'market']]
except Exception as e:
logger.warning(f"Tushare 获取股票列表失败: {e}")
return None
def get_realtime_quote(self, stock_code: str) -> Optional[UnifiedRealtimeQuote]:
"""
获取实时行情
策略:
1. 优先尝试 Pro 接口(需要2000积分):数据全,稳定性高
2. 失败降级到旧版接口:门槛低,数据较少
Args:
stock_code: 股票代码
Returns:
UnifiedRealtimeQuote 对象,失败返回 None
"""
if self._api is None:
return None
# HK stocks not supported by Tushare
if _is_hk_market(stock_code):
logger.debug(f"TushareFetcher 跳过港股实时行情 {stock_code}")
return None
normalized_code = normalize_stock_code(stock_code)
from .realtime_types import (
RealtimeSource,
safe_float, safe_int
)
# 速率限制检查
self._check_rate_limit()
# 尝试 Pro 接口
try:
ts_code = self._convert_stock_code(stock_code)
# 尝试调用 Pro 实时接口 (需要积分)
df = self._api.quotation(ts_code=ts_code)
if df is not None and not df.empty:
row = df.iloc[0]
logger.debug(f"Tushare Pro 实时行情获取成功: {stock_code}")
return UnifiedRealtimeQuote(
code=normalized_code,
name=str(row.get('name', '')),
source=RealtimeSource.TUSHARE,
price=safe_float(row.get('price')),
change_pct=safe_float(row.get('pct_chg')), # Pro 接口通常直接返回涨跌幅
change_amount=safe_float(row.get('change')),
volume=safe_int(row.get('vol')),
amount=safe_float(row.get('amount')),
high=safe_float(row.get('high')),
low=safe_float(row.get('low')),
open_price=safe_float(row.get('open')),
pre_close=safe_float(row.get('pre_close')),
turnover_rate=safe_float(row.get('turnover_ratio')), # Pro 接口可能有换手率
pe_ratio=safe_float(row.get('pe')),
pb_ratio=safe_float(row.get('pb')),
total_mv=safe_float(row.get('total_mv')),
)
except Exception as e:
# 仅记录调试日志,不报错,继续尝试降级
logger.debug(f"Tushare Pro 实时行情不可用 (可能是积分不足): {e}")
# 降级:尝试旧版接口
try:
import tushare as ts
symbol = self._get_legacy_realtime_symbol(stock_code)
# 调用旧版实时接口 (ts.get_realtime_quotes)
df = ts.get_realtime_quotes(symbol)
if df is None or df.empty:
return None
row = df.iloc[0]
# 计算涨跌幅
price = safe_float(row['price'])
pre_close = safe_float(row['pre_close'])
change_pct = 0.0
change_amount = 0.0
if price and pre_close and pre_close > 0:
change_amount = price - pre_close
change_pct = (change_amount / pre_close) * 100
# 构建统一对象
return UnifiedRealtimeQuote(
code=normalized_code,
name=str(row['name']),
source=RealtimeSource.TUSHARE,
price=price,
change_pct=round(change_pct, 2),
change_amount=round(change_amount, 2),
volume=safe_int(row['volume']) // 100, # 转换为手
amount=safe_float(row['amount']),
high=safe_float(row['high']),
low=safe_float(row['low']),
open_price=safe_float(row['open']),
pre_close=pre_close,
)
except Exception as e:
logger.warning(f"Tushare (旧版) 获取实时行情失败 {stock_code}: {e}")
return None
def get_main_indices(self, region: str = "cn") -> Optional[List[dict]]:
"""
获取主要指数实时行情 (Tushare Pro),仅支持 A 股
"""
if region != "cn":
return None
if self._api is None:
return None
from .realtime_types import safe_float
# 指数映射:Tushare代码 -> 名称
indices_map = {
'000001.SH': '上证指数',
'399001.SZ': '深证成指',
'399006.SZ': '创业板指',
'000688.SH': '科创50',
'000016.SH': '上证50',
'000300.SH': '沪深300',
}
try:
self._check_rate_limit()
# Tushare index_daily 获取历史数据,实时数据需用其他接口或估算
# 由于 Tushare 免费用户可能无法获取指数实时行情,这里作为备选
# 使用 index_daily 获取最近交易日数据
end_date = datetime.now().strftime('%Y%m%d')
start_date = (datetime.now() - pd.Timedelta(days=5)).strftime('%Y%m%d')
results = []
# 批量获取所有指数数据
for ts_code, name in indices_map.items():
try:
df = self._api.index_daily(ts_code=ts_code, start_date=start_date, end_date=end_date)
if df is not None and not df.empty:
row = df.iloc[0] # 最新一天
current = safe_float(row['close'])
prev_close = safe_float(row['pre_close'])
results.append({
'code': ts_code.split('.')[0], # 兼容 sh000001 格式需转换,这里保持纯数字
'name': name,
'current': current,
'change': safe_float(row['change']),
'change_pct': safe_float(row['pct_chg']),
'open': safe_float(row['open']),
'high': safe_float(row['high']),
'low': safe_float(row['low']),
'prev_close': prev_close,
'volume': safe_float(row['vol']),
'amount': safe_float(row['amount']) * 1000, # 千元转元
'amplitude': 0.0 # Tushare index_daily 不直接返回振幅
})
except Exception as e:
logger.debug(f"Tushare 获取指数 {name} 失败: {e}")
continue
if results:
return results
else:
logger.warning("[Tushare] 未获取到指数行情数据")
except Exception as e:
logger.error(f"[Tushare] 获取指数行情失败: {e}")
return None
def get_market_stats(self) -> Optional[dict]:
"""
获取市场涨跌统计 (Tushare Pro)
2000积分 每天访问该接口 ts.pro_api().rt_k 两次
接口限制见:https://tushare.pro/document/1?doc_id=108
"""
if self._api is None:
return None
try:
logger.info("[Tushare] ts.pro_api() 获取市场统计...")
# 获取当前中国时间,判断是否在交易时间内
china_now = self._get_china_now()
current_clock = china_now.strftime("%H:%M")
current_date = china_now.strftime("%Y%m%d")
trade_dates = self._get_trade_dates(current_date)
if not trade_dates:
return None
if current_date in trade_dates:
if current_clock < '09:30' or current_clock > '16:30':
use_realtime = False
else:
use_realtime = True
else:
use_realtime = False
# 若实盘的时候使用 则使用其他可以实盘获取的数据源 akshare、efinance
if use_realtime:
try:
df = self._call_api_with_rate_limit("rt_k", ts_code='3*.SZ,6*.SH,0*.SZ,92*.BJ')
if df is not None and not df.empty:
return self._calc_market_stats(df)
except Exception as e:
logger.error(f"[Tushare] ts.pro_api().rt_k 尝试获取实时数据失败: {e}")
return None
else:
if current_date not in trade_dates:
last_date = self._pick_trade_date(trade_dates, use_today=True) # 拿最近的日期
else:
if current_clock < '09:30':
last_date = self._pick_trade_date(trade_dates, use_today=False) # 拿取前一天的数据
else: # 即 '> 16:30'
last_date = self._pick_trade_date(trade_dates, use_today=True) # 拿取当天的数据
if last_date is None:
return None
try:
df = self._call_api_with_rate_limit(
"daily",
ts_code='3*.SZ,6*.SH,0*.SZ,92*.BJ',
start_date=last_date,
end_date=last_date,
)
# 为防止不同接口返回的列名大小写不一致(例如 rt_k 返回小写,daily 返回大写),统一将列名转为小写
df.columns = [col.lower() for col in df.columns]
# 获取股票基础信息(包含代码和名称)
df_basic = self._call_api_with_rate_limit("stock_basic", fields='ts_code,name')
df = pd.merge(df, df_basic, on='ts_code', how='left')
# 将 daily的 amount 列的值乘以 1000 来和其他数据源保持一致
if 'amount' in df.columns:
df['amount'] = df['amount'] * 1000
if df is not None and not df.empty:
return self._calc_market_stats(df)
except Exception as e:
logger.error(f"[Tushare] ts.pro_api().daily 获取数据失败: {e}")
except Exception as e:
logger.error(f"[Tushare] 获取市场统计失败: {e}")
return None
def _calc_market_stats(
self,
df: pd.DataFrame,
) -> Optional[Dict[str, Any]]:
"""从行情 DataFrame 计算涨跌统计。"""
import numpy as np
df = df.copy()
# 1. 提取基础比对数据:最新价、昨收
# 兼容不同接口返回的列名 sina/em efinance tushare xtdata
code_col = next((c for c in ['代码', '股票代码', 'ts_code','stock_code'] if c in df.columns), None)
name_col = next((c for c in ['名称', '股票名称','name','name'] if c in df.columns), None)
close_col = next((c for c in ['最新价', '最新价', 'close','lastPrice'] if c in df.columns), None)
pre_close_col = next((c for c in ['昨收', '昨日收盘', 'pre_close','lastClose'] if c in df.columns), None)
amount_col = next((c for c in ['成交额', '成交额', 'amount','amount'] if c in df.columns), None)
limit_up_count = 0
limit_down_count = 0
up_count = 0
down_count = 0
flat_count = 0
for code, name, current_price, pre_close, amount in zip(
df[code_col], df[name_col], df[close_col], df[pre_close_col], df[amount_col]
):
# 停牌过滤 efinance 的停牌数据有时候会缺失价格显示为 '-',em 显示为none
if pd.isna(current_price) or pd.isna(pre_close) or current_price in ['-'] or pre_close in ['-'] or amount == 0:
continue
# em、efinance 为str 需要转换为float
current_price = float(current_price)
pre_close = float(pre_close)
# 获取去除前缀的纯数字代码
pure_code = normalize_stock_code(str(code))
# A. 确定每只股票的涨跌幅比例 (使用纯数字代码判断)
if is_bse_code(pure_code):
ratio = 0.30
elif is_kc_cy_stock(pure_code): #pure_code.startswith(('688', '30')):
ratio = 0.20
elif is_st_stock(name): #'ST' in str_name:
ratio = 0.05
else:
ratio = 0.10
# B. 严格按照 A 股规则计算涨跌停价:昨收 * (1 ± 比例) -> 四舍五入保留2位小数
limit_up_price = np.floor(pre_close * (1 + ratio) * 100 + 0.5) / 100.0
limit_down_price = np.floor(pre_close * (1 - ratio) * 100 + 0.5) / 100.0
limit_up_price_Tolerance = round(abs(pre_close * (1 + ratio) - limit_up_price), 10)
limit_down_price_Tolerance = round(abs(pre_close * (1 - ratio) - limit_down_price), 10)
# C. 精确比对
if current_price > 0 :
is_limit_up = (current_price > 0) and (abs(current_price - limit_up_price) <= limit_up_price_Tolerance)
is_limit_down = (current_price > 0) and (abs(current_price - limit_down_price) <= limit_down_price_Tolerance)
if is_limit_up:
limit_up_count += 1
if is_limit_down:
limit_down_count += 1
if current_price > pre_close:
up_count += 1
elif current_price < pre_close:
down_count += 1
else:
flat_count += 1
# 统计数量
stats = {
'up_count': up_count,
'down_count': down_count,