462 lines
18 KiB
Plaintext
462 lines
18 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "500802dc-7a20-48b7-a470-a4bae3ec534b",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-04-09T14:57:41.532210Z",
|
||
"start_time": "2025-04-09T14:57:40.584930Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"import tushare as ts\n",
|
||
"\n",
|
||
"ts.set_token('3a0741c702ee7e5e5f2bf1f0846bafaafe4e320833240b2a7e4a685f')\n",
|
||
"pro = ts.pro_api()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "5a84bc9da6d54868",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-04-09T14:58:04.911924Z",
|
||
"start_time": "2025-04-09T14:57:41.540345Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
" ts_code trade_date\n",
|
||
"2365 300067.SZ 20250509\n",
|
||
"2364 300066.SZ 20250509\n",
|
||
"2363 300065.SZ 20250509\n",
|
||
"2374 300076.SZ 20250509\n",
|
||
"7113 920819.BJ 20250509\n",
|
||
"<class 'pandas.core.frame.DataFrame'>\n",
|
||
"Index: 10457633 entries, 0 to 7113\n",
|
||
"Data columns (total 2 columns):\n",
|
||
" # Column Dtype \n",
|
||
"--- ------ ----- \n",
|
||
" 0 ts_code object\n",
|
||
" 1 trade_date object\n",
|
||
"dtypes: object(2)\n",
|
||
"memory usage: 239.4+ MB\n",
|
||
"None\n",
|
||
"20250509\n",
|
||
"20250512\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import pandas as pd\n",
|
||
"import time\n",
|
||
"\n",
|
||
"h5_filename = '/mnt/d/PyProject/NewStock/data/stk_limit.h5'\n",
|
||
"key = '/stk_limit'\n",
|
||
"max_date = None\n",
|
||
"with pd.HDFStore(h5_filename, mode='r') as store:\n",
|
||
" df = store[key][['ts_code', 'trade_date']]\n",
|
||
" print(df.sort_values(by='trade_date', ascending=True).tail())\n",
|
||
" print(df.info())\n",
|
||
" max_date = df['trade_date'].max()\n",
|
||
"\n",
|
||
"print(max_date)\n",
|
||
"trade_cal = pro.trade_cal(exchange='', start_date='20170101', end_date='20250720')\n",
|
||
"trade_cal = trade_cal[trade_cal['is_open'] == 1] # 只保留交易日\n",
|
||
"trade_dates = trade_cal[trade_cal['cal_date'] > max_date]['cal_date'].tolist()\n",
|
||
"start_date = min(trade_dates)\n",
|
||
"print(start_date)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "bb3191de-27a2-4c89-a3b5-32a0d7b9496f",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-04-09T14:58:09.342522Z",
|
||
"start_time": "2025-04-09T14:58:05.259974Z"
|
||
},
|
||
"scrolled": true
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"任务 20250717 完成\n",
|
||
"任务 20250718 完成\n",
|
||
"任务 20250716 完成\n",
|
||
"任务 20250715 完成\n",
|
||
"任务 20250714 完成\n",
|
||
"任务 20250711 完成\n",
|
||
"任务 20250710 完成\n",
|
||
"任务 20250709 完成\n",
|
||
"任务 20250707 完成\n",
|
||
"任务 20250708 完成\n",
|
||
"任务 20250704 完成\n",
|
||
"任务 20250703 完成\n",
|
||
"任务 20250702 完成\n",
|
||
"任务 20250701 完成\n",
|
||
"任务 20250627 完成\n",
|
||
"任务 20250630 完成\n",
|
||
"任务 20250626 完成\n",
|
||
"任务 20250625 完成\n",
|
||
"任务 20250624 完成\n",
|
||
"任务 20250623 完成\n",
|
||
"任务 20250620 完成\n",
|
||
"任务 20250619 完成\n",
|
||
"任务 20250617 完成\n",
|
||
"任务 20250618 完成\n",
|
||
"任务 20250613 完成\n",
|
||
"任务 20250616 完成\n",
|
||
"任务 20250612 完成\n",
|
||
"任务 20250611 完成\n",
|
||
"任务 20250610 完成\n",
|
||
"任务 20250609 完成\n",
|
||
"任务 20250606 完成\n",
|
||
"任务 20250605 完成\n",
|
||
"任务 20250604 完成\n",
|
||
"任务 20250603 完成\n",
|
||
"任务 20250530 完成\n",
|
||
"任务 20250529 完成\n",
|
||
"任务 20250528 完成\n",
|
||
"任务 20250527 完成\n",
|
||
"任务 20250526 完成\n",
|
||
"任务 20250523 完成\n",
|
||
"任务 20250521 完成\n",
|
||
"任务 20250522 完成\n",
|
||
"任务 20250519 完成\n",
|
||
"任务 20250520 完成\n",
|
||
"任务 20250516 完成\n",
|
||
"任务 20250515 完成\n",
|
||
"任务 20250514 完成\n",
|
||
"任务 20250513 完成\n",
|
||
"任务 20250512 完成\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"from concurrent.futures import ThreadPoolExecutor, as_completed\n",
|
||
"\n",
|
||
"all_daily_data = []\n",
|
||
"\n",
|
||
"# API 调用计数和时间控制变量\n",
|
||
"api_call_count = 0\n",
|
||
"batch_start_time = time.time()\n",
|
||
"\n",
|
||
"\n",
|
||
"def get_data(trade_date):\n",
|
||
" time.sleep(0.1)\n",
|
||
" stk_limit_data = pro.stk_limit(trade_date=trade_date)\n",
|
||
" if stk_limit_data is not None and not stk_limit_data.empty:\n",
|
||
" return stk_limit_data\n",
|
||
"\n",
|
||
"\n",
|
||
"with ThreadPoolExecutor(max_workers=2) as executor:\n",
|
||
" future_to_date = {executor.submit(get_data, td): td for td in trade_dates}\n",
|
||
"\n",
|
||
" for future in as_completed(future_to_date):\n",
|
||
" trade_date = future_to_date[future] # 获取对应的交易日期\n",
|
||
" try:\n",
|
||
" result = future.result() # 获取任务执行的结果\n",
|
||
" if result is not None:\n",
|
||
" all_daily_data.append(result)\n",
|
||
" print(f\"任务 {trade_date} 完成\")\n",
|
||
" except Exception as e:\n",
|
||
" print(f\"获取 {trade_date} 数据时出错: {e}\")\n",
|
||
"\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"id": "96a81aa5890ea3c3",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-04-09T14:58:09.353560Z",
|
||
"start_time": "2025-04-09T14:58:09.346528Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[ trade_date ts_code up_limit down_limit\n",
|
||
"0 20250530 000001.SZ 12.61 10.31\n",
|
||
"1 20250530 000002.SZ 7.37 6.03\n",
|
||
"2 20250530 000004.SZ 10.38 9.40\n",
|
||
"3 20250530 000006.SZ 7.69 6.29\n",
|
||
"4 20250530 000007.SZ 8.61 7.05\n",
|
||
"... ... ... ... ...\n",
|
||
"7136 20250530 920445.BJ 13.61 7.33\n",
|
||
"7137 20250530 920489.BJ 32.64 17.58\n",
|
||
"7138 20250530 920682.BJ 13.81 7.45\n",
|
||
"7139 20250530 920799.BJ 78.92 42.50\n",
|
||
"7140 20250530 920819.BJ 5.90 3.18\n",
|
||
"\n",
|
||
"[7141 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250529 000001.SZ 12.68 10.38\n",
|
||
"1 20250529 000002.SZ 7.35 6.01\n",
|
||
"2 20250529 000004.SZ 10.44 9.44\n",
|
||
"3 20250529 000006.SZ 7.78 6.36\n",
|
||
"4 20250529 000007.SZ 8.48 6.94\n",
|
||
"... ... ... ... ...\n",
|
||
"7132 20250529 920445.BJ 13.28 7.16\n",
|
||
"7133 20250529 920489.BJ 31.73 17.09\n",
|
||
"7134 20250529 920682.BJ 13.55 7.31\n",
|
||
"7135 20250529 920799.BJ 73.17 39.41\n",
|
||
"7136 20250529 920819.BJ 5.86 3.16\n",
|
||
"\n",
|
||
"[7137 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250528 000001.SZ 12.64 10.34\n",
|
||
"1 20250528 000002.SZ 7.34 6.00\n",
|
||
"2 20250528 000004.SZ 10.52 9.52\n",
|
||
"3 20250528 000006.SZ 7.96 6.52\n",
|
||
"4 20250528 000007.SZ 8.51 6.97\n",
|
||
"... ... ... ... ...\n",
|
||
"7130 20250528 920445.BJ 13.50 7.28\n",
|
||
"7131 20250528 920489.BJ 32.70 17.62\n",
|
||
"7132 20250528 920682.BJ 13.71 7.39\n",
|
||
"7133 20250528 920799.BJ 73.60 39.64\n",
|
||
"7134 20250528 920819.BJ 5.87 3.17\n",
|
||
"\n",
|
||
"[7135 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250527 000001.SZ 12.56 10.28\n",
|
||
"1 20250527 000002.SZ 7.29 5.97\n",
|
||
"2 20250527 000004.SZ 10.02 9.06\n",
|
||
"3 20250527 000006.SZ 7.58 6.20\n",
|
||
"4 20250527 000007.SZ 8.37 6.85\n",
|
||
"... ... ... ... ...\n",
|
||
"7128 20250527 920445.BJ 13.28 7.16\n",
|
||
"7129 20250527 920489.BJ 33.96 18.30\n",
|
||
"7130 20250527 920682.BJ 13.59 7.33\n",
|
||
"7131 20250527 920799.BJ 73.38 39.52\n",
|
||
"7132 20250527 920819.BJ 5.55 2.99\n",
|
||
"\n",
|
||
"[7133 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250526 000001.SZ 12.61 10.31\n",
|
||
"1 20250526 000002.SZ 7.29 5.97\n",
|
||
"2 20250526 000004.SZ 9.54 8.64\n",
|
||
"3 20250526 000006.SZ 7.44 6.08\n",
|
||
"4 20250526 000007.SZ 8.66 7.08\n",
|
||
"... ... ... ... ...\n",
|
||
"7130 20250526 920445.BJ 12.88 6.94\n",
|
||
"7131 20250526 920489.BJ 31.96 17.22\n",
|
||
"7132 20250526 920682.BJ 12.77 6.89\n",
|
||
"7133 20250526 920799.BJ 72.35 38.97\n",
|
||
"7134 20250526 920819.BJ 5.48 2.96\n",
|
||
"\n",
|
||
"[7135 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250523 000001.SZ 12.71 10.40\n",
|
||
"1 20250523 000002.SZ 7.34 6.00\n",
|
||
"2 20250523 000004.SZ 9.87 8.93\n",
|
||
"3 20250523 000006.SZ 7.54 6.17\n",
|
||
"4 20250523 000007.SZ 8.80 7.20\n",
|
||
"... ... ... ... ...\n",
|
||
"7130 20250523 920445.BJ 13.01 7.01\n",
|
||
"7131 20250523 920489.BJ 30.58 16.48\n",
|
||
"7132 20250523 920682.BJ 12.83 6.91\n",
|
||
"7133 20250523 920799.BJ 74.10 39.90\n",
|
||
"7134 20250523 920819.BJ 5.56 3.00\n",
|
||
"\n",
|
||
"[7135 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250521 000001.SZ 12.53 10.25\n",
|
||
"1 20250521 000002.SZ 7.46 6.10\n",
|
||
"2 20250521 000004.SZ 9.47 8.57\n",
|
||
"3 20250521 000006.SZ 7.61 6.23\n",
|
||
"4 20250521 000007.SZ 8.28 6.78\n",
|
||
"... ... ... ... ...\n",
|
||
"7129 20250521 920445.BJ 14.02 7.56\n",
|
||
"7130 20250521 920489.BJ 32.89 17.71\n",
|
||
"7131 20250521 920682.BJ 13.83 7.45\n",
|
||
"7132 20250521 920799.BJ 77.87 41.93\n",
|
||
"7133 20250521 920819.BJ 5.95 3.21\n",
|
||
"\n",
|
||
"[7134 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250522 000001.SZ 12.63 10.33\n",
|
||
"1 20250522 000002.SZ 7.44 6.08\n",
|
||
"2 20250522 000004.SZ 9.94 9.00\n",
|
||
"3 20250522 000006.SZ 7.43 6.08\n",
|
||
"4 20250522 000007.SZ 8.43 6.89\n",
|
||
"... ... ... ... ...\n",
|
||
"7130 20250522 920445.BJ 13.68 7.38\n",
|
||
"7131 20250522 920489.BJ 32.95 17.75\n",
|
||
"7132 20250522 920682.BJ 13.41 7.23\n",
|
||
"7133 20250522 920799.BJ 77.42 41.70\n",
|
||
"7134 20250522 920819.BJ 5.81 3.13\n",
|
||
"\n",
|
||
"[7135 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250519 000001.SZ 12.52 10.24\n",
|
||
"1 20250519 000002.SZ 7.45 6.09\n",
|
||
"2 20250519 000004.SZ 8.68 7.86\n",
|
||
"3 20250519 000006.SZ 7.17 5.87\n",
|
||
"4 20250519 000007.SZ 8.05 6.59\n",
|
||
"... ... ... ... ...\n",
|
||
"7128 20250519 920445.BJ 13.96 7.52\n",
|
||
"7129 20250519 920489.BJ 30.29 16.31\n",
|
||
"7130 20250519 920682.BJ 13.35 7.19\n",
|
||
"7131 20250519 920799.BJ 77.87 41.93\n",
|
||
"7132 20250519 920819.BJ 5.91 3.19\n",
|
||
"\n",
|
||
"[7133 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250520 000001.SZ 12.51 10.23\n",
|
||
"1 20250520 000002.SZ 7.48 6.12\n",
|
||
"2 20250520 000004.SZ 9.02 8.16\n",
|
||
"3 20250520 000006.SZ 7.66 6.26\n",
|
||
"4 20250520 000007.SZ 8.18 6.70\n",
|
||
"... ... ... ... ...\n",
|
||
"7128 20250520 920445.BJ 13.97 7.53\n",
|
||
"7129 20250520 920489.BJ 31.75 17.11\n",
|
||
"7130 20250520 920682.BJ 13.23 7.13\n",
|
||
"7131 20250520 920799.BJ 77.83 41.91\n",
|
||
"7132 20250520 920819.BJ 5.86 3.16\n",
|
||
"\n",
|
||
"[7133 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250516 000001.SZ 12.53 10.25\n",
|
||
"1 20250516 000002.SZ 7.47 6.11\n",
|
||
"2 20250516 000004.SZ 9.14 8.27\n",
|
||
"3 20250516 000006.SZ 7.17 5.87\n",
|
||
"4 20250516 000007.SZ 8.03 6.57\n",
|
||
"... ... ... ... ...\n",
|
||
"7125 20250516 920445.BJ 14.80 7.98\n",
|
||
"7126 20250516 920489.BJ 30.31 16.33\n",
|
||
"7127 20250516 920682.BJ 13.71 7.39\n",
|
||
"7128 20250516 920799.BJ 78.03 42.03\n",
|
||
"7129 20250516 920819.BJ 5.74 3.10\n",
|
||
"\n",
|
||
"[7130 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250515 000001.SZ 12.57 10.29\n",
|
||
"1 20250515 000002.SZ 7.58 6.20\n",
|
||
"2 20250515 000004.SZ 8.90 8.06\n",
|
||
"3 20250515 000006.SZ 7.26 5.94\n",
|
||
"4 20250515 000007.SZ 8.01 6.55\n",
|
||
"... ... ... ... ...\n",
|
||
"7119 20250515 920445.BJ 14.80 7.98\n",
|
||
"7120 20250515 920489.BJ 31.12 16.76\n",
|
||
"7121 20250515 920682.BJ 16.96 9.14\n",
|
||
"7122 20250515 920799.BJ 82.13 44.23\n",
|
||
"7123 20250515 920819.BJ 5.59 3.01\n",
|
||
"\n",
|
||
"[7124 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250514 000001.SZ 12.42 10.16\n",
|
||
"1 20250514 000002.SZ 7.55 6.17\n",
|
||
"2 20250514 000004.SZ 8.96 8.10\n",
|
||
"3 20250514 000006.SZ 7.14 5.84\n",
|
||
"4 20250514 000007.SZ 8.02 6.56\n",
|
||
"... ... ... ... ...\n",
|
||
"7117 20250514 920445.BJ 14.04 7.56\n",
|
||
"7118 20250514 920489.BJ 31.42 16.92\n",
|
||
"7119 20250514 920682.BJ 17.23 9.29\n",
|
||
"7120 20250514 920799.BJ 78.22 42.12\n",
|
||
"7121 20250514 920819.BJ 5.59 3.01\n",
|
||
"\n",
|
||
"[7122 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250513 000001.SZ 12.28 10.04\n",
|
||
"1 20250513 000002.SZ 7.54 6.17\n",
|
||
"2 20250513 000004.SZ 8.53 7.71\n",
|
||
"3 20250513 000006.SZ 7.12 5.82\n",
|
||
"4 20250513 000007.SZ 7.82 6.40\n",
|
||
"... ... ... ... ...\n",
|
||
"7116 20250513 920445.BJ 13.36 7.20\n",
|
||
"7117 20250513 920489.BJ 31.07 16.73\n",
|
||
"7118 20250513 920682.BJ 16.73 9.01\n",
|
||
"7119 20250513 920799.BJ 80.47 43.33\n",
|
||
"7120 20250513 920819.BJ 5.60 3.02\n",
|
||
"\n",
|
||
"[7121 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20250512 000001.SZ 12.27 10.04\n",
|
||
"1 20250512 000002.SZ 7.46 6.10\n",
|
||
"2 20250512 000004.SZ 8.12 7.34\n",
|
||
"3 20250512 000006.SZ 7.08 5.80\n",
|
||
"4 20250512 000007.SZ 7.81 6.39\n",
|
||
"... ... ... ... ...\n",
|
||
"7112 20250512 920445.BJ 13.19 7.11\n",
|
||
"7113 20250512 920489.BJ 30.55 16.45\n",
|
||
"7114 20250512 920682.BJ 16.34 8.80\n",
|
||
"7115 20250512 920799.BJ 78.13 42.07\n",
|
||
"7116 20250512 920819.BJ 5.57 3.01\n",
|
||
"\n",
|
||
"[7117 rows x 4 columns]]\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print(all_daily_data)\n",
|
||
"# 将所有数据合并为一个 DataFrame\n",
|
||
"all_daily_data_df = pd.concat(all_daily_data, ignore_index=True)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "ad9733a1-2f42-43ee-a98c-0bf699304c21",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-04-09T14:58:09.674078Z",
|
||
"start_time": "2025-04-09T14:58:09.366441Z"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"所有每日基础数据获取并保存完毕!\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"\n",
|
||
"\n",
|
||
"# 将数据保存为 HDF5 文件(table 格式)\n",
|
||
"all_daily_data_df.to_hdf(h5_filename, key='stk_limit', mode='a', format='table', append=True, data_columns=True)\n",
|
||
"\n",
|
||
"print(\"所有每日基础数据获取并保存完毕!\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "7e777f1f-4d54-4a74-b916-691ede6af055",
|
||
"metadata": {
|
||
"ExecuteTime": {
|
||
"end_time": "2025-04-09T14:58:09.689422Z",
|
||
"start_time": "2025-04-09T14:58:09.686524Z"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "stock",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.13.2"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|