Restructure into modules/ with single-process CTP and config/ layout.

Move business code under modules/, env template to config/, PM2 single qihuo process, and _legacy shims for old imports.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-01 14:42:16 +08:00
parent b354d6c701
commit e5a586f903
209 changed files with 21962 additions and 20963 deletions
+10
View File
@@ -0,0 +1,10 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
"""CTP / vn.py integration — single-process mode."""
def register(deps) -> None:
del deps
__all__ = ["register"]
+63
View File
@@ -0,0 +1,63 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 详见 LICENSE.zh-CN.txt
"""CTP 持仓均价:仅使用柜台持仓回报(vnpy pos.price = PositionCost 加权)。"""
from __future__ import annotations
from typing import Any, Optional
from modules.core.contract_specs import get_contract_spec
from modules.ctp.ctp_symbol import ths_to_vnpy_symbol
from modules.core.symbols import ths_to_codes
def symbols_match(ctp_sym: str, ths: str) -> bool:
a = (ctp_sym or "").lower()
b = (ths or "").lower()
if a == b:
return True
if a and b and a.split(".")[0] == b.split(".")[0]:
return True
try:
vnpy_sym, _ = ths_to_vnpy_symbol(ths)
if a == vnpy_sym.lower():
return True
except Exception:
pass
try:
vnpy_sym, _ = ths_to_vnpy_symbol(ctp_sym)
if vnpy_sym.lower() == b.split(".")[0]:
return True
except Exception:
pass
return False
def _ths_code(sym: str) -> str:
codes = ths_to_codes(sym) or {}
return codes.get("ths_code") or sym
def round_to_tick(price: float, sym: str) -> float:
tick = float(get_contract_spec(_ths_code(sym)).get("tick_size") or 1.0)
if tick <= 0:
return round(price, 2)
return round(round(price / tick) * tick, 4)
def resolve_ctp_entry(
sym: str,
direction: str,
ctp: Optional[dict[str, Any]],
trades: Optional[list[dict[str, Any]]] = None,
*,
tick: Optional[float] = None,
) -> tuple[float, str]:
"""均价:仅柜台持仓价(trades/tick 参数保留兼容,不参与计算)。"""
del direction, trades, tick
if not ctp:
return 0.0, "none"
pos_avg = float(ctp.get("avg_price") or 0)
if pos_avg > 0:
return round_to_tick(pos_avg, sym), "ctp"
return 0.0, "none"
+144
View File
@@ -0,0 +1,144 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""从 CTP 柜台同步手续费率(SimNow / 期货公司)。"""
from __future__ import annotations
import logging
import re
import time
from typing import Optional
from modules.core.contract_specs import get_contract_spec
from modules.fees.fee_specs import upsert_fee_rate
from modules.ctp.vnpy_bridge import get_bridge
logger = logging.getLogger(__name__)
def _product_from_instrument(instrument_id: str) -> str:
m = re.match(r"^([A-Za-z]+)", instrument_id or "")
return m.group(1).lower() if m else ""
def ctp_commission_to_fee_fields(data: dict, ths_code: str) -> dict:
"""CTP OnRspQryInstrumentCommissionRate → fee_rates 字段。"""
mult = int(get_contract_spec(ths_code)["mult"])
exchange = str(data.get("ExchangeID") or "").strip()
return {
"exchange": exchange,
"mult": mult,
"open_fixed": float(data.get("OpenRatioByVolume") or 0),
"open_ratio": float(data.get("OpenRatioByMoney") or 0),
"close_yesterday_fixed": float(data.get("CloseRatioByVolume") or 0),
"close_yesterday_ratio": float(data.get("CloseRatioByMoney") or 0),
"close_today_fixed": float(data.get("CloseTodayRatioByVolume") or 0),
"close_today_ratio": float(data.get("CloseTodayRatioByMoney") or 0),
"source": "ctp",
}
def _collect_main_ths_codes() -> list[str]:
"""从主力列表收集同花顺合约代码(供 CTP 手续费查询)。"""
from datetime import date
from modules.core.symbols import PRODUCTS, build_ths_code, list_main_contracts_grouped
symbols: list[str] = []
for group in list_main_contracts_grouped():
for item in group.get("items") or []:
ths = (item.get("ths_code") or item.get("ths") or item.get("code") or "").strip()
if ths and not ths.endswith("888"):
symbols.append(ths)
if symbols:
return symbols
today = date.today()
for p in PRODUCTS:
symbols.append(build_ths_code(p, today.year, today.month))
return symbols
def sync_fees_from_ctp(mode: str, *, max_symbols: int = 80) -> tuple[int, str]:
"""CTP 已连接时查询手续费并写入 fee_rates(source=ctp,覆盖同品种旧数据)。"""
bridge = get_bridge()
if not bridge.available():
return 0, "vnpy 未安装"
if bridge.connected_mode != mode:
return 0, "请先连接 CTP"
if not bridge.ping():
return 0, "CTP 连接无效,请重连"
seen: set[str] = set()
ok = 0
errors = 0
batch = bridge.query_all_commissions(mode=mode)
if batch:
for raw in batch:
inst = str(raw.get("InstrumentID") or "").strip()
product = _product_from_instrument(inst)
if not product or product in seen:
continue
seen.add(product)
try:
fields = ctp_commission_to_fee_fields(raw, inst or product)
upsert_fee_rate(product, fields)
ok += 1
except Exception as exc:
logger.debug("CTP fee batch %s: %s", inst, exc)
errors += 1
if ok > 0:
msg = f"已从 CTP 批量同步 {ok} 个品种手续费"
if errors:
msg += f"{errors} 个跳过)"
return ok, msg
symbols = _collect_main_ths_codes()[:max_symbols]
if not symbols:
return 0, "无主力合约列表"
for ths in symbols:
product = _product_from_instrument(ths)
if not product or product in seen:
continue
seen.add(product)
try:
raw = bridge.query_instrument_commission(ths, mode=mode)
if not raw:
errors += 1
continue
fields = ctp_commission_to_fee_fields(raw, ths)
upsert_fee_rate(product, fields)
ok += 1
time.sleep(0.35)
except Exception as exc:
logger.debug("CTP fee sync %s: %s", ths, exc)
errors += 1
if ok == 0:
return 0, f"CTP 未返回手续费率(失败 {errors} 次),请确认柜台支持查询"
msg = f"已从 CTP 同步 {ok} 个品种手续费"
if errors:
msg += f"{errors} 个跳过)"
return ok, msg
def sync_fee_for_symbol(mode: str, ths_code: str) -> Optional[dict]:
"""单品种按需从 CTP 拉取并缓存。"""
bridge = get_bridge()
if bridge.connected_mode != mode or not bridge.ping():
return None
raw = bridge.query_instrument_commission(ths_code, mode=mode)
if not raw:
return None
product = _product_from_instrument(ths_code)
if not product:
return None
fields = ctp_commission_to_fee_fields(raw, ths_code)
upsert_fee_rate(product, fields)
return fields
+131
View File
@@ -0,0 +1,131 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""CTP 手续费后台同步:每日一次写入数据库,前端只读展示。"""
from __future__ import annotations
import logging
import threading
import time
from datetime import date, datetime
from typing import Callable, Optional
from zoneinfo import ZoneInfo
logger = logging.getLogger(__name__)
TZ = ZoneInfo("Asia/Shanghai")
FEE_SYNC_KEY = "ctp_fee_last_sync"
CHECK_INTERVAL_SEC = 3600
_sync_lock = threading.Lock()
def fee_sync_in_progress() -> bool:
return _sync_lock.locked()
def _today_str() -> str:
return datetime.now(TZ).date().isoformat()
def get_fee_last_sync(get_setting: Callable[[str, str], str]) -> str:
return (get_setting(FEE_SYNC_KEY, "") or "").strip()
def fees_synced_today(get_setting: Callable[[str, str], str]) -> bool:
last = get_fee_last_sync(get_setting)
return bool(last) and last[:10] == _today_str()
def mark_fees_synced(set_setting: Callable[[str, str], None]) -> None:
set_setting(FEE_SYNC_KEY, datetime.now(TZ).isoformat(timespec="seconds"))
def try_daily_ctp_fee_sync(
mode: str,
*,
get_setting: Callable[[str, str], str],
set_setting: Callable[[str, str], None],
force: bool = False,
) -> tuple[int, str]:
"""CTP 已连接且今日未同步时拉取费率入库;force=True 忽略日期限制。"""
if not force and fees_synced_today(get_setting):
return 0, "今日已从 CTP 同步过,无需重复(可点「立即同步」强制刷新)"
with _sync_lock:
if not force and fees_synced_today(get_setting):
return 0, "今日已从 CTP 同步过"
t0 = time.monotonic()
from modules.ctp.ctp_fee_sync import sync_fees_from_ctp
count, msg = sync_fees_from_ctp(mode)
elapsed = time.monotonic() - t0
if count > 0:
mark_fees_synced(set_setting)
msg = f"{msg}(耗时 {elapsed:.1f} 秒)"
logger.info("CTP 手续费每日同步: %s", msg)
elif force:
msg = f"{msg}(耗时 {elapsed:.1f} 秒)"
logger.warning("CTP 手续费强制同步未写入: %s", msg)
return count, msg
def schedule_ctp_fee_sync(
mode: str,
*,
get_setting: Callable[[str, str], str],
set_setting: Callable[[str, str], None],
force: bool = False,
) -> tuple[bool, str]:
"""后台线程同步,避免阻塞 Web 请求。"""
if _sync_lock.locked():
return False, "手续费同步进行中,请稍后再试(约 1~3 分钟)"
def _run() -> None:
try:
try_daily_ctp_fee_sync(
mode,
get_setting=get_setting,
set_setting=set_setting,
force=force,
)
except Exception as exc:
logger.exception("CTP 手续费后台同步失败: %s", exc)
threading.Thread(target=_run, daemon=True, name="ctp-fee-sync-run").start()
if force:
return True, "已在后台开始同步,约 30 秒~2 分钟完成,请稍后刷新本页查看"
return True, "已在后台检查同步,请稍后刷新本页"
def start_ctp_fee_worker(
*,
get_mode_fn: Callable[[], str],
get_setting_fn: Callable[[str, str], str],
set_setting_fn: Callable[[str, str], None],
interval: int = CHECK_INTERVAL_SEC,
) -> None:
"""后台线程:每小时检查,CTP 已连接且当日未同步则自动同步。"""
def _loop() -> None:
time.sleep(20)
while True:
try:
from modules.ctp.vnpy_bridge import ctp_status
mode = get_mode_fn()
st = ctp_status(mode)
if st.get("connected") and not fees_synced_today(get_setting_fn):
try_daily_ctp_fee_sync(
mode,
get_setting=get_setting_fn,
set_setting=set_setting_fn,
force=False,
)
except Exception as exc:
logger.warning("CTP fee worker: %s", exc)
time.sleep(max(300, interval))
threading.Thread(target=_loop, daemon=True, name="ctp-fee-worker").start()
+226
View File
@@ -0,0 +1,226 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""Local HTTP client for the isolated CTP worker process."""
from __future__ import annotations
import json
import os
import time
import urllib.error
import urllib.request
from typing import Any, Optional
DEFAULT_BASE_URL = "http://127.0.0.1:6601"
DEFAULT_TIMEOUT_SEC = 2.5
STATUS_TIMEOUT_SEC = 5.0
MUTATION_TIMEOUT_SEC = 8.0
class CtpWorkerUnavailable(RuntimeError):
"""Raised when the local CTP worker cannot be reached."""
def ctp_role() -> str:
return (os.getenv("QIHUO_CTP_ROLE", "client") or "client").strip().lower()
def is_worker_role() -> bool:
return ctp_role() == "worker"
def worker_base_url() -> str:
return (os.getenv("QIHUO_CTP_WORKER_URL", DEFAULT_BASE_URL) or DEFAULT_BASE_URL).rstrip("/")
def worker_token() -> str:
token = (os.getenv("QIHUO_CTP_WORKER_TOKEN", "") or "").strip()
if token:
return token
# Localhost-only default keeps old deployments working; PM2 sets a shared token.
return "qihuo-local-ctp"
def _request(
method: str,
path: str,
payload: Optional[dict[str, Any]] = None,
*,
timeout: float = DEFAULT_TIMEOUT_SEC,
) -> dict[str, Any]:
url = f"{worker_base_url()}{path}"
body = None
headers = {
"Accept": "application/json",
"X-Qihuo-CTP-Token": worker_token(),
}
if payload is not None:
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
headers["Content-Type"] = "application/json"
req = urllib.request.Request(url, data=body, headers=headers, method=method.upper())
try:
with urllib.request.urlopen(req, timeout=timeout) as resp:
raw = resp.read().decode("utf-8", errors="replace")
except (urllib.error.URLError, TimeoutError, OSError) as exc:
raise CtpWorkerUnavailable(f"CTP worker unavailable: {exc}") from exc
if not raw:
return {}
try:
data = json.loads(raw)
except json.JSONDecodeError as exc:
raise CtpWorkerUnavailable(f"CTP worker returned invalid JSON: {raw[:120]}") from exc
if not isinstance(data, dict):
raise CtpWorkerUnavailable("CTP worker returned non-object JSON")
if data.get("ok") is False:
raise RuntimeError(str(data.get("error") or "CTP worker request failed"))
return data
def get(path: str, *, timeout: float = DEFAULT_TIMEOUT_SEC) -> dict[str, Any]:
return _request("GET", path, timeout=timeout)
def post(
path: str,
payload: Optional[dict[str, Any]] = None,
*,
timeout: float = DEFAULT_TIMEOUT_SEC,
) -> dict[str, Any]:
return _request("POST", path, payload or {}, timeout=timeout)
def health() -> dict[str, Any]:
try:
return get("/health", timeout=1.0)
except Exception as exc:
return {
"ok": False,
"worker_online": False,
"error": str(exc),
"ts": time.time(),
}
def status(mode: str) -> dict[str, Any]:
try:
data = get(f"/ctp/status?mode={mode}", timeout=STATUS_TIMEOUT_SEC)
return dict(data.get("status") or {})
except Exception as exc:
return {
"connected": False,
"connecting": False,
"worker_online": False,
"last_error": f"CTP worker 离线或重启中:{exc}",
}
def connect(mode: str, *, force: bool = False) -> dict[str, Any]:
data = post(
"/ctp/connect",
{"mode": mode, "force": bool(force)},
timeout=MUTATION_TIMEOUT_SEC,
)
return dict(data.get("status") or data)
def start_connect(mode: str, *, force: bool = False, scheduled: bool = False) -> dict[str, Any]:
return post(
"/ctp/start_connect",
{"mode": mode, "force": bool(force), "scheduled": bool(scheduled)},
timeout=MUTATION_TIMEOUT_SEC,
)
def disconnect(*, set_disabled_hint: bool = False) -> None:
post(
"/ctp/disconnect",
{"set_disabled_hint": bool(set_disabled_hint)},
timeout=MUTATION_TIMEOUT_SEC,
)
def account(mode: str) -> dict[str, Any]:
data = get(f"/ctp/account?mode={mode}")
return dict(data.get("account") or {})
def positions(
mode: str,
*,
refresh_if_empty: bool = True,
refresh_margin: bool = False,
) -> list[dict[str, Any]]:
data = post(
"/ctp/positions",
{
"mode": mode,
"refresh_if_empty": bool(refresh_if_empty),
"refresh_margin": bool(refresh_margin),
},
)
return list(data.get("positions") or [])
def trades(mode: str, *, refresh: bool = False) -> list[dict[str, Any]]:
data = post("/ctp/trades", {"mode": mode, "refresh": bool(refresh)})
return list(data.get("trades") or [])
def active_orders(mode: str) -> list[dict[str, Any]]:
data = get(f"/ctp/active_orders?mode={mode}")
return list(data.get("orders") or [])
def tick_price(mode: str, symbol: str) -> Optional[float]:
data = post("/ctp/tick_price", {"mode": mode, "symbol": symbol})
value = data.get("price")
return float(value) if value not in (None, "") else None
def tick_detail(mode: str, symbol: str) -> dict[str, Any]:
data = post("/ctp/tick_detail", {"mode": mode, "symbol": symbol})
return dict(data.get("detail") or {})
def estimate_margin_one_lot(
mode: str,
symbol: str,
price: float,
*,
direction: str = "long",
) -> Optional[float]:
data = post(
"/ctp/estimate_margin_one_lot",
{"mode": mode, "symbol": symbol, "price": price, "direction": direction},
)
value = data.get("margin")
return float(value) if value not in (None, "") else None
def contract_spec(mode: str, symbol: str) -> Optional[dict[str, Any]]:
data = post("/ctp/contract_spec", {"mode": mode, "symbol": symbol})
spec = data.get("spec")
return dict(spec) if isinstance(spec, dict) else None
def send_order(payload: dict[str, Any]) -> dict[str, Any]:
return post("/ctp/order", payload, timeout=MUTATION_TIMEOUT_SEC)
def cancel_order(mode: str, vt_orderid: str) -> bool:
data = post(
"/ctp/cancel",
{"mode": mode, "vt_orderid": vt_orderid},
timeout=MUTATION_TIMEOUT_SEC,
)
return bool(data.get("cancelled"))
def bridge_action(action: str, payload: Optional[dict[str, Any]] = None) -> dict[str, Any]:
return post(
f"/ctp/bridge/{action}",
payload or {},
timeout=MUTATION_TIMEOUT_SEC,
)
+89
View File
@@ -0,0 +1,89 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""CTP tick 聚合 K 线(1 分钟为基础,再合成各周期)。"""
from __future__ import annotations
import logging
from typing import Optional
from modules.market.kline_chart import (
PERIOD_MINUTES,
_aggregate_bars,
_bar_datetime,
_merge_bars,
_timeshare_session,
_weekly_from_daily,
)
logger = logging.getLogger(__name__)
PERIOD_AGG = {
"2m": 2,
"3m": 3,
"5m": 5,
"15m": 15,
"30m": 30,
"1h": 60,
"2h": 120,
"4h": 240,
}
def _daily_from_1m(bars_1m: list) -> list:
if not bars_1m:
return []
buckets: dict[str, list] = {}
for bar in bars_1m:
dt = _bar_datetime(bar)
if not dt:
continue
key = dt.strftime("%Y-%m-%d")
buckets.setdefault(key, []).append(bar)
out = []
for day in sorted(buckets.keys()):
chunk = buckets[day]
merged = _merge_bars(chunk)
merged["d"] = day + " 15:00:00"
out.append(merged)
return out
def compose_period_bars(bars_1m: list, period: str) -> list:
p = (period or "15m").lower()
if p == "timeshare":
return _timeshare_session(bars_1m)
if p in ("1d", "d"):
return _daily_from_1m(bars_1m)
if p == "w":
return _weekly_from_daily(_daily_from_1m(bars_1m))
if p == "1m":
return list(bars_1m)
n = PERIOD_AGG.get(p)
if n:
return _aggregate_bars(bars_1m, n)
if p in PERIOD_MINUTES:
try:
n = int(PERIOD_MINUTES[p])
return _aggregate_bars(bars_1m, n)
except (TypeError, ValueError):
pass
return list(bars_1m)
def fetch_ctp_klines(symbol: str, period: str, mode: str) -> Optional[list]:
"""CTP 已连接时由 tick 聚合 K 线;失败返回 None。"""
try:
from modules.ctp.vnpy_bridge import ctp_status, get_bridge
if not ctp_status(mode).get("connected"):
return None
bars_1m = get_bridge().get_kline_bars_1m(symbol, mode=mode)
if not bars_1m:
return None
return compose_period_bars(bars_1m, period)
except Exception as exc:
logger.debug("fetch_ctp_klines %s %s: %s", symbol, period, exc)
return None
+116
View File
@@ -0,0 +1,116 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""CTP 按计划自动连接:盘前 30 分钟检查;交易时段断线后台重连;不自动强制断开。"""
from __future__ import annotations
import logging
import os
import threading
import time
from typing import Callable
from modules.market.market_sessions import (
in_premarket_connect_window,
in_postmarket_grace_window,
is_trading_session,
should_keep_ctp_connected,
)
from modules.ctp.vnpy_bridge import ctp_start_connect, ctp_status
logger = logging.getLogger(__name__)
CHECK_INTERVAL_SEC = 60
TRADING_CHECK_INTERVAL_SEC = 15
PREMARKET_CHECK_INTERVAL_SEC = 30
DEFAULT_MINUTES_BEFORE = 30
DEFAULT_MINUTES_AFTER = 30
def premarket_minutes_before() -> int:
try:
return max(5, int(os.getenv("CTP_PREMARKET_MINUTES", str(DEFAULT_MINUTES_BEFORE))))
except (TypeError, ValueError):
return DEFAULT_MINUTES_BEFORE
def postmarket_minutes_after() -> int:
try:
return max(5, int(os.getenv("CTP_POSTMARKET_MINUTES", str(DEFAULT_MINUTES_AFTER))))
except (TypeError, ValueError):
return DEFAULT_MINUTES_AFTER
def _scheduled_connect_enabled() -> bool:
return (os.getenv("CTP_PREMARKET_CONNECT", "true") or "true").strip().lower() in (
"1",
"true",
"yes",
)
def should_auto_connect_now(*, minutes_before: int | None = None) -> bool:
"""是否应保持/发起 CTP 连接(供重连、权限判断复用)。"""
mins_b = premarket_minutes_before() if minutes_before is None else minutes_before
mins_a = postmarket_minutes_after()
if not _scheduled_connect_enabled() and not is_trading_session():
if not in_postmarket_grace_window(minutes_after=mins_a):
return False
return should_keep_ctp_connected(
minutes_before=mins_b,
minutes_after=mins_a,
)
def start_ctp_premarket_connect_worker(
*,
get_mode_fn: Callable[[], str],
get_setting_fn: Callable[[str, str], str] | None = None,
interval: int = CHECK_INTERVAL_SEC,
) -> None:
"""盘前 30 分钟:未连接则自动连;已连接则不重复发起。不自动强制断开。"""
def _loop() -> None:
time.sleep(10)
while True:
sleep_sec = max(30, interval)
try:
mins_b = premarket_minutes_before()
mins_a = postmarket_minutes_after()
keep = should_auto_connect_now()
mode = get_mode_fn()
st = ctp_status(mode)
if keep:
if (
not st.get("connected")
and not st.get("connecting")
and int(st.get("login_cooldown_sec") or 0) <= 0
):
info = ctp_start_connect(mode, force=False, scheduled=True)
if info.get("started"):
if is_trading_session():
logger.info("交易时段内自动连接 CTP [%s]", mode)
elif in_postmarket_grace_window(minutes_after=mins_a):
logger.info(
"盘后宽限期内恢复 CTP 连接 [%s](收盘后 %d 分钟内)",
mode,
mins_a,
)
else:
logger.info(
"盘前自动连接 CTP [%s](开盘前 %d 分钟)",
mode,
mins_b,
)
if is_trading_session():
sleep_sec = TRADING_CHECK_INTERVAL_SEC
elif in_premarket_connect_window(minutes_before=mins_b):
sleep_sec = PREMARKET_CHECK_INTERVAL_SEC
except Exception as exc:
logger.warning("CTP scheduled connect worker: %s", exc)
time.sleep(sleep_sec)
threading.Thread(target=_loop, daemon=True, name="ctp-premarket-connect").start()
+59
View File
@@ -0,0 +1,59 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""CTP 断线自动重连(后台线程)。"""
from __future__ import annotations
import logging
import os
import threading
import time
from typing import Callable
from modules.ctp.ctp_premarket_connect import premarket_minutes_before, should_auto_connect_now
from modules.market.market_sessions import in_premarket_connect_window, is_trading_session
from modules.ctp.vnpy_bridge import ctp_try_auto_reconnect
logger = logging.getLogger(__name__)
RECONNECT_INTERVAL_SEC = 60
TRADING_RECONNECT_INTERVAL_SEC = 15
PREMARKET_RECONNECT_INTERVAL_SEC = 30
def _auto_reconnect_enabled() -> bool:
return (os.getenv("CTP_AUTO_RECONNECT", "true") or "true").strip().lower() in (
"1",
"true",
"yes",
)
def start_ctp_reconnect_worker(
*,
get_mode_fn: Callable[[], str],
get_setting_fn: Callable[[str, str], str] | None = None,
interval: int = RECONNECT_INTERVAL_SEC,
) -> None:
"""交易时段 / 盘前窗口内检测 CTP;断线则后台自动重连。"""
def _loop() -> None:
while True:
sleep_sec = max(5, interval)
try:
if _auto_reconnect_enabled() and should_auto_connect_now():
mode = get_mode_fn()
ctp_try_auto_reconnect(mode)
if is_trading_session():
sleep_sec = TRADING_RECONNECT_INTERVAL_SEC
elif in_premarket_connect_window(
minutes_before=premarket_minutes_before(),
):
sleep_sec = PREMARKET_RECONNECT_INTERVAL_SEC
except Exception as exc:
logger.warning("CTP reconnect worker: %s", exc)
time.sleep(sleep_sec)
threading.Thread(target=_loop, daemon=True, name="ctp-reconnect-worker").start()
+154
View File
@@ -0,0 +1,154 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""CTP / SimNow 配置:系统设置优先,.env 作兜底。"""
from __future__ import annotations
import os
from typing import Any, Callable
# (db_key, env_key, vnpy字段名, 默认值)
SIMNOW_FIELDS: tuple[tuple[str, str, str, str], ...] = (
("simnow_user", "SIMNOW_USER", "用户名", ""),
("simnow_password", "SIMNOW_PASSWORD", "密码", ""),
("simnow_broker_id", "SIMNOW_BROKER_ID", "经纪商代码", "9999"),
("simnow_td_address", "SIMNOW_TD_ADDRESS", "交易服务器", "tcp://180.168.146.187:10201"),
("simnow_md_address", "SIMNOW_MD_ADDRESS", "行情服务器", "tcp://180.168.146.187:10211"),
("simnow_app_id", "SIMNOW_APP_ID", "产品名称", "simnow_client_test"),
("simnow_auth_code", "SIMNOW_AUTH_CODE", "授权编码", "0000000000000000"),
("simnow_env", "SIMNOW_ENV", "柜台环境", "实盘"),
)
LIVE_FIELDS: tuple[tuple[str, str, str, str], ...] = (
("ctp_live_user", "CTP_LIVE_USER", "用户名", ""),
("ctp_live_password", "CTP_LIVE_PASSWORD", "密码", ""),
("ctp_live_broker_id", "CTP_LIVE_BROKER_ID", "经纪商代码", ""),
("ctp_live_td_address", "CTP_LIVE_TD_ADDRESS", "交易服务器", ""),
("ctp_live_md_address", "CTP_LIVE_MD_ADDRESS", "行情服务器", ""),
("ctp_live_app_id", "CTP_LIVE_APP_ID", "产品名称", ""),
("ctp_live_auth_code", "CTP_LIVE_AUTH_CODE", "授权编码", ""),
("ctp_live_env", "CTP_LIVE_ENV", "柜台环境", "实盘"),
)
PASSWORD_DB_KEYS = frozenset({"simnow_password", "ctp_live_password"})
CTP_AUTO_CONNECT_KEY = "ctp_auto_connect"
CTP_DISABLED_HINT = "CTP 自动连接已关闭(非交易时段不重连;开盘前 30 分钟及交易时段仍会按计划连接;断开请手动操作)"
def is_ctp_auto_connect_enabled(get_setting=None) -> bool:
"""系统设置:是否允许手动连接及非交易时段自动重连(盘前/交易时段计划连接不受此限制)。"""
if get_setting is None:
from modules.fees.fee_specs import get_setting as _gs
get_setting = _gs
val = (get_setting(CTP_AUTO_CONNECT_KEY, "1") or "1").strip().lower()
return val in ("1", "true", "yes", "on")
def save_ctp_auto_connect(form: Any, set_setting: Callable[[str, str], None]) -> bool:
enabled = (form.get("ctp_auto_connect") or "").strip().lower() in (
"1",
"on",
"true",
"yes",
)
set_setting(CTP_AUTO_CONNECT_KEY, "1" if enabled else "0")
return enabled
def _get_db_setting(key: str, default: str = "") -> str:
from modules.fees.fee_specs import get_setting
return (get_setting(key, default) or default).strip()
def resolve_ctp_value(db_key: str, env_key: str, default: str = "") -> str:
v = _get_db_setting(db_key, "")
if v:
return v
return (os.getenv(env_key) or default).strip()
def _build_setting_dict(fields: tuple[tuple[str, str, str, str], ...]) -> dict[str, str]:
out: dict[str, str] = {}
for db_key, env_key, vnpy_key, default in fields:
out[vnpy_key] = resolve_ctp_value(db_key, env_key, default)
return out
def simnow_setting_dict() -> dict[str, str]:
return _build_setting_dict(SIMNOW_FIELDS)
def live_setting_dict() -> dict[str, str]:
return _build_setting_dict(LIVE_FIELDS)
def seed_ctp_settings_from_env(set_setting: Callable[[str, str], None]) -> None:
"""首次启动:将 .env 中已有 CTP 配置写入 settings 表。"""
for db_key, env_key, _, _ in (*SIMNOW_FIELDS, *LIVE_FIELDS):
if _get_db_setting(db_key, ""):
continue
env_val = (os.getenv(env_key) or "").strip()
if env_val:
set_setting(db_key, env_val)
def get_ctp_settings_for_ui() -> dict[str, Any]:
ui: dict[str, Any] = {}
for db_key, env_key, _, default in SIMNOW_FIELDS:
ui[db_key] = resolve_ctp_value(db_key, env_key, default)
if db_key in PASSWORD_DB_KEYS:
ui[f"{db_key}_set"] = bool(ui[db_key])
ui[db_key] = ""
for db_key, env_key, _, default in LIVE_FIELDS:
ui[db_key] = resolve_ctp_value(db_key, env_key, default)
if db_key in PASSWORD_DB_KEYS:
ui[f"{db_key}_set"] = bool(ui[db_key])
ui[db_key] = ""
ui["ctp_auto_connect"] = is_ctp_auto_connect_enabled()
return ui
def save_ctp_settings_from_form(
form: Any,
set_setting: Callable[[str, str], None],
) -> dict[str, Any]:
"""保存 CTP 配置;密码留空表示不修改。返回摘要供页面提示。"""
passwords_updated: list[str] = []
passwords_submitted_empty: list[str] = []
for db_key, _, _, default in SIMNOW_FIELDS:
if db_key in PASSWORD_DB_KEYS:
raw = form.get(db_key)
val = (raw or "").strip()
if val:
set_setting(db_key, val)
passwords_updated.append(db_key)
else:
passwords_submitted_empty.append(db_key)
continue
val = (form.get(db_key) or "").strip()
set_setting(db_key, val or default)
for db_key, _, _, default in LIVE_FIELDS:
if db_key in PASSWORD_DB_KEYS:
raw = form.get(db_key)
val = (raw or "").strip()
if val:
set_setting(db_key, val)
passwords_updated.append(db_key)
else:
passwords_submitted_empty.append(db_key)
continue
val = (form.get(db_key) or "").strip()
if default or val:
set_setting(db_key, val or default)
return {
"passwords_updated": passwords_updated,
"passwords_submitted_empty": passwords_submitted_empty,
}
+66
View File
@@ -0,0 +1,66 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""同花顺合约代码 → vnpy Symbol + Exchange。"""
from __future__ import annotations
import re
from typing import Optional, Tuple
from modules.core.symbols import ths_to_codes
try:
from vnpy.trader.constant import Exchange
except ImportError:
Exchange = None # type: ignore
_EX_MAP = {
"SHFE": "SHFE",
"DCE": "DCE",
"CZCE": "CZCE",
"CFFEX": "CFFEX",
"INE": "INE",
}
def ths_to_vnpy_symbol(ths_code: str) -> Tuple[str, str]:
"""
返回 (symbol, exchange_enum_name)。
例:rb2610 → rb2610, SHFESR609 → SR609, CZCE
"""
code = (ths_code or "").strip()
codes = ths_to_codes(code)
ex = (codes.get("ex") if codes else None)
if not ex and codes:
mc = (codes.get("market_code") or "")
if "." in mc:
ex = mc.rsplit(".", 1)[-1]
ex = _EX_MAP.get(ex or "SHFE", "SHFE")
m = re.match(r"^([A-Za-z]+)(\d+)$", code)
if not m:
return code, ex
letters, digits = m.group(1), m.group(2)
if ex == "CZCE":
# 郑商所 CTP 常为大写 + 3 位年月(如 SR509);4 位则取后 3 位
sym = letters.upper() + (digits[-3:] if len(digits) >= 3 else digits)
else:
sym = letters.lower() + digits
return sym, ex
def to_vnpy_exchange(ex_name: str):
if Exchange is None:
raise ImportError("vnpy 未安装")
mapping = {
"SHFE": Exchange.SHFE,
"DCE": Exchange.DCE,
"CZCE": Exchange.CZCE,
"CFFEX": Exchange.CFFEX,
"INE": Exchange.INE,
}
ex = mapping.get((ex_name or "").upper())
if ex is None:
raise ValueError(f"未知交易所: {ex_name}")
return ex
+337
View File
@@ -0,0 +1,337 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""从 CTP 柜台同步成交,写入 trade_logs(以交易所成交为准)。"""
from __future__ import annotations
import logging
from collections import defaultdict
from datetime import datetime
from typing import Any, Callable, Optional
from zoneinfo import ZoneInfo
from modules.core.contract_specs import calc_position_metrics
from modules.ctp.ctp_symbol import ths_to_vnpy_symbol
from modules.fees.fee_specs import calc_round_trip_fee
from modules.core.symbols import ths_to_codes
from modules.trading.trade_log_lib import (
calc_equity_after,
purge_duplicate_local_trade_logs,
ensure_trade_log_columns,
refresh_trade_log_equity_chain,
)
from modules.ctp.vnpy_bridge import ctp_list_trades, ctp_status
logger = logging.getLogger(__name__)
TZ = ZoneInfo("Asia/Shanghai")
def _match_symbol(ctp_sym: str, ths: str) -> bool:
a = (ctp_sym or "").lower()
b = (ths or "").lower()
if a == b:
return True
if a and b and a.split(".")[0] == b.split(".")[0]:
return True
try:
vnpy_sym, _ = ths_to_vnpy_symbol(ths)
if a == vnpy_sym.lower():
return True
except Exception:
pass
return False
def _to_ths_code(symbol: str) -> str:
sym = (symbol or "").strip()
if not sym:
return ""
codes = ths_to_codes(sym)
if codes:
return codes.get("ths_code") or sym
return sym.lower()
def _allocate_commission(total_comm: float, matched: int, total_lots: int) -> float:
if total_comm <= 0 or matched <= 0 or total_lots <= 0:
return 0.0
return round(total_comm * matched / total_lots, 2)
def build_round_trips(trades: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""按 FIFO 将开/平仓成交配对为完整回合。"""
stacks: dict[tuple[str, str], list[dict[str, Any]]] = defaultdict(list)
trips: list[dict[str, Any]] = []
ordered = sorted(
trades,
key=lambda t: ((t.get("datetime") or ""), str(t.get("trade_id") or "")),
)
for t in ordered:
sym = (t.get("symbol") or "").lower()
pos_dir = (t.get("position_direction") or "long").strip().lower()
offset = (t.get("offset") or "open").strip().lower()
lots = int(t.get("lots") or 0)
if not sym or lots <= 0:
continue
key = (sym, pos_dir)
if offset == "open":
stacks[key].append({
**t,
"remaining": lots,
"commission_remaining": float(t.get("commission") or 0),
})
continue
close_lots_total = lots
close_lots_left = lots
close_price = float(t.get("price") or 0)
close_time = t.get("datetime") or ""
close_trade_id = str(t.get("trade_id") or "")
close_comm_total = float(t.get("commission") or 0)
while close_lots_left > 0 and stacks[key]:
open_t = stacks[key][0]
open_rem = int(open_t.get("remaining") or 0)
matched = min(close_lots_left, open_rem)
if matched <= 0:
stacks[key].pop(0)
continue
open_comm_rem = float(open_t.get("commission_remaining") or 0)
open_comm_share = (
_allocate_commission(open_comm_rem, matched, open_rem)
if open_rem > 0 else 0.0
)
close_comm_share = _allocate_commission(
close_comm_total, matched, close_lots_total,
)
open_t["remaining"] = open_rem - matched
open_t["commission_remaining"] = round(
max(0.0, open_comm_rem - open_comm_share), 2,
)
if open_t["remaining"] <= 0:
stacks[key].pop(0)
close_lots_left -= matched
open_trade_id = str(open_t.get("trade_id") or "")
ctp_key = f"{open_trade_id}|{close_trade_id}|{sym}|{pos_dir}|{matched}"
trip_fee = round(open_comm_share + close_comm_share, 2)
trips.append({
"ctp_trade_key": ctp_key,
"symbol": sym,
"ths_code": _to_ths_code(sym),
"direction": pos_dir,
"lots": matched,
"entry_price": float(open_t.get("price") or 0),
"close_price": close_price,
"open_time": open_t.get("datetime") or "",
"close_time": close_time,
"open_trade_id": open_trade_id,
"close_trade_id": close_trade_id,
"fee": trip_fee,
"fee_from_ctp": trip_fee > 0,
})
return trips
def _find_monitor_meta(
conn,
*,
symbol: str,
direction: str,
open_time: str,
match_symbol_fn: Callable[[str, str], bool] | None = None,
) -> dict[str, Any]:
match = match_symbol_fn or _match_symbol
direction = (direction or "long").strip().lower()
best: Optional[dict[str, Any]] = None
for r in conn.execute(
"SELECT * FROM trade_order_monitors ORDER BY id DESC LIMIT 200"
).fetchall():
row = dict(r)
if (row.get("direction") or "long").strip().lower() != direction:
continue
if not match(symbol, row.get("symbol") or ""):
continue
if best is None:
best = row
continue
ot = (row.get("open_time") or "").strip()
if open_time and ot and abs(len(ot) - len(open_time)) <= 2 and ot[:16] == open_time[:16]:
return row
return best or {}
def _holding_minutes(open_time: str, close_time: str) -> int:
try:
from app import holding_to_minutes
return int(holding_to_minutes(open_time, close_time) or 0)
except Exception:
return 0
def sync_trade_logs_from_ctp(
conn,
mode: str,
*,
capital: float = 0.0,
trading_mode: str = "simulation",
) -> dict[str, Any]:
"""查询 CTP 成交并 upsert 到 trade_logs。返回同步摘要。"""
stats = {"synced": 0, "updated": 0, "skipped": 0, "connected": False}
if not ctp_status(mode).get("connected"):
return stats
stats["connected"] = True
ensure_trade_log_columns(conn)
try:
conn.execute("ALTER TABLE trade_logs ADD COLUMN source TEXT DEFAULT 'local'")
except Exception:
pass
try:
conn.execute("ALTER TABLE trade_logs ADD COLUMN ctp_trade_key TEXT")
except Exception:
pass
trades = ctp_list_trades(mode, refresh=True)
trips = build_round_trips(trades)
for trip in trips:
key = trip.get("ctp_trade_key") or ""
if not key:
stats["skipped"] += 1
continue
existing = conn.execute(
"SELECT id FROM trade_logs WHERE ctp_trade_key=?",
(key,),
).fetchone()
ths = trip.get("ths_code") or trip.get("symbol") or ""
codes = ths_to_codes(ths) or {}
direction = trip.get("direction") or "long"
entry = float(trip.get("entry_price") or 0)
close_px = float(trip.get("close_price") or 0)
lots = float(trip.get("lots") or 0)
open_time = trip.get("open_time") or ""
close_time = trip.get("close_time") or datetime.now(TZ).strftime("%Y-%m-%dT%H:%M")
mon = _find_monitor_meta(
conn,
symbol=trip.get("symbol") or ths,
direction=direction,
open_time=open_time,
)
sl = mon.get("stop_loss")
tp = mon.get("take_profit")
try:
sl_f = float(sl) if sl is not None else entry
tp_f = float(tp) if tp is not None else entry
except (TypeError, ValueError):
sl_f, tp_f = entry, entry
metrics = calc_position_metrics(
direction, entry, sl_f, tp_f, lots, close_px, capital, ths,
)
pnl = float(metrics.get("float_pnl") or 0)
trip_fee = float(trip.get("fee") or 0)
if trip_fee > 0:
fee = round(trip_fee, 2)
else:
fee = calc_round_trip_fee(
ths, entry, close_px, lots, open_time, close_time, trading_mode=trading_mode,
)
pnl_net = round(pnl - fee, 2)
margin_pct = metrics.get("position_pct")
equity_after = calc_equity_after(capital, pnl_net)
minutes = _holding_minutes(open_time, close_time)
result = "CTP同步"
monitor_type = mon.get("monitor_type") or "CTP同步"
row_vals = (
ths,
codes.get("name") or mon.get("symbol_name") or ths,
codes.get("market_code") or mon.get("market_code") or "",
codes.get("sina_code") or mon.get("sina_code") or "",
monitor_type,
direction,
entry,
sl if sl is not None else None,
tp if tp is not None else None,
close_px,
lots,
metrics.get("margin"),
margin_pct,
minutes,
open_time,
close_time,
pnl,
fee,
pnl_net,
equity_after,
result,
)
if existing:
conn.execute(
"""UPDATE trade_logs SET
symbol=?, symbol_name=?, market_code=?, sina_code=?, monitor_type=?,
direction=?, entry_price=?, stop_loss=?, take_profit=?, close_price=?,
lots=?, margin=?, margin_pct=?, holding_minutes=?, open_time=?, close_time=?,
pnl=?, fee=?, pnl_net=?, equity_after=?, result=?, source='ctp', verified=1
WHERE ctp_trade_key=?""",
row_vals + (key,),
)
stats["updated"] += 1
else:
conn.execute(
"""INSERT INTO trade_logs
(symbol, symbol_name, market_code, sina_code, monitor_type, direction,
entry_price, stop_loss, take_profit, close_price, lots, margin,
margin_pct, holding_minutes, open_time, close_time, pnl, fee, pnl_net,
equity_after, result, source, ctp_trade_key, verified)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
row_vals + ("ctp", key, 1),
)
stats["synced"] += 1
try:
from modules.trading.trade_notify import notify_trade_log_close
from modules.core.trading_context import trading_mode_label
from app import get_setting, send_wechat_msg
from modules.notify.ai_worker import schedule_ai_event_analysis
from modules.core.db_conn import DB_PATH
notify_trade_log_close(
send_wechat=send_wechat_msg,
get_setting=get_setting,
mode_label=trading_mode_label(get_setting),
capital=capital,
sym=ths,
symbol_name=codes.get("name") or mon.get("symbol_name") or ths,
direction=direction,
entry=entry,
close_price=close_px,
sl=float(sl) if sl is not None else None,
tp=float(tp) if tp is not None else None,
lots=lots,
pnl_net=pnl_net,
equity_after=equity_after,
holding_minutes=minutes,
result=result,
monitor_type=monitor_type,
schedule_ai_fn=schedule_ai_event_analysis,
db_path=DB_PATH,
)
except Exception as exc:
logger.debug("ctp close notify: %s", exc)
if stats["synced"] or stats["updated"]:
try:
from modules.stats.stats_engine import refresh_stats_cache
refresh_stats_cache(conn, capital)
except Exception as exc:
logger.debug("stats refresh after ctp trade sync: %s", exc)
purged = purge_duplicate_local_trade_logs(conn)
if purged:
stats["purged"] = purged
try:
refresh_trade_log_equity_chain(conn)
except Exception as exc:
logger.debug("equity chain refresh after ctp sync: %s", exc)
return stats
+270
View File
@@ -0,0 +1,270 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 详见 LICENSE.zh-CN.txt
"""CTP 权威内存簿:委托、持仓、同步状态(事件增量 + 定期全量校准)。"""
from __future__ import annotations
import logging
import threading
import time
from typing import Any, Callable, Optional
logger = logging.getLogger(__name__)
CALIBRATE_INTERVAL_SEC = 30.0
def position_key(exchange: str, symbol: str, direction: str) -> str:
"""统一持仓键:exchange|symbol|direction"""
ex = (exchange or "").strip().upper()
sym = (symbol or "").strip().lower()
d = (direction or "long").strip().lower()
if ex:
return f"{ex}|{sym}|{d}"
return f"{sym}|{d}"
def parse_position_key(key: str) -> tuple[str, str, str]:
parts = (key or "").split("|")
if len(parts) >= 3:
return parts[0], parts[1], parts[2]
if len(parts) == 2:
return "", parts[0], parts[1]
return "", (key or "").lower(), "long"
def reconcile_position_avg(
old: Optional[dict[str, Any]],
new: dict[str, Any],
tick: Optional[float],
*,
trades: Optional[list[dict[str, Any]]] = None,
ths_sym: str = "",
) -> dict[str, Any]:
"""手数变化时采用柜台回报均价;手数不变时保持已锁定柜台价。"""
del tick, trades
from modules.ctp.ctp_entry_price import round_to_tick
row = dict(new)
lots = int(row.get("lots") or 0)
if lots <= 0:
return row
old_lots = int(old.get("lots") or 0) if old else 0
lots_changed = not old or old_lots != lots
sym = ths_sym or (row.get("symbol") or "")
pos_avg = float(row.get("avg_price") or 0)
if pos_avg > 0:
row["avg_price"] = round_to_tick(pos_avg, sym)
row["avg_price_locked"] = True
return row
if not lots_changed and old and float(old.get("avg_price") or 0) > 0:
row["avg_price"] = float(old["avg_price"])
row["avg_price_locked"] = True
return row
class CtpTradingState:
"""进程内 CTP 快照:柜台回报为准,SQLite 仅挂 SL/TP 元数据。"""
def __init__(self) -> None:
self._lock = threading.RLock()
self._orders: dict[str, dict[str, Any]] = {}
self._positions: dict[str, dict[str, Any]] = {}
self._tick_prices: dict[str, float] = {}
self._sync_state = "idle"
self._last_event_ts: float = 0.0
self._last_calibrate_ts: float = 0.0
self._on_change: Optional[Callable[[], None]] = None
def set_change_callback(self, fn: Optional[Callable[[], None]]) -> None:
self._on_change = fn
def _notify(self) -> None:
self._last_event_ts = time.time()
fn = self._on_change
if fn:
try:
fn()
except Exception as exc:
logger.debug("trading state change callback: %s", exc)
@property
def sync_state(self) -> str:
with self._lock:
return self._sync_state
def sync_label(self) -> str:
st = self.sync_state
if st == "syncing":
return "同步中…"
if st == "ready":
return "已同步"
return ""
def begin_sync(self) -> None:
with self._lock:
self._sync_state = "syncing"
def finish_sync(self) -> None:
with self._lock:
self._sync_state = "ready"
self._last_calibrate_ts = time.time()
def needs_calibrate(self) -> bool:
with self._lock:
if self._sync_state == "idle":
return True
return (time.time() - self._last_calibrate_ts) >= CALIBRATE_INTERVAL_SEC
def upsert_order(self, row: dict[str, Any], *, notify: bool = True) -> None:
oid = str(row.get("order_id") or row.get("vt_order_id") or "").strip()
if not oid:
return
with self._lock:
self._orders[oid] = dict(row)
if notify:
self._notify()
def remove_order(self, order_id: str, *, notify: bool = True) -> None:
oid = (order_id or "").strip()
if not oid:
return
removed = False
with self._lock:
if oid in self._orders:
del self._orders[oid]
removed = True
else:
for k in list(self._orders.keys()):
if k == oid or k.endswith(oid) or oid.endswith(k):
del self._orders[k]
removed = True
break
if removed and notify:
self._notify()
def get_position(self, pk: str) -> Optional[dict[str, Any]]:
with self._lock:
row = self._positions.get(pk)
return dict(row) if row else None
def try_lock_entry_prices(self) -> bool:
"""均价以柜台为准,不按 tick 反推(避免均价随行情跳动)。"""
return False
def upsert_position(
self,
row: dict[str, Any],
*,
notify: bool = True,
trades: Optional[list[dict[str, Any]]] = None,
ths_sym: str = "",
) -> None:
lots = int(row.get("lots") or 0)
ex = row.get("exchange") or ""
sym = row.get("symbol") or ""
direction = row.get("direction") or "long"
pk = position_key(ex, sym, direction)
tick = self.get_tick_price(ex, sym)
with self._lock:
if lots <= 0:
self._positions.pop(pk, None)
else:
old = self._positions.get(pk)
row = reconcile_position_avg(
old, dict(row), tick, trades=trades, ths_sym=ths_sym or sym,
)
row["position_key"] = pk
self._positions[pk] = row
if notify:
self._notify()
def remove_position(self, pk: str, *, notify: bool = True) -> None:
with self._lock:
self._positions.pop(pk, None)
if notify:
self._notify()
def set_tick_price(self, exchange: str, symbol: str, price: float) -> None:
if not symbol or price <= 0:
return
key = f"{(exchange or '').upper()}|{symbol.lower()}"
with self._lock:
self._tick_prices[key] = float(price)
def get_tick_price(self, exchange: str, symbol: str) -> Optional[float]:
key = f"{(exchange or '').upper()}|{symbol.lower()}"
with self._lock:
return self._tick_prices.get(key)
def get_active_orders(self) -> list[dict[str, Any]]:
with self._lock:
return list(self._orders.values())
def get_positions(self) -> list[dict[str, Any]]:
with self._lock:
return list(self._positions.values())
def position_keys(self) -> set[str]:
with self._lock:
return set(self._positions.keys())
def clear(self) -> None:
with self._lock:
self._orders.clear()
self._positions.clear()
self._tick_prices.clear()
self._sync_state = "idle"
def calibrate_from_lists(
self,
orders: list[dict[str, Any]],
positions: list[dict[str, Any]],
*,
trades: Optional[list[dict[str, Any]]] = None,
ths_for_vnpy_sym: Optional[Callable[[str, str], str]] = None,
preserve_positions_if_margin: float = 0.0,
) -> None:
"""全量校准:以 vnpy 内存为准重建订单/持仓簿。"""
self.begin_sync()
new_orders: dict[str, dict[str, Any]] = {}
for o in orders or []:
oid = str(o.get("order_id") or o.get("vt_order_id") or "").strip()
if oid:
new_orders[oid] = dict(o)
new_positions: dict[str, dict[str, Any]] = {}
for p in positions or []:
lots = int(p.get("lots") or 0)
if lots <= 0:
continue
ex = p.get("exchange") or ""
sym = p.get("symbol") or ""
direction = p.get("direction") or "long"
pk = position_key(ex, sym, direction)
row = dict(p)
row["position_key"] = pk
old = self._positions.get(pk)
tick = self.get_tick_price(ex, sym)
ths = sym
if ths_for_vnpy_sym:
try:
ths = ths_for_vnpy_sym(sym, ex) or sym
except Exception:
ths = sym
new_positions[pk] = reconcile_position_avg(
old, row, tick, trades=trades, ths_sym=ths,
)
if not new_positions and self._positions and preserve_positions_if_margin > 0:
with self._lock:
new_positions = {k: dict(v) for k, v in self._positions.items()}
with self._lock:
self._orders = new_orders
self._positions = new_positions
self.finish_sync()
self._notify()
trading_state = CtpTradingState()
+494
View File
@@ -0,0 +1,494 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 专有软件 — 未经授权禁止复制、传播、转售。
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
"""Isolated local CTP worker.
This process is the only process that should instantiate vn.py / vnpy_ctp.
The Flask web app talks to it through localhost HTTP via ctp_ipc_client.py.
"""
from __future__ import annotations
import logging
import os
import threading
import time
from typing import Any
os.environ.setdefault("QIHUO_CTP_ROLE", "worker")
from flask import Flask, jsonify, request
from modules.ctp.ctp_ipc_client import worker_token
from modules.core.db_conn import DB_PATH, commit_retry, connect_db
from modules.fees.fee_specs import get_setting, set_setting
from modules.core.locale_fix import ensure_process_locale
from modules.market.market_sessions import is_trading_session
from modules.trading.sl_tp_guard import check_sl_tp_on_tick, ensure_monitor_order_columns, start_sl_tp_guard_worker
from strategy.strategy_db import init_strategy_tables
from modules.core.trading_context import get_account_capital, get_trading_mode, get_trailing_be_tick_buffer
from modules.ctp.vnpy_bridge import (
_ctp_td_lock,
ctp_cancel_order,
ctp_disconnect,
ctp_estimate_margin_one_lot,
ctp_get_account,
ctp_get_tick_detail,
ctp_get_tick_price,
ctp_list_active_orders,
ctp_list_positions,
ctp_list_trades,
ctp_lookup_contract_spec,
ctp_start_connect,
ctp_status,
ctp_try_auto_reconnect,
execute_order,
get_bridge,
set_ctp_connected_callback,
set_position_refresh_callback,
set_tick_quote_callback,
set_tick_sl_tp_callback,
try_init_vnpy,
)
logging.basicConfig(
level=os.getenv("LOG_LEVEL", "INFO"),
format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
)
logger = logging.getLogger(__name__)
app = Flask(__name__)
_started_workers = False
_last_snapshot_ts = 0.0
_snapshot_lock = threading.Lock()
def _json_ok(**payload: Any):
return jsonify({"ok": True, **payload})
def _json_error(exc: Exception, *, status_code: int = 500):
return jsonify({"ok": False, "error": str(exc)}), status_code
def _require_token() -> None:
expected = worker_token()
got = request.headers.get("X-Qihuo-CTP-Token", "")
if expected and got != expected:
raise PermissionError("unauthorized")
@app.before_request
def _auth():
_require_token()
@app.errorhandler(Exception)
def _handle_error(exc: Exception):
code = 401 if isinstance(exc, PermissionError) else 500
logger.warning("ctp worker request failed: %s", exc)
return _json_error(exc, status_code=code)
def _mode_from_request() -> str:
data = request.get_json(silent=True) or {}
return (
data.get("mode")
or request.args.get("mode")
or get_trading_mode(get_setting)
or "simulation"
)
def _fast_status(mode: str) -> dict[str, Any]:
"""Return worker/native bridge state without slow network probing."""
from modules.ctp.ctp_settings import CTP_DISABLED_HINT, is_ctp_auto_connect_enabled
try:
st = dict(get_bridge().status(mode) or {})
except Exception as exc:
st = {
"connected": False,
"connecting": False,
"connected_mode": None,
"last_error": str(exc),
"mode_label": "SimNow" if mode == "simulation" else "期货公司实盘",
}
auto = is_ctp_auto_connect_enabled()
st["auto_connect_enabled"] = auto
st["worker_online"] = True
if not auto:
st["disabled_hint"] = CTP_DISABLED_HINT
if not st.get("connected") and not st.get("connecting"):
st["last_error"] = ""
st["td_reachable"] = None
return st
def _send_wechat_msg(content: str) -> None:
webhook = get_setting("wechat_webhook", "")
if not webhook:
return
try:
import requests
requests.post(
webhook,
json={"msgtype": "text", "text": {"content": f"【国内期货】\n{content}"}},
timeout=10,
)
except Exception as exc:
logger.debug("wechat notify failed: %s", exc)
def _init_worker_tables(conn) -> None:
init_strategy_tables(conn)
ensure_monitor_order_columns(conn)
def _capital(conn) -> float:
try:
return float(get_account_capital(get_setting, conn=conn) or 0)
except Exception:
return 0.0
def _persist_snapshot(mode: str) -> None:
global _last_snapshot_ts
with _snapshot_lock:
now = time.time()
if now - _last_snapshot_ts < 0.25:
return
_last_snapshot_ts = now
try:
import json
st = _fast_status(mode)
positions = ctp_list_positions(mode, refresh_if_empty=False, refresh_margin=False)
account = ctp_get_account(mode) if st.get("connected") else {}
conn = connect_db(DB_PATH)
try:
conn.execute(
"""CREATE TABLE IF NOT EXISTS ctp_worker_snapshots (
key TEXT PRIMARY KEY,
value TEXT,
updated_at REAL
)"""
)
for key, value in (
("status", st),
("positions", positions),
("account", account),
):
conn.execute(
"""INSERT INTO ctp_worker_snapshots(key, value, updated_at)
VALUES(?,?,?)
ON CONFLICT(key) DO UPDATE SET
value=excluded.value,
updated_at=excluded.updated_at""",
(key, json.dumps(value, ensure_ascii=False), now),
)
commit_retry(conn)
finally:
conn.close()
except Exception as exc:
logger.debug("persist ctp snapshot: %s", exc)
def _on_position_refresh() -> None:
try:
_persist_snapshot(get_trading_mode(get_setting))
except Exception as exc:
logger.debug("position refresh callback: %s", exc)
def _on_tick_quote() -> None:
_on_position_refresh()
def _on_tick_sl_tp(exchange: str, symbol: str, price: float) -> None:
mode = get_trading_mode(get_setting)
if not ctp_status(mode).get("connected"):
return
conn = connect_db(DB_PATH)
try:
_init_worker_tables(conn)
capital = _capital(conn)
n = check_sl_tp_on_tick(
conn,
mode,
exchange,
symbol,
price,
capital=capital,
notify_fn=_send_wechat_msg,
be_tick_mult=get_trailing_be_tick_buffer(get_setting),
)
if n:
commit_retry(conn)
_persist_snapshot(mode)
except Exception as exc:
logger.warning("worker tick sl/tp: %s", exc)
finally:
conn.close()
def _on_ctp_connected(mode: str) -> None:
try:
with _ctp_td_lock:
get_bridge().request_position_snapshot(force=True)
get_bridge().calibrate_trading_state()
_persist_snapshot(mode)
except Exception as exc:
logger.debug("worker ctp connected callback: %s", exc)
def _start_background_workers() -> None:
global _started_workers
if _started_workers:
return
_started_workers = True
set_position_refresh_callback(_on_position_refresh)
set_tick_quote_callback(_on_tick_quote)
set_tick_sl_tp_callback(_on_tick_sl_tp)
set_ctp_connected_callback(_on_ctp_connected)
from modules.ctp.ctp_fee_worker import start_ctp_fee_worker
from modules.ctp.ctp_premarket_connect import start_ctp_premarket_connect_worker
from modules.ctp.ctp_reconnect import start_ctp_reconnect_worker
from modules.trading.order_pending import reconcile_pending_orders
from modules.trading.pending_order_worker import start_pending_order_worker
def _mode() -> str:
return get_trading_mode(get_setting)
start_ctp_reconnect_worker(get_mode_fn=_mode, get_setting_fn=get_setting)
start_ctp_premarket_connect_worker(get_mode_fn=_mode, get_setting_fn=get_setting)
start_ctp_fee_worker(
get_mode_fn=_mode,
get_setting_fn=get_setting,
set_setting_fn=set_setting,
)
start_pending_order_worker(
db_path=DB_PATH,
get_mode_fn=_mode,
init_tables_fn=_init_worker_tables,
get_capital_fn=_capital,
reconcile_fn=reconcile_pending_orders,
on_changed_fn=lambda: _persist_snapshot(_mode()),
)
start_sl_tp_guard_worker(
db_path=DB_PATH,
get_mode_fn=_mode,
init_tables_fn=_init_worker_tables,
get_capital_fn=_capital,
get_be_tick_buffer_fn=lambda: get_trailing_be_tick_buffer(get_setting),
notify_fn=_send_wechat_msg,
)
def _snapshot_loop() -> None:
time.sleep(3)
while True:
try:
mode = _mode()
if _fast_status(mode).get("connected"):
_persist_snapshot(mode)
except Exception as exc:
logger.debug("worker snapshot loop: %s", exc)
time.sleep(2 if is_trading_session() else 15)
threading.Thread(target=_snapshot_loop, daemon=True, name="ctp-worker-snapshot").start()
@app.route("/health")
def health():
mode = request.args.get("mode") or get_trading_mode(get_setting)
st = _fast_status(mode)
return _json_ok(
worker_online=True,
role=os.getenv("QIHUO_CTP_ROLE", "worker"),
mode=mode,
status=st,
ts=time.time(),
)
@app.route("/ctp/status")
def api_status():
mode = _mode_from_request()
return _json_ok(status=_fast_status(mode))
@app.route("/ctp/connect", methods=["POST"])
def api_connect():
data = request.get_json(silent=True) or {}
mode = data.get("mode") or get_trading_mode(get_setting)
info = ctp_start_connect(mode, force=bool(data.get("force")))
st = info.get("status") or _fast_status(mode)
return _json_ok(status=st, **{k: v for k, v in info.items() if k != "status"})
@app.route("/ctp/start_connect", methods=["POST"])
def api_start_connect():
data = request.get_json(silent=True) or {}
mode = data.get("mode") or get_trading_mode(get_setting)
return _json_ok(**ctp_start_connect(
mode,
force=bool(data.get("force")),
scheduled=bool(data.get("scheduled")),
))
@app.route("/ctp/disconnect", methods=["POST"])
def api_disconnect():
data = request.get_json(silent=True) or {}
ctp_disconnect(set_disabled_hint=bool(data.get("set_disabled_hint")))
return _json_ok(disconnected=True)
@app.route("/ctp/account")
def api_account():
mode = _mode_from_request()
if not _fast_status(mode).get("connected"):
return _json_ok(account={})
return _json_ok(account=ctp_get_account(mode))
@app.route("/ctp/positions", methods=["POST"])
def api_positions():
data = request.get_json(silent=True) or {}
mode = data.get("mode") or get_trading_mode(get_setting)
return _json_ok(positions=ctp_list_positions(
mode,
refresh_if_empty=bool(data.get("refresh_if_empty", True)),
refresh_margin=bool(data.get("refresh_margin", False)),
))
@app.route("/ctp/trades", methods=["POST"])
def api_trades():
data = request.get_json(silent=True) or {}
mode = data.get("mode") or get_trading_mode(get_setting)
return _json_ok(trades=ctp_list_trades(mode, refresh=bool(data.get("refresh"))))
@app.route("/ctp/active_orders")
def api_active_orders():
mode = _mode_from_request()
return _json_ok(orders=ctp_list_active_orders(mode))
@app.route("/ctp/tick_price", methods=["POST"])
def api_tick_price():
data = request.get_json(silent=True) or {}
return _json_ok(price=ctp_get_tick_price(
data.get("mode") or get_trading_mode(get_setting),
data.get("symbol") or "",
))
@app.route("/ctp/tick_detail", methods=["POST"])
def api_tick_detail():
data = request.get_json(silent=True) or {}
return _json_ok(detail=ctp_get_tick_detail(
data.get("mode") or get_trading_mode(get_setting),
data.get("symbol") or "",
))
@app.route("/ctp/estimate_margin_one_lot", methods=["POST"])
def api_estimate_margin():
data = request.get_json(silent=True) or {}
return _json_ok(margin=ctp_estimate_margin_one_lot(
data.get("mode") or get_trading_mode(get_setting),
data.get("symbol") or "",
float(data.get("price") or 0),
direction=data.get("direction") or "long",
))
@app.route("/ctp/contract_spec", methods=["POST"])
def api_contract_spec():
data = request.get_json(silent=True) or {}
return _json_ok(spec=ctp_lookup_contract_spec(
data.get("mode") or get_trading_mode(get_setting),
data.get("symbol") or "",
))
@app.route("/ctp/order", methods=["POST"])
def api_order():
data = request.get_json(silent=True) or {}
mode = data.get("mode") or get_trading_mode(get_setting)
result = execute_order(
None,
mode=mode,
offset=data.get("offset") or "open",
symbol=data.get("symbol") or "",
direction=data.get("direction") or "long",
lots=int(data.get("lots") or 1),
price=float(data.get("price") or 0),
settings=data.get("settings") or {},
order_type=data.get("order_type") or "limit",
)
_persist_snapshot(mode)
return _json_ok(**result)
@app.route("/ctp/cancel", methods=["POST"])
def api_cancel():
data = request.get_json(silent=True) or {}
mode = data.get("mode") or get_trading_mode(get_setting)
cancelled = ctp_cancel_order(mode, data.get("vt_orderid") or "")
_persist_snapshot(mode)
return _json_ok(cancelled=cancelled)
@app.route("/ctp/bridge/<action>", methods=["POST"])
def api_bridge_action(action: str):
data = request.get_json(silent=True) or {}
b = get_bridge()
if action == "calibrate_trading_state":
return _json_ok(result=b.calibrate_trading_state())
if action == "request_position_snapshot":
return _json_ok(result=b.request_position_snapshot(force=bool(data.get("force"))))
if action == "subscribe_symbol":
return _json_ok(result=b.subscribe_symbol(data.get("symbol") or ""))
if action == "refresh_positions":
return _json_ok(result=b.refresh_positions())
if action == "connect_in_progress":
return _json_ok(result=b.connect_in_progress())
if action == "reconnect_after_settings_saved":
mode = data.get("mode") or get_trading_mode(get_setting)
return _json_ok(result=b.reconnect_after_settings_saved(mode))
if action == "query_all_commissions":
return _json_ok(result=b.query_all_commissions(
mode=data.get("mode") or get_trading_mode(get_setting),
))
if action == "query_instrument_commission":
return _json_ok(result=b.query_instrument_commission(
data.get("symbol") or "",
mode=data.get("mode") or get_trading_mode(get_setting),
))
if action == "get_kline_bars_1m":
return _json_ok(result=b.get_kline_bars_1m(
data.get("symbol") or "",
mode=data.get("mode") or get_trading_mode(get_setting),
))
return _json_error(ValueError(f"unsupported bridge action: {action}"), status_code=404)
def main() -> None:
ensure_process_locale()
try_init_vnpy({})
_start_background_workers()
host = os.getenv("QIHUO_CTP_WORKER_HOST", "127.0.0.1")
port = int(os.getenv("QIHUO_CTP_WORKER_PORT", "6601") or 6601)
logger.info("starting qihuo-ctp worker on %s:%s", host, port)
app.run(host=host, port=port, debug=False, threaded=True, use_reloader=False)
if __name__ == "__main__":
main()
File diff suppressed because it is too large Load Diff