- 新增 /api/messages API 接口,支持从 Redis Stream 读取消息 - 支持按策略筛选消息和分页展示 - 前端新增消息列表卡片,展示时间、策略、股票代码、动作、价格和状态 - 自动判断消息处理状态(已处理/待处理) - 消息列表每30秒自动刷新,支持手动刷新
281 lines
9.0 KiB
Plaintext
281 lines
9.0 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",
|
||
"4990 600201.SH 20260213\n",
|
||
"4991 600202.SH 20260213\n",
|
||
"4992 600203.SH 20260213\n",
|
||
"4980 600188.SH 20260213\n",
|
||
"7466 920946.BJ 20260213\n",
|
||
"<class 'pandas.core.frame.DataFrame'>\n",
|
||
"Index: 11850195 entries, 0 to 37373\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: 271.2+ MB\n",
|
||
"None\n",
|
||
"20260213\n",
|
||
"20260224\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='20260310')\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": [
|
||
"任务 20260310 完成\n",
|
||
"任务 20260309 完成\n",
|
||
"任务 20260306 完成\n",
|
||
"任务 20260305 完成\n",
|
||
"任务 20260304 完成\n",
|
||
"任务 20260303 完成\n",
|
||
"任务 20260302 完成\n",
|
||
"任务 20260227 完成\n",
|
||
"任务 20260226 完成\n",
|
||
"任务 20260225 完成\n",
|
||
"任务 20260224 完成\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 20260227 000001.SZ 11.96 9.78\n",
|
||
"1 20260227 000002.SZ 5.30 4.34\n",
|
||
"2 20260227 000004.SZ 7.52 6.80\n",
|
||
"3 20260227 000006.SZ 10.25 8.39\n",
|
||
"4 20260227 000007.SZ 14.48 11.84\n",
|
||
"... ... ... ... ...\n",
|
||
"7481 20260227 920978.BJ 39.63 21.35\n",
|
||
"7482 20260227 920981.BJ 43.95 23.67\n",
|
||
"7483 20260227 920982.BJ 284.68 153.30\n",
|
||
"7484 20260227 920985.BJ 10.79 5.81\n",
|
||
"7485 20260227 920992.BJ 21.69 11.69\n",
|
||
"\n",
|
||
"[7486 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20260226 000001.SZ 11.95 9.77\n",
|
||
"1 20260226 000002.SZ 5.50 4.50\n",
|
||
"2 20260226 000004.SZ 7.92 7.16\n",
|
||
"3 20260226 000006.SZ 10.65 8.71\n",
|
||
"4 20260226 000007.SZ 13.87 11.35\n",
|
||
"... ... ... ... ...\n",
|
||
"7480 20260226 920978.BJ 40.09 21.59\n",
|
||
"7481 20260226 920981.BJ 42.57 22.93\n",
|
||
"7482 20260226 920982.BJ 285.33 153.65\n",
|
||
"7483 20260226 920985.BJ 11.08 5.98\n",
|
||
"7484 20260226 920992.BJ 21.74 11.72\n",
|
||
"\n",
|
||
"[7485 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20260225 000001.SZ 12.00 9.82\n",
|
||
"1 20260225 000002.SZ 5.41 4.43\n",
|
||
"2 20260225 000004.SZ 8.34 7.54\n",
|
||
"3 20260225 000006.SZ 10.20 8.34\n",
|
||
"4 20260225 000007.SZ 13.97 11.43\n",
|
||
"... ... ... ... ...\n",
|
||
"7477 20260225 920978.BJ 39.75 21.41\n",
|
||
"7478 20260225 920981.BJ 41.96 22.60\n",
|
||
"7479 20260225 920982.BJ 284.15 153.01\n",
|
||
"7480 20260225 920985.BJ 11.15 6.01\n",
|
||
"7481 20260225 920992.BJ 21.52 11.60\n",
|
||
"\n",
|
||
"[7482 rows x 4 columns], trade_date ts_code up_limit down_limit\n",
|
||
"0 20260224 000001.SZ 12.00 9.82\n",
|
||
"1 20260224 000002.SZ 5.47 4.47\n",
|
||
"2 20260224 000004.SZ 8.78 7.94\n",
|
||
"3 20260224 000006.SZ 10.37 8.49\n",
|
||
"4 20260224 000007.SZ 13.65 11.17\n",
|
||
"... ... ... ... ...\n",
|
||
"7477 20260224 920978.BJ 39.05 21.03\n",
|
||
"7478 20260224 920981.BJ 41.52 22.36\n",
|
||
"7479 20260224 920982.BJ 282.77 152.27\n",
|
||
"7480 20260224 920985.BJ 11.08 5.98\n",
|
||
"7481 20260224 920992.BJ 21.32 11.48\n",
|
||
"\n",
|
||
"[7482 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.12.11"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|