| | |
| | | import json |
| | | import argparse |
| | | import time |
| | | import socket |
| | | |
| | | os.makedirs('logs', exist_ok=True) |
| | | |
| | |
| | | def to_baostock_code(stock_code): |
| | | """将6位A股代码转换为BaoStock代码格式。""" |
| | | code = str(stock_code).strip().zfill(6) |
| | | if code.startswith(('6', '9')): |
| | | if code.startswith(('4', '8', '9')): |
| | | return f"bj.{code}" |
| | | if code.startswith('6'): |
| | | return f"sh.{code}" |
| | | if code.startswith(('0', '2', '3')): |
| | | return f"sz.{code}" |
| | | if code.startswith(('4', '8')): |
| | | return f"bj.{code}" |
| | | return None |
| | | |
| | | |
| | |
| | | max_retries=3, |
| | | retry_delay=1, |
| | | request_delay=0.1, |
| | | socket_timeout=30, |
| | | ): |
| | | """从BaoStock获取真实股票历史数据,作为东方财富/AKShare不可用时的备用源。""" |
| | | logger.info("="*60) |
| | |
| | | all_data = [] |
| | | |
| | | login_result = None |
| | | previous_socket_timeout = socket.getdefaulttimeout() |
| | | try: |
| | | if socket_timeout: |
| | | socket.setdefaulttimeout(socket_timeout) |
| | | logger.info(f"✓ BaoStock网络超时设置为 {socket_timeout} 秒") |
| | | |
| | | login_result = bs.login() |
| | | if getattr(login_result, 'error_code', '0') != '0': |
| | | logger.error(f"❌ BaoStock登录失败: {login_result.error_msg}") |
| | |
| | | bs.logout() |
| | | except Exception as e: |
| | | logger.warning(f"⚠️ BaoStock登出失败: {e}") |
| | | socket.setdefaulttimeout(previous_socket_timeout) |
| | | |
| | | |
| | | def calculate_technical_factors(df): |