log
2026-07-09 c4fc32d73b36eb42beed034a375a82ecc992da06
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
#!/usr/bin/env python3
"""
数据验证模块
验证市场数据、个股数据的合理性,防止异常数据
"""
 
import re
from datetime import datetime, date
from typing import Dict, List, Optional, Tuple, Any
import logging
 
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
 
 
class DataValidator:
    """数据验证器"""
    
    INDEX_RANGES = {
        'sh': {'min': 2000, 'max': 8000, 'name': '上证指数'},
        'sz': {'min': 5000, 'max': 20000, 'name': '深证成指'},
        'cyb': {'min': 1500, 'max': 5000, 'name': '创业板指'},
        'hs300': {'min': 2500, 'max': 8000, 'name': '沪深300'}
    }
    
    MARKET_VOLUME_RANGE = {'min': 5000, 'max': 50000}
    
    STOCK_PRICE_RANGE = {'min': 0.1, 'max': 2000}
    
    PE_RANGE = {'min': -500, 'max': 5000}
    PB_RANGE = {'min': -10, 'max': 100}
    ROE_RANGE = {'min': -100, 'max': 200}
    MARKET_CAP_RANGE = {'min': 5, 'max': 50000}
    
    VOLATILITY_RANGE = {'min': 5, 'max': 150}
    VAR_RANGE = {'min': 0, 'max': 30}
    DRAWDOWN_RANGE = {'min': 0, 'max': 100}
    
    def __init__(self, strict: bool = True):
        """
        初始化
        
        Args:
            strict: 是否严格模式(严格模式下警告也会报错)
        """
        self.strict = strict
        self.errors = []
        self.warnings = []
    
    def validate_index_data(self, index_data: Dict) -> Tuple[bool, List[str]]:
        """
        验证指数数据
        
        Args:
            index_data: 指数数据字典
            
        Returns:
            (是否有效, 错误信息列表)
        """
        errors = []
        warnings = []
        
        for key, config in self.INDEX_RANGES.items():
            if key not in index_data:
                errors.append(f"缺少{config['name']}数据")
                continue
            
            data = index_data[key]
            price = data.get('price', 0)
            change_pct = data.get('change_pct', 0)
            
            if price == 0:
                errors.append(f"{config['name']}价格为0")
            elif not (config['min'] <= price <= config['max']):
                errors.append(
                    f"{config['name']}价格{price}超出合理范围[{config['min']}, {config['max']}]"
                )
            
            if abs(change_pct) > 20:
                warnings.append(
                    f"{config['name']}单日涨跌幅{change_pct}%异常"
                )
        
        self.errors.extend(errors)
        self.warnings.extend(warnings)
        
        is_valid = len(errors) == 0 and (not self.strict or len(warnings) == 0)
        return is_valid, errors + warnings
    
    def validate_market_sentiment(self, sentiment: Dict) -> Tuple[bool, List[str]]:
        """
        验证市场情绪数据
        
        Args:
            sentiment: 市场情绪数据
            
        Returns:
            (是否有效, 错误信息列表)
        """
        errors = []
        warnings = []
        
        up_count = sentiment.get('up_count', 0)
        down_count = sentiment.get('down_count', 0)
        total_volume = sentiment.get('total_volume', 0)
        north_flow = sentiment.get('north_flow', 0)
        
        if up_count < 0 or up_count > 5000:
            errors.append(f"涨停数量{up_count}不合理")
        
        if down_count < 0 or down_count > 5000:
            errors.append(f"跌停数量{down_count}不合理")
        
        if total_volume > 0:
            if not (self.MARKET_VOLUME_RANGE['min'] <= total_volume <= self.MARKET_VOLUME_RANGE['max']):
                errors.append(
                    f"成交量{total_volume}亿超出合理范围"
                )
        
        if abs(north_flow) > 500:
            warnings.append(f"北向资金{north_flow}亿数值较大,请核实")
        
        self.errors.extend(errors)
        self.warnings.extend(warnings)
        
        is_valid = len(errors) == 0 and (not self.strict or len(warnings) == 0)
        return is_valid, errors + warnings
    
    def validate_stock_data(self, stock_data: Dict) -> Tuple[bool, List[str]]:
        """
        验证个股数据
        
        Args:
            stock_data: 个股数据
            
        Returns:
            (是否有效, 错误信息列表)
        """
        errors = []
        warnings = []
        
        code = stock_data.get('code', '')
        name = stock_data.get('name', '')
        
        if not code:
            errors.append("股票代码为空")
        
        price = stock_data.get('price', 0)
        if price > 0:
            if not (self.STOCK_PRICE_RANGE['min'] <= price <= self.STOCK_PRICE_RANGE['max']):
                errors.append(f"{name}({code})价格{price}超出合理范围")
        else:
            errors.append(f"{name}({code})价格无效: {price}")
        
        change_pct = stock_data.get('change_pct', 0)
        if abs(change_pct) > 20:
            warnings.append(f"{name}({code})涨跌幅{change_pct}%异常")
        
        pe = stock_data.get('pe_ttm')
        if pe is not None:
            if not (self.PE_RANGE['min'] <= pe <= self.PE_RANGE['max']):
                warnings.append(f"{name}({code})PE({pe})超出常规范围")
        
        pb = stock_data.get('pb')
        if pb is not None:
            if not (self.PB_RANGE['min'] <= pb <= self.PB_RANGE['max']):
                warnings.append(f"{name}({code})PB({pb})超出常规范围")
        
        roe = stock_data.get('roe')
        if roe is not None:
            if not (self.ROE_RANGE['min'] <= roe <= self.ROE_RANGE['max']):
                warnings.append(f"{name}({code})ROE({roe}%)超出常规范围")
        
        market_cap = stock_data.get('market_cap')
        if market_cap is not None and market_cap > 0:
            if not (self.MARKET_CAP_RANGE['min'] <= market_cap <= self.MARKET_CAP_RANGE['max']):
                errors.append(
                    f"{name}({code})市值{market_cap}亿超出合理范围"
                )
        
        self.errors.extend(errors)
        self.warnings.extend(warnings)
        
        is_valid = len(errors) == 0 and (not self.strict or len(warnings) == 0)
        return is_valid, errors + warnings
    
    def validate_risk_metrics(self, risk_data: Dict) -> Tuple[bool, List[str]]:
        """
        验证风险指标
        
        Args:
            risk_data: 风险指标数据
            
        Returns:
            (是否有效, 错误信息列表)
        """
        errors = []
        warnings = []
        
        volatility = risk_data.get('volatility', 0)
        if volatility > 0:
            if not (self.VOLATILITY_RANGE['min'] <= volatility <= self.VOLATILITY_RANGE['max']):
                errors.append(f"波动率{volatility}%超出合理范围")
        
        var_95 = risk_data.get('var_95', 0)
        if var_95 > 0:
            if not (self.VAR_RANGE['min'] <= var_95 <= self.VAR_RANGE['max']):
                warnings.append(f"VaR(95%){var_95}%超出常规范围")
        
        max_dd = risk_data.get('max_drawdown', 0)
        if max_dd > 0:
            if not (self.DRAWDOWN_RANGE['min'] <= max_dd <= self.DRAWDOWN_RANGE['max']):
                errors.append(f"最大回撤{max_dd}%超出合理范围")
        
        beta = risk_data.get('beta')
        if beta is not None:
            if not (-2 <= beta <= 3):
                warnings.append(f"Beta值{beta}超出常规范围[-2, 3]")
        
        sharpe = risk_data.get('sharpe_ratio')
        if sharpe is not None:
            if sharpe < -5 or sharpe > 10:
                warnings.append(f"夏普比率{sharpe}超出常规范围")
        
        self.errors.extend(errors)
        self.warnings.extend(warnings)
        
        is_valid = len(errors) == 0 and (not self.strict or len(warnings) == 0)
        return is_valid, errors + warnings
    
    def validate_industry_weights(self, weights: Dict[str, float]) -> Tuple[bool, List[str]]:
        """
        验证行业权重
        
        Args:
            weights: 行业权重字典
            
        Returns:
            (是否有效, 错误信息列表)
        """
        errors = []
        warnings = []
        
        if not weights:
            errors.append("行业权重数据为空")
            return False, errors
        
        total_weight = sum(weights.values())
        
        if abs(total_weight - 100) > 5:
            errors.append(f"行业权重总和{total_weight:.2f}%不等于100%")
        
        for industry, weight in weights.items():
            if weight < 0:
                errors.append(f"行业{industry}权重为负: {weight}%")
            elif weight > 60:
                warnings.append(f"行业{industry}权重过高: {weight}%")
        
        self.errors.extend(errors)
        self.warnings.extend(warnings)
        
        is_valid = len(errors) == 0 and (not self.strict or len(warnings) == 0)
        return is_valid, errors + warnings
    
    def validate_all(self, data: Dict) -> Tuple[bool, Dict[str, List[str]]]:
        """
        验证所有数据
        
        Args:
            data: 包含所有数据的字典
            
        Returns:
            (是否全部有效, {数据类型: 错误信息列表})
        """
        all_errors = {}
        
        if 'index' in data:
            _, errors = self.validate_index_data(data['index'])
            if errors:
                all_errors['index'] = errors
        
        if 'sentiment' in data:
            _, errors = self.validate_market_sentiment(data['sentiment'])
            if errors:
                all_errors['sentiment'] = errors
        
        if 'stocks' in data:
            stock_errors = []
            for stock in data['stocks']:
                _, errors = self.validate_stock_data(stock)
                stock_errors.extend(errors)
            if stock_errors:
                all_errors['stocks'] = stock_errors
        
        if 'risk' in data:
            _, errors = self.validate_risk_metrics(data['risk'])
            if errors:
                all_errors['risk'] = errors
        
        if 'industry_weights' in data:
            _, errors = self.validate_industry_weights(data['industry_weights'])
            if errors:
                all_errors['industry_weights'] = errors
        
        is_valid = len(all_errors) == 0
        return is_valid, all_errors
    
    def get_validation_report(self) -> str:
        """获取验证报告"""
        lines = ["=" * 50, "数据验证报告", "=" * 50]
        
        if self.errors:
            lines.append("\n【错误】")
            for i, err in enumerate(self.errors, 1):
                lines.append(f"  {i}. {err}")
        
        if self.warnings:
            lines.append("\n【警告】")
            for i, warn in enumerate(self.warnings, 1):
                lines.append(f"  {i}. {warn}")
        
        if not self.errors and not self.warnings:
            lines.append("\n✓ 所有数据验证通过")
        
        return "\n".join(lines)
    
    def clear(self):
        """清空错误和警告"""
        self.errors = []
        self.warnings = []
 
 
