feat: complete strategy and market data improvements

This commit is contained in:
leefer
2026-07-29 16:50:40 +08:00
parent c30d2107b3
commit 0030bb8cc1
18 changed files with 1622 additions and 162 deletions
+40 -9
View File
@@ -8,7 +8,7 @@ import urllib.error
import urllib.parse
import urllib.request
from dataclasses import dataclass
from datetime import datetime, timedelta
from datetime import datetime, time as dt_time, timedelta
from threading import Lock
from typing import Any, ClassVar
@@ -168,8 +168,24 @@ class MarketChartClient:
previous = normalized[index - 1]["close"] if index > 0 else 0
row["change"] = round((row["close"] / previous - 1) * 100, 4) if previous else 0.0
today = datetime.now().astimezone().strftime("%Y%m%d")
if compact_end == today:
market_now = datetime.now().astimezone()
today = market_now.strftime("%Y%m%d")
market_open = (
market_now.weekday() < 5
and market_now.time().replace(tzinfo=None) >= dt_time(9, 30)
)
today_display = market_now.date().isoformat()
if normalized and normalized[-1]["trade_date"] == today_display:
current_bar = normalized[-1]
current_bar_is_valid = (
current_bar["open"] > 0
and current_bar["high"] >= max(current_bar["open"], current_bar["close"])
and 0 < current_bar["low"] <= min(current_bar["open"], current_bar["close"])
and (current_bar["volume"] > 0 or current_bar["amount_billion"] > 0)
)
if not market_open or not current_bar_is_valid:
normalized.pop()
if compact_end == today and market_open:
try:
quote_rows = self.ifind.real_time(
ifind_code,
@@ -179,16 +195,31 @@ class MarketChartClient:
quote = quote_rows[0] if quote_rows else {}
latest = _number(quote.get("latest"))
previous = _number(quote.get("preClose"))
if latest > 0:
open_price = _number(quote.get("open"))
high = _number(quote.get("high"))
low = _number(quote.get("low"))
volume = _number(quote.get("volume"))
amount = _number(quote.get("amount"))
quote_date = str(quote.get("time") or "")[:10].replace("-", "")
quote_is_current = not quote_date or quote_date == today
has_market_activity = volume > 0 or amount > 0
if (
latest > 0
and open_price > 0
and high >= max(open_price, latest)
and 0 < low <= min(open_price, latest)
and has_market_activity
and quote_is_current
):
realtime = {
"trade_date": end.strftime("%Y-%m-%d"),
"open": _number(quote.get("open")) or latest,
"high": _number(quote.get("high")) or latest,
"low": _number(quote.get("low")) or latest,
"open": open_price,
"high": high,
"low": low,
"close": latest,
"change": round((latest / previous - 1) * 100, 4) if previous else 0.0,
"volume": _number(quote.get("volume")),
"amount_billion": _number(quote.get("amount")) / 100_000_000,
"volume": volume,
"amount_billion": amount / 100_000_000,
"realtime": True,
}
if normalized and normalized[-1]["trade_date"] == realtime["trade_date"]: