Signed-off-by: TIANHE <TIANHE@GMAIL.COM>
This commit is contained in:
TIANHE
2025-12-29 03:12:20 +08:00
parent f43312a858
commit d5e987b8ee
3 changed files with 138 additions and 304 deletions
+97 -112
View File
@@ -1,138 +1,123 @@
# QuantDinger Python API Server
# QuantDinger Python API (backend)
Python 后端服务,提供金融数据获取、技术指标分析、AI 智能体分析和回测功能。
Flask-based local-first backend for QuantDinger: market data, indicators, AI analysis, backtesting, and a strategy runtime (with an optional pending-order worker).
## 项目结构
This repository is intentionally simple: **no external database is required by default**. Data is stored in a local SQLite file (`quantdinger.db`) created/updated automatically on startup.
```
## What you get
- **Multi-market data layer**: factory-based providers (crypto / US stocks / CN&HK stocks / futures, etc.)
- **Indicators + backtesting**: persisted runs/history in SQLite
- **AI multi-agent analysis**: optional web search + OpenRouter LLM integration
- **Strategy runtime**: thread-based executor, with optional auto-restore on startup
- **Pending orders worker (optional)**: polls queued orders and dispatches signals (webhook/notifications)
- **Local auth (single-user)**: `/login` with env-configured admin credentials (JWT)
## Project layout
```text
backend_api_python/
├─ app/ # 应用主目录
├── __init__.py # Flask 应用工厂
├── config/ # 配置模块 ⭐ (支持数据库动态配置)
│ ├── __init__.py # 配置统一导出
│ ├── settings.py # 服务配置
│ ├── api_keys.py # API 密钥集中管理
│ ├── database.py # 数据库/Redis/缓存配置
│ │ └── data_sources.py # 数据源配置
│ ├── data_sources/ # 数据源模块
├── base.py # 数据源基类
│ │ ├── factory.py # 数据源工厂
│ │ ├── crypto.py # 加密货币 (CCXT)
│ │ ├── us_stock.py # 美股 (yfinance/Finnhub)
│ │ ├── cn_stock.py # A股/港股 (yfinance/akshare)
│ │ └── futures.py # 期货数据源
│ ├── services/ # 业务服务层
│ │ ├── agents/ # AI 智能体系统 (多智能体架构) ⭐
│ │ │ ├── coordinator.py # 智能体协调器
│ │ │ ├── tools.py # 智能体工具集 (含搜索/数据获取)
│ │ │ ├── analyst_agents.py # 各类分析师 (技术/基本面/新闻等)
│ │ │ └── researcher_agents.py # 研究员 (多空辩论)
│ │ ├── kline.py # K线数据服务
│ │ ├── search.py # 搜索服务 (Google/Bing)
│ │ ├── llm.py # LLM 调用封装 (OpenRouter)
│ │ └── analysis.py # 分析服务入口
│ ├── routes/ # API 路由
│ │ ├── health.py # 健康检查
│ │ ├── kline.py # K线数据 API
│ │ ├── analysis.py # AI 分析 API
│ │ └── market.py # 市场数据 API
│ └── utils/ # 工具模块
│ ├── config_loader.py # 数据库配置加载器
│ ├── logger.py # 日志工具
│ └── http.py # HTTP 请求
├── run.py # 入口文件 ⭐
├── gunicorn_config.py # Gunicorn 配置
├── requirements.txt # 依赖列表
├── start.sh # 启动脚本
└── README.md
├─ app/
─ __init__.py # Flask app factory + startup hooks
─ config/ # Settings (env-driven)
├─ data_sources/ # Data sources + factory
├─ routes/ # REST endpoints
├─ services/ # Analysis, agents, strategies, search, ...
└─ utils/ # SQLite helpers, config loader, logging, HTTP utils
├─ env.example # Copy to .env for local config
├─ requirements.txt
├─ run.py # Entrypoint (loads .env, applies proxy env, starts Flask)
├─ gunicorn_config.py # Optional production config
└─ README.md
```
## 快速开始
## Quick start (local development)
### 1. 安装依赖
### Prerequisites
- Python 3.10+ recommended
### 1) Install dependencies
```bash
cd backend_api_python
pip install -r requirements.txt
```
### 2. 数据库配置
### 2) Create your local `.env`
本系统依赖 MySQL 数据库中的 `qd_addon_config` 表进行配置管理。请确保导入 `update_config_search.sql` 等 SQL 文件以初始化配置。
Windows (CMD):
### 3. 启动服务
```bash
copy env.example .env
```
Windows (PowerShell):
```bash
Copy-Item env.example .env
```
Then edit `.env` and set at least:
- `SECRET_KEY`
- `ADMIN_USER`
- `ADMIN_PASSWORD`
Optional but common:
- `OPENROUTER_API_KEY` (for AI analysis)
- `FINNHUB_API_KEY` / `SEARCH_GOOGLE_*` / `SEARCH_BING_API_KEY` (for richer data/search)
- `PROXY_PORT` or `PROXY_URL` (if your network blocks some providers)
### 3) Start the API server
**开发环境:**
```bash
python run.py
```
**生产环境(使用 Gunicorn):**
Default address: `http://localhost:5000`
## Database (SQLite)
- Default file: `backend_api_python/quantdinger.db` (override via `SQLITE_DATABASE_FILE`)
- Tables are created/updated automatically on startup (see `app/utils/db.py`)
- `qd_addon_config` exists for backward compatibility, but **this backend reads secrets from `.env` / OS env**, not from the database (see `app/utils/config_loader.py`)
## Frontend integration (Vue dev server)
The Vue dev server proxies `/api/*` to this backend by default:
- Frontend: `http://localhost:8000`
- Backend: `http://localhost:5000`
Proxy config: `quantdinger_vue/vue.config.js`
## Useful endpoints
```text
GET /health
POST /login
GET /info
GET /api/indicator/kline
POST /api/analysis/multi
```
## Production (optional)
Gunicorn example:
```bash
gunicorn -c gunicorn_config.py "run:app"
```
服务将在 `http://localhost:5000` 启动
## Troubleshooting
## 主要功能
### 1. 多维度 AI 分析
基于多智能体架构 (Multi-Agent Architecture),模拟真实投研团队:
- **市场分析师**: 技术面分析 (K线, MACD, RSI, 均线)
- **基本面分析师**: 公司财务、估值、行业地位
- **新闻分析师**: 实时新闻、舆情分析 (集成 Finnhub + Google/Bing 搜索)
- **情绪分析师**: 市场情绪评估
- **风险分析师**: 波动率、流动性风险评估
- **多空辩论**: 模拟看涨/看跌研究员辩论,提供平衡观点
### 2. 数据获取增强
- **美股**: 实时行情 (Finnhub/yfinance) + 深度公司资料
- **加密货币**: 实时行情 (CCXT/Binance) + 项目资讯搜索
- **A股/港股**: 延迟行情 + 网络资讯搜索补充
### 3. 智能搜索集成
支持 Google Custom Search 和 Bing Search,自动补全传统数据源缺失的信息(如公司简介、最新突发新闻)。
## API 接口
### 健康检查
```
GET /health
```
### K线数据
```
GET /api/indicator/kline
参数: market (Crypto/USStock/AShare), symbol, timeframe, limit
```
### AI 多维度分析
```
POST /api/analysis/multi
{
"market": "USStock",
"symbol": "NVDA",
"language": "zh-CN"
}
```
## 配置说明
系统配置采用 **数据库 + 环境变量** 混合模式,支持热更新。
### 核心环境变量 (启动参数)
| 变量名 | 说明 |
|--------|------|
| PYTHON_API_HOST | 监听地址 |
| PYTHON_API_PORT | 监听端口 |
| MYSQL_HOST | MySQL 主机 |
| REDIS_HOST | Redis 主机 |
### 数据库配置 (`qd_addon_config` 表)
大部分业务配置已迁移至数据库,支持动态调整:
- **API Keys**: Finnhub, Google Search, Bing Search
- **AI 模型**: OpenRouter 模型选择
- **系统参数**: 超时时间、重试次数、缓存策略
- If outbound data/search requests fail, configure `PROXY_PORT` (or `PROXY_URL`) in `.env`.
- If you dont want strategies to auto-restore on startup, set `DISABLE_RESTORE_RUNNING_STRATEGIES=true`.
- If you dont want the pending-order worker, set `ENABLE_PENDING_ORDER_WORKER=false`.
## License
This project is released under the Apache License 2.0.
See the repository root `LICENSE` for details.
Apache License 2.0. See repository root `LICENSE`.
+41 -82
View File
@@ -1,103 +1,62 @@
English | [简体中文](./README.zh-CN.md)
# QuantDinger Web UI (Vue 2)
<h1 align="center">Ant Design Vue Pro</h1>
<div align="center">
An out-of-box UI solution for enterprise applications as a Vue boilerplate. based on <a href="https://vuecomponent.github.io/ant-design-vue/docs/vue/introduce-cn/" target="_blank">Ant Design of Vue</a>
</div>
This is the QuantDinger frontend web UI built with **Vue 2** + **Ant Design Vue**. It connects to the Python backend (`backend_api_python/`) through HTTP APIs to provide charts, indicators, backtests, AI analysis, and strategy management.
<div align="center">
> This UI is based on the open-source `ant-design-vue-pro` ecosystem, heavily adapted for QuantDinger.
[![License](https://img.shields.io/npm/l/package.json.svg?style=flat)](https://github.com/vueComponent/ant-design-vue-pro/blob/master/LICENSE)
[![Release](https://img.shields.io/github/release/vueComponent/ant-design-vue-pro.svg?style=flat)](https://github.com/vueComponent/ant-design-vue-pro/releases/latest)
[![Support Vue Version](https://img.shields.io/badge/Support-Vue2-green?style=flat)](https://github.com/vueComponent/ant-design-vue-pro/releases/latest)
[![Travis branch](https://travis-ci.org/vueComponent/ant-design-vue-pro.svg?branch=master)](https://travis-ci.org/vueComponent/ant-design-vue-pro)
## What you get
</div>
- **Dashboards**: summary views and operational panels
- **Indicator analysis**: Kline charts + indicator editing + backtest history
- **AI analysis**: multi-agent reports (optional LLM/search, configured on backend)
- **Trading assistant**: strategy lifecycle + positions/records (depending on backend capability)
- **Local auth**: login with backend-configured admin credentials
- Preview: https://preview.pro.antdv.com
- Home Page: https://pro.antdv.com
- Documentation: https://pro.antdv.com/docs/getting-started
- ChangeLog: https://pro.antdv.com/docs/changelog
- FAQ: https://pro.antdv.com/docs/faq
- Vue3 ProLayout: https://github.com/vueComponent/pro-layout
## Quick start (local development)
Overview
----
### Prerequisites
![dashboard](https://static-2.loacg.com/open/static/github/SP1.png)
- Node.js 16+ recommended
- Backend running at `http://localhost:5000` (see `backend_api_python/README.md`)
### Env and dependencies
### 1) Install dependencies
- node
- yarn
- webpack
- eslint
- @vue/cli
- [ant-design-vue@1.x](https://github.com/vueComponent/ant-design-vue) - Ant Design Of Vue
- [vue-cropper](https://github.com/xyxiao001/vue-cropper) - Picture edit
- [@antv/g2](https://antv.alipay.com/zh-cn/index.html) - AntV G2
- [Viser-vue](https://viserjs.github.io/docs.html#/viser/guide/installation) - Antv/G2 of Vue
> Note: [Yarn](https://yarnpkg.com/) package management is recommended, the exact same version loaded with the demo site of this project (yarn.lock) . but you can also use npm
### Project setup
- Clone repo
```bash
git clone https://github.com/vueComponent/ant-design-vue-pro.git
cd ant-design-vue-pro
cd quantdinger_vue
npm install
```
- Install dependencies
```
yarn install
### 2) Start dev server
```bash
npm run serve
```
- Compiles and hot-reloads for development
```
yarn run serve
Dev server runs at `http://localhost:8000`.
### 3) API proxy (important)
In dev mode, this project proxies `/api/*` to the backend:
- Proxy config: `quantdinger_vue/vue.config.js`
- Default target: `http://localhost:5000`
If your backend runs on a different host/port, update `vue.config.js` accordingly.
## Production build
```bash
npm run build
```
- Compiles and minifies for production
```
yarn run build
```
The output will be generated under `quantdinger_vue/dist/`.
- Lints and fixes files
```
yarn run lint
```
## Notes
- **CORS**: when using the dev proxy, you typically dont need extra CORS config.
- **Login**: use the credentials defined in `backend_api_python/.env` (`ADMIN_USER` / `ADMIN_PASSWORD`).
### Other
## License
- **IMPORTANT : Issue feedback !! when opening Issue read [Issue / PR Contributing](https://github.com/vueComponent/ant-design-vue-pro/issues/90)**
Apache License 2.0. See repository root `LICENSE`.
- [Vue-cli3](https://cli.vuejs.org/guide/) used by the project.
- Disable Eslint (not recommended): remove `eslintConfig` field in `package.json` and `vue.config.js` field `lintOnSave: false`
- Load on Demand `/src/main.js` L14, in `import './core/lazy_use'`, `import './core/use''`. more [load-on-demand.md](./docs/load-on-demand.md)
- Customize Theme: [Custom Theme Config (@kokoroli)](https://github.com/kokoroli/antd-awesome/blob/master/docs/Ant_Design_%E6%A0%B7%E5%BC%8F%E8%A6%86%E7%9B%96.md)
- I18n: [locales (@musnow)](./src/locales/index.js)
- Production env `mock` is disabled. use `src/mock/index.js`
- pls use `release` version
## Browsers support
Modern browsers and IE10.
| [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>IE / Edge | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>Firefox | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>Chrome | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>Safari | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/opera/opera_48x48.png" alt="Opera" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>Opera |
| --- | --- | --- | --- | --- |
| IE10, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
## Contributors
This project exists thanks to all the people who contribute.
<a href="https://github.com/vueComponent/ant-design-vue-pro/graphs/contributors"><img src="https://opencollective.com/ant-design-pro-vue/contributors.svg?width=890&button=false" /></a>
-110
View File
@@ -1,110 +0,0 @@
[English](./README.md) | 简体中文
<h1 align="center">Ant Design Vue Pro</h1>
<div align="center">
An out-of-box UI solution for enterprise applications as a Vue boilerplate. based on <a href="https://vuecomponent.github.io/ant-design-vue/docs/vue/introduce-cn/" target="_blank">Ant Design of Vue</a>
</div>
<div align="center">
[![License](https://img.shields.io/npm/l/package.json.svg?style=flat)](https://github.com/vueComponent/ant-design-vue-pro/blob/master/LICENSE)
[![Release](https://img.shields.io/github/release/vueComponent/ant-design-vue-pro.svg?style=flat)](https://github.com/vueComponent/ant-design-vue-pro/releases/latest)
[![Support Vue Version](https://img.shields.io/badge/Support-Vue2-green?style=flat)](https://github.com/vueComponent/ant-design-vue-pro/releases/latest)
[![Travis branch](https://travis-ci.org/vueComponent/ant-design-vue-pro.svg?branch=master)](https://travis-ci.org/vueComponent/ant-design-vue-pro)
</div>
- 预览: https://preview.pro.antdv.com
- 首页: https://pro.antdv.com
- 文档: https://pro.antdv.com/docs/getting-started
- 更新日志: https://pro.antdv.com/docs/changelog
- 常见问题: https://pro.antdv.com/docs/faq
- Vue3 ProLayout: https://github.com/vueComponent/pro-layout
Overview
----
基于 [Ant Design of Vue](https://vuecomponent.github.io/ant-design-vue/docs/vue/introduce-cn/) 实现的 [Ant Design Pro](https://pro.ant.design/)
![dashboard](https://static-2.loacg.com/open/static/github/SP1.png)
环境和依赖
----
- node
- yarn
- webpack
- eslint
- @vue/cli
- [ant-design-vue@1.x](https://github.com/vueComponent/ant-design-vue) - Ant Design Of Vue 实现
- [vue-cropper](https://github.com/xyxiao001/vue-cropper) - 头像裁剪组件
- [@antv/g2](https://antv.alipay.com/zh-cn/index.html) - Alipay AntV 数据可视化图表
- [Viser-vue](https://viserjs.github.io/docs.html#/viser/guide/installation) - antv/g2 封装实现
> 请注意,我们强烈建议本项目使用 [Yarn](https://yarnpkg.com/) 包管理工具,这样可以与本项目演示站所加载完全相同的依赖版本 (yarn.lock) 。由于我们没有对依赖进行强制的版本控制,采用非 yarn 包管理进行引入时,可能由于 Pro 所依赖的库已经升级版本而引入了新版本所导致的问题。作者可能会由于时间问题无法及时排查而导致您采用本项目作为基项目而出现问题。
项目下载和运行
----
- 拉取项目代码
```bash
git clone https://github.com/vueComponent/ant-design-vue-pro.git
cd ant-design-vue-pro
```
- 安装依赖
```
yarn install
```
- 开发模式运行
```
yarn run serve
```
- 编译项目
```
yarn run build
```
- Lints and fixes files
```
yarn run lint
```
其他说明
----
- **关于 Issue 反馈 (重要!重要!重要!) 请在开 *Issue* 前,先阅读该内容:[Issue / PR 编写建议](https://github.com/vueComponent/ant-design-vue-pro/issues/90)**
- 项目使用的 [vue-cli3](https://cli.vuejs.org/guide/), 请确保你所使用的 vue-cli 是新版,并且已经学习 cli 官方文档使用教程
- 关闭 Eslint (不推荐) 移除 `package.json``eslintConfig` 整个节点代码, `vue.config.js` 下的 `lintOnSave` 值改为 `false`
- 组件按需加载 `/src/main.js` L14 相关代码 `import './core/lazy_use'` / `import './core/use'`
- [修改 Ant Design 配色 (@kokoroli)](https://github.com/kokoroli/antd-awesome/blob/master/docs/Ant_Design_%E6%A0%B7%E5%BC%8F%E8%A6%86%E7%9B%96.md)
- I18n: [多语言支持 (@musnow)](./src/locales/index.js)
- 生产环境默认不加载 `mock`,更多详情请看 `src/mock/index.js`
- **用于生产环境,请使用 `release` 版本代码,使用 master 代码出现的任何问题需要你自行解决**
## 浏览器兼容
Modern browsers and IE10.
| [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>IE / Edge | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>Firefox | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>Chrome | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>Safari | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/opera/opera_48x48.png" alt="Opera" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)</br>Opera |
| --- | --- | --- | --- | --- |
| IE10, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
## Contributors
This project exists thanks to all the people who contribute.
<a href="https://github.com/vueComponent/ant-design-vue-pro/graphs/contributors"><img src="https://opencollective.com/ant-design-pro-vue/contributors.svg?width=890&button=false" /></a>