Repository navigation
Expand file tree
/
Copy pathfinancial_data.py
More file actions
220 lines (183 loc) · 6.29 KB
/
Copy pathfinancial_data.py
File metadata and controls
220 lines (183 loc) · 6.29 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
"""
BaoStock数据缓存系统 - 财务数据模块
包含财务相关的数据获取和处理方法
"""
import logging
import baostock as bs
from .core import BaoStockDataCache
logger = logging.getLogger(__name__)
class FinancialDataMixin:
"""
财务数据获取和处理混入类
"""
def query_profit_data(self, code, year=None, quarter=None, use_cache=True):
"""
获取季频盈利能力数据
与BaoStock API完全兼容
"""
return self._query_financial_data(
'profit',
code=code,
year=year,
quarter=quarter,
use_cache=use_cache
)
def query_operation_data(self, code, year=None, quarter=None, use_cache=True):
"""
获取季频运营能力数据
与BaoStock API完全兼容
"""
return self._query_financial_data(
'operation',
code=code,
year=year,
quarter=quarter,
use_cache=use_cache
)
def query_growth_data(self, code, year=None, quarter=None, use_cache=True):
"""
获取季频成长能力数据
与BaoStock API完全兼容
"""
return self._query_financial_data(
'growth',
code=code,
year=year,
quarter=quarter,
use_cache=use_cache
)
def query_balance_data(self, code, year=None, quarter=None, use_cache=True):
"""
获取季频资产负债表
与BaoStock API完全兼容
"""
return self._query_financial_data(
'balance',
code=code,
year=year,
quarter=quarter,
use_cache=use_cache
)
def query_cash_flow_data(self, code, year=None, quarter=None, use_cache=True):
"""
获取季频现金流数据
与BaoStock API完全兼容
"""
return self._query_financial_data(
'cash_flow',
code=code,
year=year,
quarter=quarter,
use_cache=use_cache
)
def query_dividend_data(self, code, use_cache=True):
"""
获取分红配送数据
与BaoStock API完全兼容
"""
return self._query_financial_data(
'dividend',
code=code,
use_cache=use_cache
)
def query_dupont_data(self, code, year=None, quarter=None, use_cache=True):
"""
获取杜邦分析数据
与BaoStock API完全兼容
"""
return self._query_financial_data(
'dupont',
code=code,
year=year,
quarter=quarter,
use_cache=use_cache
)
def query_adjust_factor(self, code, start_date=None, end_date=None, use_cache=True):
"""
获取复权因子
与BaoStock API完全兼容
"""
cache_key = self._get_cache_key(
'adjust_factor',
code,
start_date,
end_date
)
if use_cache:
cached_data = self._get_from_cache(cache_key)
if cached_data is not None:
logger.info(f"从缓存获取复权因子: {code}")
return self._convert_to_baostock_result(cached_data)
logger.info(f"从BaoStock获取复权因子: {code}")
try:
rs = bs.query_adjust_factor(
code=code,
start_date=start_date,
end_date=end_date
)
with self.performance_metrics_lock:
self.performance_metrics['api_calls'] += 1
data_list = []
while (rs.error_code == '0') & rs.next():
data_list.append(rs.get_row_data())
if data_list:
import pandas as pd
df = pd.DataFrame(data_list, columns=rs.fields)
if use_cache:
self._set_to_memory_cache(cache_key, df)
return self._convert_to_baostock_result(df)
except Exception as e:
logger.error(f"从BaoStock获取复权因子失败: {e}")
return self._create_empty_result()
def _query_financial_data(self, data_type, code=None, year=None, quarter=None, use_cache=True):
"""
获取财务数据的内部方法
"""
api_mapping = {
'profit': 'query_profit_data',
'operation': 'query_operation_data',
'growth': 'query_growth_data',
'balance': 'query_balance_data',
'cash_flow': 'query_cash_flow_data',
'dividend': 'query_dividend_data',
'dupont': 'query_dupont_data'
}
api_name = api_mapping.get(data_type)
if not api_name:
logger.error(f"未知的财务数据类型: {data_type}")
return self._create_empty_result()
cache_key = f"financial_{data_type}_{code}_{year}_{quarter}"
if use_cache:
cached_data = self._get_from_cache(cache_key)
if cached_data is not None:
logger.info(f"从缓存获取{data_type}数据: {code}")
return self._convert_to_baostock_result(cached_data)
logger.info(f"从BaoStock获取{data_type}数据: {code}")
try:
if data_type == 'dividend':
rs = getattr(bs, api_name)(code=code)
else:
rs = getattr(bs, api_name)(
code=code,
year=year,
quarter=quarter
)
with self.performance_metrics_lock:
self.performance_metrics['api_calls'] += 1
data_list = []
while (rs.error_code == '0') & rs.next():
data_list.append(rs.get_row_data())
if data_list:
import pandas as pd
df = pd.DataFrame(data_list, columns=rs.fields)
if use_cache:
self._set_to_memory_cache(cache_key, df)
return self._convert_to_baostock_result(df)
except Exception as e:
logger.error(f"从BaoStock获取{data_type}数据失败: {e}")
return self._create_empty_result()
class BaoStockFinancialCache(BaoStockDataCache, FinancialDataMixin):
"""
BaoStock财务数据缓存系统
"""
pass