Files
DinQuant/backend_api_python/README.md
T

137 lines
4.1 KiB
Markdown
Raw Normal View History

2025-12-29 03:12:20 +08:00
# QuantDinger Python API (backend)
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
Flask-based local-first backend for QuantDinger: market data, indicators, AI analysis, backtesting, and a strategy runtime (with an optional pending-order worker).
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
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.
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
## 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
2025-12-29 03:06:49 +08:00
backend_api_python/
2025-12-29 03:12:20 +08:00
├─ 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
2025-12-29 03:06:49 +08:00
```
2025-12-29 03:12:20 +08:00
## Quick start (local development)
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
### Prerequisites
- Python 3.10+ recommended
### 1) Install dependencies
2025-12-29 03:06:49 +08:00
```bash
2025-12-29 03:12:20 +08:00
cd backend_api_python
2025-12-29 03:06:49 +08:00
pip install -r requirements.txt
```
2025-12-29 03:12:20 +08:00
### 2) Create your local `.env`
Windows (CMD):
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
```bash
copy env.example .env
```
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
Windows (PowerShell):
2025-12-29 03:06:49 +08:00
```bash
2025-12-29 03:12:20 +08:00
Copy-Item env.example .env
2025-12-29 03:06:49 +08:00
```
2025-12-29 03:12:20 +08:00
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
2025-12-29 03:06:49 +08:00
```bash
2025-12-29 03:12:20 +08:00
python run.py
2025-12-29 03:06:49 +08:00
```
2025-12-29 03:12:20 +08:00
Default address: `http://localhost:5000`
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
## Database (SQLite)
2025-12-29 03:06:49 +08:00
2025-12-30 21:02:38 +08:00
- Default file: `backend_api_python/data/quantdinger.db` (override via `SQLITE_DATABASE_FILE`)
2025-12-29 03:12:20 +08:00
- 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`)
2025-12-29 03:06:49 +08:00
2025-12-30 21:02:38 +08:00
## AI memory augmentation (local-only)
This backend includes a lightweight, privacy-first **memory-augmented multi-agent** system:
- Memory DBs (per role): `backend_api_python/data/memory/*_memory.db`
- Reflection DB (optional auto-verify loop): `backend_api_python/data/memory/reflection_records.db`
- API hooks:
- `POST /api/analysis/multi` (main entry)
- `POST /api/analysis/reflect` (manual learn from post-trade outcomes)
- Controls: see `.env` / `env.example`:
- `ENABLE_AGENT_MEMORY`, `AGENT_MEMORY_*`
- `ENABLE_REFLECTION_WORKER`, `REFLECTION_WORKER_INTERVAL_SEC`
2025-12-29 03:12:20 +08:00
## Frontend integration (Vue dev server)
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
The Vue dev server proxies `/api/*` to this backend by default:
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
- Frontend: `http://localhost:8000`
- Backend: `http://localhost:5000`
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
Proxy config: `quantdinger_vue/vue.config.js`
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
## Useful endpoints
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
```text
GET /health
POST /login
GET /info
GET /api/indicator/kline
2025-12-29 03:06:49 +08:00
POST /api/analysis/multi
```
2025-12-29 03:12:20 +08:00
## Production (optional)
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
Gunicorn example:
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
```bash
gunicorn -c gunicorn_config.py "run:app"
```
2025-12-29 03:06:49 +08:00
2025-12-29 03:12:20 +08:00
## Troubleshooting
- 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`.
2025-12-29 03:06:49 +08:00
## License
2025-12-29 03:12:20 +08:00
Apache License 2.0. See repository root `LICENSE`.