281 lines
8.1 KiB
Plaintext
281 lines
8.1 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",
|
||
"2364 300067.SZ 20250508\n",
|
||
"2363 300066.SZ 20250508\n",
|
||
"2362 300065.SZ 20250508\n",
|
||
"2373 300076.SZ 20250508\n",
|
||
"7111 920819.BJ 20250508\n",
|
||
"<class 'pandas.core.frame.DataFrame'>\n",
|
||
"Index: 10450519 entries, 0 to 7111\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.2+ MB\n",
|
||
"None\n",
|
||
"20250508\n",
|
||
"20250509\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import pandas as pd\n",
|
||
"import time\n",
|
||
"\n",
|
||
"h5_filename = '../../../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] # å<>ªä¿<C3A4>留交易日\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": [
|
||
"任务 20250718 完æˆ<C3A6>\n",
|
||
"任务 20250717 完æˆ<C3A6>\n",
|
||
"任务 20250715 完æˆ<C3A6>\n",
|
||
"任务 20250716 完æˆ<C3A6>\n",
|
||
"任务 20250714 完æˆ<C3A6>\n",
|
||
"任务 20250711 完æˆ<C3A6>\n",
|
||
"任务 20250710 完æˆ<C3A6>\n",
|
||
"任务 20250709 完æˆ<C3A6>\n",
|
||
"任务 20250708 完æˆ<C3A6>\n",
|
||
"任务 20250707 完æˆ<C3A6>\n",
|
||
"任务 20250704 完æˆ<C3A6>\n",
|
||
"任务 20250703 完æˆ<C3A6>\n",
|
||
"任务 20250702 完æˆ<C3A6>\n",
|
||
"任务 20250701 完æˆ<C3A6>\n",
|
||
"任务 20250630 完æˆ<C3A6>\n",
|
||
"任务 20250627 完æˆ<C3A6>\n",
|
||
"任务 20250626 完æˆ<C3A6>\n",
|
||
"任务 20250625 完æˆ<C3A6>\n",
|
||
"任务 20250624 完æˆ<C3A6>\n",
|
||
"任务 20250623 完æˆ<C3A6>\n",
|
||
"任务 20250620 完æˆ<C3A6>\n",
|
||
"任务 20250619 完æˆ<C3A6>\n",
|
||
"任务 20250618 完æˆ<C3A6>\n",
|
||
"任务 20250617 完æˆ<C3A6>\n",
|
||
"任务 20250616 完æˆ<C3A6>\n",
|
||
"任务 20250613 完æˆ<C3A6>\n",
|
||
"任务 20250612 完æˆ<C3A6>\n",
|
||
"任务 20250611 完æˆ<C3A6>\n",
|
||
"任务 20250610 完æˆ<C3A6>\n",
|
||
"任务 20250609 完æˆ<C3A6>\n",
|
||
"任务 20250606 完æˆ<C3A6>\n",
|
||
"任务 20250605 完æˆ<C3A6>\n",
|
||
"任务 20250604 完æˆ<C3A6>\n",
|
||
"任务 20250603 完æˆ<C3A6>\n",
|
||
"任务 20250530 完æˆ<C3A6>\n",
|
||
"任务 20250528 完æˆ<C3A6>\n",
|
||
"任务 20250529 完æˆ<C3A6>\n",
|
||
"任务 20250527 完æˆ<C3A6>\n",
|
||
"任务 20250526 完æˆ<C3A6>\n",
|
||
"任务 20250523 完æˆ<C3A6>\n",
|
||
"任务 20250522 完æˆ<C3A6>\n",
|
||
"任务 20250521 完æˆ<C3A6>\n",
|
||
"任务 20250520 完æˆ<C3A6>\n",
|
||
"任务 20250519 完æˆ<C3A6>\n",
|
||
"任务 20250516 完æˆ<C3A6>\n",
|
||
"任务 20250515 完æˆ<C3A6>\n",
|
||
"任务 20250514 完æˆ<C3A6>\n",
|
||
"任务 20250513 完æˆ<C3A6>\n",
|
||
"任务 20250512 完æˆ<C3A6>\n",
|
||
"任务 20250509 完æˆ<C3A6>\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"from concurrent.futures import ThreadPoolExecutor, as_completed\n",
|
||
"\n",
|
||
"all_daily_data = []\n",
|
||
"\n",
|
||
"# API 调用计数和时间控制å<C2B6>˜é‡<C3A9>\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] # 获å<C2B7>–对应的交易日期\n",
|
||
" try:\n",
|
||
" result = future.result() # 获å<C2B7>–任务执行的结果\n",
|
||
" if result is not None:\n",
|
||
" all_daily_data.append(result)\n",
|
||
" print(f\"任务 {trade_date} 完æˆ<C3A6>\")\n",
|
||
" except Exception as e:\n",
|
||
" print(f\"获å<C2B7>– {trade_date} æ•°æ<C2B0>®æ—¶å‡ºé”™: {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 20250509 000001.SZ 12.19 9.97\n",
|
||
"1 20250509 000002.SZ 7.57 6.19\n",
|
||
"2 20250509 000004.SZ 7.86 7.12\n",
|
||
"3 20250509 000006.SZ 7.33 5.99\n",
|
||
"4 20250509 000007.SZ 7.66 6.26\n",
|
||
"... ... ... ... ...\n",
|
||
"7109 20250509 920445.BJ 13.14 7.08\n",
|
||
"7110 20250509 920489.BJ 31.70 17.08\n",
|
||
"7111 20250509 920682.BJ 16.17 8.71\n",
|
||
"7112 20250509 920799.BJ 78.39 42.21\n",
|
||
"7113 20250509 920819.BJ 5.74 3.10\n",
|
||
"\n",
|
||
"[7114 rows x 4 columns]]\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print(all_daily_data)\n",
|
||
"# 将所有数æ<C2B0>®å<C2AE>ˆå¹¶ä¸ºä¸€ä¸ª 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": [
|
||
"所有æ¯<C3A6>日基础数æ<C2B0>®èŽ·å<C2B7>–å¹¶ä¿<C3A4>å˜å®Œæ¯•ï¼<C3AF>\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"\n",
|
||
"\n",
|
||
"# 将数æ<C2B0>®ä¿<C3A4>å˜ä¸º HDF5 文件(table æ ¼å¼<C3A5>)\n",
|
||
"all_daily_data_df.to_hdf(h5_filename, key='stk_limit', mode='a', format='table', append=True, data_columns=True)\n",
|
||
"\n",
|
||
"print(\"所有æ¯<C3A6>日基础数æ<C2B0>®èŽ·å<C2B7>–å¹¶ä¿<C3A4>å˜å®Œæ¯•ï¼<C3AF>\")"
|
||
]
|
||
},
|
||
{
|
||
"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": "new_trader",
|
||
"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.11.11"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|