class StockDataSanitizer:
    """股票数据清洗器"""
    
    @staticmethod
    def sanitize_pe(pe: Optional[float]) -> Optional[float]:
        """清洗PE数据"""
        if pe is None:
            return None
        if pe < -500 or pe > 5000:
            return None
        return round(pe, 2)
    
    @staticmethod
    def sanitize_pb(pb: Optional[float]) -> Optional[float]:
        """清洗PB数据"""
        if pb is None:
            return None
        if pb < -10 or pb > 100:
            return None
        return round(pb, 2)
    
    @staticmethod
    def sanitize_roe(roe: Optional[float]) -> Optional[float]:
        """清洗ROE数据"""
        if roe is None:
            return None
        if roe < -100 or roe > 200:
            return None
        return round(roe, 2)
    
    @staticmethod
    def sanitize_market_cap(market_cap: Optional[float]) -> Optional[float]:
        """清洗市值数据"""
        if market_cap is None:
            return None
        if market_cap < 0:
            return None
        return round(market_cap, 2)
    
    @staticmethod
    def sanitize_stock_data(data: Dict) -> Dict:
        """清洗股票数据"""
        sanitized = data.copy()
        
        sanitized['pe_ttm'] = StockDataSanitizer.sanitize_pe(data.get('pe_ttm'))
        sanitized['pb'] = StockDataSanitizer.sanitize_pb(data.get('pb'))
        sanitized['roe'] = StockDataSanitizer.sanitize_roe(data.get('roe'))
        sanitized['roa'] = StockDataSanitizer.sanitize_roe(data.get('roa'))
        sanitized['market_cap'] = StockDataSanitizer.sanitize_market_cap(data.get('market_cap'))
        
        if 'price' in data and data['price'] > 0:
            sanitized['price'] = round(data['price'], 2)
        
        if 'change_pct' in data:
            sanitized['change_pct'] = round(data['change_pct'], 2)
        
        return sanitized
 
 
