同花顺合约代码映射与/root部署路径

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-15 11:10:33 +08:00
parent ae480cb3e7
commit bd7f0da1ca
8 changed files with 370 additions and 178 deletions
+33 -26
View File
@@ -14,7 +14,7 @@ from flask import (
)
from werkzeug.security import check_password_hash, generate_password_hash
from symbols import search_symbols, get_by_code
from symbols import search_symbols, get_price, ths_to_sina_code
load_dotenv()
@@ -82,6 +82,9 @@ def init_db():
"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",
"ALTER TABLE order_plans ADD COLUMN sina_code TEXT",
"ALTER TABLE key_monitors ADD COLUMN sina_code TEXT",
"ALTER TABLE trade_records ADD COLUMN sina_code TEXT",
]
for sql in migrations:
try:
@@ -118,20 +121,20 @@ def send_wechat_msg(content: str):
# —————————————— 行情 ——————————————
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:
def resolve_sina_code(ths_code: str, sina_code: str = "") -> Optional[str]:
"""同花顺代码 -> 新浪行情代码;兼容旧数据中的新浪格式。"""
if sina_code:
return sina_code
if ths_code.startswith("nf_") or ths_code.startswith("CFF_RE_"):
return ths_code
return ths_to_sina_code(ths_code)
def fetch_price(ths_code: str, sina_code: str = "") -> Optional[float]:
code = resolve_sina_code(ths_code, sina_code)
if not code:
return None
return get_price(code)
# —————————————— 监控逻辑 ——————————————
@@ -143,7 +146,8 @@ def check_order_plans():
for r in rows:
sym = r["symbol"]
p = get_price(sym)
sina = r["sina_code"] if "sina_code" in r.keys() else ""
p = fetch_price(sym, sina)
if not p:
continue
@@ -224,8 +228,9 @@ def check_key_monitors():
low_trig = r["lower_triggered"]
name = r["symbol_name"] or sym
pid = r["id"]
sina = r["sina_code"] if "sina_code" in r.keys() else ""
p = get_price(sym)
p = fetch_price(sym, sina)
if not p:
continue
@@ -332,19 +337,20 @@ def add_plan():
direction = d.get("direction")
symbol = d.get("symbol", "").strip()
symbol_name = d.get("symbol_name", "").strip()
sina_code = d.get("sina_code", "").strip()
if not direction:
flash("请选择多空方向")
return redirect(url_for("plans"))
if not symbol:
flash("选择有效品种")
if not symbol or not sina_code:
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, sina_code, direction, zone_upper, zone_lower, stop_loss, take_profit)
VALUES (?,?,?,?,?,?,?,?)""",
(
symbol, symbol_name, direction,
symbol, symbol_name, sina_code, direction,
float(d["zone_upper"]), float(d["zone_lower"]),
float(d["stop_loss"]), float(d["take_profit"]),
),
@@ -382,18 +388,19 @@ def add_key():
direction = d.get("direction")
symbol = d.get("symbol", "").strip()
symbol_name = d.get("symbol_name", "").strip()
sina_code = d.get("sina_code", "").strip()
if not direction:
flash("请选择多空方向")
return redirect(url_for("keys"))
if not symbol:
flash("选择有效品种")
if not symbol or not sina_code:
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"])),
(symbol, symbol_name, sina_code, monitor_type, direction, upper, lower)
VALUES (?,?,?,?,?,?,?)""",
(symbol, symbol_name, sina_code, d["type"], direction, float(d["upper"]), float(d["lower"])),
)
conn.commit()
conn.close()