Files
NewStock/main/data/qlib.ipynb
2025-11-29 00:23:12 +08:00

180 lines
5.8 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": 2,
"id": "2d9eb12f",
"metadata": {},
"outputs": [],
"source": [
"from operator import index\n",
"\n",
"import tushare as ts\n",
"import pandas as pd\n",
"import time\n",
"\n",
"ts.set_token('3a0741c702ee7e5e5f2bf1f0846bafaafe4e320833240b2a7e4a685f')\n",
"pro = ts.pro_api()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0c5a87ba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ts_code trade_date his_low his_high cost_5pct cost_15pct \\\n",
"0 000001.SZ 20180104 0.2 12.3 7.2 8.7 \n",
"1 000002.SZ 20180104 0.2 25.6 15.0 17.6 \n",
"2 000004.SZ 20180104 0.8 53.2 21.6 22.0 \n",
"3 000008.SZ 20180104 0.1 13.9 7.2 7.8 \n",
"4 000009.SZ 20180104 0.3 15.0 5.8 5.9 \n",
"... ... ... ... ... ... ... \n",
"3095 603991.SH 20180104 12.0 67.8 26.4 27.0 \n",
"3096 603993.SH 20180104 1.4 8.6 5.4 5.6 \n",
"3097 603997.SH 20180104 5.4 31.5 9.9 10.2 \n",
"3098 603998.SH 20180104 3.8 18.3 9.5 9.8 \n",
"3099 603999.SH 20180104 3.6 30.6 6.9 6.9 \n",
"\n",
" cost_50pct cost_85pct cost_95pct weight_avg winner_rate \n",
"0 10.8 11.8 12.1 10.39 44.59 \n",
"1 22.2 24.4 24.8 21.31 97.14 \n",
"2 23.6 27.6 29.6 24.71 45.41 \n",
"3 8.6 9.2 10.5 8.64 47.04 \n",
"4 6.6 7.6 7.8 6.74 38.85 \n",
"... ... ... ... ... ... \n",
"3095 27.6 30.6 34.2 28.54 57.36 \n",
"3096 6.1 6.9 7.3 6.15 72.78 \n",
"3097 10.5 11.7 11.7 10.84 11.28 \n",
"3098 11.5 13.0 15.2 11.72 18.44 \n",
"3099 7.8 9.3 9.9 8.00 31.89 \n",
"\n",
"[3100 rows x 11 columns]\n"
]
}
],
"source": [
"\n",
"df = pro.cyq_perf(trade_date='20180104')\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "500292d5",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import time\n",
"\n",
"\n",
"trade_cal = pro.trade_cal(exchange='', start_date='20170101', end_date='20250820')\n",
"trade_cal = trade_cal[trade_cal['is_open'] == 1] # 只保留交易日\n",
"trade_dates = trade_cal['cal_date'].tolist()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ae3cb65",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✅ 日历文件已保存至: /mnt/d/PyProject/NewStock/data/qlib/calendar/day.txt\n",
"📅 共 2097 个交易日\n"
]
}
],
"source": [
"import os\n",
"\n",
"calendar_dir = \"/mnt/d/PyProject/NewStock/data/qlib/calendars\"\n",
"os.makedirs(calendar_dir, exist_ok=True) # 自动创建目录(包括父目录)\n",
"\n",
"# 排序为升序Qlib 要求日历按时间升序)\n",
"trade_dates_sorted = sorted(trade_dates)\n",
"\n",
"# 写入 day.txt\n",
"day_txt_path = os.path.join(calendar_dir, \"day.txt\")\n",
"with open(day_txt_path, \"w\") as f:\n",
" for date_str in trade_dates_sorted:\n",
" f.write(date_str + \"\\n\")\n",
"\n",
"print(f\"✅ 日历文件已保存至: {day_txt_path}\")\n",
"print(f\"📅 共 {len(trade_dates_sorted)} 个交易日\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7a6e529b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✅ all.txt 已生成,共 5685 只股票\n"
]
}
],
"source": [
"stocks_df = pd.read_csv('/mnt/d/PyProject/NewStock/stocks_list.csv', encoding='utf-8-sig')\n",
"\n",
"import os\n",
"\n",
"# 假设你有一个包含所有股票代码的列表(来自 stocks_df['ts_code']\n",
"# 例如:\n",
"# instrument_list = ['600000.SH', '000001.SZ', '300001.SZ', ...]\n",
"\n",
"instrument_list = stocks_df['ts_code'].unique().tolist()\n",
"\n",
"# 获取你的数据时间范围(从 trade_dates\n",
"start_date = min(trade_dates) # e.g., '20201106'\n",
"end_date = max(trade_dates) # e.g., '20210125'\n",
"\n",
"# 创建 instruments 目录\n",
"instr_dir = \"/mnt/d/PyProject/NewStock/data/qlib/instruments\"\n",
"os.makedirs(instr_dir, exist_ok=True)\n",
"\n",
"# 写入 all.txt\n",
"with open(os.path.join(instr_dir, \"all.txt\"), \"w\") as f:\n",
" for inst in instrument_list:\n",
" f.write(f\"{inst}\\t{start_date}\\t{end_date}\\n\")\n",
"\n",
"print(f\"✅ all.txt 已生成,共 {len(instrument_list)} 只股票\")"
]
}
],
"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
}