if __name__ == "__main__":
    validator = DataValidator(strict=False)
    sanitizer = StockDataSanitizer()
    
    print("=" * 60)
    print("数据验证模块测试")
    print("=" * 60)
    
    test_index = {
        'sh': {'name': '上证指数', 'price': 4182.59, 'change_pct': 0.47},
        'sz': {'name': '深证成指', 'price': 14465.79, 'change_pct': -0.20},
        'cyb': {'name': '创业板指', 'price': 3294.16, 'change_pct': -0.49},
        'hs300': {'name': '沪深300', 'price': 3950.23, 'change_pct': 0.15}
    }
    
    print("\n【指数数据验证】")
    is_valid, errors = validator.validate_index_data(test_index)
    print(f"  有效: {is_valid}")
    if errors:
        print(f"  问题: {errors}")
    
    test_stock = {
        'code': 'sh600000',
        'name': '浦发银行',
        'price': 9.68,
        'change_pct': -0.41,
        'pe_ttm': 5.23,
        'pb': 0.45,
        'roe': 10.5,
        'market_cap': 3224.5
    }
    
    print("\n【个股数据验证】")
    is_valid, errors = validator.validate_stock_data(test_stock)
    print(f"  有效: {is_valid}")
    if errors:
        print(f"  问题: {errors}")
    
    print("\n【验证报告】")
    print(validator.get_validation_report())