diff --git a/quantitative_data/.env.example b/quantitative_data/.env.example new file mode 100644 index 0000000..c78fb02 --- /dev/null +++ b/quantitative_data/.env.example @@ -0,0 +1,19 @@ +# ======================================== +# 量化数据库 — 环境变量配置模板 +# 复制此文件为 .env 并填入真实值 +# ======================================== + +# PostgreSQL 数据库连接 +QUANT_DB_HOST=192.168.27.15 +QUANT_DB_PORT=12345 +QUANT_DB_NAME=quant_db +QUANT_DB_USER=postgres +QUANT_DB_PASSWORD=your_password_here + +# Tushare API Token (在 https://tushare.pro 获取) +TUSHARE_TOKEN=your_tushare_token_here + +# 批量导入参数 (可选,已有默认值) +# QUANT_BATCH_SIZE=5000 +# QUANT_START_DATE=2010-01-01 +# QUANT_END_DATE=2025-12-31 \ No newline at end of file diff --git a/quantitative_data/.gitignore b/quantitative_data/.gitignore new file mode 100644 index 0000000..751b966 --- /dev/null +++ b/quantitative_data/.gitignore @@ -0,0 +1,17 @@ +# 环境变量文件 (包含敏感信息,不要提交到版本控制) +.env + +# Python +__pycache__/ +*.py[cod] +*.egg-info/ + +# 日志文件 +*.log + +# Jupyter +.ipynb_checkpoints/ + +# IDE +.vscode/ +.idea/ \ No newline at end of file diff --git a/quantitative_data/config.py b/quantitative_data/config.py index d1f9638..c4b137b 100644 --- a/quantitative_data/config.py +++ b/quantitative_data/config.py @@ -1,19 +1,30 @@ """ 量化数据库配置文件 + +敏感信息(数据库密码、API Token)通过环境变量管理: + - QUANT_DB_PASSWORD : 数据库密码 + - TUSHARE_TOKEN : Tushare API Token + +设置方式: + Windows: set QUANT_DB_PASSWORD=your_password && set TUSHARE_TOKEN=your_token + Linux: export QUANT_DB_PASSWORD=your_password && export TUSHARE_TOKEN=your_token + 也可创建 .env 文件(参考 .env.example) """ +import os + # PostgreSQL Docker 连接配置 DB_CONFIG = { - "host": "192.168.27.15", - "port": 12345, - "database": "quant_db", - "user": "postgres", - "password": "postgres", # 请修改为实际密码 + "host": os.environ.get("QUANT_DB_HOST", "192.168.27.15"), + "port": int(os.environ.get("QUANT_DB_PORT", "12345")), + "database": os.environ.get("QUANT_DB_NAME", "quant_db"), + "user": os.environ.get("QUANT_DB_USER", "postgres"), + "password": os.environ.get("QUANT_DB_PASSWORD", ""), } # Tushare API Token -TUSHARE_TOKEN = "你的token" # 请替换为你的tushare token +TUSHARE_TOKEN = os.environ.get("TUSHARE_TOKEN", "") # 批量导入参数 -BATCH_SIZE = 5000 # 每批次插入行数 -START_DATE = "2010-01-01" # 数据起始日期 -END_DATE = "2025-12-31" # 数据结束日期 \ No newline at end of file +BATCH_SIZE = int(os.environ.get("QUANT_BATCH_SIZE", "5000")) # 每批次插入行数 +START_DATE = os.environ.get("QUANT_START_DATE", "2010-01-01") # 数据起始日期 +END_DATE = os.environ.get("QUANT_END_DATE", "2025-12-31") # 数据结束日期 diff --git a/quantitative_data/数据批量导入.ipynb b/quantitative_data/数据批量导入.ipynb index 5476d3f..d22d592 100644 --- a/quantitative_data/数据批量导入.ipynb +++ b/quantitative_data/数据批量导入.ipynb @@ -26,8 +26,8 @@ "| index_daily | 指数日线行情 | index_daily |\n", "\n", "## 使用步骤\n", - "1. 修改 `config.py` 中的数据库密码和 Tushare Token\n", - "2. 逐 Cell 运行本 Notebook" + "1. 复制 `.env.example` 为 `.env`,填入真实密码和 Token\n", + "2. 逐 Cell 运行本 Notebook(敏感信息通过环境变量读取)" ] }, { @@ -124,7 +124,7 @@ " print(f\"✗ 连接失败: {e}\")\n", " print(\"请检查:\")\n", " print(\" 1. Docker 容器是否已启动: docker ps | findstr postgres\")\n", - " print(\" 2. config.py 中的连接参数是否正确\")\n", + " print(\" 2. 环境变量 (.env) 中的连接参数是否正确\")\n", " print(\" 3. 防火墙是否开放 12345 端口\")" ] }, @@ -143,7 +143,7 @@ " print(f\" 测试查询: {df.iloc[0].to_dict()}\")\n", "except Exception as e:\n", " print(f\"✗ Tushare API 连接失败: {e}\")\n", - " print(\"请检查 config.py 中的 TUSHARE_TOKEN 是否正确\")" + " print(\"请检查环境变量 TUSHARE_TOKEN 是否正确(或 .env 文件)\")" ] }, { @@ -214,7 +214,7 @@ "metadata": {}, "outputs": [], "source": [ - "# 导入交易日历 (默认从 config.py 的 START_DATE ~ END_DATE)\n", + "# 导入交易日历 (默认从环境变量 QUANT_START_DATE ~ QUANT_END_DATE)\n", "import_trade_cal()" ] },