refactor(qmt): 优化配置模型和百分比交易逻辑

- 统一使用配置模型属性访问替代字典下标
- 完善百分比模式买卖的日志记录和错误处理
- 代码格式化和死代码清理
- 更新 notebook 数据及测试脚本
This commit is contained in:
2026-03-03 15:16:37 +08:00
parent 7bb0a0537b
commit 4090b3c5be
5 changed files with 309 additions and 188 deletions

View File

@@ -137,7 +137,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.11"
"version": "3.13.2"
}
},
"nbformat": 4,

View File

@@ -18,10 +18,10 @@
"Please upgrade to Gymnasium, the maintained drop-in replacement of Gym, or contact the authors of your software and request that they upgrade.\n",
"Users of this version of Gym should be able to simply replace 'import gym' with 'import gymnasium as gym' in the vast majority of cases.\n",
"See the migration guide at https://gymnasium.farama.org/introduction/migration_guide/ for additional information.\n",
"[177513:MainThread](2026-02-25 22:33:43,460) INFO - qlib.Initialization - [config.py:452] - default_conf: client.\n",
"[177513:MainThread](2026-02-25 22:33:43,461) WARNING - qlib.Initialization - [config.py:459] - Unrecognized config freq\n",
"[177513:MainThread](2026-02-25 22:33:43,467) INFO - qlib.Initialization - [__init__.py:75] - qlib successfully initialized based on client settings.\n",
"[177513:MainThread](2026-02-25 22:33:43,469) INFO - qlib.Initialization - [__init__.py:77] - data_path={'__DEFAULT_FREQ': PosixPath('/mnt/d/PyProject/NewStock/data/qlib')}\n"
"[65879:MainThread](2026-03-01 22:39:22,291) INFO - qlib.Initialization - [config.py:452] - default_conf: client.\n",
"[65879:MainThread](2026-03-01 22:39:22,291) WARNING - qlib.Initialization - [config.py:459] - Unrecognized config freq\n",
"[65879:MainThread](2026-03-01 22:39:22,297) INFO - qlib.Initialization - [__init__.py:75] - qlib successfully initialized based on client settings.\n",
"[65879:MainThread](2026-03-01 22:39:22,298) INFO - qlib.Initialization - [__init__.py:77] - data_path={'__DEFAULT_FREQ': PosixPath('/mnt/d/PyProject/NewStock/data/qlib')}\n"
]
},
{
@@ -96,7 +96,7 @@
"cyq perf\n",
"left merge on ['ts_code', 'trade_date']\n",
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 9436343 entries, 0 to 9436342\n",
"RangeIndex: 9456764 entries, 0 to 9456763\n",
"Data columns (total 33 columns):\n",
" # Column Dtype \n",
"--- ------ ----- \n",
@@ -174,7 +174,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 7,
"id": "5f3847ec",
"metadata": {},
"outputs": [
@@ -182,15 +182,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
" trade_date return_5_rank\n",
"6527870 2024-06-03 0.523969\n",
" trade_date ma5 ma10 close\n",
"6527870 2024-06-03 10.526 10.216 10.37\n"
" trade_date ma5 ma10 return_5 close\n",
"6527953 2024-01-23 7.358 7.500 -0.043364 7.28\n",
"6527952 2024-01-24 7.412 7.507 0.036339 7.70\n",
"6527951 2024-01-25 7.552 7.556 0.094213 8.13\n",
"6527950 2024-01-26 7.692 7.609 0.093583 8.18\n",
"6527949 2024-01-29 7.910 7.667 0.152022 8.26\n"
]
}
],
"source": [
"\n",
"df = df.sort_values(by=['trade_date'])\n",
"df['return_5'] = df.groupby('ts_code')['close'].pct_change(periods=5)\n",
"\n",
"df['return_5_rank'] = df.groupby('trade_date')['return_5'].transform(\n",
@@ -198,10 +200,19 @@
" )\n",
"\n",
"df['ma5'] = df.groupby('ts_code')['close'].transform(lambda x: x.rolling(window=5, min_periods=1).mean())\n",
"df['ma10'] = df.groupby('ts_code')['close'].transform(lambda x: x.rolling(window=10, min_periods=1).mean())\n",
"df['ma10'] = df.groupby('ts_code')['close'].transform(lambda x: x.rolling(window=10, min_periods=1).mean())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6de3c12",
"metadata": {},
"outputs": [],
"source": [
"\n",
"print(df[(df['ts_code'] == '601117.SH') & (df['trade_date'] == '2024-06-03')][['trade_date', 'return_5_rank']])\n",
"print(df[(df['ts_code'] == '601117.SH') & (df['trade_date'] == '2024-06-03')][['trade_date', 'ma5', 'ma10', 'close']])"
"# print(df[(df['ts_code'] == '601117.SH') & (df['trade_date'] >= '2024-01-23')][['trade_date', 'return_5_rank']])\n",
"print(df[(df['ts_code'] == '601117.SH') & (df['trade_date'] >= '2024-01-23') & (df['trade_date'] <= '2024-01-29')][['trade_date', 'ma5', 'ma10', 'return_5', 'close']])"
]
},
{
@@ -9370,7 +9381,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
"version": "3.12.11"
}
},
"nbformat": 4,

View File

@@ -2508,7 +2508,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
"version": "3.12.11"
}
},
"nbformat": 4,

View File

@@ -2,14 +2,9 @@ from operator import index
import tushare as ts
import sys
print(sys.path)
from main.factor.factor import calculate_arbr
ts.set_token('3a0741c702ee7e5e5f2bf1f0846bafaafe4e320833240b2a7e4a685f')
pro = ts.pro_api()
df = pro.dc_member(trade_date='20190105')
df = ts.pro_bar(ts_code='601117.SH', adj='qfq', start_date='20240129', end_date='20240129', fields='trade_date,ts_code,close,tor')
print(df)