| | |
| | | return fallback_stock_pool() |
| | | |
| | | |
| | | def fetch_real_stock_data_from_akshare_only(n_stocks=500, days=365): |
| | | def fetch_real_stock_data_from_akshare_only( |
| | | n_stocks=500, |
| | | days=365, |
| | | max_consecutive_failures=10, |
| | | ): |
| | | """从AKShare获取真实股票历史数据""" |
| | | logger.info("="*60) |
| | | logger.info("从AKShare获取真实A股数据") |
| | |
| | | |
| | | stock_list = stock_list.head(n_stocks) |
| | | |
| | | for idx, row in stock_list.iterrows(): |
| | | consecutive_failures = 0 |
| | | |
| | | for position, (_, row) in enumerate(stock_list.iterrows(), start=1): |
| | | stock_code = row['代码'] |
| | | stock_name = row['名称'] |
| | | |
| | | try: |
| | | logger.info(f" [{idx+1}/{len(stock_list)}] 获取 {stock_name}({stock_code})...") |
| | | logger.info(f" [{position}/{len(stock_list)}] 获取 {stock_name}({stock_code})...") |
| | | |
| | | df = ak.stock_zh_a_hist( |
| | | symbol=stock_code, |
| | |
| | | df['date'] = pd.to_datetime(df['date']) |
| | | |
| | | df = calculate_technical_factors(df) |
| | | |
| | | |
| | | all_data.append(df) |
| | | consecutive_failures = 0 |
| | | logger.info(f" ✓ 获取 {len(df)} 条记录") |
| | | else: |
| | | logger.warning(f" ⚠️ 无数据") |
| | | |
| | | except Exception as e: |
| | | consecutive_failures += 1 |
| | | logger.warning(f" ⚠️ 获取失败: {e}") |
| | | if ( |
| | | max_consecutive_failures |
| | | and consecutive_failures >= max_consecutive_failures |
| | | ): |
| | | logger.error( |
| | | f"❌ AKShare历史行情连续失败 {consecutive_failures} 次," |
| | | "提前停止并切换备用数据源" |
| | | ) |
| | | if not all_data: |
| | | return None |
| | | break |
| | | continue |
| | | |
| | | if all_data: |