| import pandas as pd |
| import requests |
| import json |
| from datetime import datetime, timedelta |
| import os |
| import time |
| import random |
|
|
|
|
| def get_stock_market(stock_code): |
| """ |
| 根据股票代码判断市场类型 |
| 返回: 市场前缀 '0'-深交所, '1'-上交所 |
| """ |
| if stock_code.startswith(('0', '2', '3')): |
| return '0' |
| elif stock_code.startswith(('6', '9')): |
| return '1' |
| else: |
| return '1' |
|
|
|
|
| def get_stock_data_eastmoney_all_history(stock_code="002354"): |
| """ |
| 使用东方财富网API获取股票所有历史数据 |
| """ |
| try: |
| print(f"正在从东方财富网获取股票 {stock_code} 的全部历史数据...") |
|
|
| |
| market = get_stock_market(stock_code) |
| secid = f"{market}.{stock_code}" |
|
|
| |
| url = "http://push2his.eastmoney.com/api/qt/stock/kline/get" |
|
|
| |
| start_date = "19900101" |
| end_date = datetime.now().strftime('%Y%m%d') |
|
|
| params = { |
| 'secid': secid, |
| 'fields1': 'f1,f2,f3,f4,f5,f6', |
| 'fields2': 'f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61', |
| 'klt': '101', |
| 'fqt': '1', |
| 'beg': start_date, |
| 'end': end_date, |
| 'lmt': '50000', |
| 'ut': 'fa5fd1943c7b386f172d6893dbfba10b', |
| 'cb': f'jQuery{random.randint(1000000, 9999999)}_{int(time.time() * 1000)}' |
| } |
|
|
| headers = { |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36', |
| 'Referer': 'https://quote.eastmoney.com/', |
| 'Accept': '*/*', |
| } |
|
|
| time.sleep(random.uniform(1, 2)) |
|
|
| response = requests.get(url, params=params, headers=headers, timeout=15) |
|
|
| print(f"API响应状态码: {response.status_code}") |
|
|
| if response.status_code == 200: |
| |
| response_text = response.text |
|
|
| |
| if response_text.startswith('/**/'): |
| response_text = response_text[4:] |
|
|
| |
| start_idx = response_text.find('(') |
| end_idx = response_text.rfind(')') |
|
|
| if start_idx != -1 and end_idx != -1: |
| json_str = response_text[start_idx + 1:end_idx] |
| try: |
| data = json.loads(json_str) |
| except json.JSONDecodeError: |
| print("❌ JSON解析失败,尝试直接解析...") |
| return parse_kline_data_directly_all_history(response_text, stock_code) |
| else: |
| print("❌ 无法找到JSON数据边界") |
| return None |
|
|
| print(f"API返回数据状态: {data.get('rc', 'N/A')}") |
|
|
| if data and data.get('data') is not None: |
| klines = data['data'].get('klines', []) |
| print(f"获取到 {len(klines)} 条历史K线数据") |
|
|
| if not klines: |
| print("⚠️ K线数据为空") |
| return None |
|
|
| |
| stock_data = [] |
| for kline in klines: |
| try: |
| items = kline.split(',') |
| if len(items) >= 6: |
| stock_data.append({ |
| '日期': items[0], |
| '股票代码': stock_code, |
| '开盘价': float(items[1]), |
| '收盘价': float(items[2]), |
| '最高价': float(items[3]), |
| '最低价': float(items[4]), |
| '成交量': float(items[5]), |
| '成交额': float(items[6]) if len(items) > 6 else 0, |
| '振幅': float(items[7]) if len(items) > 7 else 0, |
| '涨跌幅': float(items[8]) if len(items) > 8 else 0, |
| '涨跌额': float(items[9]) if len(items) > 9 else 0, |
| '换手率': float(items[10]) if len(items) > 10 else 0 |
| }) |
| except (ValueError, IndexError) as e: |
| continue |
|
|
| if not stock_data: |
| print("❌ 解析后无有效数据") |
| return None |
|
|
| df = pd.DataFrame(stock_data) |
| df['日期'] = pd.to_datetime(df['日期']) |
| df.set_index('日期', inplace=True) |
| df = df.sort_index() |
|
|
| print(f"✅ 成功获取 {len(df)} 条历史数据") |
| print( |
| f"历史数据时间范围: {df.index.min().strftime('%Y-%m-%d')} 到 {df.index.max().strftime('%Y-%m-%d')}") |
| return df |
| else: |
| print("❌ API返回数据为空") |
| return None |
| else: |
| print(f"❌ 请求失败,状态码: {response.status_code}") |
| return None |
|
|
| except Exception as e: |
| print(f"❌ 获取历史数据时出错: {str(e)}") |
| return None |
|
|
|
|
| def parse_kline_data_directly_all_history(response_text, stock_code): |
| """ |
| 直接解析K线数据(当JSON解析失败时使用)- 全历史版本 |
| """ |
| try: |
| |
| if '"klines":[' in response_text: |
| start_idx = response_text.find('"klines":[') + 10 |
| end_idx = response_text.find(']', start_idx) |
| klines_str = response_text[start_idx:end_idx] |
|
|
| |
| klines = [k.strip().strip('"') for k in klines_str.split('","') if k.strip()] |
|
|
| stock_data = [] |
| for kline in klines: |
| if kline.strip(): |
| items = kline.split(',') |
| if len(items) >= 6: |
| stock_data.append({ |
| '日期': items[0], |
| '股票代码': stock_code, |
| '开盘价': float(items[1]), |
| '收盘价': float(items[2]), |
| '最高价': float(items[3]), |
| '最低价': float(items[4]), |
| '成交量': float(items[5]), |
| '成交额': float(items[6]) if len(items) > 6 else 0, |
| }) |
|
|
| if stock_data: |
| df = pd.DataFrame(stock_data) |
| df['日期'] = pd.to_datetime(df['日期']) |
| df.set_index('日期', inplace=True) |
| df = df.sort_index() |
| print(f"✅ 直接解析获取 {len(df)} 条历史数据") |
| return df |
| except Exception as e: |
| print(f"❌ 直接解析也失败: {e}") |
|
|
| return None |
|
|
|
|
| def get_stock_data_akshare_all_history(stock_code="002354"): |
| """ |
| 使用AKShare作为备用数据源 - 全历史版本 |
| """ |
| try: |
| print(f"尝试使用AKShare获取股票 {stock_code} 全部历史数据...") |
| import akshare as ak |
|
|
| |
| df = ak.stock_zh_a_hist(symbol=stock_code, period="daily", |
| adjust="qfq") |
|
|
| if df is not None and not df.empty: |
| |
| column_mapping = { |
| '日期': '日期', |
| '开盘': '开盘价', |
| '收盘': '收盘价', |
| '最高': '最高价', |
| '最低': '最低价', |
| '成交量': '成交量', |
| '成交额': '成交额', |
| '振幅': '振幅', |
| '涨跌幅': '涨跌幅', |
| '涨跌额': '涨跌额', |
| '换手率': '换手率' |
| } |
|
|
| |
| actual_mapping = {k: v for k, v in column_mapping.items() if k in df.columns} |
| df = df.rename(columns=actual_mapping) |
|
|
| |
| df['股票代码'] = stock_code |
| df['日期'] = pd.to_datetime(df['日期']) |
| df.set_index('日期', inplace=True) |
| df = df.sort_index() |
|
|
| print(f"✅ AKShare成功获取 {len(df)} 条历史数据") |
| print(f"时间范围: {df.index.min().strftime('%Y-%m-%d')} 到 {df.index.max().strftime('%Y-%m-%d')}") |
| return df |
| else: |
| print("❌ AKShare未返回数据") |
| return None |
|
|
| except ImportError: |
| print("⚠️ AKShare未安装,使用 pip install akshare 安装") |
| return None |
| except Exception as e: |
| print(f"❌ AKShare获取历史数据失败: {e}") |
| return None |
|
|
|
|
| def get_stock_data_baostock_all_history(stock_code="002354"): |
| """ |
| 使用Baostock作为第三个数据源 - 全历史版本 |
| """ |
| try: |
| print(f"尝试使用Baostock获取股票 {stock_code} 全部历史数据...") |
| import baostock as bs |
| import pandas as pd |
|
|
| |
| lg = bs.login() |
|
|
| |
| market = get_stock_market(stock_code) |
| if market == '0': |
| full_code = f"sz.{stock_code}" |
| else: |
| full_code = f"sh.{stock_code}" |
|
|
| |
| rs = bs.query_stock_basic(code=full_code) |
| if rs.error_code != '0': |
| print(f"❌ 获取股票基本信息失败: {rs.error_msg}") |
| bs.logout() |
| return None |
|
|
| |
| list_date = None |
| while (rs.error_code == '0') & rs.next(): |
| list_date = rs.get_row_data()[2] |
|
|
| if not list_date: |
| print("❌ 无法获取上市日期") |
| bs.logout() |
| return None |
|
|
| print(f"股票上市日期: {list_date}") |
|
|
| |
| end_date = datetime.now().strftime('%Y-%m-%d') |
|
|
| |
| rs = bs.query_history_k_data_plus( |
| full_code, |
| "date,open,high,low,close,volume,amount,turn,pctChg", |
| start_date=list_date, |
| end_date=end_date, |
| frequency="d", |
| adjustflag="2" |
| ) |
|
|
| data_list = [] |
| while (rs.error_code == '0') & rs.next(): |
| data_list.append(rs.get_row_data()) |
|
|
| |
| bs.logout() |
|
|
| if data_list: |
| df = pd.DataFrame(data_list, columns=rs.fields) |
|
|
| |
| df['date'] = pd.to_datetime(df['date']) |
| df['open'] = pd.to_numeric(df['open'], errors='coerce') |
| df['high'] = pd.to_numeric(df['high'], errors='coerce') |
| df['low'] = pd.to_numeric(df['low'], errors='coerce') |
| df['close'] = pd.to_numeric(df['close'], errors='coerce') |
| df['volume'] = pd.to_numeric(df['volume'], errors='coerce') |
| df['amount'] = pd.to_numeric(df['amount'], errors='coerce') |
| df['turn'] = pd.to_numeric(df['turn'], errors='coerce') |
| df['pctChg'] = pd.to_numeric(df['pctChg'], errors='coerce') |
|
|
| |
| df = df.rename(columns={ |
| 'date': '日期', |
| 'open': '开盘价', |
| 'high': '最高价', |
| 'low': '最低价', |
| 'close': '收盘价', |
| 'volume': '成交量', |
| 'amount': '成交额', |
| 'turn': '换手率', |
| 'pctChg': '涨跌幅' |
| }) |
|
|
| |
| df['股票代码'] = stock_code |
| df.set_index('日期', inplace=True) |
| df = df.sort_index() |
|
|
| |
| df['涨跌额'] = df['收盘价'].diff() |
|
|
| |
| df = df.dropna() |
|
|
| print(f"✅ Baostock成功获取 {len(df)} 条历史数据") |
| print(f"时间范围: {df.index.min().strftime('%Y-%m-%d')} 到 {df.index.max().strftime('%Y-%m-%d')}") |
| return df |
| else: |
| print("❌ Baostock未返回数据") |
| return None |
|
|
| except ImportError: |
| print("⚠️ Baostock未安装,使用 pip install baostock 安装") |
| return None |
| except Exception as e: |
| print(f"❌ Baostock获取历史数据失败: {e}") |
| return None |
|
|
|
|
| def get_stock_data_with_retry_all_history(stock_code="002354", retry_count=2): |
| """ |
| 带重试机制的数据获取 - 多数据源全历史版本 |
| """ |
| data_sources = [ |
| ("AKShare", get_stock_data_akshare_all_history), |
| ("Baostock", get_stock_data_baostock_all_history), |
| ("东方财富", get_stock_data_eastmoney_all_history) |
| ] |
|
|
| for source_name, data_func in data_sources: |
| print(f"\n🔍 尝试从 {source_name} 获取全部历史数据...") |
| data = data_func(stock_code) |
|
|
| if data is not None and not data.empty: |
| print(f"✅ {source_name} 历史数据获取成功!") |
| |
| data.attrs['data_source'] = source_name |
| return data |
|
|
| print("❌ 所有真实数据源都失败,使用示例数据...") |
| return create_sample_data_all_history(stock_code) |
|
|
|
|
| def create_sample_data_all_history(stock_code="002354"): |
| """ |
| 创建更真实的历史示例数据 - 从上市年份开始 |
| """ |
| |
| list_years = { |
| '600580': 2002, |
| '002354': 2010, |
| '300418': 2015, |
| '300207': 2011, |
| } |
|
|
| list_year = list_years.get(stock_code, 2010) |
| current_year = datetime.now().year |
|
|
| print(f"📊 创建 {stock_code} 从 {list_year} 年上市至今的示例数据...") |
|
|
| |
| start_date = datetime(list_year, 1, 1) |
| end_date = datetime.now() |
| all_dates = pd.bdate_range(start=start_date, end=end_date, freq='B') |
|
|
| |
| import numpy as np |
| np.random.seed(42) |
|
|
| |
| base_prices = { |
| '600580': 8.0, |
| '002354': 15.0, |
| '300418': 20.0, |
| '300207': 12.0, |
| } |
| base_price = base_prices.get(stock_code, 10.0) |
|
|
| stock_data = [] |
| current_price = base_price |
|
|
| for i, date in enumerate(all_dates): |
| |
| volatility = 0.02 |
|
|
| if i > 0: |
| |
| daily_return = np.random.normal(0, volatility) |
|
|
| |
| year = date.year |
| if year <= list_year + 2: |
| daily_return += np.random.normal(0.001, 0.01) |
| elif year <= list_year + 5: |
| daily_return += np.random.normal(0.0005, 0.005) |
| else: |
| daily_return += np.random.normal(0.0002, 0.003) |
|
|
| current_price = current_price * (1 + daily_return) |
|
|
| |
| current_price = max(base_price * 0.3, min(base_price * 10.0, current_price)) |
| else: |
| current_price = base_price |
|
|
| |
| open_variation = np.random.normal(0, volatility * 0.2) |
| open_price = current_price * (1 + open_variation) |
|
|
| daily_range = abs(np.random.normal(volatility * 0.8, volatility * 0.3)) |
| high_price = max(open_price, current_price) * (1 + daily_range) |
| low_price = min(open_price, current_price) * (1 - daily_range) |
| close_price = current_price |
|
|
| |
| high_price = max(open_price, close_price, low_price, high_price) |
| low_price = min(open_price, close_price, high_price, low_price) |
|
|
| |
| base_volume = 100000 + (year - list_year) * 50000 |
| volume_variation = abs(daily_return) * 5000000 if i > 0 else 0 |
| volume = int(base_volume + volume_variation + np.random.randint(-200000, 400000)) |
| volume = max(50000, volume) |
|
|
| |
| amount = volume * close_price / 10000 |
|
|
| |
| if i > 0: |
| prev_close = stock_data[-1]['收盘价'] |
| price_change = close_price - prev_close |
| pct_change = (price_change / prev_close) * 100 |
| else: |
| price_change = 0 |
| pct_change = 0 |
|
|
| |
| amplitude = ((high_price - low_price) / open_price) * 100 |
|
|
| |
| turnover_rate = np.random.uniform(1.0, 15.0) |
|
|
| stock_data.append({ |
| '日期': date, |
| '股票代码': stock_code, |
| '开盘价': round(open_price, 2), |
| '收盘价': round(close_price, 2), |
| '最高价': round(high_price, 2), |
| '最低价': round(low_price, 2), |
| '成交量': volume, |
| '成交额': round(amount, 2), |
| '振幅': round(amplitude, 2), |
| '涨跌幅': round(pct_change, 2), |
| '涨跌额': round(price_change, 2), |
| '换手率': round(turnover_rate, 2) |
| }) |
|
|
| df = pd.DataFrame(stock_data) |
| df.set_index('日期', inplace=True) |
|
|
| print(f"✅ 已创建 {len(df)} 条从 {list_year} 年至今的模拟历史数据") |
| print(f"时间范围: {df.index.min().strftime('%Y-%m-%d')} 到 {df.index.max().strftime('%Y-%m-%d')}") |
|
|
| |
| df.attrs['data_source'] = '模拟历史数据' |
|
|
| return df |
|
|
|
|
| def display_all_history_data_info(df, stock_code): |
| """显示全历史数据信息""" |
| if df is None or df.empty: |
| print("没有数据可显示") |
| return |
|
|
| |
| data_source = df.attrs.get('data_source', '未知来源') |
|
|
| print(f"\n{'=' * 60}") |
| print(f"股票 {stock_code} 全部历史数据摘要") |
| print(f"{'=' * 60}") |
|
|
| print(f"数据时间范围: {df.index.min().strftime('%Y-%m-%d')} 到 {df.index.max().strftime('%Y-%m-%d')}") |
| print(f"总交易天数: {len(df):,}") |
| print(f"数据来源: {data_source}") |
|
|
| |
| years = sorted(df.index.year.unique()) |
| print(f"\n历史年份: {years}") |
|
|
| |
| key_years = [years[0]] |
| if len(years) > 1: |
| key_years.append(years[-1]) |
| if len(years) > 5: |
| key_years.extend([years[len(years) // 2], years[len(years) // 4], years[3 * len(years) // 4]]) |
|
|
| for year in sorted(set(key_years)): |
| year_data = df[df.index.year == year] |
| if len(year_data) > 0: |
| print(f"\n{year}年统计:") |
| print(f" 交易天数: {len(year_data)}") |
| print(f" 平均收盘价: {year_data['收盘价'].mean():.2f} 元") |
| print(f" 最高价: {year_data['最高价'].max():.2f} 元") |
| print(f" 最低价: {year_data['最低价'].min():.2f} 元") |
| if len(year_data) > 1: |
| year_return = (year_data['收盘价'].iloc[-1] / year_data['收盘价'].iloc[0] - 1) * 100 |
| print(f" 年度涨跌幅: {year_return:+.2f}%") |
|
|
| |
| print(f"\n整体统计:") |
| total_return = (df['收盘价'].iloc[-1] / df['收盘价'].iloc[0] - 1) * 100 |
| print(f" 总涨跌幅: {total_return:+.2f}%") |
| print(f" 历史最高价: {df['最高价'].max():.2f} 元") |
| print(f" 历史最低价: {df['最低价'].min():.2f} 元") |
| print(f" 平均日成交量: {df['成交量'].mean():,.0f} 股") |
|
|
| |
| latest_date = df.index.max() |
| print(f"\n最新交易日 ({latest_date.strftime('%Y-%m-%d')}) 数据:") |
| latest_data = df.loc[latest_date] |
| for col, value in latest_data.items(): |
| if col != '股票代码': |
| if col in ['成交量']: |
| print(f" {col}: {value:,.0f}") |
| elif col in ['成交额']: |
| print(f" {col}: {value:,.2f} 万元") |
| else: |
| print(f" {col}: {value}") |
|
|
|
|
| def save_all_history_stock_data(df, stock_code, save_dir="D:/lianghuajiaoyi/Kronos/examples/data"): |
| """ |
| 保存全历史股票数据到指定目录 |
| """ |
| if df is not None and not df.empty: |
| |
| os.makedirs(save_dir, exist_ok=True) |
|
|
| |
| csv_file = os.path.join(save_dir, f"{stock_code}_all_history.csv") |
|
|
| |
| df_reset = df.reset_index() |
| df_reset.to_csv(csv_file, encoding='utf-8-sig', index=False) |
|
|
| print(f"\n📁 全历史股票数据已保存: {csv_file}") |
|
|
| |
| years = df_reset['日期'].dt.year.unique() |
| for year in years: |
| year_data = df_reset[df_reset['日期'].dt.year == year] |
| year_file = os.path.join(save_dir, f"{stock_code}_{year}.csv") |
| year_data.to_csv(year_file, encoding='utf-8-sig', index=False) |
|
|
| print(f"📁 同时保存了 {len(years)} 个年份的单独数据文件") |
| return True |
| return False |
|
|
|
|
| def main_all_history(stock_code="002354"): |
| """ |
| 主函数:获取并保存股票全历史数据 |
| """ |
| |
| save_directory = "D:/lianghuajiaoyi/Kronos/examples/data" |
|
|
| print("=" * 60) |
| print(f"开始获取股票 {stock_code} 的全部历史数据") |
| print("=" * 60) |
| print(f"数据将保存到: {save_directory}") |
|
|
| |
| try: |
| import requests |
| import numpy as np |
| except ImportError: |
| print("正在安装必要库...") |
| import subprocess |
| subprocess.check_call(["pip", "install", "requests", "numpy", "pandas"]) |
| import requests |
| import numpy as np |
|
|
| |
| stock_data = get_stock_data_with_retry_all_history(stock_code) |
|
|
| if stock_data is not None: |
| |
| display_all_history_data_info(stock_data, stock_code) |
|
|
| |
| save_all_history_stock_data(stock_data, stock_code, save_directory) |
|
|
| print(f"\n🎉 股票 {stock_code} 全历史数据处理完成!") |
| print( |
| f"数据时间跨度: {stock_data.index.min().strftime('%Y-%m-%d')} 到 {stock_data.index.max().strftime('%Y-%m-%d')}") |
| print(f"总交易天数: {len(stock_data):,}") |
|
|
| |
| csv_file = os.path.join(save_directory, f"{stock_code}_all_history.csv") |
| if os.path.exists(csv_file): |
| file_size = os.path.getsize(csv_file) / 1024 |
| print(f"📄 生成的文件: {csv_file} ({file_size:.1f} KB)") |
| else: |
| print("❌ 未能获取股票全历史数据") |
|
|
|
|
| |
| if __name__ == "__main__": |
| """ |
| 使用方法: |
| 修改下面的参数来获取不同股票的全历史数据 |
| """ |
|
|
| |
| TARGET_STOCK_CODE = "300418" |
| |
|
|
| print("股票全历史数据获取工具") |
| print("说明:修改代码中的 TARGET_STOCK_CODE 来获取不同股票的全部历史数据") |
| print(f"当前设置: 股票代码={TARGET_STOCK_CODE}") |
| print() |
|
|
| |
| main_all_history(stock_code=TARGET_STOCK_CODE) |
|
|
| print(f"\n💡 提示:要获取其他股票的全历史数据,请修改代码中的 TARGET_STOCK_CODE 变量") |