Files
NewStock/main/data/update/update_money_flow.ipynb
liaozhaorun 4607555eaf feat: 完善 QMT 交易模块文档和配置展示功能
- 优化前端仪表盘界面
- 添加配置文件可视化展示
- 编写 QMT 模块配置文档
- 完善项目规则体系(KiloCode)
2026-01-27 00:52:35 +08:00

281 lines
10 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b94bb1f2-5332-485e-ae1b-eea01f938106",
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-09T14:57:40.184418Z",
"start_time": "2025-04-09T14:57:39.137312Z"
}
},
"outputs": [],
"source": [
"import tushare as ts\n",
"\n",
"ts.set_token('3a0741c702ee7e5e5f2bf1f0846bafaafe4e320833240b2a7e4a685f')\n",
"pro = ts.pro_api()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "742c29d453b9bb38",
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-09T14:58:10.515830Z",
"start_time": "2025-04-09T14:57:40.190466Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"Index: 9336127 entries, 0 to 25845\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: 213.7+ MB\n",
"None\n",
"20260116\n",
"start_date: 20260119\n"
]
}
],
"source": [
"import pandas as pd\n",
"import time\n",
"\n",
"h5_filename = '/mnt/d/PyProject/NewStock/data/money_flow.h5'\n",
"key = '/money_flow'\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.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='20260201')\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(f'start_date: {start_date}')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "679ce40e-8d62-4887-970c-e1d8cbdeee6b",
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-09T14:58:17.197319Z",
"start_time": "2025-04-09T14:58:10.724923Z"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"任务 20260129 完成\n",
"任务 20260130 完成\n",
"任务 20260128 完成\n",
"任务 20260127 完成\n",
"任务 20260126 完成\n",
"任务 20260123 完成\n",
"任务 20260122 完成\n",
"任务 20260121 完成\n",
"任务 20260120 完成\n",
"任务 20260119 完成\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",
" money_flow_data = pro.moneyflow(trade_date=trade_date)\n",
" if money_flow_data is not None and not money_flow_data.empty:\n",
" return money_flow_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",
" 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": "9af80516849d4e80",
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-09T14:58:17.214168Z",
"start_time": "2025-04-09T14:58:17.210734Z"
}
},
"outputs": [],
"source": [
"all_daily_data_df = pd.concat(all_daily_data, ignore_index=True)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a2b05187-437f-4053-bc43-bd80d4cf8b0e",
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-09T14:58:19.633456Z",
"start_time": "2025-04-09T14:58:17.229837Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"所有每日基础数据获取并保存完毕!\n"
]
}
],
"source": [
"\n",
"# 将所有数据合并为一个 DataFrame\n",
"\n",
"# 将数据保存为 HDF5 文件table 格式)\n",
"all_daily_data_df.to_hdf(h5_filename, key='money_flow', mode='a', format='table', append=True, data_columns=True)\n",
"\n",
"print(\"所有每日基础数据获取并保存完毕!\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e6f2a2fe",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ts_code trade_date buy_sm_vol buy_sm_amount sell_sm_vol \\\n",
"0 300284.SZ 20260123 57213 4682.01 45561 \n",
"1 002835.SZ 20260123 10930 1886.39 9809 \n",
"2 603175.SH 20260123 28945 21106.65 29993 \n",
"3 600284.SH 20260123 62561 5324.31 55101 \n",
"4 300855.SZ 20260123 41944 15903.55 33566 \n",
"... ... ... ... ... ... \n",
"25876 600211.SH 20260119 10915 4796.09 12174 \n",
"25877 601229.SH 20260119 225161 21704.91 267726 \n",
"25878 003042.SZ 20260119 17500 2893.60 11703 \n",
"25879 601155.SH 20260119 75731 11076.12 70817 \n",
"25880 600169.SH 20260119 81734 2005.16 84188 \n",
"\n",
" sell_sm_amount buy_md_vol buy_md_amount sell_md_vol sell_md_amount \\\n",
"0 3728.28 48486 3966.28 59366 4856.95 \n",
"1 1693.39 6499 1121.44 6017 1038.84 \n",
"2 21840.97 22701 16511.38 23142 16835.79 \n",
"3 4687.48 68181 5800.70 62114 5285.81 \n",
"4 12712.79 35383 13409.44 40111 15213.47 \n",
"... ... ... ... ... ... \n",
"25876 5348.74 9594 4215.26 8253 3626.54 \n",
"25877 25799.99 204041 19664.19 207361 19990.67 \n",
"25878 1933.74 11780 1946.25 14398 2379.72 \n",
"25879 10351.64 45622 6659.41 46251 6765.59 \n",
"25880 2063.68 129391 3172.21 137053 3360.95 \n",
"\n",
" buy_lg_vol buy_lg_amount sell_lg_vol sell_lg_amount buy_elg_vol \\\n",
"0 39133 3201.55 32920 2693.69 6576 \n",
"1 4067 702.11 4633 799.04 241 \n",
"2 11291 8249.67 11377 8302.90 3400 \n",
"3 46517 3960.29 57644 4906.75 23366 \n",
"4 24315 9212.24 24640 9342.34 5111 \n",
"... ... ... ... ... ... \n",
"25876 5507 2419.57 5204 2286.10 2 \n",
"25877 134694 12983.30 86737 8363.03 24453 \n",
"25878 4038 668.49 7218 1194.87 0 \n",
"25879 23725 3458.86 23634 3453.46 21891 \n",
"25880 63062 1545.61 49278 1208.05 5904 \n",
"\n",
" buy_elg_amount sell_elg_vol sell_elg_amount net_mf_vol \\\n",
"0 539.18 13561 1110.11 -4914 \n",
"1 41.60 1278 220.26 1923 \n",
"2 2437.55 1825 1325.59 1620 \n",
"3 1991.65 25767 2196.91 46602 \n",
"4 1934.73 8437 3191.36 -19491 \n",
"... ... ... ... ... \n",
"25876 0.88 388 170.41 -44 \n",
"25877 2359.23 26525 2557.95 -37774 \n",
"25878 0.00 0 0.00 6587 \n",
"25879 3125.39 26267 3749.10 28653 \n",
"25880 144.65 9571 234.95 -10865 \n",
"\n",
" net_mf_amount \n",
"0 -391.89 \n",
"1 333.04 \n",
"2 1372.40 \n",
"3 3982.07 \n",
"4 -7354.89 \n",
"... ... \n",
"25876 -16.04 \n",
"25877 -3603.98 \n",
"25878 1092.73 \n",
"25879 4152.29 \n",
"25880 -256.23 \n",
"\n",
"[25881 rows x 20 columns]\n"
]
}
],
"source": [
"print(all_daily_data_df)"
]
}
],
"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.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}