@ftshare-lab/dsh-kline 0.0.0-stage → 0.6.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +601 -0
- package/LICENSE +21 -0
- package/PROVENANCE.md +13 -0
- package/README.en.md +116 -0
- package/README.md +115 -2
- package/chart_service.py +687 -0
- package/config/basic-symbols.json +570 -0
- package/config/dsh-kline.patch.yml +4 -0
- package/config/dsh-tool-catalog.json +3875 -0
- package/cordis.patch.yml +33 -0
- package/core/__init__.py +0 -0
- package/core/adjustment.py +16 -0
- package/core/calc.py +691 -0
- package/core/rows.py +228 -0
- package/core/ta_engine.py +99 -0
- package/docs/anonymous-market-support.md +33 -0
- package/docs/automatic-drawing.md +24 -0
- package/docs/chart-functions.md +136 -0
- package/docs/drawing-examples.en.md +13 -0
- package/docs/drawing-examples.md +13 -0
- package/docs/embedding.md +43 -0
- package/docs/fixtures/price-pattern-real-v1.json +1 -0
- package/docs/free-foreign-daily.md +44 -0
- package/docs/guide.en.md +232 -0
- package/docs/guide.md +248 -0
- package/docs/images/abc-drawing-v0.5.0.jpg +0 -0
- package/docs/images/channel-drawing-v0.5.0.jpg +0 -0
- package/docs/images/drawing-workbench-v0.5.0.jpg +0 -0
- package/docs/images/dsh-kline-tab-workspace.png +0 -0
- package/docs/images/dsh-market.png +0 -0
- package/docs/images/kline-news.png +0 -0
- package/docs/images/kline-range-stats.png +0 -0
- package/docs/images/kline-support.png +0 -0
- package/docs/images/main-chart-comparison-v0.6.9.png +0 -0
- package/docs/images/market-overview-v0.6.9.png +0 -0
- package/docs/images/natural-language-abc-wolong-2026-10-27.png +0 -0
- package/docs/images/natural-language-descending-channel-wolong-2026-10-27.png +0 -0
- package/docs/images/platform-breakout-msft-v0.6.9.png +0 -0
- package/docs/npm-release.md +33 -0
- package/docs/price-pattern-rules-v1.md +40 -0
- package/docs/price-pattern-samples-v1.md +42 -0
- package/docs/provider-adaptation.md +73 -0
- package/docs/release-acceptance-v0.4.0.md +46 -0
- package/docs/release-acceptance-v0.4.1.md +43 -0
- package/docs/release-acceptance-v0.5.0.md +40 -0
- package/docs/release-acceptance-v0.6.0.md +33 -0
- package/docs/release-acceptance-v0.6.1.md +19 -0
- package/docs/release-acceptance-v0.6.10.md +21 -0
- package/docs/release-acceptance-v0.6.2.md +31 -0
- package/docs/release-acceptance-v0.6.3.md +25 -0
- package/docs/release-acceptance-v0.6.4.md +24 -0
- package/docs/release-acceptance-v0.6.5.md +21 -0
- package/docs/release-acceptance-v0.6.6.md +21 -0
- package/docs/release-acceptance-v0.6.7.md +18 -0
- package/docs/release-acceptance-v0.6.8.md +18 -0
- package/docs/release-acceptance-v0.6.9.md +98 -0
- package/docs/release-notes-v0.5.0.md +23 -0
- package/docs/release-notes-v0.6.0.md +11 -0
- package/docs/release-notes-v0.6.1.md +20 -0
- package/docs/release-notes-v0.6.10.md +11 -0
- package/docs/release-notes-v0.6.2.md +14 -0
- package/docs/release-notes-v0.6.3.md +26 -0
- package/docs/release-notes-v0.6.4.md +12 -0
- package/docs/release-notes-v0.6.5.md +13 -0
- package/docs/release-notes-v0.6.6.md +13 -0
- package/docs/release-notes-v0.6.7.md +17 -0
- package/docs/release-notes-v0.6.8.md +10 -0
- package/docs/release-notes-v0.6.9.md +16 -0
- package/lib/candle-sequence.js +15 -0
- package/lib/chart-adjustment.js +7 -0
- package/lib/chart-annotations.js +33 -0
- package/lib/chart-applications.js +171 -0
- package/lib/chart-context.js +164 -0
- package/lib/chart-observation.js +251 -0
- package/lib/chart-service-status.js +82 -0
- package/lib/chart-source-status.js +39 -0
- package/lib/chart-sources.js +354 -0
- package/lib/client/BetterSidebarTab.js +70 -0
- package/lib/client/ClassicShell.js +254 -0
- package/lib/client/KlineContent.js +155 -0
- package/lib/client/chart-apply.js +73 -0
- package/lib/client/chart-context.js +233 -0
- package/lib/client/chart-handoff.js +34 -0
- package/lib/client/chart-session-resolver.js +74 -0
- package/lib/client/client-context.js +1 -0
- package/lib/client/conversation.js +88 -0
- package/lib/client/default-tab.js +83 -0
- package/lib/client/embed.js +50 -0
- package/lib/client/generated-view.js +12983 -0
- package/lib/client/horizontal-lines.js +19 -0
- package/lib/client/index.js +62 -0
- package/lib/client/mode-preferences.js +28 -0
- package/lib/client/native-sidebar-contract.js +45 -0
- package/lib/client/native-sidebar-register.js +133 -0
- package/lib/client/native-sidebar-services.js +1 -0
- package/lib/client/native-sidebar.js +96 -0
- package/lib/client/sidebar-integration.js +93 -0
- package/lib/client/ui-onboarding.js +42 -0
- package/lib/client/version.js +14 -0
- package/lib/client.js +17382 -0
- package/lib/client.js.map +1 -0
- package/lib/drawing-feedback.js +284 -0
- package/lib/fibonacci.js +18 -0
- package/lib/ftshare-settings-bridge.js +159 -0
- package/lib/index.js +279 -0
- package/lib/labeled-polyline.js +17 -0
- package/lib/pattern-groups.js +91 -0
- package/lib/price-bands.js +55 -0
- package/lib/price-series.js +47 -0
- package/lib/price-sticks.js +32 -0
- package/lib/types/candle-sequence.d.ts +2 -0
- package/lib/types/chart-adjustment.d.ts +2 -0
- package/lib/types/chart-annotations.d.ts +4 -0
- package/lib/types/chart-applications.d.ts +26 -0
- package/lib/types/chart-context.d.ts +14 -0
- package/lib/types/chart-observation.d.ts +8 -0
- package/lib/types/chart-service-status.d.ts +22 -0
- package/lib/types/chart-source-status.d.ts +47 -0
- package/lib/types/chart-sources.d.ts +25 -0
- package/lib/types/client/BetterSidebarTab.d.ts +12 -0
- package/lib/types/client/ClassicShell.d.ts +4 -0
- package/lib/types/client/KlineContent.d.ts +37 -0
- package/lib/types/client/chart-apply.d.ts +13 -0
- package/lib/types/client/chart-context.d.ts +23 -0
- package/lib/types/client/chart-handoff.d.ts +5 -0
- package/lib/types/client/chart-session-resolver.d.ts +2 -0
- package/lib/types/client/client-context.d.ts +14 -0
- package/lib/types/client/conversation.d.ts +43 -0
- package/lib/types/client/default-tab.d.ts +3 -0
- package/lib/types/client/embed.d.ts +37 -0
- package/lib/types/client/generated-view.d.ts +2 -0
- package/lib/types/client/horizontal-lines.d.ts +1 -0
- package/lib/types/client/index.d.ts +4 -0
- package/lib/types/client/mode-preferences.d.ts +13 -0
- package/lib/types/client/native-sidebar-contract.d.ts +52 -0
- package/lib/types/client/native-sidebar-register.d.ts +46 -0
- package/lib/types/client/native-sidebar-services.d.ts +45 -0
- package/lib/types/client/native-sidebar.d.ts +47 -0
- package/lib/types/client/sidebar-integration.d.ts +12 -0
- package/lib/types/client/ui-onboarding.d.ts +6 -0
- package/lib/types/client/version.d.ts +1 -0
- package/lib/types/drawing-feedback.d.ts +5 -0
- package/lib/types/fibonacci.d.ts +5 -0
- package/lib/types/ftshare-settings-bridge.d.ts +50 -0
- package/lib/types/index.d.ts +26 -0
- package/lib/types/labeled-polyline.d.ts +11 -0
- package/lib/types/pattern-groups.d.ts +2 -0
- package/lib/types/price-bands.d.ts +3 -0
- package/lib/types/price-series.d.ts +5 -0
- package/lib/types/price-sticks.d.ts +3 -0
- package/package.json +153 -4
- package/requirements.txt +6 -0
- package/screenshots.json +11 -0
- package/scripts/bootstrap.sh +5 -0
- package/scripts/dsh-bootstrap.mjs +179 -0
- package/scripts/dsh-mcp-plugin.mjs +27 -0
- package/scripts/run-dsh-kline.mjs +658 -0
- package/scripts/run-dsh-kline.sh +5 -0
- package/server.py +1755 -0
- package/tools/__init__.py +0 -0
- package/tools/annotations.py +119 -0
- package/tools/automatic_analysis.py +158 -0
- package/tools/baostock_source.py +176 -0
- package/tools/calc.py +39 -0
- package/tools/channel_candidates.py +260 -0
- package/tools/channel_preview.py +82 -0
- package/tools/channels.py +104 -0
- package/tools/chart_applications.py +204 -0
- package/tools/chart_context.py +186 -0
- package/tools/chart_edits.py +363 -0
- package/tools/chart_functions.py +451 -0
- package/tools/chart_indicators.py +65 -0
- package/tools/chart_measurements.py +84 -0
- package/tools/chart_source_status.py +32 -0
- package/tools/chart_sources.py +31 -0
- package/tools/closed_daily.py +68 -0
- package/tools/draw.py +410 -0
- package/tools/drawing_feedback.py +45 -0
- package/tools/drawing_interaction.py +117 -0
- package/tools/drawing_verification.py +141 -0
- package/tools/fetch.py +4680 -0
- package/tools/fibonacci.py +83 -0
- package/tools/foreign_daily.py +185 -0
- package/tools/free_sources.py +483 -0
- package/tools/lines.py +91 -0
- package/tools/nine_count.py +66 -0
- package/tools/pattern_groups.py +103 -0
- package/tools/pivots.py +60 -0
- package/tools/polylines.py +131 -0
- package/tools/price_bands.py +165 -0
- package/tools/price_patterns.py +311 -0
- package/tools/price_series.py +162 -0
- package/tools/price_sticks.py +114 -0
- package/tools/range_candidates.py +170 -0
- package/tools/recent_drawing_candidates.py +123 -0
- package/tools/rectangles.py +76 -0
- package/tools/regression_channels.py +87 -0
- package/tools/ta_functions.py +228 -0
- package/tools/trend_lines.py +106 -0
- package/tools/watchlist.py +149 -0
- package/view/ft-logo.jpg +0 -0
- package/view/kline.html +12436 -0
- package/view/vendor/klinecharts.min.js +7 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Small, read-only presentation helpers. Never interpret intent or authorize edits."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from copy import deepcopy
|
|
5
|
+
import re
|
|
6
|
+
from typing import get_args
|
|
7
|
+
|
|
8
|
+
from tools.lines import ColorInput
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def drawing_capabilities() -> dict:
|
|
12
|
+
return {"color_inputs": list(get_args(ColorInput)), "editable_origins": ["ai"],
|
|
13
|
+
"drawing_selection_supported": False, "ambiguous_target": "ask_user",
|
|
14
|
+
"unspecified_price_amount": "ask_user"}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def edit_interaction(data: dict, change: dict | None = None) -> dict:
|
|
18
|
+
status = data.get("apply_status")
|
|
19
|
+
states = {
|
|
20
|
+
"not_requested": ("prepared", "修改方案已准备,尚未执行。", "read_current_context_before_apply"),
|
|
21
|
+
"queued": ("pending", "修改已排队,尚未确认完成。", "check_original_candidate_status"),
|
|
22
|
+
"delivered": ("pending", "修改已送达网页,尚未确认完成。", "check_original_candidate_status"),
|
|
23
|
+
"rejected": ("not_confirmed", "本次修改被拒绝,未获成功确认。", "read_current_context_before_new_edit"),
|
|
24
|
+
"failed": ("not_confirmed", "本次修改失败,请先检查失败原因。", "inspect_failure_do_not_resend"),
|
|
25
|
+
"outcome_unknown": ("unknown", "本次修改结果尚不确定,不要重复执行。", "check_original_candidate_status_do_not_resend"),
|
|
26
|
+
}
|
|
27
|
+
value = states.get(status, ("unknown", "本次修改状态无法确认。", "stop_and_inspect"))
|
|
28
|
+
token = data.get("candidate_chart_session")
|
|
29
|
+
confirmed = (status == "applied" and isinstance(token, str) and re.fullmatch(r"[A-Za-z0-9_-]{32}", token) is not None
|
|
30
|
+
and data.get("confirmed_chart_session") == data["candidate_chart_session"]
|
|
31
|
+
and data.get("confirmation_scope") == "view_state_only")
|
|
32
|
+
if confirmed:
|
|
33
|
+
value = ("confirmed", "本次修改已获网页状态确认;目标图形和画面像素仍需核验。", "read_current_context_before_next_edit")
|
|
34
|
+
if data.get("needs_attention"):
|
|
35
|
+
value = ("needs_review", "网页已确认应用,但图形反馈仍需检查;画面像素未核验。", "inspect_drawing_feedback")
|
|
36
|
+
target = (change.get("after") or change.get("before") or {}).get("id") if isinstance(change, dict) else None
|
|
37
|
+
feedback = data.get("drawing_feedback")
|
|
38
|
+
item = next((entry for entry in feedback.get("items", []) if entry.get("id") == target), None) if (
|
|
39
|
+
target and isinstance(feedback, dict) and feedback.get("report_status") == "reported"
|
|
40
|
+
and feedback.get("phase") == "applied") else None
|
|
41
|
+
object_status = item.get("status", "unknown") if item else "unknown"
|
|
42
|
+
time_overlap = item.get("time_range_overlap") if item else None
|
|
43
|
+
if confirmed and value[0] == "confirmed" and item:
|
|
44
|
+
if object_status == "created":
|
|
45
|
+
message = ("网页已确认应用,目标序列已在图表登记;画面像素未核验。" if item.get("reason") == "indicator_registered"
|
|
46
|
+
else "网页已确认应用,目标图形对象与锚点已核对;画面像素未核验。")
|
|
47
|
+
if item.get("reason") != "indicator_registered":
|
|
48
|
+
if time_overlap is False: message = "网页已确认应用,目标图形对象已核对,但在应用当时的时间视口之外;画面像素未核验。"
|
|
49
|
+
elif time_overlap is True: message = "网页已确认应用,目标图形对象与锚点已核对,时间范围覆盖应用当时的视口;画面像素未核验。"
|
|
50
|
+
value = (value[0], message, value[2])
|
|
51
|
+
elif object_status == "removed":
|
|
52
|
+
value = (value[0], "网页已确认删除,目标图形对象已从图表对象表移除;画面像素未核验。", value[2])
|
|
53
|
+
elif object_status == "hidden":
|
|
54
|
+
value = (value[0], "网页已确认应用,但目标图形在当前周期或数据范围内隐藏;画面像素未核验。", value[2])
|
|
55
|
+
return {"status": value[0], "message": value[1], "next_action": value[2],
|
|
56
|
+
"completion_confirmed": value[0] == "confirmed", "render_verified": False,
|
|
57
|
+
"object_status": object_status, "time_range_overlap": time_overlap, "pixel_verified": False,
|
|
58
|
+
"record_is_historical": data.get("record_is_historical") is True}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def edit_change(candidate: dict, parent: dict) -> dict | None:
|
|
62
|
+
# Derive an object-level diff from the exact named snapshots, not prose or
|
|
63
|
+
# a global latest chart. These are candidate effects, not a live receipt.
|
|
64
|
+
from tools.chart_edits import snapshot_inventory
|
|
65
|
+
try:
|
|
66
|
+
guard = candidate["drawing_guard"]
|
|
67
|
+
kind, action = guard["kind"], guard["action"]
|
|
68
|
+
before = {obj["id"]: obj for obj in snapshot_inventory(parent)}
|
|
69
|
+
after = {obj["id"]: obj for obj in snapshot_inventory(candidate)}
|
|
70
|
+
if action == "create":
|
|
71
|
+
added = set(after) - set(before)
|
|
72
|
+
if len(added) != 1:
|
|
73
|
+
return None
|
|
74
|
+
target = added.pop()
|
|
75
|
+
elif action in {"update", "delete"}:
|
|
76
|
+
target = guard["object_id"]
|
|
77
|
+
else:
|
|
78
|
+
return None
|
|
79
|
+
old, new = before.get(target), after.get(target)
|
|
80
|
+
if (action == "create" and old is not None or action == "delete" and new is not None
|
|
81
|
+
or action != "create" and old is None or action != "delete" and new is None
|
|
82
|
+
or any(obj["kind"] != kind for obj in (old, new) if obj is not None)):
|
|
83
|
+
return None
|
|
84
|
+
fields = sorted(k for k in set(old or {}) | set(new or {})
|
|
85
|
+
if k not in {"id", "origin", "kind"} and (old or {}).get(k) != (new or {}).get(k))
|
|
86
|
+
detail = {}
|
|
87
|
+
if kind == "labeled_polyline" and old is not None and new is not None:
|
|
88
|
+
previous_points = {p["label"]: p for p in old["points"]}
|
|
89
|
+
next_points = {p["label"]: p for p in new["points"]}
|
|
90
|
+
detail["point_changes"] = [{"label": label, "before": deepcopy(previous_points.get(label)),
|
|
91
|
+
"after": deepcopy(next_points.get(label))}
|
|
92
|
+
for label in sorted(previous_points.keys() | next_points.keys())
|
|
93
|
+
if previous_points.get(label) != next_points.get(label)]
|
|
94
|
+
return {"kind": kind, "action": action, "before": deepcopy(old), "after": deepcopy(new), **detail,
|
|
95
|
+
"changed_fields": fields, "basis": "prepared_candidate_not_live_state",
|
|
96
|
+
"other_ai_drawings_unchanged": {k: v for k, v in before.items() if k != target}
|
|
97
|
+
== {k: v for k, v in after.items() if k != target}}
|
|
98
|
+
except (KeyError, TypeError, AttributeError):
|
|
99
|
+
return None
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def error_interaction(code: str) -> dict:
|
|
103
|
+
if code in {"invalid_color", "invalid_price", "invalid_object_identity", "ambiguous_drawing_id", "drawing_not_editable",
|
|
104
|
+
"invalid_annotation_text", "invalid_annotation_value", "invalid_annotation_anchor", "annotation_anchor_required", "annotation_not_found",
|
|
105
|
+
"invalid_polyline_points", "invalid_point_label", "duplicate_point_label", "point_not_found", "point_label_required", "empty_point_update"}:
|
|
106
|
+
next_action = "ask_user"
|
|
107
|
+
elif code in {"chart_context_stale", "chart_view_inactive", "chart_context_revision_conflict",
|
|
108
|
+
"chart_snapshot_not_aligned", "chart_context_unavailable", "ambiguous_chart_views"}:
|
|
109
|
+
next_action = "read_current_context"
|
|
110
|
+
elif code in {"chart_application_unavailable", "chart_edit_outcome_unknown"}:
|
|
111
|
+
next_action = "check_original_candidate_status_do_not_resend"
|
|
112
|
+
else:
|
|
113
|
+
next_action = "stop_and_inspect"
|
|
114
|
+
result = {"status": "not_confirmed", "completion_confirmed": False, "next_action": next_action}
|
|
115
|
+
if code == "invalid_color":
|
|
116
|
+
result["color_inputs"] = list(get_args(ColorInput))
|
|
117
|
+
return result
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""Read-only comparison of explicitly requested parameters with a named snapshot."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from typing import Annotated, Literal
|
|
5
|
+
from pydantic import BaseModel, ConfigDict, Field, ValidationError
|
|
6
|
+
|
|
7
|
+
from tools.lines import ColorInput, LINE_COMMAND, LineError, validate_line_fields
|
|
8
|
+
from tools.trend_lines import TREND_COMMAND, resolve_anchor_time
|
|
9
|
+
from tools.channels import CHANNEL_COMMAND
|
|
10
|
+
from tools.rectangles import RECTANGLE_COMMAND
|
|
11
|
+
from tools.fibonacci import FIBONACCI_COMMAND, validate_ratios
|
|
12
|
+
from tools.polylines import POLYLINE_COMMAND, normalize_point_label
|
|
13
|
+
from tools.annotations import ANNOTATION_COMMAND, ANNOTATION_KINDS
|
|
14
|
+
from tools.price_series import SERIES_COMMAND
|
|
15
|
+
from tools.price_bands import BAND_COMMAND
|
|
16
|
+
from tools.price_sticks import STICKS_COMMAND
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class DrawingExpectation(BaseModel):
|
|
20
|
+
model_config = ConfigDict(extra="forbid")
|
|
21
|
+
kind: Literal["horizontal_line", "trend_line", "channel", "rectangle", "fibonacci_retracement", "labeled_polyline", "text_annotation", "number_annotation", "icon_annotation", "series_line", "price_band", "price_sticks"]
|
|
22
|
+
id: str = Field(min_length=1, max_length=128, description="工具返回的稳定图形 ID,不按名称猜测")
|
|
23
|
+
exists: bool = Field(default=True, strict=True, description="删除后填 false,并且不传其他期望参数")
|
|
24
|
+
name: str | None = Field(default=None, max_length=48, description="用户原始指定的完整名称,不能缩写;空字符串也会核对")
|
|
25
|
+
color: ColorInput | None = Field(default=None, description="用户原始指定颜色,可直接填绿色、红色等,核对时标准化")
|
|
26
|
+
price: float | None = Field(default=None, gt=0, allow_inf_nan=False, strict=True)
|
|
27
|
+
start_time: str | None = None
|
|
28
|
+
start_price: float | None = Field(default=None, gt=0, allow_inf_nan=False, strict=True)
|
|
29
|
+
end_time: str | None = None
|
|
30
|
+
end_price: float | None = Field(default=None, gt=0, allow_inf_nan=False, strict=True)
|
|
31
|
+
parallel_time: str | None = None
|
|
32
|
+
parallel_price: float | None = Field(default=None, gt=0, allow_inf_nan=False, strict=True)
|
|
33
|
+
show_midline: bool | None = Field(default=None, strict=True)
|
|
34
|
+
lower_price: float | None = Field(default=None, gt=0, allow_inf_nan=False, strict=True)
|
|
35
|
+
upper_price: float | None = Field(default=None, gt=0, allow_inf_nan=False, strict=True)
|
|
36
|
+
show_fill: bool | None = Field(default=None, strict=True)
|
|
37
|
+
point_label: str | None = Field(default=None, min_length=1, max_length=16)
|
|
38
|
+
point_time: str | None = None
|
|
39
|
+
point_price: float | None = Field(default=None, gt=0, allow_inf_nan=False, strict=True)
|
|
40
|
+
time: str | None = None
|
|
41
|
+
text: str | None = Field(default=None, max_length=120)
|
|
42
|
+
value: float | None = Field(default=None, allow_inf_nan=False, strict=True)
|
|
43
|
+
icon: Literal["arrow_up", "arrow_down", "diamond"] | None = None
|
|
44
|
+
ratios: list[Annotated[float, Field(strict=True, ge=0, le=1, allow_inf_nan=False)]] | None = Field(default=None, min_length=1, max_length=16)
|
|
45
|
+
series_hash: str | None = Field(default=None, pattern=r"^[a-f0-9]{64}$")
|
|
46
|
+
bar_count: int | None = Field(default=None, strict=True, ge=1, le=4000)
|
|
47
|
+
band_hash: str | None = Field(default=None, pattern=r"^[a-f0-9]{64}$")
|
|
48
|
+
sticks_hash: str | None = Field(default=None, pattern=r"^[a-f0-9]{64}$")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def verify_drawings(payload: dict, expectations: list[DrawingExpectation | dict]) -> dict:
|
|
52
|
+
if not isinstance(expectations, list) or not 1 <= len(expectations) <= 180:
|
|
53
|
+
raise LineError("invalid_expectations", "请提供 1 至 180 项明确的图形核对条件,不能用空列表声称核验通过。")
|
|
54
|
+
types = {"horizontal_line": LINE_COMMAND, "trend_line": TREND_COMMAND, "channel": CHANNEL_COMMAND, "rectangle": RECTANGLE_COMMAND,
|
|
55
|
+
"fibonacci_retracement": FIBONACCI_COMMAND, "labeled_polyline": POLYLINE_COMMAND,
|
|
56
|
+
"text_annotation": ANNOTATION_COMMAND, "number_annotation": ANNOTATION_COMMAND, "icon_annotation": ANNOTATION_COMMAND,
|
|
57
|
+
"series_line": SERIES_COMMAND, "price_band": BAND_COMMAND, "price_sticks": STICKS_COMMAND}
|
|
58
|
+
allowed = {"name", "color"}
|
|
59
|
+
points = {"start": "from", "end": "to", "parallel": "parallel"}
|
|
60
|
+
checks = []
|
|
61
|
+
for raw in expectations:
|
|
62
|
+
try:
|
|
63
|
+
spec = raw if isinstance(raw, DrawingExpectation) else DrawingExpectation.model_validate(raw)
|
|
64
|
+
except ValidationError as exc:
|
|
65
|
+
raise LineError("invalid_expectations", "核对参数无效,请检查图形类型、字段、价格与颜色。") from exc
|
|
66
|
+
requested = spec.model_dump(exclude_none=True)
|
|
67
|
+
fields = set(requested) - {"kind", "id", "exists"}
|
|
68
|
+
supported = allowed | {
|
|
69
|
+
"horizontal_line": {"price"},
|
|
70
|
+
"trend_line": {"start_time", "start_price", "end_time", "end_price"},
|
|
71
|
+
"channel": {"start_time", "start_price", "end_time", "end_price"},
|
|
72
|
+
"rectangle": {"start_time", "end_time", "lower_price", "upper_price", "show_fill"},
|
|
73
|
+
"fibonacci_retracement": {"start_time", "start_price", "end_time", "end_price", "ratios"},
|
|
74
|
+
"labeled_polyline": {"point_label", "point_time", "point_price"},
|
|
75
|
+
"text_annotation": {"time", "price", "text"},
|
|
76
|
+
"number_annotation": {"time", "price", "value"},
|
|
77
|
+
"icon_annotation": {"time", "price", "icon"},
|
|
78
|
+
"series_line": {"series_hash", "bar_count"},
|
|
79
|
+
"price_band": {"band_hash", "bar_count"},
|
|
80
|
+
"price_sticks": {"sticks_hash", "bar_count"},
|
|
81
|
+
}[spec.kind]
|
|
82
|
+
if spec.kind == "channel":
|
|
83
|
+
supported |= {"parallel_time", "parallel_price", "show_midline"}
|
|
84
|
+
if fields - supported or not spec.exists and fields:
|
|
85
|
+
raise LineError("invalid_expectation_fields", "核对条件包含不适用的字段;删除核对只能指定类型、ID 和 exists=false。")
|
|
86
|
+
if spec.kind == "labeled_polyline" and spec.point_label is None and {"point_time", "point_price"} & fields:
|
|
87
|
+
raise LineError("point_label_required", "核对单点需要明确的点名。")
|
|
88
|
+
_, normalized_name, normalized_color = validate_line_fields(name=spec.name, color=spec.color)
|
|
89
|
+
if spec.name is not None: requested["name"] = normalized_name
|
|
90
|
+
if spec.color is not None: requested["color"] = normalized_color
|
|
91
|
+
if spec.ratios is not None: requested["ratios"] = validate_ratios(spec.ratios)
|
|
92
|
+
if spec.point_label is not None: requested["point_label"] = normalize_point_label(spec.point_label)
|
|
93
|
+
for field in fields:
|
|
94
|
+
if field.endswith("_time"):
|
|
95
|
+
requested[field] = resolve_anchor_time(payload, requested[field])
|
|
96
|
+
if spec.kind in ANNOTATION_KINDS and "time" in fields:
|
|
97
|
+
requested["time"] = resolve_anchor_time(payload, requested["time"])
|
|
98
|
+
candidates = [c for c in payload.get("chartCommands", []) if c.get("type") == types[spec.kind] and c.get("id") == spec.id]
|
|
99
|
+
if len(candidates) > 1:
|
|
100
|
+
raise LineError("ambiguous_drawing_id", "该 ID 对应多个图形,不能安全核对。")
|
|
101
|
+
drawing = candidates[0] if candidates else None
|
|
102
|
+
actual = {"exists": drawing is not None}
|
|
103
|
+
if drawing:
|
|
104
|
+
actual.update(name=drawing.get("label", ""), color=drawing.get("color", "info"))
|
|
105
|
+
if spec.kind == "horizontal_line": actual["price"] = drawing.get("price")
|
|
106
|
+
elif spec.kind in ANNOTATION_KINDS:
|
|
107
|
+
actual.update(time=drawing.get("time"), price=drawing.get("price"))
|
|
108
|
+
field = "text" if spec.kind == "text_annotation" else "value"
|
|
109
|
+
if spec.kind == "icon_annotation": actual["icon"] = drawing.get("icon")
|
|
110
|
+
else: actual[field] = drawing.get(field)
|
|
111
|
+
elif spec.kind == "rectangle":
|
|
112
|
+
actual.update({f: drawing.get(f) for f in ("start_time", "end_time", "lower_price", "upper_price", "show_fill")})
|
|
113
|
+
elif spec.kind == "labeled_polyline":
|
|
114
|
+
actual["points"] = drawing.get("points")
|
|
115
|
+
targets = [p for p in drawing.get("points", []) if p.get("label") == requested.get("point_label")]
|
|
116
|
+
if len(targets) > 1: raise LineError("duplicate_point_label", "目标点名重复,不能安全核对。")
|
|
117
|
+
if targets:
|
|
118
|
+
actual.update(point_label=targets[0]["label"], point_time=targets[0]["time"], point_price=targets[0]["price"])
|
|
119
|
+
elif spec.kind == "series_line":
|
|
120
|
+
actual.update(series_hash=drawing.get("series_hash"), bar_count=drawing.get("bar_count"))
|
|
121
|
+
elif spec.kind == "price_band":
|
|
122
|
+
actual.update(band_hash=drawing.get("band_hash"), bar_count=drawing.get("bar_count"))
|
|
123
|
+
elif spec.kind == "price_sticks":
|
|
124
|
+
actual.update(sticks_hash=drawing.get("sticks_hash"), bar_count=drawing.get("bar_count"))
|
|
125
|
+
else:
|
|
126
|
+
for prefix, key in points.items():
|
|
127
|
+
if key in drawing:
|
|
128
|
+
actual[prefix + "_time"] = drawing[key].get("time")
|
|
129
|
+
actual[prefix + "_price"] = drawing[key].get("price")
|
|
130
|
+
if spec.kind == "channel": actual["show_midline"] = drawing.get("show_midline")
|
|
131
|
+
if spec.kind == "fibonacci_retracement":
|
|
132
|
+
actual["ratios"] = drawing.get("ratios")
|
|
133
|
+
actual["levels"] = drawing.get("levels")
|
|
134
|
+
compare = fields | {"exists"}
|
|
135
|
+
mismatches = [{"field": f, "expected": requested[f], "actual": actual.get(f)}
|
|
136
|
+
for f in sorted(compare) if requested[f] != actual.get(f)]
|
|
137
|
+
checks.append({"kind": spec.kind, "id": spec.id, "matches": not mismatches,
|
|
138
|
+
"expected": requested, "actual": actual, "mismatches": mismatches})
|
|
139
|
+
matched = all(c["matches"] for c in checks)
|
|
140
|
+
return {"matches": matched, "snapshot_verified": matched, "render_verified": False,
|
|
141
|
+
"verification_scope": "provided_expectations_only", "checks": checks}
|