重构期货监控系统:多页面导航、开单计划、Ubuntu PM2 部署
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
# 服务配置
|
||||||
|
HOST=0.0.0.0
|
||||||
|
PORT=6600
|
||||||
|
DEBUG=false
|
||||||
|
|
||||||
|
# Flask Session 密钥(部署时请改为随机字符串)
|
||||||
|
SECRET_KEY=change-this-to-a-random-secret-key
|
||||||
|
|
||||||
|
# 初始管理员账号(仅首次初始化数据库时使用)
|
||||||
|
ADMIN_USERNAME=admin
|
||||||
|
ADMIN_PASSWORD=change-me-on-first-login
|
||||||
|
|
||||||
|
# 企业微信 Webhook(也可在系统设置页面修改)
|
||||||
|
WECHAT_WEBHOOK=
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
.env
|
||||||
|
*.db
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
.Python
|
||||||
|
venv/
|
||||||
|
.venv/
|
||||||
|
*.log
|
||||||
|
.DS_Store
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
# 国内期货交易监控复盘系统
|
||||||
|
|
||||||
|
基于 Flask 的国内期货监控与复盘 Web 应用,支持开单计划、关键位监控、止盈止损自动跟踪、企业微信推送与统计分析。
|
||||||
|
|
||||||
|
## 功能模块
|
||||||
|
|
||||||
|
| 模块 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| **开单计划** | 品种、方向、决策区间、止损/止盈;价格进入区间后激活并推送 |
|
||||||
|
| **关键位监控** | 箱体/收敛突破、阻力/支撑位突破提醒(触发后去重) |
|
||||||
|
| **交易记录与复盘** | 自动记录止盈/止损结果 |
|
||||||
|
| **统计分析** | 总交易、胜率,按品种/类型/方向统计 |
|
||||||
|
| **系统设置** | 修改密码、配置企业微信 Webhook |
|
||||||
|
|
||||||
|
## 品种输入
|
||||||
|
|
||||||
|
在各表单中输入中文品种名(如「白银」「螺纹钢」),自动联想新浪行情代码及主力合约标识。
|
||||||
|
|
||||||
|
## 环境要求
|
||||||
|
|
||||||
|
- Ubuntu 20.04+(推荐)
|
||||||
|
- Python 3.10+
|
||||||
|
- Node.js(仅 PM2 进程守护需要)
|
||||||
|
- 网络可访问 `hq.sinajs.cn`(行情)及企业微信 API
|
||||||
|
|
||||||
|
## 快速部署(Ubuntu + /opt)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 克隆仓库后,在项目目录执行一键部署
|
||||||
|
sudo bash deploy.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
默认安装路径:`/opt/qihuo`,服务端口:`6600`。
|
||||||
|
|
||||||
|
部署完成后访问:`http://服务器IP:6600`
|
||||||
|
|
||||||
|
## 手动部署
|
||||||
|
|
||||||
|
### 1. 安装系统依赖
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install -y python3 python3-venv python3-pip git
|
||||||
|
# PM2(进程守护)
|
||||||
|
curl -fsSL https://get.pnpm.io/install.sh | sh - # 或直接用 npm
|
||||||
|
sudo npm install -g pm2
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 克隆到 /opt
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo mkdir -p /opt
|
||||||
|
sudo git clone https://git.bz121.com/dekun/qihuo.git /opt/qihuo
|
||||||
|
sudo chown -R $USER:$USER /opt/qihuo
|
||||||
|
cd /opt/qihuo
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 虚拟环境与依赖
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 -m venv venv
|
||||||
|
source venv/bin/activate
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. 配置环境变量
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cp .env.example .env
|
||||||
|
# 编辑 .env,至少修改 SECRET_KEY 和 ADMIN_PASSWORD
|
||||||
|
nano .env
|
||||||
|
```
|
||||||
|
|
||||||
|
`.env` 主要字段:
|
||||||
|
|
||||||
|
```env
|
||||||
|
HOST=0.0.0.0
|
||||||
|
PORT=6600
|
||||||
|
SECRET_KEY=随机长字符串
|
||||||
|
ADMIN_USERNAME=admin
|
||||||
|
ADMIN_PASSWORD=首次登录密码
|
||||||
|
WECHAT_WEBHOOK=企业微信机器人地址(可选,也可在页面配置)
|
||||||
|
```
|
||||||
|
|
||||||
|
> 管理员密码首次从 `.env` 写入数据库并哈希存储,之后请在「系统设置」中修改。
|
||||||
|
|
||||||
|
### 5. PM2 启动
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pm2 start ecosystem.config.cjs
|
||||||
|
pm2 save
|
||||||
|
pm2 startup # 按提示执行生成的命令,实现开机自启
|
||||||
|
```
|
||||||
|
|
||||||
|
### 6. 常用 PM2 命令
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pm2 status
|
||||||
|
pm2 logs qihuo
|
||||||
|
pm2 restart qihuo
|
||||||
|
pm2 stop qihuo
|
||||||
|
```
|
||||||
|
|
||||||
|
## 本地开发
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 -m venv venv
|
||||||
|
source venv/bin/activate
|
||||||
|
pip install -r requirements.txt
|
||||||
|
cp .env.example .env
|
||||||
|
python app.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## 目录结构
|
||||||
|
|
||||||
|
```
|
||||||
|
qihuo/
|
||||||
|
├── app.py # 主程序
|
||||||
|
├── symbols.py # 期货品种映射
|
||||||
|
├── requirements.txt
|
||||||
|
├── .env.example
|
||||||
|
├── deploy.sh # Ubuntu 一键部署
|
||||||
|
├── ecosystem.config.cjs # PM2 配置
|
||||||
|
├── static/js/symbol.js # 品种联想
|
||||||
|
├── templates/ # 页面模板
|
||||||
|
└── futures.db # SQLite 数据库(运行后生成)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 监控逻辑说明
|
||||||
|
|
||||||
|
### 开单计划
|
||||||
|
|
||||||
|
1. **待触发**:当前价进入「决策区间 [下限, 上限]」→ 企业微信通知,状态变为「已激活」
|
||||||
|
2. **已激活**:监控止盈止损直至触发,写入交易记录,计划关闭
|
||||||
|
|
||||||
|
### 关键位监控
|
||||||
|
|
||||||
|
- 箱体/收敛:突破上沿或跌破下沿各推送一次
|
||||||
|
- 阻力/支撑:单向突破推送一次
|
||||||
|
|
||||||
|
后台线程每 3 秒轮询行情。
|
||||||
|
|
||||||
|
## 安全建议
|
||||||
|
|
||||||
|
- 部署后立即修改默认密码
|
||||||
|
- 勿将 `.env` 提交到仓库
|
||||||
|
- 生产环境建议用 Nginx 反代并配置 HTTPS
|
||||||
|
- 限制 6600 端口仅内网或 VPN 访问
|
||||||
|
|
||||||
|
## 仓库地址
|
||||||
|
|
||||||
|
https://git.bz121.com/dekun/qihuo.git
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Private / 个人使用
|
||||||
@@ -0,0 +1,519 @@
|
|||||||
|
import os
|
||||||
|
import sqlite3
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
import requests
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from flask import (
|
||||||
|
Flask, render_template, request, redirect, url_for,
|
||||||
|
flash, session, jsonify,
|
||||||
|
)
|
||||||
|
from werkzeug.security import check_password_hash, generate_password_hash
|
||||||
|
|
||||||
|
from symbols import search_symbols, get_by_code
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.secret_key = os.getenv("SECRET_KEY", "futures_monitor_default_secret")
|
||||||
|
|
||||||
|
HOST = os.getenv("HOST", "0.0.0.0")
|
||||||
|
PORT = int(os.getenv("PORT", "6600"))
|
||||||
|
DEBUG = os.getenv("DEBUG", "false").lower() in ("1", "true", "yes")
|
||||||
|
|
||||||
|
DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "futures.db")
|
||||||
|
|
||||||
|
# —————————————— 设置读写 ——————————————
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
def get_setting(key: str, default: str = "") -> str:
|
||||||
|
conn = get_db()
|
||||||
|
row = conn.execute("SELECT value FROM settings WHERE key=?", (key,)).fetchone()
|
||||||
|
conn.close()
|
||||||
|
return row["value"] if row else default
|
||||||
|
|
||||||
|
|
||||||
|
def set_setting(key: str, value: str):
|
||||||
|
conn = get_db()
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO settings (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value=?",
|
||||||
|
(key, value, value),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def init_db():
|
||||||
|
conn = get_db()
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute("CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value TEXT)")
|
||||||
|
c.execute('''CREATE TABLE IF NOT EXISTS order_plans
|
||||||
|
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
symbol TEXT, symbol_name TEXT, direction TEXT,
|
||||||
|
zone_upper REAL, zone_lower REAL,
|
||||||
|
stop_loss REAL, take_profit REAL,
|
||||||
|
status TEXT DEFAULT "planned",
|
||||||
|
triggered_at TIMESTAMP,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
|
||||||
|
c.execute('''CREATE TABLE IF NOT EXISTS key_monitors
|
||||||
|
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
symbol TEXT, symbol_name TEXT, monitor_type TEXT, direction TEXT,
|
||||||
|
upper REAL, lower REAL,
|
||||||
|
upper_triggered INTEGER DEFAULT 0,
|
||||||
|
lower_triggered INTEGER DEFAULT 0,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
|
||||||
|
c.execute('''CREATE TABLE IF NOT EXISTS trade_records
|
||||||
|
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
symbol TEXT, symbol_name TEXT, monitor_type TEXT, direction TEXT,
|
||||||
|
trigger_price REAL, stop_loss REAL, take_profit REAL,
|
||||||
|
result TEXT,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
|
||||||
|
migrations = [
|
||||||
|
"ALTER TABLE key_monitors ADD COLUMN symbol_name TEXT",
|
||||||
|
"ALTER TABLE key_monitors ADD COLUMN upper_triggered INTEGER DEFAULT 0",
|
||||||
|
"ALTER TABLE key_monitors ADD COLUMN lower_triggered INTEGER DEFAULT 0",
|
||||||
|
"ALTER TABLE trade_records ADD COLUMN symbol_name TEXT",
|
||||||
|
]
|
||||||
|
for sql in migrations:
|
||||||
|
try:
|
||||||
|
c.execute(sql)
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
pass
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if not get_setting("admin_username"):
|
||||||
|
username = os.getenv("ADMIN_USERNAME", "admin")
|
||||||
|
password = os.getenv("ADMIN_PASSWORD", "admin123")
|
||||||
|
set_setting("admin_username", username)
|
||||||
|
set_setting("admin_password_hash", generate_password_hash(password))
|
||||||
|
|
||||||
|
if not get_setting("wechat_webhook") and os.getenv("WECHAT_WEBHOOK"):
|
||||||
|
set_setting("wechat_webhook", os.getenv("WECHAT_WEBHOOK"))
|
||||||
|
|
||||||
|
|
||||||
|
init_db()
|
||||||
|
|
||||||
|
# —————————————— 推送 ——————————————
|
||||||
|
|
||||||
|
def send_wechat_msg(content: str):
|
||||||
|
webhook = get_setting("wechat_webhook")
|
||||||
|
if not webhook:
|
||||||
|
return
|
||||||
|
full = f"【国内期货】\n{content}"
|
||||||
|
data = {"msgtype": "text", "text": {"content": full}}
|
||||||
|
try:
|
||||||
|
requests.post(webhook, json=data, timeout=10)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# —————————————— 行情 ——————————————
|
||||||
|
|
||||||
|
def get_price(symbol: str) -> Optional[float]:
|
||||||
|
try:
|
||||||
|
url = f"https://hq.sinajs.cn/list={symbol}"
|
||||||
|
headers = {"Referer": "https://finance.sina.com.cn"}
|
||||||
|
resp = requests.get(url, headers=headers, timeout=5)
|
||||||
|
text = resp.text
|
||||||
|
if "=" not in text:
|
||||||
|
return None
|
||||||
|
data = text.split("=")[1].strip().strip('"').split(",")
|
||||||
|
if len(data) < 9:
|
||||||
|
return None
|
||||||
|
return float(data[8])
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# —————————————— 监控逻辑 ——————————————
|
||||||
|
|
||||||
|
def check_order_plans():
|
||||||
|
conn = get_db()
|
||||||
|
rows = conn.execute(
|
||||||
|
"SELECT * FROM order_plans WHERE status IN ('planned', 'active')"
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
for r in rows:
|
||||||
|
sym = r["symbol"]
|
||||||
|
p = get_price(sym)
|
||||||
|
if not p:
|
||||||
|
continue
|
||||||
|
|
||||||
|
direction = r["direction"]
|
||||||
|
zone_upper = r["zone_upper"]
|
||||||
|
zone_lower = r["zone_lower"]
|
||||||
|
stop_loss = r["stop_loss"]
|
||||||
|
take_profit = r["take_profit"]
|
||||||
|
status = r["status"]
|
||||||
|
pid = r["id"]
|
||||||
|
name = r["symbol_name"] or sym
|
||||||
|
|
||||||
|
# 计划状态:价格进入决策区间则激活并通知
|
||||||
|
if status == "planned":
|
||||||
|
in_zone = zone_lower <= p <= zone_upper
|
||||||
|
if in_zone:
|
||||||
|
msg = (
|
||||||
|
f"【开单计划触发】{name} ({sym})\n"
|
||||||
|
f"方向:{'做多' if direction == 'long' else '做空'}\n"
|
||||||
|
f"决策区间:{zone_lower} ~ {zone_upper}\n"
|
||||||
|
f"当前价:{p}\n"
|
||||||
|
f"止损:{stop_loss} 止盈:{take_profit}"
|
||||||
|
)
|
||||||
|
send_wechat_msg(msg)
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE order_plans SET status='active', triggered_at=? WHERE id=?",
|
||||||
|
(datetime.now().isoformat(), pid),
|
||||||
|
)
|
||||||
|
status = "active"
|
||||||
|
|
||||||
|
# 激活状态:监控止盈止损
|
||||||
|
if status == "active":
|
||||||
|
res = None
|
||||||
|
if direction == "long":
|
||||||
|
if p >= take_profit:
|
||||||
|
res = "止盈"
|
||||||
|
elif p <= stop_loss:
|
||||||
|
res = "止损"
|
||||||
|
elif direction == "short":
|
||||||
|
if p <= take_profit:
|
||||||
|
res = "止盈"
|
||||||
|
elif p >= stop_loss:
|
||||||
|
res = "止损"
|
||||||
|
|
||||||
|
if res:
|
||||||
|
msg = (
|
||||||
|
f"[{'做多' if direction == 'long' else '做空'}] {name} 已{res}\n"
|
||||||
|
f"决策区间:{zone_lower} ~ {zone_upper}\n"
|
||||||
|
f"止损:{stop_loss} 止盈:{take_profit}\n"
|
||||||
|
f"当前价:{p}"
|
||||||
|
)
|
||||||
|
send_wechat_msg(msg)
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO trade_records
|
||||||
|
(symbol, symbol_name, monitor_type, direction,
|
||||||
|
trigger_price, stop_loss, take_profit, result)
|
||||||
|
VALUES (?,?,?,?,?,?,?,?)""",
|
||||||
|
(sym, name, "开单计划", direction, p, stop_loss, take_profit, res),
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE order_plans SET status='closed' WHERE id=?", (pid,)
|
||||||
|
)
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def check_key_monitors():
|
||||||
|
conn = get_db()
|
||||||
|
rows = conn.execute("SELECT * FROM key_monitors").fetchall()
|
||||||
|
|
||||||
|
for r in rows:
|
||||||
|
sym = r["symbol"]
|
||||||
|
typ = r["monitor_type"]
|
||||||
|
up = r["upper"]
|
||||||
|
low = r["lower"]
|
||||||
|
up_trig = r["upper_triggered"]
|
||||||
|
low_trig = r["lower_triggered"]
|
||||||
|
name = r["symbol_name"] or sym
|
||||||
|
pid = r["id"]
|
||||||
|
|
||||||
|
p = get_price(sym)
|
||||||
|
if not p:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if typ in ("箱体突破", "收敛突破"):
|
||||||
|
if p > up and not up_trig:
|
||||||
|
send_wechat_msg(f"{name} 突破{typ}上沿 {up}\n当前价:{p}")
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE key_monitors SET upper_triggered=1 WHERE id=?", (pid,)
|
||||||
|
)
|
||||||
|
if p < low and not low_trig:
|
||||||
|
send_wechat_msg(f"{name} 跌破{typ}下沿 {low}\n当前价:{p}")
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE key_monitors SET lower_triggered=1 WHERE id=?", (pid,)
|
||||||
|
)
|
||||||
|
elif typ == "关键阻力位" and p > up and not up_trig:
|
||||||
|
send_wechat_msg(f"{name} 突破阻力位 {up}\n当前价:{p}")
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE key_monitors SET upper_triggered=1 WHERE id=?", (pid,)
|
||||||
|
)
|
||||||
|
elif typ == "关键支撑位" and p < low and not low_trig:
|
||||||
|
send_wechat_msg(f"{name} 跌破支撑位 {low}\n当前价:{p}")
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE key_monitors SET lower_triggered=1 WHERE id=?", (pid,)
|
||||||
|
)
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def background_task():
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
check_key_monitors()
|
||||||
|
check_order_plans()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
|
# —————————————— 登录 ——————————————
|
||||||
|
|
||||||
|
def login_required(f):
|
||||||
|
@wraps(f)
|
||||||
|
def wrap(*args, **kwargs):
|
||||||
|
if not session.get("logged_in"):
|
||||||
|
return redirect(url_for("login"))
|
||||||
|
return f(*args, **kwargs)
|
||||||
|
return wrap
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/login", methods=["GET", "POST"])
|
||||||
|
def login():
|
||||||
|
if request.method == "POST":
|
||||||
|
u = request.form.get("username", "").strip()
|
||||||
|
p = request.form.get("password", "")
|
||||||
|
admin_u = get_setting("admin_username")
|
||||||
|
admin_hash = get_setting("admin_password_hash")
|
||||||
|
if u == admin_u and check_password_hash(admin_hash, p):
|
||||||
|
session["logged_in"] = True
|
||||||
|
session["username"] = u
|
||||||
|
return redirect(url_for("plans"))
|
||||||
|
flash("账号或密码错误")
|
||||||
|
return render_template("login.html")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/logout")
|
||||||
|
def logout():
|
||||||
|
session.clear()
|
||||||
|
return redirect(url_for("login"))
|
||||||
|
|
||||||
|
# —————————————— API ——————————————
|
||||||
|
|
||||||
|
@app.route("/api/symbols/search")
|
||||||
|
@login_required
|
||||||
|
def api_symbol_search():
|
||||||
|
q = request.args.get("q", "")
|
||||||
|
return jsonify(search_symbols(q))
|
||||||
|
|
||||||
|
# —————————————— 页面路由 ——————————————
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
@login_required
|
||||||
|
def index():
|
||||||
|
return redirect(url_for("plans"))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/plans")
|
||||||
|
@login_required
|
||||||
|
def plans():
|
||||||
|
conn = get_db()
|
||||||
|
plan_list = conn.execute(
|
||||||
|
"SELECT * FROM order_plans WHERE status != 'closed' ORDER BY id DESC"
|
||||||
|
).fetchall()
|
||||||
|
closed = conn.execute(
|
||||||
|
"SELECT * FROM order_plans WHERE status='closed' ORDER BY id DESC LIMIT 20"
|
||||||
|
).fetchall()
|
||||||
|
conn.close()
|
||||||
|
return render_template("plans.html", plans=plan_list, closed=closed)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/add_plan", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def add_plan():
|
||||||
|
d = request.form
|
||||||
|
direction = d.get("direction")
|
||||||
|
symbol = d.get("symbol", "").strip()
|
||||||
|
symbol_name = d.get("symbol_name", "").strip()
|
||||||
|
if not direction:
|
||||||
|
flash("请选择多空方向")
|
||||||
|
return redirect(url_for("plans"))
|
||||||
|
if not symbol:
|
||||||
|
flash("请选择有效品种")
|
||||||
|
return redirect(url_for("plans"))
|
||||||
|
conn = get_db()
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO order_plans
|
||||||
|
(symbol, symbol_name, direction, zone_upper, zone_lower, stop_loss, take_profit)
|
||||||
|
VALUES (?,?,?,?,?,?,?)""",
|
||||||
|
(
|
||||||
|
symbol, symbol_name, direction,
|
||||||
|
float(d["zone_upper"]), float(d["zone_lower"]),
|
||||||
|
float(d["stop_loss"]), float(d["take_profit"]),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
flash("开单计划已添加")
|
||||||
|
return redirect(url_for("plans"))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/del_plan/<int:pid>")
|
||||||
|
@login_required
|
||||||
|
def del_plan(pid):
|
||||||
|
conn = get_db()
|
||||||
|
conn.execute("DELETE FROM order_plans WHERE id=?", (pid,))
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
flash("已删除")
|
||||||
|
return redirect(url_for("plans"))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/keys")
|
||||||
|
@login_required
|
||||||
|
def keys():
|
||||||
|
conn = get_db()
|
||||||
|
key_list = conn.execute("SELECT * FROM key_monitors ORDER BY id DESC").fetchall()
|
||||||
|
conn.close()
|
||||||
|
return render_template("keys.html", keys=key_list)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/add_key", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def add_key():
|
||||||
|
d = request.form
|
||||||
|
direction = d.get("direction")
|
||||||
|
symbol = d.get("symbol", "").strip()
|
||||||
|
symbol_name = d.get("symbol_name", "").strip()
|
||||||
|
if not direction:
|
||||||
|
flash("请选择多空方向")
|
||||||
|
return redirect(url_for("keys"))
|
||||||
|
if not symbol:
|
||||||
|
flash("请选择有效品种")
|
||||||
|
return redirect(url_for("keys"))
|
||||||
|
conn = get_db()
|
||||||
|
conn.execute(
|
||||||
|
"""INSERT INTO key_monitors
|
||||||
|
(symbol, symbol_name, monitor_type, direction, upper, lower)
|
||||||
|
VALUES (?,?,?,?,?,?)""",
|
||||||
|
(symbol, symbol_name, d["type"], direction, float(d["upper"]), float(d["lower"])),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
flash("关键位监控已添加")
|
||||||
|
return redirect(url_for("keys"))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/del_key/<int:pid>")
|
||||||
|
@login_required
|
||||||
|
def del_key(pid):
|
||||||
|
conn = get_db()
|
||||||
|
conn.execute("DELETE FROM key_monitors WHERE id=?", (pid,))
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
flash("已删除")
|
||||||
|
return redirect(url_for("keys"))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/records")
|
||||||
|
@login_required
|
||||||
|
def records():
|
||||||
|
conn = get_db()
|
||||||
|
record_list = conn.execute(
|
||||||
|
"SELECT * FROM trade_records ORDER BY id DESC"
|
||||||
|
).fetchall()
|
||||||
|
conn.close()
|
||||||
|
return render_template("records.html", records=record_list)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/del_record/<int:rid>")
|
||||||
|
@login_required
|
||||||
|
def del_record(rid):
|
||||||
|
conn = get_db()
|
||||||
|
conn.execute("DELETE FROM trade_records WHERE id=?", (rid,))
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
flash("已删除")
|
||||||
|
return redirect(url_for("records"))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/stats")
|
||||||
|
@login_required
|
||||||
|
def stats():
|
||||||
|
conn = get_db()
|
||||||
|
total = conn.execute(
|
||||||
|
"SELECT COUNT(*) FROM trade_records WHERE result IN ('止盈','止损')"
|
||||||
|
).fetchone()[0]
|
||||||
|
win = conn.execute(
|
||||||
|
"SELECT COUNT(*) FROM trade_records WHERE result='止盈'"
|
||||||
|
).fetchone()[0]
|
||||||
|
loss = conn.execute(
|
||||||
|
"SELECT COUNT(*) FROM trade_records WHERE result='止损'"
|
||||||
|
).fetchone()[0]
|
||||||
|
rate = round(win / total * 100, 2) if total else 0
|
||||||
|
|
||||||
|
by_symbol = conn.execute(
|
||||||
|
"""SELECT symbol_name, symbol, COUNT(*) as cnt,
|
||||||
|
SUM(CASE WHEN result='止盈' THEN 1 ELSE 0 END) as wins
|
||||||
|
FROM trade_records WHERE result IN ('止盈','止损')
|
||||||
|
GROUP BY symbol ORDER BY cnt DESC"""
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
by_type = conn.execute(
|
||||||
|
"""SELECT monitor_type, COUNT(*) as cnt,
|
||||||
|
SUM(CASE WHEN result='止盈' THEN 1 ELSE 0 END) as wins
|
||||||
|
FROM trade_records WHERE result IN ('止盈','止损')
|
||||||
|
GROUP BY monitor_type ORDER BY cnt DESC"""
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
by_direction = conn.execute(
|
||||||
|
"""SELECT direction, COUNT(*) as cnt,
|
||||||
|
SUM(CASE WHEN result='止盈' THEN 1 ELSE 0 END) as wins
|
||||||
|
FROM trade_records WHERE result IN ('止盈','止损')
|
||||||
|
GROUP BY direction"""
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
recent = conn.execute(
|
||||||
|
"SELECT * FROM trade_records ORDER BY id DESC LIMIT 10"
|
||||||
|
).fetchall()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
"stats.html",
|
||||||
|
total=total, win=win, loss=loss, rate=rate,
|
||||||
|
by_symbol=by_symbol, by_type=by_type, by_direction=by_direction,
|
||||||
|
recent=recent,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/settings", methods=["GET", "POST"])
|
||||||
|
@login_required
|
||||||
|
def settings():
|
||||||
|
if request.method == "POST":
|
||||||
|
action = request.form.get("action")
|
||||||
|
if action == "wechat":
|
||||||
|
webhook = request.form.get("wechat_webhook", "").strip()
|
||||||
|
set_setting("wechat_webhook", webhook)
|
||||||
|
flash("企业微信配置已保存")
|
||||||
|
elif action == "password":
|
||||||
|
old_p = request.form.get("old_password", "")
|
||||||
|
new_p = request.form.get("new_password", "")
|
||||||
|
new_p2 = request.form.get("new_password2", "")
|
||||||
|
admin_hash = get_setting("admin_password_hash")
|
||||||
|
if not check_password_hash(admin_hash, old_p):
|
||||||
|
flash("原密码错误")
|
||||||
|
elif len(new_p) < 6:
|
||||||
|
flash("新密码至少 6 位")
|
||||||
|
elif new_p != new_p2:
|
||||||
|
flash("两次新密码不一致")
|
||||||
|
else:
|
||||||
|
set_setting("admin_password_hash", generate_password_hash(new_p))
|
||||||
|
flash("密码修改成功")
|
||||||
|
return redirect(url_for("settings"))
|
||||||
|
|
||||||
|
webhook = get_setting("wechat_webhook")
|
||||||
|
username = get_setting("admin_username")
|
||||||
|
return render_template("settings.html", webhook=webhook, username=username)
|
||||||
|
|
||||||
|
# —————————————— 启动 ——————————————
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
threading.Thread(target=background_task, daemon=True).start()
|
||||||
|
app.run(host=HOST, port=PORT, debug=DEBUG)
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# 国内期货监控系统 - Ubuntu 一键部署脚本
|
||||||
|
# 安装路径: /opt/qihuo 端口: 6600 进程守护: PM2
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
APP_DIR="/opt/qihuo"
|
||||||
|
REPO_URL="https://git.bz121.com/dekun/qihuo.git"
|
||||||
|
SERVICE_NAME="qihuo"
|
||||||
|
|
||||||
|
echo "==> 检查系统依赖..."
|
||||||
|
|
||||||
|
install_pkg() {
|
||||||
|
if ! command -v "$1" &>/dev/null; then
|
||||||
|
echo "安装 $1..."
|
||||||
|
sudo apt-get install -y "$2"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
sudo apt-get update -qq
|
||||||
|
install_pkg python3 python3
|
||||||
|
install_pkg python3-venv python3-venv
|
||||||
|
install_pkg git git
|
||||||
|
|
||||||
|
if ! command -v pm2 &>/dev/null; then
|
||||||
|
echo "==> 安装 PM2..."
|
||||||
|
if command -v npm &>/dev/null; then
|
||||||
|
sudo npm install -g pm2
|
||||||
|
else
|
||||||
|
install_pkg nodejs nodejs
|
||||||
|
install_pkg npm npm
|
||||||
|
sudo npm install -g pm2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> 准备应用目录 ${APP_DIR}..."
|
||||||
|
sudo mkdir -p "$(dirname "$APP_DIR")"
|
||||||
|
|
||||||
|
if [ -d "$APP_DIR/.git" ]; then
|
||||||
|
echo "==> 更新已有仓库..."
|
||||||
|
cd "$APP_DIR"
|
||||||
|
git pull origin main || git pull origin master || true
|
||||||
|
else
|
||||||
|
if [ -d "$APP_DIR" ] && [ "$(ls -A "$APP_DIR" 2>/dev/null)" ]; then
|
||||||
|
echo "目录 ${APP_DIR} 已存在且非 git 仓库,请手动处理后重试"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "==> 克隆仓库..."
|
||||||
|
sudo git clone "$REPO_URL" "$APP_DIR"
|
||||||
|
cd "$APP_DIR"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 确保当前用户可写(若以 root 克隆则 chown)
|
||||||
|
if [ "$(id -u)" -ne 0 ]; then
|
||||||
|
sudo chown -R "$(whoami):$(whoami)" "$APP_DIR"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> 创建 Python 虚拟环境..."
|
||||||
|
python3 -m venv "$APP_DIR/venv"
|
||||||
|
source "$APP_DIR/venv/bin/activate"
|
||||||
|
pip install --upgrade pip -q
|
||||||
|
pip install -r "$APP_DIR/requirements.txt" -q
|
||||||
|
|
||||||
|
if [ ! -f "$APP_DIR/.env" ]; then
|
||||||
|
echo "==> 生成 .env(请稍后编辑 SECRET_KEY 和密码)..."
|
||||||
|
cp "$APP_DIR/.env.example" "$APP_DIR/.env"
|
||||||
|
# 生成随机 SECRET_KEY
|
||||||
|
RAND_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))")
|
||||||
|
sed -i "s/change-this-to-a-random-secret-key/${RAND_KEY}/" "$APP_DIR/.env"
|
||||||
|
echo "已创建 $APP_DIR/.env ,请编辑 ADMIN_PASSWORD 后重启服务"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> PM2 启动/重启服务..."
|
||||||
|
cd "$APP_DIR"
|
||||||
|
mkdir -p "$APP_DIR/logs"
|
||||||
|
pm2 delete "$SERVICE_NAME" 2>/dev/null || true
|
||||||
|
pm2 start ecosystem.config.cjs
|
||||||
|
pm2 save
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "=========================================="
|
||||||
|
echo " 部署完成"
|
||||||
|
echo " 目录: ${APP_DIR}"
|
||||||
|
echo " 端口: 6600"
|
||||||
|
echo " 访问: http://<服务器IP>:6600"
|
||||||
|
echo " 日志: pm2 logs ${SERVICE_NAME}"
|
||||||
|
echo " 开机自启: pm2 startup && pm2 save"
|
||||||
|
echo "=========================================="
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
module.exports = {
|
||||||
|
apps: [
|
||||||
|
{
|
||||||
|
name: "qihuo",
|
||||||
|
script: "app.py",
|
||||||
|
cwd: "/opt/qihuo",
|
||||||
|
interpreter: "/opt/qihuo/venv/bin/python",
|
||||||
|
instances: 1,
|
||||||
|
autorestart: true,
|
||||||
|
watch: false,
|
||||||
|
max_memory_restart: "300M",
|
||||||
|
env: {
|
||||||
|
NODE_ENV: "production",
|
||||||
|
},
|
||||||
|
error_file: "/opt/qihuo/logs/pm2-error.log",
|
||||||
|
out_file: "/opt/qihuo/logs/pm2-out.log",
|
||||||
|
time: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
Flask==3.0.3
|
||||||
|
requests==2.32.3
|
||||||
|
python-dotenv==1.0.1
|
||||||
|
Werkzeug==3.0.3
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
(function () {
|
||||||
|
function initSymbolInput(wrapper) {
|
||||||
|
const input = wrapper.querySelector('.symbol-input');
|
||||||
|
const hiddenCode = wrapper.querySelector('input[name="symbol"]');
|
||||||
|
const hiddenName = wrapper.querySelector('input[name="symbol_name"]');
|
||||||
|
const dropdown = wrapper.querySelector('.symbol-dropdown');
|
||||||
|
const selectedEl = wrapper.querySelector('.symbol-selected');
|
||||||
|
let timer = null;
|
||||||
|
|
||||||
|
function hideDropdown() {
|
||||||
|
dropdown.classList.remove('show');
|
||||||
|
}
|
||||||
|
|
||||||
|
function selectItem(item) {
|
||||||
|
input.value = item.name;
|
||||||
|
hiddenCode.value = item.code;
|
||||||
|
hiddenName.value = item.name;
|
||||||
|
selectedEl.textContent = item.display + ' | ' + item.code;
|
||||||
|
hideDropdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderItems(items) {
|
||||||
|
dropdown.innerHTML = '';
|
||||||
|
if (!items.length) {
|
||||||
|
dropdown.innerHTML = '<div class="symbol-option">无匹配品种</div>';
|
||||||
|
} else {
|
||||||
|
items.forEach(function (item) {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.className = 'symbol-option';
|
||||||
|
div.innerHTML = item.display + '<div class="sub">' + item.code + ' · ' + item.exchange + '</div>';
|
||||||
|
div.addEventListener('mousedown', function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
selectItem(item);
|
||||||
|
});
|
||||||
|
dropdown.appendChild(div);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
dropdown.classList.add('show');
|
||||||
|
}
|
||||||
|
|
||||||
|
input.addEventListener('input', function () {
|
||||||
|
hiddenCode.value = '';
|
||||||
|
hiddenName.value = '';
|
||||||
|
selectedEl.textContent = '';
|
||||||
|
const q = input.value.trim();
|
||||||
|
if (!q) {
|
||||||
|
hideDropdown();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
clearTimeout(timer);
|
||||||
|
timer = setTimeout(function () {
|
||||||
|
fetch('/api/symbols/search?q=' + encodeURIComponent(q))
|
||||||
|
.then(function (r) { return r.json(); })
|
||||||
|
.then(renderItems)
|
||||||
|
.catch(function () { hideDropdown(); });
|
||||||
|
}, 200);
|
||||||
|
});
|
||||||
|
|
||||||
|
input.addEventListener('blur', function () {
|
||||||
|
setTimeout(hideDropdown, 150);
|
||||||
|
});
|
||||||
|
|
||||||
|
input.addEventListener('focus', function () {
|
||||||
|
const q = input.value.trim();
|
||||||
|
if (q && !hiddenCode.value) {
|
||||||
|
fetch('/api/symbols/search?q=' + encodeURIComponent(q))
|
||||||
|
.then(function (r) { return r.json(); })
|
||||||
|
.then(renderItems);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
document.querySelectorAll('.symbol-wrap').forEach(initSymbolInput);
|
||||||
|
|
||||||
|
document.querySelectorAll('form').forEach(function (form) {
|
||||||
|
if (!form.querySelector('.symbol-wrap')) return;
|
||||||
|
form.addEventListener('submit', function (e) {
|
||||||
|
const hidden = form.querySelector('input[name="symbol"]');
|
||||||
|
if (hidden && !hidden.value.trim()) {
|
||||||
|
e.preventDefault();
|
||||||
|
alert('请从下拉列表中选择品种');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})();
|
||||||
+95
@@ -0,0 +1,95 @@
|
|||||||
|
# 国内期货品种映射:中文名 -> 新浪行情代码(0 表示主力连续)
|
||||||
|
SYMBOLS = [
|
||||||
|
{"name": "白银", "code": "nf_AG0", "exchange": "上期所"},
|
||||||
|
{"name": "黄金", "code": "nf_AU0", "exchange": "上期所"},
|
||||||
|
{"name": "铜", "code": "nf_CU0", "exchange": "上期所"},
|
||||||
|
{"name": "铝", "code": "nf_AL0", "exchange": "上期所"},
|
||||||
|
{"name": "锌", "code": "nf_ZN0", "exchange": "上期所"},
|
||||||
|
{"name": "铅", "code": "nf_PB0", "exchange": "上期所"},
|
||||||
|
{"name": "镍", "code": "nf_NI0", "exchange": "上期所"},
|
||||||
|
{"name": "锡", "code": "nf_SN0", "exchange": "上期所"},
|
||||||
|
{"name": "螺纹钢", "code": "nf_RB0", "exchange": "上期所"},
|
||||||
|
{"name": "热卷", "code": "nf_HC0", "exchange": "上期所"},
|
||||||
|
{"name": "不锈钢", "code": "nf_SS0", "exchange": "上期所"},
|
||||||
|
{"name": "原油", "code": "nf_SC0", "exchange": "上期所"},
|
||||||
|
{"name": "燃油", "code": "nf_FU0", "exchange": "上期所"},
|
||||||
|
{"name": "沥青", "code": "nf_BU0", "exchange": "上期所"},
|
||||||
|
{"name": "橡胶", "code": "nf_RU0", "exchange": "上期所"},
|
||||||
|
{"name": "纸浆", "code": "nf_SP0", "exchange": "上期所"},
|
||||||
|
{"name": "铁矿石", "code": "nf_I0", "exchange": "大商所"},
|
||||||
|
{"name": "焦炭", "code": "nf_J0", "exchange": "大商所"},
|
||||||
|
{"name": "焦煤", "code": "nf_JM0", "exchange": "大商所"},
|
||||||
|
{"name": "豆粕", "code": "nf_M0", "exchange": "大商所"},
|
||||||
|
{"name": "豆油", "code": "nf_Y0", "exchange": "大商所"},
|
||||||
|
{"name": "棕榈油", "code": "nf_P0", "exchange": "大商所"},
|
||||||
|
{"name": "玉米", "code": "nf_C0", "exchange": "大商所"},
|
||||||
|
{"name": "淀粉", "code": "nf_CS0", "exchange": "大商所"},
|
||||||
|
{"name": "鸡蛋", "code": "nf_JD0", "exchange": "大商所"},
|
||||||
|
{"name": "生猪", "code": "nf_LH0", "exchange": "大商所"},
|
||||||
|
{"name": "聚乙烯", "code": "nf_L0", "exchange": "大商所"},
|
||||||
|
{"name": "聚丙烯", "code": "nf_PP0", "exchange": "大商所"},
|
||||||
|
{"name": "PVC", "code": "nf_V0", "exchange": "大商所"},
|
||||||
|
{"name": "乙二醇", "code": "nf_EG0", "exchange": "大商所"},
|
||||||
|
{"name": "苯乙烯", "code": "nf_EB0", "exchange": "大商所"},
|
||||||
|
{"name": "液化气", "code": "nf_PG0", "exchange": "大商所"},
|
||||||
|
{"name": "菜粕", "code": "nf_RM0", "exchange": "郑商所"},
|
||||||
|
{"name": "菜油", "code": "nf_OI0", "exchange": "郑商所"},
|
||||||
|
{"name": "白糖", "code": "nf_SR0", "exchange": "郑商所"},
|
||||||
|
{"name": "棉花", "code": "nf_CF0", "exchange": "郑商所"},
|
||||||
|
{"name": "甲醇", "code": "nf_MA0", "exchange": "郑商所"},
|
||||||
|
{"name": "PTA", "code": "nf_TA0", "exchange": "郑商所"},
|
||||||
|
{"name": "玻璃", "code": "nf_FG0", "exchange": "郑商所"},
|
||||||
|
{"name": "纯碱", "code": "nf_SA0", "exchange": "郑商所"},
|
||||||
|
{"name": "尿素", "code": "nf_UR0", "exchange": "郑商所"},
|
||||||
|
{"name": "硅铁", "code": "nf_SF0", "exchange": "郑商所"},
|
||||||
|
{"name": "锰硅", "code": "nf_SM0", "exchange": "郑商所"},
|
||||||
|
{"name": "苹果", "code": "nf_AP0", "exchange": "郑商所"},
|
||||||
|
{"name": "红枣", "code": "nf_CJ0", "exchange": "郑商所"},
|
||||||
|
{"name": "花生", "code": "nf_PK0", "exchange": "郑商所"},
|
||||||
|
{"name": "沪深300", "code": "CFF_RE_IF0", "exchange": "中金所"},
|
||||||
|
{"name": "上证50", "code": "CFF_RE_IH0", "exchange": "中金所"},
|
||||||
|
{"name": "中证500", "code": "CFF_RE_IC0", "exchange": "中金所"},
|
||||||
|
{"name": "中证1000", "code": "CFF_RE_IM0", "exchange": "中金所"},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def _contract_label(code: str) -> str:
|
||||||
|
"""从新浪代码提取品种字母部分,用于展示主力合约标识。"""
|
||||||
|
raw = code.replace("nf_", "").replace("CFF_RE_", "")
|
||||||
|
letters = "".join(c for c in raw if c.isalpha())
|
||||||
|
return f"{letters}主力"
|
||||||
|
|
||||||
|
|
||||||
|
def search_symbols(query: str) -> list:
|
||||||
|
q = query.strip().lower()
|
||||||
|
if not q:
|
||||||
|
return []
|
||||||
|
results = []
|
||||||
|
for s in SYMBOLS:
|
||||||
|
name = s["name"]
|
||||||
|
code = s["code"]
|
||||||
|
contract = _contract_label(code)
|
||||||
|
if q in name.lower() or q in code.lower() or q in contract.lower():
|
||||||
|
results.append({
|
||||||
|
"name": name,
|
||||||
|
"code": code,
|
||||||
|
"exchange": s["exchange"],
|
||||||
|
"contract": contract,
|
||||||
|
"display": f"{name} ({contract})",
|
||||||
|
})
|
||||||
|
return results[:12]
|
||||||
|
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
def get_by_code(code: str) -> Optional[dict]:
|
||||||
|
for s in SYMBOLS:
|
||||||
|
if s["code"] == code:
|
||||||
|
return {
|
||||||
|
"name": s["name"],
|
||||||
|
"code": s["code"],
|
||||||
|
"exchange": s["exchange"],
|
||||||
|
"contract": _contract_label(s["code"]),
|
||||||
|
}
|
||||||
|
return None
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>{% block title %}国内期货监控系统{% endblock %}</title>
|
||||||
|
<style>
|
||||||
|
*{margin:0;padding:0;box-sizing:border-box}
|
||||||
|
body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;background:#0a0a10;color:#eaeaea;min-height:100vh}
|
||||||
|
.layout{display:flex;min-height:100vh}
|
||||||
|
.sidebar{width:220px;background:#12121a;border-right:1px solid #242435;padding:1.5rem 0;flex-shrink:0}
|
||||||
|
.sidebar .logo{padding:0 1.5rem 1.5rem;font-size:1.1rem;font-weight:600;background:linear-gradient(90deg,#4cc2ff,#7b42ff);-webkit-background-clip:text;-webkit-text-fill-color:transparent}
|
||||||
|
.nav a{display:block;padding:.75rem 1.5rem;color:#a9a9c4;text-decoration:none;font-size:.9rem;border-left:3px solid transparent;transition:.2s}
|
||||||
|
.nav a:hover{color:#fff;background:#1a1a29}
|
||||||
|
.nav a.active{color:#4cc2ff;border-left-color:#4cc2ff;background:#1a1a29}
|
||||||
|
.main{flex:1;padding:2rem;overflow-x:auto}
|
||||||
|
.page-title{font-size:1.5rem;margin-bottom:1.5rem;color:#fff}
|
||||||
|
.flash{padding:1rem;background:#1e2533;color:#4cc2ff;border-radius:10px;margin-bottom:1.5rem;text-align:center}
|
||||||
|
.card{background:#12121a;border-radius:16px;padding:1.5rem;border:1px solid #242435;margin-bottom:1.5rem}
|
||||||
|
.card h2{font-size:1.15rem;margin-bottom:1rem;color:#c4c4ff;display:flex;align-items:center;gap:.5rem}
|
||||||
|
.card h2:before{content:"";width:4px;height:16px;background:#4cc2ff;border-radius:2px}
|
||||||
|
.form-row{display:flex;gap:.75rem;flex-wrap:wrap;margin-bottom:1rem;align-items:flex-start}
|
||||||
|
input,select,button{padding:.7rem 1rem;border-radius:10px;border:1px solid #2e2e45;background:#1a1a29;color:#fff;font-size:.9rem;outline:none}
|
||||||
|
input:focus,select:focus{border-color:#4cc2ff}
|
||||||
|
button.btn-primary{background:linear-gradient(90deg,#4285f4,#7b42ff);border:none;cursor:pointer;color:#fff}
|
||||||
|
button.btn-primary:hover{opacity:.9}
|
||||||
|
.list{display:flex;flex-direction:column;gap:.75rem}
|
||||||
|
.list-item{display:flex;justify-content:space-between;align-items:center;padding:1rem;background:#161625;border-radius:10px;gap:1rem;flex-wrap:wrap}
|
||||||
|
.btn-del{padding:.4rem .8rem;background:#291d2f;color:#ff6666;border-radius:8px;text-decoration:none;font-size:.85rem}
|
||||||
|
table{width:100%;border-collapse:collapse}
|
||||||
|
th,td{padding:.85rem;text-align:left;border-bottom:1px solid #242435;font-size:.9rem}
|
||||||
|
th{color:#a9a9ff}
|
||||||
|
.badge{padding:.25rem .5rem;border-radius:6px;font-size:.75rem}
|
||||||
|
.badge.profit{background:#1e332f;color:#4cd97f}
|
||||||
|
.badge.loss{background:#331e24;color:#ff6666}
|
||||||
|
.badge.dir{background:#1e2533;color:#4cc2ff}
|
||||||
|
.badge.planned{background:#29241e;color:#eac147}
|
||||||
|
.badge.active{background:#1e332f;color:#4cd97f}
|
||||||
|
.stat-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:1rem;margin-bottom:1.5rem}
|
||||||
|
.stat-item{background:#161625;padding:1rem;border-radius:12px;text-align:center}
|
||||||
|
.stat-item .label{font-size:.8rem;color:#999}
|
||||||
|
.stat-item .value{font-size:1.4rem;font-weight:600;color:#fff;margin-top:.25rem}
|
||||||
|
.symbol-wrap{position:relative;min-width:180px}
|
||||||
|
.symbol-wrap input{width:100%}
|
||||||
|
.symbol-dropdown{position:absolute;top:100%;left:0;right:0;background:#1a1a29;border:1px solid #2e2e45;border-radius:10px;margin-top:4px;z-index:100;max-height:240px;overflow-y:auto;display:none}
|
||||||
|
.symbol-dropdown.show{display:block}
|
||||||
|
.symbol-option{padding:.65rem 1rem;cursor:pointer;font-size:.85rem;border-bottom:1px solid #242435}
|
||||||
|
.symbol-option:hover{background:#242435}
|
||||||
|
.symbol-option .sub{font-size:.75rem;color:#888;margin-top:2px}
|
||||||
|
.symbol-selected{font-size:.75rem;color:#4cc2ff;margin-top:4px}
|
||||||
|
.user-bar{padding:1rem 1.5rem;border-top:1px solid #242435;margin-top:1rem;font-size:.8rem;color:#888}
|
||||||
|
.user-bar a{color:#ff6666;text-decoration:none}
|
||||||
|
@media(max-width:768px){
|
||||||
|
.layout{flex-direction:column}
|
||||||
|
.sidebar{width:100%}
|
||||||
|
.nav{display:flex;flex-wrap:wrap}
|
||||||
|
.nav a{border-left:none;border-bottom:2px solid transparent;padding:.5rem 1rem}
|
||||||
|
.nav a.active{border-bottom-color:#4cc2ff}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
{% block extra_css %}{% endblock %}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="layout">
|
||||||
|
<aside class="sidebar">
|
||||||
|
<div class="logo">期货监控复盘</div>
|
||||||
|
<nav class="nav">
|
||||||
|
<a href="{{ url_for('plans') }}" class="{% if request.endpoint == 'plans' %}active{% endif %}">开单计划</a>
|
||||||
|
<a href="{{ url_for('keys') }}" class="{% if request.endpoint == 'keys' %}active{% endif %}">关键位监控</a>
|
||||||
|
<a href="{{ url_for('records') }}" class="{% if request.endpoint == 'records' %}active{% endif %}">交易记录与复盘</a>
|
||||||
|
<a href="{{ url_for('stats') }}" class="{% if request.endpoint == 'stats' %}active{% endif %}">统计分析</a>
|
||||||
|
<a href="{{ url_for('settings') }}" class="{% if request.endpoint == 'settings' %}active{% endif %}">系统设置</a>
|
||||||
|
</nav>
|
||||||
|
<div class="user-bar">
|
||||||
|
{{ session.username or '用户' }}
|
||||||
|
<a href="{{ url_for('logout') }}">退出</a>
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
<main class="main">
|
||||||
|
{% with msg=get_flashed_messages() %}{% if msg %}<div class="flash">{{ msg[0] }}</div>{% endif %}{% endwith %}
|
||||||
|
{% block content %}{% endblock %}
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
<script src="{{ url_for('static', filename='js/symbol.js') }}"></script>
|
||||||
|
{% block extra_js %}{% endblock %}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}关键位监控 - 国内期货监控系统{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
<h1 class="page-title">关键位监控</h1>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>新增监控</h2>
|
||||||
|
<form action="{{ url_for('add_key') }}" method="post" class="form-row">
|
||||||
|
<div class="symbol-wrap">
|
||||||
|
<input type="text" class="symbol-input" placeholder="输入中文品种名" autocomplete="off" required>
|
||||||
|
<input type="hidden" name="symbol" required>
|
||||||
|
<input type="hidden" name="symbol_name">
|
||||||
|
<div class="symbol-dropdown"></div>
|
||||||
|
<div class="symbol-selected"></div>
|
||||||
|
</div>
|
||||||
|
<select name="type" required>
|
||||||
|
<option value="箱体突破">箱体突破</option>
|
||||||
|
<option value="收敛突破">收敛突破</option>
|
||||||
|
<option value="关键阻力位">关键阻力位</option>
|
||||||
|
<option value="关键支撑位">关键支撑位</option>
|
||||||
|
</select>
|
||||||
|
<select name="direction" required>
|
||||||
|
<option value="">选择方向</option>
|
||||||
|
<option value="long">做多</option>
|
||||||
|
<option value="short">做空</option>
|
||||||
|
</select>
|
||||||
|
<input name="upper" type="number" step="0.0001" placeholder="上沿/阻力" required>
|
||||||
|
<input name="lower" type="number" step="0.0001" placeholder="下沿/支撑" required>
|
||||||
|
<button type="submit" class="btn-primary">添加监控</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>监控列表</h2>
|
||||||
|
<div class="list">
|
||||||
|
{% for k in keys %}
|
||||||
|
<div class="list-item">
|
||||||
|
<div>
|
||||||
|
<strong>{{ k.symbol_name or k.symbol }}</strong> | {{ k.monitor_type }}
|
||||||
|
<span class="badge dir">{{ '做多' if k.direction == 'long' else '做空' }}</span>
|
||||||
|
</div>
|
||||||
|
<div>上: {{ k.upper }} | 下: {{ k.lower }}</div>
|
||||||
|
<div style="font-size:.8rem;color:#888">{{ k.symbol }}</div>
|
||||||
|
<a href="{{ url_for('del_key', pid=k.id) }}" class="btn-del" onclick="return confirm('删除?')">删除</a>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
|
<div style="color:#888;padding:1rem">暂无关键位监控</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>系统登录</title>
|
||||||
|
<style>
|
||||||
|
*{margin:0;padding:0;box-sizing:border-box}
|
||||||
|
body{background:#0a0a10;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;display:flex;align-items:center;justify-content:center;height:100vh;color:#fff}
|
||||||
|
.login-box{background:#12121a;padding:2.5rem;border-radius:16px;width:100%;max-width:400px;border:1px solid #242435;box-shadow:0 8px 24px rgba(0,0,0,.3)}
|
||||||
|
.login-box h2{margin-bottom:2rem;text-align:center;font-size:1.5rem;background:linear-gradient(90deg,#4cc2ff,#7b42ff);-webkit-background-clip:text;-webkit-text-fill-color:transparent}
|
||||||
|
.form-group{margin-bottom:1.25rem}
|
||||||
|
.form-group label{display:block;margin-bottom:.5rem;font-size:.9rem;color:#a9a9ff}
|
||||||
|
.form-group input{width:100%;padding:.85rem 1rem;border-radius:10px;border:1px solid #2e2e45;background:#1a1a29;color:#fff;font-size:.95rem;outline:none}
|
||||||
|
.form-group input:focus{border-color:#4cc2ff}
|
||||||
|
button{width:100%;padding:.9rem;border-radius:10px;border:none;background:linear-gradient(90deg,#4285f4,#7b42ff);color:#fff;font-size:1rem;font-weight:500;cursor:pointer}
|
||||||
|
button:hover{opacity:.9}
|
||||||
|
.flash{padding:.8rem;margin-bottom:1rem;background:#331e24;color:#ff6666;border-radius:8px;text-align:center;font-size:.85rem}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="login-box">
|
||||||
|
<h2>期货监控系统</h2>
|
||||||
|
{% with messages = get_flashed_messages() %}
|
||||||
|
{% if messages %}<div class="flash">{{ messages[0] }}</div>{% endif %}
|
||||||
|
{% endwith %}
|
||||||
|
<form method="POST">
|
||||||
|
<div class="form-group">
|
||||||
|
<label>账号</label>
|
||||||
|
<input type="text" name="username" required placeholder="请输入账号">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>密码</label>
|
||||||
|
<input type="password" name="password" required placeholder="请输入密码">
|
||||||
|
</div>
|
||||||
|
<button type="submit">登录</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}开单计划 - 国内期货监控系统{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
<h1 class="page-title">开单计划</h1>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>新增计划</h2>
|
||||||
|
<form action="{{ url_for('add_plan') }}" method="post" class="form-row">
|
||||||
|
<div class="symbol-wrap">
|
||||||
|
<input type="text" class="symbol-input" placeholder="输入中文品种名" autocomplete="off" required>
|
||||||
|
<input type="hidden" name="symbol" required>
|
||||||
|
<input type="hidden" name="symbol_name">
|
||||||
|
<div class="symbol-dropdown"></div>
|
||||||
|
<div class="symbol-selected"></div>
|
||||||
|
</div>
|
||||||
|
<select name="direction" required>
|
||||||
|
<option value="">选择方向</option>
|
||||||
|
<option value="long">做多</option>
|
||||||
|
<option value="short">做空</option>
|
||||||
|
</select>
|
||||||
|
<input name="zone_lower" type="number" step="0.0001" placeholder="决策区间下限" required>
|
||||||
|
<input name="zone_upper" type="number" step="0.0001" placeholder="决策区间上限" required>
|
||||||
|
<input name="stop_loss" type="number" step="0.0001" placeholder="止损价位" required>
|
||||||
|
<input name="take_profit" type="number" step="0.0001" placeholder="止盈价位" required>
|
||||||
|
<button type="submit" class="btn-primary">添加计划</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>进行中计划</h2>
|
||||||
|
<div class="list">
|
||||||
|
{% for p in plans %}
|
||||||
|
<div class="list-item">
|
||||||
|
<div>
|
||||||
|
<strong>{{ p.symbol_name or p.symbol }}</strong>
|
||||||
|
<span class="badge dir">{{ '做多' if p.direction == 'long' else '做空' }}</span>
|
||||||
|
{% if p.status == 'planned' %}
|
||||||
|
<span class="badge planned">待触发</span>
|
||||||
|
{% else %}
|
||||||
|
<span class="badge active">已激活</span>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
<div>区间: {{ p.zone_lower }} ~ {{ p.zone_upper }}</div>
|
||||||
|
<div>止损: {{ p.stop_loss }} | 止盈: {{ p.take_profit }}</div>
|
||||||
|
<div style="font-size:.8rem;color:#888">{{ p.symbol }}</div>
|
||||||
|
<a href="{{ url_for('del_plan', pid=p.id) }}" class="btn-del" onclick="return confirm('删除此计划?')">删除</a>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
|
<div style="color:#888;padding:1rem">暂无进行中的开单计划</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% if closed %}
|
||||||
|
<div class="card">
|
||||||
|
<h2>最近已完成</h2>
|
||||||
|
<div class="list">
|
||||||
|
{% for p in closed %}
|
||||||
|
<div class="list-item">
|
||||||
|
<div><strong>{{ p.symbol_name or p.symbol }}</strong> <span class="badge dir">{{ '做多' if p.direction == 'long' else '做空' }}</span></div>
|
||||||
|
<div>区间: {{ p.zone_lower }} ~ {{ p.zone_upper }} | 损: {{ p.stop_loss }} 盈: {{ p.take_profit }}</div>
|
||||||
|
<a href="{{ url_for('del_plan', pid=p.id) }}" class="btn-del" onclick="return confirm('删除?')">删除</a>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}交易记录与复盘 - 国内期货监控系统{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
<h1 class="page-title">交易记录与复盘</h1>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>全部记录</h2>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>品种</th>
|
||||||
|
<th>类型</th>
|
||||||
|
<th>方向</th>
|
||||||
|
<th>触发价</th>
|
||||||
|
<th>止损</th>
|
||||||
|
<th>止盈</th>
|
||||||
|
<th>结果</th>
|
||||||
|
<th>时间</th>
|
||||||
|
<th></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for r in records %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ r.symbol_name or r.symbol }}</td>
|
||||||
|
<td>{{ r.monitor_type }}</td>
|
||||||
|
<td><span class="badge dir">{{ '做多' if r.direction == 'long' else '做空' }}</span></td>
|
||||||
|
<td>{{ r.trigger_price }}</td>
|
||||||
|
<td>{{ r.stop_loss }}</td>
|
||||||
|
<td>{{ r.take_profit }}</td>
|
||||||
|
<td>
|
||||||
|
{% if r.result == '止盈' %}
|
||||||
|
<span class="badge profit">止盈</span>
|
||||||
|
{% else %}
|
||||||
|
<span class="badge loss">止损</span>
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
<td>{{ r.created_at[:16] if r.created_at else '' }}</td>
|
||||||
|
<td><a href="{{ url_for('del_record', rid=r.id) }}" class="btn-del" onclick="return confirm('删除?')">删</a></td>
|
||||||
|
</tr>
|
||||||
|
{% else %}
|
||||||
|
<tr><td colspan="9" style="color:#888">暂无交易记录</td></tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}系统设置 - 国内期货监控系统{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
<h1 class="page-title">系统设置</h1>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>企业微信推送</h2>
|
||||||
|
<form action="{{ url_for('settings') }}" method="post" class="form-row">
|
||||||
|
<input type="hidden" name="action" value="wechat">
|
||||||
|
<input name="wechat_webhook" type="url" placeholder="企业微信 Webhook 地址" value="{{ webhook }}" style="flex:1;min-width:300px">
|
||||||
|
<button type="submit" class="btn-primary">保存</button>
|
||||||
|
</form>
|
||||||
|
<p style="font-size:.8rem;color:#888;margin-top:.75rem">在企业微信群中添加机器人后,将 Webhook 地址粘贴到上方保存即可。</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>修改密码</h2>
|
||||||
|
<form action="{{ url_for('settings') }}" method="post" style="max-width:400px">
|
||||||
|
<input type="hidden" name="action" value="password">
|
||||||
|
<div style="margin-bottom:.75rem">
|
||||||
|
<label style="font-size:.85rem;color:#a9a9ff;display:block;margin-bottom:.35rem">当前账号</label>
|
||||||
|
<input type="text" value="{{ username }}" disabled style="width:100%">
|
||||||
|
</div>
|
||||||
|
<div style="margin-bottom:.75rem">
|
||||||
|
<label style="font-size:.85rem;color:#a9a9ff;display:block;margin-bottom:.35rem">原密码</label>
|
||||||
|
<input name="old_password" type="password" required style="width:100%">
|
||||||
|
</div>
|
||||||
|
<div style="margin-bottom:.75rem">
|
||||||
|
<label style="font-size:.85rem;color:#a9a9ff;display:block;margin-bottom:.35rem">新密码(至少 6 位)</label>
|
||||||
|
<input name="new_password" type="password" required minlength="6" style="width:100%">
|
||||||
|
</div>
|
||||||
|
<div style="margin-bottom:.75rem">
|
||||||
|
<label style="font-size:.85rem;color:#a9a9ff;display:block;margin-bottom:.35rem">确认新密码</label>
|
||||||
|
<input name="new_password2" type="password" required minlength="6" style="width:100%">
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn-primary">修改密码</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block title %}统计分析 - 国内期货监控系统{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
<h1 class="page-title">统计分析</h1>
|
||||||
|
|
||||||
|
<div class="stat-grid">
|
||||||
|
<div class="stat-item"><div class="label">总交易</div><div class="value">{{ total }}</div></div>
|
||||||
|
<div class="stat-item"><div class="label">止盈</div><div class="value" style="color:#4cd97f">{{ win }}</div></div>
|
||||||
|
<div class="stat-item"><div class="label">止损</div><div class="value" style="color:#ff6666">{{ loss }}</div></div>
|
||||||
|
<div class="stat-item"><div class="label">胜率</div><div class="value">{{ rate }}%</div></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>按品种统计</h2>
|
||||||
|
<table>
|
||||||
|
<thead><tr><th>品种</th><th>交易次数</th><th>止盈次数</th><th>胜率</th></tr></thead>
|
||||||
|
<tbody>
|
||||||
|
{% for s in by_symbol %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ s.symbol_name or s.symbol }}</td>
|
||||||
|
<td>{{ s.cnt }}</td>
|
||||||
|
<td>{{ s.wins }}</td>
|
||||||
|
<td>{{ round(s.wins / s.cnt * 100, 2) if s.cnt else 0 }}%</td>
|
||||||
|
</tr>
|
||||||
|
{% else %}
|
||||||
|
<tr><td colspan="4" style="color:#888">暂无数据</td></tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>按类型统计</h2>
|
||||||
|
<table>
|
||||||
|
<thead><tr><th>类型</th><th>交易次数</th><th>止盈次数</th><th>胜率</th></tr></thead>
|
||||||
|
<tbody>
|
||||||
|
{% for t in by_type %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ t.monitor_type }}</td>
|
||||||
|
<td>{{ t.cnt }}</td>
|
||||||
|
<td>{{ t.wins }}</td>
|
||||||
|
<td>{{ round(t.wins / t.cnt * 100, 2) if t.cnt else 0 }}%</td>
|
||||||
|
</tr>
|
||||||
|
{% else %}
|
||||||
|
<tr><td colspan="4" style="color:#888">暂无数据</td></tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>按方向统计</h2>
|
||||||
|
<table>
|
||||||
|
<thead><tr><th>方向</th><th>交易次数</th><th>止盈次数</th><th>胜率</th></tr></thead>
|
||||||
|
<tbody>
|
||||||
|
{% for d in by_direction %}
|
||||||
|
<tr>
|
||||||
|
<td><span class="badge dir">{{ '做多' if d.direction == 'long' else '做空' }}</span></td>
|
||||||
|
<td>{{ d.cnt }}</td>
|
||||||
|
<td>{{ d.wins }}</td>
|
||||||
|
<td>{{ round(d.wins / d.cnt * 100, 2) if d.cnt else 0 }}%</td>
|
||||||
|
</tr>
|
||||||
|
{% else %}
|
||||||
|
<tr><td colspan="4" style="color:#888">暂无数据</td></tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<h2>最近 10 笔交易</h2>
|
||||||
|
<table>
|
||||||
|
<thead><tr><th>品种</th><th>方向</th><th>结果</th><th>时间</th></tr></thead>
|
||||||
|
<tbody>
|
||||||
|
{% for r in recent %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ r.symbol_name or r.symbol }}</td>
|
||||||
|
<td><span class="badge dir">{{ '做多' if r.direction == 'long' else '做空' }}</span></td>
|
||||||
|
<td>
|
||||||
|
{% if r.result == '止盈' %}<span class="badge profit">止盈</span>
|
||||||
|
{% else %}<span class="badge loss">止损</span>{% endif %}
|
||||||
|
</td>
|
||||||
|
<td>{{ r.created_at[:16] if r.created_at else '' }}</td>
|
||||||
|
</tr>
|
||||||
|
{% else %}
|
||||||
|
<tr><td colspan="4" style="color:#888">暂无数据</td></tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user