Fix D1 open elapsed (use current bar); upsert floating P/L on dashboard.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -288,12 +288,13 @@ class Mt5Bridge:
|
||||
return age < 180 # 3 分钟内有报价视为开市
|
||||
|
||||
def day_ohlc(self, symbol: str) -> dict[str, Any]:
|
||||
"""最近交易日日线开高低(休市仍显示);开市时用 tick 修正最高/最低。"""
|
||||
"""当前/最近交易日日线开高低(休市仍显示);开市时用 tick 修正最高/最低。"""
|
||||
self.ensure_symbol(symbol)
|
||||
rates = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_D1, 0, 2)
|
||||
rates = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_D1, 0, 3)
|
||||
if rates is None or len(rates) < 1:
|
||||
raise TradeError("无法读取日线,请确认品种有 D1 数据")
|
||||
bar = rates[0]
|
||||
# MT5 返回数组从旧到新:[-1] 才是当前日线
|
||||
bar = rates[-1]
|
||||
open_px = float(bar["open"])
|
||||
high = float(bar["high"])
|
||||
low = float(bar["low"])
|
||||
@@ -514,7 +515,7 @@ class Mt5Bridge:
|
||||
"close_price": None,
|
||||
"open_time": time.time(),
|
||||
"close_time": None,
|
||||
"profit": None,
|
||||
"profit": 0.0,
|
||||
"sl": sl,
|
||||
"tp": tp,
|
||||
"snapshot": {"event": "open"},
|
||||
@@ -1383,7 +1384,7 @@ class OrderApp(tk.Tk):
|
||||
]
|
||||
hub_client.report_bars(symbol, name, bars)
|
||||
|
||||
def _hub_report(self, payload: Union[dict, list, None]) -> None:
|
||||
def _hub_report(self, payload: Union[dict, list, None], with_bars: bool = True) -> None:
|
||||
if hub_client is None or not is_activated() or not payload:
|
||||
return
|
||||
|
||||
@@ -1394,13 +1395,47 @@ class OrderApp(tk.Tk):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
hub_client.report_trade(item)
|
||||
sym = str(item.get("symbol") or self._symbol())
|
||||
self._hub_upload_bars(sym)
|
||||
if with_bars:
|
||||
sym = str(item.get("symbol") or self._symbol())
|
||||
self._hub_upload_bars(sym)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
threading.Thread(target=run, daemon=True).start()
|
||||
|
||||
def _hub_sync_floating(self, symbol: str) -> None:
|
||||
"""定期把持仓浮盈 upsert 到中控,总览盈亏才有数。"""
|
||||
if hub_client is None or not is_activated():
|
||||
return
|
||||
now = time.time()
|
||||
last = float(getattr(self, "_last_float_sync", 0) or 0)
|
||||
if now - last < 2.0:
|
||||
return
|
||||
self._last_float_sync = now
|
||||
try:
|
||||
poss = self.bridge.positions(symbol)
|
||||
except Exception:
|
||||
return
|
||||
if not poss:
|
||||
return
|
||||
reports = []
|
||||
for p in poss:
|
||||
reports.append({
|
||||
"ticket": str(p.ticket),
|
||||
"symbol": str(p.symbol),
|
||||
"side": "buy" if int(p.type) == mt5.POSITION_TYPE_BUY else "sell",
|
||||
"volume": float(p.volume),
|
||||
"open_price": float(p.price_open),
|
||||
"close_price": None,
|
||||
"open_time": float(p.time),
|
||||
"close_time": None,
|
||||
"profit": float(p.profit) + float(p.swap),
|
||||
"sl": float(p.sl),
|
||||
"tp": float(p.tp),
|
||||
"snapshot": {"event": "float"},
|
||||
})
|
||||
self._hub_report(reports, with_bars=False)
|
||||
|
||||
def _run_async(self, work) -> None:
|
||||
if self._busy:
|
||||
self.set_status("上一次操作还未完成", ok=False)
|
||||
@@ -1597,6 +1632,7 @@ class OrderApp(tk.Tk):
|
||||
|
||||
self._refresh_positions(symbol, digits)
|
||||
self._sync_open_buttons(symbol)
|
||||
self._hub_sync_floating(symbol)
|
||||
except TradeError:
|
||||
# 断连时仍尝试刷新日线卡片
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user