@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
package/core/rows.py
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"""Canonical OHLCV row helpers for dsh_kline.
|
|
2
|
+
|
|
3
|
+
Public API time unit: **unix seconds** (int) end-to-end.
|
|
4
|
+
The **only** place that multiplies by 1000 is the view (klinecharts needs ms).
|
|
5
|
+
|
|
6
|
+
Bar time (v1, pinned to 8001):
|
|
7
|
+
Canonical ``time`` = bar **close** timestamp. When the upstream source provides
|
|
8
|
+
an explicit start timestamp, ``open_time`` retains it as optional metadata.
|
|
9
|
+
FTShare: prefer ``ts_millis`` (close), never silently prefer ``ts_millis_open``.
|
|
10
|
+
Day bars: open/close same calendar day → fine.
|
|
11
|
+
Minute bars: close-time shifts marks by ~1 interval — known landmine; v1 is
|
|
12
|
+
day-first. Document only; do not switch to open without migrating 8001 marks.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from datetime import datetime, timezone, timedelta
|
|
18
|
+
import math
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
# Values above this are treated as milliseconds regardless of field name.
|
|
22
|
+
# unix-sec for year ~2286 ≈ 1e10; ms values for modern dates are ≥ 1e12.
|
|
23
|
+
_MS_THRESHOLD = 10_000_000_000 # 1e10
|
|
24
|
+
|
|
25
|
+
# A-share calendar dates are wall-clock China. Fixed offset (no DST) so the same
|
|
26
|
+
# "YYYY-MM-DD" is reproducible on UTC servers and Shanghai laptops.
|
|
27
|
+
_CN_TZ = timezone(timedelta(hours=8))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class RowsValidationError(ValueError):
|
|
31
|
+
"""Raised when incoming rows fail the contract gate."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _to_float(v: Any) -> float | None:
|
|
35
|
+
if v is None or v == "":
|
|
36
|
+
return None
|
|
37
|
+
try:
|
|
38
|
+
return float(v)
|
|
39
|
+
except (TypeError, ValueError):
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _date_text_to_sec(date_txt: str) -> int | None:
|
|
44
|
+
"""Parse YYYY-MM-DD as Asia/Shanghai midnight → unix seconds (portable)."""
|
|
45
|
+
try:
|
|
46
|
+
dt = datetime.strptime(date_txt[:10], "%Y-%m-%d").replace(tzinfo=_CN_TZ)
|
|
47
|
+
return int(dt.timestamp())
|
|
48
|
+
except Exception:
|
|
49
|
+
return None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _to_time_sec(v: Any) -> int | None:
|
|
53
|
+
"""Coerce a timestamp-like value to unix **seconds**.
|
|
54
|
+
|
|
55
|
+
Magnitude-based ms detection (not field-name-based):
|
|
56
|
+
if value > 1e10, treat as ms and ÷1000. Handles callers who stuff ms into
|
|
57
|
+
a field named ``time``.
|
|
58
|
+
"""
|
|
59
|
+
n = _to_float(v)
|
|
60
|
+
if n is None or not math.isfinite(n) or n <= 0:
|
|
61
|
+
return None
|
|
62
|
+
if n > _MS_THRESHOLD:
|
|
63
|
+
n = n / 1000.0
|
|
64
|
+
return int(n)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _pick_raw_time(raw: dict[str, Any]) -> Any:
|
|
68
|
+
"""Pick bar **close** timestamp from heterogeneous keys.
|
|
69
|
+
|
|
70
|
+
Semantic lock: every accepted field is interpreted as **close** time.
|
|
71
|
+
If a data source puts open-time under ``ts``/``timestamp`` without also
|
|
72
|
+
providing the FTShare close/open pair, the caller must align it first —
|
|
73
|
+
otherwise marks silently drift vs 8001.
|
|
74
|
+
|
|
75
|
+
Priority (locked):
|
|
76
|
+
0. FTShare pair present (both ``ts_millis`` + ``ts_millis_open``):
|
|
77
|
+
**always** ``ts_millis`` (close). Beats generic ``ts``/``time`` so a
|
|
78
|
+
coincidental open-named ``ts`` cannot steal the bar clock.
|
|
79
|
+
1. Explicit host canonical: ``time`` / ``timestamp`` / ``ts``
|
|
80
|
+
2. FTShare close alone: ``ts_millis``
|
|
81
|
+
3. Last resort open: ``ts_millis_open`` only if close missing
|
|
82
|
+
"""
|
|
83
|
+
has_close = raw.get("ts_millis") is not None and raw.get("ts_millis") != ""
|
|
84
|
+
has_open = raw.get("ts_millis_open") is not None and raw.get("ts_millis_open") != ""
|
|
85
|
+
|
|
86
|
+
# FTShare contract detected → force close. Do not let generic ``ts`` win.
|
|
87
|
+
if has_close and has_open:
|
|
88
|
+
return raw.get("ts_millis")
|
|
89
|
+
|
|
90
|
+
# Explicit host-provided canonical (interpreted as close by semantic lock).
|
|
91
|
+
for key in ("time", "timestamp", "ts"):
|
|
92
|
+
if raw.get(key) is not None and raw.get(key) != "":
|
|
93
|
+
return raw.get(key)
|
|
94
|
+
|
|
95
|
+
if has_close:
|
|
96
|
+
return raw.get("ts_millis")
|
|
97
|
+
|
|
98
|
+
if has_open:
|
|
99
|
+
return raw.get("ts_millis_open")
|
|
100
|
+
|
|
101
|
+
return None
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _pick_raw_open_time(raw: dict[str, Any]) -> Any:
|
|
105
|
+
"""Return an explicit bar-open timestamp without changing ``time`` semantics."""
|
|
106
|
+
for key in ("ts_millis_open", "open_time", "open_timestamp", "start_time"):
|
|
107
|
+
if raw.get(key) is not None and raw.get(key) != "":
|
|
108
|
+
return raw.get(key)
|
|
109
|
+
return None
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def normalize_rows(rows: Any) -> list[dict[str, Any]]:
|
|
113
|
+
"""Normalize heterogeneous candle dicts to canonical rows (no length check).
|
|
114
|
+
|
|
115
|
+
Accepts keys:
|
|
116
|
+
time / timestamp / ts / ts_millis / ts_millis_open / date / trade_date
|
|
117
|
+
open high low close volume/vol
|
|
118
|
+
turnover / amount (optional passthrough — no v1 metric uses it)
|
|
119
|
+
|
|
120
|
+
Output always:
|
|
121
|
+
{time: sec, open, high, low, close, volume, [open_time?], [turnover?]}
|
|
122
|
+
sorted ascending by time; duplicate times keep last.
|
|
123
|
+
"""
|
|
124
|
+
out: list[dict[str, Any]] = []
|
|
125
|
+
if not isinstance(rows, list):
|
|
126
|
+
return out
|
|
127
|
+
for raw in rows:
|
|
128
|
+
if not isinstance(raw, dict):
|
|
129
|
+
continue
|
|
130
|
+
t = _to_time_sec(_pick_raw_time(raw))
|
|
131
|
+
if t is None:
|
|
132
|
+
date_txt = str(raw.get("date") or raw.get("trade_date") or "").strip()[:10]
|
|
133
|
+
if date_txt:
|
|
134
|
+
t = _date_text_to_sec(date_txt)
|
|
135
|
+
o = _to_float(raw.get("open"))
|
|
136
|
+
h = _to_float(raw.get("high"))
|
|
137
|
+
l = _to_float(raw.get("low"))
|
|
138
|
+
c = _to_float(raw.get("close"))
|
|
139
|
+
vol = _to_float(raw.get("volume") if raw.get("volume") is not None else raw.get("vol"))
|
|
140
|
+
volume = 0.0 if vol is None else float(vol)
|
|
141
|
+
if t is None or None in (o, h, l, c) or not all(
|
|
142
|
+
math.isfinite(float(value)) for value in (o, h, l, c, volume)
|
|
143
|
+
):
|
|
144
|
+
# Drop incomplete bars (OHLC missing). validate_rows may still fail
|
|
145
|
+
# if too few remain.
|
|
146
|
+
continue
|
|
147
|
+
row: dict[str, Any] = {
|
|
148
|
+
"time": int(t),
|
|
149
|
+
"open": float(o),
|
|
150
|
+
"high": float(h),
|
|
151
|
+
"low": float(l),
|
|
152
|
+
"close": float(c),
|
|
153
|
+
"volume": volume,
|
|
154
|
+
}
|
|
155
|
+
open_time = _to_time_sec(_pick_raw_open_time(raw))
|
|
156
|
+
if open_time is not None and open_time <= int(t):
|
|
157
|
+
row["open_time"] = int(open_time)
|
|
158
|
+
turnover = _to_float(
|
|
159
|
+
raw.get("turnover") if raw.get("turnover") is not None else raw.get("amount")
|
|
160
|
+
)
|
|
161
|
+
if turnover is not None:
|
|
162
|
+
row["turnover"] = float(turnover)
|
|
163
|
+
out.append(row)
|
|
164
|
+
|
|
165
|
+
# Contract: sort by time ascending (even if input was shuffled).
|
|
166
|
+
out.sort(key=lambda r: r["time"])
|
|
167
|
+
# De-dup by time (keep last) — basic hygiene; advanced "reject dups" is P1.
|
|
168
|
+
dedup: dict[int, dict[str, Any]] = {}
|
|
169
|
+
for r in out:
|
|
170
|
+
dedup[int(r["time"])] = r
|
|
171
|
+
return [dedup[k] for k in sorted(dedup)]
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def validate_rows(rows: Any, *, min_len: int = 2, strict_prices: bool = False) -> list[dict[str, Any]]:
|
|
175
|
+
"""Contract gate for calc_* / draw_*.
|
|
176
|
+
|
|
177
|
+
P0 basics (hard fail):
|
|
178
|
+
1. empty / fewer than min_len valid bars
|
|
179
|
+
2. OHLC missing after normalize (absorbed into length failure;
|
|
180
|
+
if input is non-empty list but all bars lack OHLC → explicit message)
|
|
181
|
+
3. timestamps sorted ascending (normalize always sorts)
|
|
182
|
+
|
|
183
|
+
Set strict_prices for public OHLCV input. Pivot/shape algorithms also use
|
|
184
|
+
synthetic high/low sequences, so their existing contract stays unchanged.
|
|
185
|
+
"""
|
|
186
|
+
if rows is None:
|
|
187
|
+
raise RowsValidationError("rows is required (got None)")
|
|
188
|
+
if not isinstance(rows, list):
|
|
189
|
+
raise RowsValidationError(f"rows must be a list, got {type(rows).__name__}")
|
|
190
|
+
if len(rows) == 0:
|
|
191
|
+
raise RowsValidationError("rows is empty")
|
|
192
|
+
|
|
193
|
+
for index, raw in enumerate(rows):
|
|
194
|
+
if not isinstance(raw, dict):
|
|
195
|
+
continue
|
|
196
|
+
raw_time = _to_float(_pick_raw_time(raw))
|
|
197
|
+
if raw_time is not None and not math.isfinite(raw_time):
|
|
198
|
+
raise RowsValidationError(f"bar {index}: timestamp must be finite")
|
|
199
|
+
|
|
200
|
+
raw_count = len(rows)
|
|
201
|
+
norm = normalize_rows(rows)
|
|
202
|
+
|
|
203
|
+
if raw_count > 0 and len(norm) == 0:
|
|
204
|
+
raise RowsValidationError(
|
|
205
|
+
f"no valid bars after normalize (input had {raw_count} items; "
|
|
206
|
+
"need time + open/high/low/close on each bar)"
|
|
207
|
+
)
|
|
208
|
+
if len(norm) < min_len:
|
|
209
|
+
raise RowsValidationError(
|
|
210
|
+
f"need at least {min_len} valid bars, got {len(norm)} "
|
|
211
|
+
f"(input items={raw_count})"
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
# Defensive: enforce monotonic times (normalize already sorts + dedups).
|
|
215
|
+
prev = None
|
|
216
|
+
for r in norm:
|
|
217
|
+
t = int(r["time"])
|
|
218
|
+
if strict_prices and r["high"] < r["low"]:
|
|
219
|
+
raise RowsValidationError(f"bar at {t}: high must be at least low")
|
|
220
|
+
if strict_prices and r["volume"] < 0:
|
|
221
|
+
raise RowsValidationError(f"bar at {t}: volume must be non-negative")
|
|
222
|
+
if prev is not None and t < prev:
|
|
223
|
+
raise RowsValidationError(
|
|
224
|
+
f"timestamps not sorted after normalize (saw {t} after {prev})"
|
|
225
|
+
)
|
|
226
|
+
prev = t
|
|
227
|
+
|
|
228
|
+
return norm
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Pinned TA-Lib engine shared by public analysis and built-in indicators."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import talib
|
|
10
|
+
from talib import abstract
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
ENGINE_VERSION = "0.8.2"
|
|
14
|
+
FUNCTION_COUNT = 201
|
|
15
|
+
if talib.__version__ != ENGINE_VERSION or len(talib.get_functions()) != FUNCTION_COUNT:
|
|
16
|
+
raise RuntimeError("TA-Lib version/catalog mismatch; legacy formulas are not a fallback")
|
|
17
|
+
|
|
18
|
+
NATIVE_VERSION = getattr(talib, "__ta_version__", b"unknown")
|
|
19
|
+
if isinstance(NATIVE_VERSION, bytes):
|
|
20
|
+
NATIVE_VERSION = NATIVE_VERSION.decode("utf-8", errors="replace")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def arrays(rows: list[dict[str, Any]]) -> dict[str, np.ndarray]:
|
|
24
|
+
return {key: np.asarray([float(row[key]) for row in rows], dtype=np.float64)
|
|
25
|
+
for key in ("open", "high", "low", "close", "volume")}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def finite_map(rows: list[dict[str, Any]], values: Any, digits: int = 6) -> dict[int, float]:
|
|
29
|
+
result: dict[int, float] = {}
|
|
30
|
+
for row, value in zip(rows, values):
|
|
31
|
+
number = float(value)
|
|
32
|
+
if math.isfinite(number):
|
|
33
|
+
result[int(row["time"])] = round(number, digits)
|
|
34
|
+
return result
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def catalog() -> list[dict[str, Any]]:
|
|
38
|
+
return [{"name": name, **abstract.Function(name).info} for name in talib.get_functions()]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def calculate(name: str, rows: list[dict[str, Any]], *, parameters: dict[str, int | float] | None = None,
|
|
42
|
+
price_sources: dict[str, str] | None = None, periods_constant: int = 14) -> tuple[dict[str, Any], dict[str, list[int | float | None]]]:
|
|
43
|
+
if name not in talib.get_functions():
|
|
44
|
+
raise ValueError("unknown TA-Lib function")
|
|
45
|
+
if not 1 <= len(rows) <= 4000:
|
|
46
|
+
raise ValueError("TA-Lib requires 1..4000 loaded candles")
|
|
47
|
+
function = abstract.Function(name)
|
|
48
|
+
info = function.info
|
|
49
|
+
defaults = info["parameters"]
|
|
50
|
+
selected = dict(parameters or {})
|
|
51
|
+
if set(selected) - set(defaults):
|
|
52
|
+
raise ValueError("unsupported TA-Lib parameter")
|
|
53
|
+
for key, value in selected.items():
|
|
54
|
+
default = defaults[key]
|
|
55
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value):
|
|
56
|
+
raise ValueError("TA-Lib parameters must be finite numbers")
|
|
57
|
+
if type(default) is int and (type(value) is not int or abs(value) > 4000):
|
|
58
|
+
raise ValueError("TA-Lib integer parameter out of bounds")
|
|
59
|
+
if type(default) is float:
|
|
60
|
+
if abs(value) > 1_000_000:
|
|
61
|
+
raise ValueError("TA-Lib numeric parameter out of bounds")
|
|
62
|
+
# JSON callers naturally send 2 for a deviation of 2.0. The
|
|
63
|
+
# TA-Lib abstract wrapper requires an actual Python float.
|
|
64
|
+
selected[key] = float(value)
|
|
65
|
+
function.parameters = selected
|
|
66
|
+
sources = dict(price_sources or {})
|
|
67
|
+
allowed_roles = set(info["input_names"]) & {"price", "price0", "price1"}
|
|
68
|
+
if set(sources) - allowed_roles:
|
|
69
|
+
raise ValueError(f"{name} price_sources keys must be {sorted(allowed_roles)}; "
|
|
70
|
+
"see list_ta_functions input_names, not low-level TA-Lib names")
|
|
71
|
+
if any(value not in {"open", "high", "low", "close"} for value in sources.values()):
|
|
72
|
+
raise ValueError("price_sources values must be open, high, low, or close")
|
|
73
|
+
if sources:
|
|
74
|
+
function.input_names = sources
|
|
75
|
+
data = arrays(rows)
|
|
76
|
+
if name == "MAVP":
|
|
77
|
+
if type(periods_constant) is not int or not 2 <= periods_constant <= 4000:
|
|
78
|
+
raise ValueError("MAVP periods_constant must be an integer from 2 to 4000")
|
|
79
|
+
minperiod = selected.get("minperiod", defaults["minperiod"])
|
|
80
|
+
maxperiod = selected.get("maxperiod", defaults["maxperiod"])
|
|
81
|
+
if not minperiod <= periods_constant <= maxperiod:
|
|
82
|
+
raise ValueError("MAVP periods_constant must be within minperiod and maxperiod")
|
|
83
|
+
data["periods"] = np.full(len(rows), float(periods_constant), dtype=np.float64)
|
|
84
|
+
elif periods_constant != 14:
|
|
85
|
+
raise ValueError("periods_constant applies only to MAVP")
|
|
86
|
+
function.set_input_arrays(data)
|
|
87
|
+
raw = function()
|
|
88
|
+
outputs = raw if isinstance(raw, (list, tuple)) else [raw]
|
|
89
|
+
names = list(info["output_names"])
|
|
90
|
+
if len(outputs) != len(names):
|
|
91
|
+
raise RuntimeError("TA-Lib output metadata mismatch")
|
|
92
|
+
values: dict[str, list[int | float | None]] = {}
|
|
93
|
+
for output_name, output in zip(names, outputs):
|
|
94
|
+
vector = np.asarray(output)
|
|
95
|
+
if len(vector) != len(rows):
|
|
96
|
+
raise RuntimeError("TA-Lib output length mismatch")
|
|
97
|
+
integer_output = np.issubdtype(vector.dtype, np.integer)
|
|
98
|
+
values[output_name] = [int(x) if integer_output else float(x) if math.isfinite(float(x)) else None for x in vector]
|
|
99
|
+
return info, values
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# 无 Key 搜索与跨市场行情
|
|
2
|
+
|
|
3
|
+
证券目录、K 线、板块排行是三种独立能力。搜到代码不保证数据接口允许取数;没有 Key 也不表示所有美股数据都不可用。
|
|
4
|
+
|
|
5
|
+
## v0.6.6 起的搜索路径
|
|
6
|
+
|
|
7
|
+
- 搜索只读取本地目录和内置常用条目,不在每次输入时请求行情供应商。
|
|
8
|
+
- 港股后台目录来自港交所公开证券列表;美股来自 Nasdaq 的 `nasdaqlisted.txt` 和 `otherlisted.txt`,不依赖 FTShare SDK 或 Key。目录只覆盖支持表示的交易所证券,不代表 OTC 全市场或行情权限。
|
|
9
|
+
- FTShare 仍提供 A 股、指数与 ETF 目录;美股公开目录不可用时可尝试已有 FTShare 目录接口。网络失败保留原缓存和常用条目。
|
|
10
|
+
- 常用目录包含中文别名,后台刷新合并英文名称时会保留这些别名;只缓存了 A 股的旧目录不会再阻止港美股刷新至第二天。目录格式保持兼容,不清除已有缓存。
|
|
11
|
+
|
|
12
|
+
公开目录说明:[Nasdaq 字段定义](https://www.nasdaqtrader.com/trader.aspx?id=symboldirdefs)、[港交所证券列表](https://www.hkex.com.hk/eng/services/trading/securities/securitieslists/ListOfSecurities.xlsx)。目录不包含行情价格。
|
|
13
|
+
|
|
14
|
+
## v0.6.7 港美股免费日 K
|
|
15
|
+
|
|
16
|
+
无 Key 时先尝试腾讯公开日 K;配置 Key 时 FTShare 优先,仅在失败后尝试腾讯。港股支持不复权、前复权和后复权,美股支持不复权和前复权;不支持的复权不会改名冒充。按页回溯最多 4,000 根,实际数量、起止日期和不足原因会返回。成交量为历史当日股数,不随拆股复权缩放。分钟、周月线、新闻和板块排行未增加免费路径。详细合同、事件核验及边界见[免费港美股日 K](free-foreign-daily.md)。
|
|
17
|
+
|
|
18
|
+
## 行情与错误
|
|
19
|
+
|
|
20
|
+
- 港股历史 K 线使用 `hk_candlesticks`,美股使用 `eastmoney_us_stock_daily_ohlc`;失败不会改走 A 股 `stock-candlesticks`,原始的权限、限流和上游故障分类会被保留。
|
|
21
|
+
- 匿名接口拒绝不再显示“Key 有效”;HTTP 400 表示需要检查市场、代码或参数,不能据此认定需要升级套餐。
|
|
22
|
+
- 美股股类会保留,例如 `BRK.B.US` 向专用接口传 `BRK.B`。
|
|
23
|
+
- 无 Key 时板块排行仍未接入免费源,页面会明确说明匿名模式的范围;指数、已支持的 K 线与搜索可继续使用。配置 Key 也不自动保证所有接口有权限。
|
|
24
|
+
|
|
25
|
+
## 2026-11-05 实测与免费源评估
|
|
26
|
+
|
|
27
|
+
隔离的无 Key 环境中:FTShare 的 `AAPL.US` 日 K 请求成功,港股 `00700.HK` 专用接口返回 403 `route_not_allowed`。旧实现随后把港股请求送入 A 股接口,返回 400 并覆盖了原始原因;本次修复停止这种跨市场回退。
|
|
28
|
+
|
|
29
|
+
不使用 FTShare SDK 的公开目录实测得到 3,270 个港股目录项及 12,864 个美国交易所目录项,能够搜索特斯拉、Adobe、港交所等。数量反映本次快照,会随上市和目录过滤规则变化;英文全目录不等于全市场中文别名覆盖。
|
|
30
|
+
|
|
31
|
+
腾讯现有公开 JSON 日 K 路径也作了只读评估:请求 10 根时,`hk00700` 返回 10 根可解析日 K,`usAAPL` 仅返回 2 根。这是 v0.6.6 的初步评估记录;v0.6.7 已核对专用路由、完整美股交易所代码、历史分页与拆股/分红样本,并新增自动备用链路。未经交易所限定的 `usAAPL` 两行响应不再被采用。
|
|
32
|
+
|
|
33
|
+
上述目录和错误提示改进自 v0.6.6 提供。固定版本用户需更新至 v0.6.6;已发布 v0.6.5 的安装包保持不变。
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# 绘图中的自动识别
|
|
2
|
+
|
|
3
|
+
## 共用流程与扩展约定
|
|
4
|
+
|
|
5
|
+
五种自动分析统一由 `tools/automatic_analysis.py` 注册和分发:普通拐点绘图、平行通道、平台框、价格形态与九转。DSH 和 Codex 从这里读取相同的参数默认值、计算结果、候选到绘图对象的映射;图表界面按一份请求映射调用分析,只读取引擎顺序,默认选择第一项,不再根据形态状态或距今远近二次筛选。候选有多项时切换仍更新当前对象;无候选就不画。九转是整段计数序列,不参与候选排序。
|
|
6
|
+
|
|
7
|
+
统一骨架为:**当前图绑定 → 只读分析 → 按引擎顺序选候选 → 使用结果引用准备绘图 → 应用并核对回执 → 更新同一对象或清除**。自动候选均由受保护的结果引用解析,普通图形也不由浏览器手抄识别锚点;手动选点是明确的另一种输入方式,但沿用同一准备/应用/回执链路。九转以整体序列结果引用替代候选序号,并在行情变化时重新计算。
|
|
8
|
+
|
|
9
|
+
新增图形时先确定类型是“候选结构”还是“整段序列”,再在共享注册表中声明参数、默认值、结果版本和选择方式,最后接入受保护的准备/应用/回执及画布渲染。每个识别器继续负责自己的几何条件、因果时间、拒绝原因和排序:普通拐点按最新锚点、通道按最新终点、平台框在同一截止日按边界质量、价格形态按首次可知日期排序。这些规则并不等价,不能用一个通用评分替代。
|
|
10
|
+
|
|
11
|
+
`channel_candidates` 和 `range_candidates` 的公开旧接口保留 `closed_daily_only` 可选参数;绘图界面调用它们时显式传 `true`。因此兼容旧接口的非日线分析不等于本版“一键绘图”的日线承诺。
|
|
12
|
+
|
|
13
|
+
「绘图」只有一个工具选择框,所有图形、形态和九转从这里选择。点击「一键绘图」时默认显示该工具返回的最新合格候选;形态还会标明形成后发生的确认、失效或过期状态。用户可切换候选;没有候选时不强行绘图。九转直接计算并显示数字。所有自动分析只读取当前图已加载的单日线,不抓新行情;A/港/美股当日 K 线只有到达交易所本地的保守收盘时点后才参与计算。
|
|
14
|
+
|
|
15
|
+
- 水平线:最近严格高/低拐点;趋势线:最近同侧两个已确认拐点;斐波那契:最近两个交替拐点。ABC 要求起点及 A/B/C 共四个交替拐点、三段完整走势,B 回撤为 A 段的 30%–80%,C 段为 A 段的 80%–250% 且越过 A;仍只是价格几何候选,不宣称验证完整艾略特子浪。候选最新锚点距最后一根已收盘日线不超过 30 根,均显示可修改的锚点。没有可信候选时仍可手动选点。
|
|
16
|
+
- 平行通道和平台框继续使用既有候选规则,但绘图面板只用已收盘日线调用,优先给出当前候选;可手动选点。平台突破/失效的状态由价格形态规则中的平台候选给出,不从普通矩形推断。
|
|
17
|
+
- 双顶/双底、头肩顶/底和平台状态使用共享的 `price_pattern_candidates` 规则。选中对应工具即分析并绘制最近候选;用户可切换候选,图形在同一对象中更新。四类反转形态允许从左到右手动指定 3/5 个锚点,仍须通过同一标准规则,不能凭手点绕过结构验证。平台手动框选走普通平台框工具。
|
|
18
|
+
- 九转打开后使用共享的 `nine_count` 引擎自动计算并绘制逐根数字,默认展示最近 30 根已收盘日线以免窄栏图表过密;历史数据仍用于预热计数。当前图的行情变化时重新计算并更新同一个图表对象。
|
|
19
|
+
|
|
20
|
+
自动候选通过受保护的准备、应用和回执流程直接绘制,关闭「绘图」不会删除图形;只有「绘图」内的「清除绘图」会移除当前图的图形与标注。任意标签、任意文字、交易结论和无法由已加载行情确定的形状不自动生成。自动选点只是一种可审查的几何建议,不表示交易收益或买卖信号。
|
|
21
|
+
|
|
22
|
+
对话宿主也能完成同样的两种请求。用户说“分析一下双顶”时只返回分析;说“把最近符合规则的双顶画出来”或明确指定绘图工具时,宿主使用共享引擎返回的第一项合格候选,立即绘图并回报其当前状态,不再要求点击保存。用户指定日期、点位或候选序号时,优先照其选择;无合格候选时说明原因,不强行绘制。DSH 和 Codex 使用同一 Python 规则及受保护的图表应用链路,图上的候选切换和对话中的更新都复用原对象。
|
|
23
|
+
|
|
24
|
+
选择自动候选时,图表会将候选锚点移入画布中央并按跨度调整缩放。之后仅缩放画布会重新定位图形;行情、标的或来源变化仍须重新分析。用户手动拖动时间轴时保留其视角。
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# 图表函数:确定性计算与受保护绘图
|
|
2
|
+
|
|
3
|
+
## TA-Lib 计算层(v0.5.0 候选)
|
|
4
|
+
|
|
5
|
+
标准技术指标统一使用固定版本 TA-Lib 0.8.2;启动时核对版本与 201 个函数,失败即报错,不回退旧公式。`list_ta_functions` 按类别或名称搜索、分页返回输入、参数默认值、输出名;`run_ta_function` 只读取当前图最多 4000 根已加载 OHLCV,不抓新行情、不接收宿主数组或代码,返回简短摘要和 `analysis_only` 结果引用;`read_ta_result` 按输出名分页读取原始序列。每张图最多保留 16 个临时结果,第 17 个起淘汰最早者;被淘汰的引用须重新计算。价格型输入可选择当前 O/H/L/C;`MAVP` 首版使用明确的固定逐根周期。暖机或无定义值为 `null`,形态函数的 0 保留。没有有限输出会如实标记 `no_finite_output`,不编造交易信号;运行函数本身不创建图形、不修改视口,仅白名单结果可在用户明确要求后进入受保护绘图链。
|
|
6
|
+
|
|
7
|
+
新图表负载与指标摘要标记 `calculation_engine` 版本。已保存的旧负载不后台改写;没有此字段的旧快照不应被称为 TA-Lib 计算结果。现有 MA、成交量均线、MACD、KDJ、BOLL、RSI、ATR、VWAP 的新计算均走 TA-Lib。迁移口径:ATR 首个有效值从旧索引 13 变为 14;MACD 柱从 `2 × (DIF − DEA)` 变为原始 `DIF − DEA`;平盘 RSI 等边界值也可能变化,不能用旧值强行判错。支撑压力、回撤等非 TA-Lib 分析保留原逻辑。TA-Lib 不提供自动通道、艾略特 ABC 或缠论识别;缠论本轮搁置,不安装 CZSC。
|
|
8
|
+
|
|
9
|
+
网页版自身开图、切换周期/范围和调整指标时,现通过内部 `calculate_chart_indicators` 入口,把当前已加载 K 线交给插件侧统一计算。这个入口不是新的宿主分析工具,不抓行情、不画图;返回 TA-Lib 版本和完整 K 线指纹供网页核对。核对失败会显示错误,不回退旧前端公式。这里的 `calculation_engine` 指指标序列的计算口径;摘要中的支撑压力、最大回撤和基于指标的事件判断仍包含自有规则,不能统称为 TA-Lib 函数结果。网页视觉一致性仍须单独验收。
|
|
10
|
+
|
|
11
|
+
TA 结果落图白名单支持 `SMA(close, 2..200)`、`EMA(close, 2..200)` 单线和 `BBANDS(close)` 三线:`run_ta_function` 仍是只读计算,但有效结果会标出可选的 `series_line/real` 或 `price_band/upperband,middleband,lowerband`。BBANDS 限定 `timeperiod=2..200`、`matype=0`、上下偏差各大于0且不超过5,三个有限价位须按上/中/下严格排序。用户明确要求画线后,宿主把该 `result_ref` 放入 `prepare_chart_edit` 的 `edit.ta_result_ref`(对应 `kind=series_line` 或 `price_band`),再执行应用与状态核对。插件从本地结果记录读取完整序列,核对同一视图、观察版本、标的、周期、复权与全部已加载 K 线;不要求宿主抄写数组。准备回执中的 `object` 只给 ID、来源、有效根数与哈希;`drawing_guard.parameters` 也仅保留参数 SHA-256 摘要,完整值留在受保护的候选快照。新线或三线组沿用各自稳定 ID,改参数需在新观察版本重新计算,改颜色及删除可直接使用对象 ID。这些是独立的 AI 主图对象,**不是**修改图表已有的 MA/BOLL 指标开关;其他 TA 函数仍不能用此入口绘图。计算成功、对象应用成功和实际像素可见仍分开验收。
|
|
12
|
+
|
|
13
|
+
序列对象(条件线、价带、柱线)的准备回执统一只返回对象摘要和参数摘要,不回传逐根价格数组;候选快照保留完整序列供应用。`run_ta_function.price_sources` 使用目录中的通用输入名,例如 SMA 为 `{"price":"close"}`,不是底层 TA-Lib 的 `inReal`;默认收盘价可省略此字段。
|
|
14
|
+
|
|
15
|
+
宿主理解自然语言、选择函数和锚点;插件执行确定性计算、绘图与反馈。不增加插件 LLM、不运行任意代码、不用预置话术代替分析。M2 已接入带标签折线及独立文字 / 数值 / 图标标注。M3 候选仍需更多专业质量样本;M4.1 序列线已在本地网页验证跨周期恢复与跨标的隔离,细小断线空档仍缺足够视觉证据。鼠标选中、拖拽 / 撤销尚未完成。
|
|
16
|
+
|
|
17
|
+
## 已接入的函数
|
|
18
|
+
|
|
19
|
+
| 函数 | 必填参数 | 计算口径 |
|
|
20
|
+
| --- | --- | --- |
|
|
21
|
+
| `fibonacci_retracement` | start_time、start_price、end_time、end_price | 从 A 到 B,回撤价位为 B-r*(B-A) |
|
|
22
|
+
| `parallel_channel` | 上述两点及 parallel_time、parallel_price | 复用现有等距 K 线索引三点平行通道 |
|
|
23
|
+
| `range_box` | start_time、end_time、lower_price、upper_price | 指定范围的平台矩形,不是平台识别 |
|
|
24
|
+
| `labeled_polyline` | points:2至8个 label / time / price | 按给定顺序连接带标签锚点,不是自动识浪 |
|
|
25
|
+
| `pivot_candidates` | 可选 left_bars、right_bars、mode、limit | 返回已加载K线中的严格局部高 / 低点候选,只分析不绘图 |
|
|
26
|
+
| `regression_channel` | start_time、end_time;可选 multiplier | 对明确区间的收盘价做线性回归并计算统计上下轨,只分析不绘图 |
|
|
27
|
+
| `channel_candidates` | 可选 direction、lookback_bars、left_bars、right_bars、min_span_bars、max_candidates | 用已确认拐点提出上升/下降三锚点通道候选,只分析不绘图 |
|
|
28
|
+
| `range_candidates` | 可选 window_sizes、max_candidates | 用最新已加载K线的多个固定窗口提出水平平台区间候选,只分析不绘图 |
|
|
29
|
+
| `price_pattern_candidates` | 可选 pattern_types、lookback_bars、max_candidates | 用已收盘单日线识别双顶底、头肩顶底与冻结平台状态;直接绘图请求选返回的最新合格候选,纯分析请求不落图 |
|
|
30
|
+
| `nine_count` | 可选 display_bars | 逐根给出收盘价相对四根前的1–9计数;结果引用可保存为一组九转快照 |
|
|
31
|
+
| `price_series_line` | value_source;可选 condition | 对当前加载K线计算 OHLC、SMA 或 EMA(close, 2–200) 的主图序列线;条件为价格来源/固定价格的大小比较 |
|
|
32
|
+
| `conditional_annotation` | annotation_kind、anchor_source、condition;对应的一项内容 | 在当前加载K线中找到最近一根符合条件的K线,创建一枚文字、数值或图标标注 |
|
|
33
|
+
| `price_band` | upper_source、lower_source;可选 condition | 在两条当前图价格来源之间按K线填充,空档断开;上边界须始终高于下边界 |
|
|
34
|
+
| `price_sticks` | start_source、end_source;可选 condition | 每根命中K线独立绘制一条两价位之间的垂直柱线;等价端点不绘制 |
|
|
35
|
+
|
|
36
|
+
`pivot_candidates` 是 M3 第一个规则型分析函数:默认左右各2根确认K线,窗口可分别设为1至20根;`mode` 为 `high`、`low` 或 `both`;默认只返回按时间排序的最近20项,最多50项。候选必须严格高于 / 低于左右窗口内每一根K线的最高 / 最低价;相邻等高 / 等低不算拐点,不做平台合并、峰谷交替、趋势判断或形态评分。右侧确认K线不足的末端K线不参与计算;数据不足以形成完整窗口时报错,不补取行情。结果携带每个候选的 side、精确加载K线时间、价格和距最新K线的 bars_ago;`truncated` 标明是否省略较早候选。
|
|
37
|
+
|
|
38
|
+
`regression_channel` 是 M3 第二个规则型分析函数:必须明确给出首尾时间,均精确对应当前已加载K线,区间至少10根、最多4000根;不自动选起止点。以K线序号为等间距 x 轴,对区间收盘价做普通最小二乘线性回归;通道上下轨分别为拟合线 ± `multiplier × 残差总体 RMS`,`multiplier` 默认为2、范围0.5至5。结果返回斜率、回归线与上下轨首尾价格、残差总体标准差、R²、方向及计算口径。它不做统计显著性或趋势质量判断,不识别自动通道,不绘图;`result_ref` 为 `analysis_only`,不能传给 `prepare_chart_edit`。下轨非正或数据不足时明确报错。回归斜率的单位是每根已加载K线,不是每个自然日。
|
|
39
|
+
|
|
40
|
+
`channel_candidates` v3 是 M3 的只读选点实验:默认查看最近120根已加载K线,使用左右各2根确认的严格 pivot;上升候选由两个抬高的低点和中间一处高点构成,下降候选对称。两端至少相隔12根K线,基准线首尾价格变化至少3%;候选宽度不得超过锚点区间收盘价中位数的12%,也不得超过基准两点价格变动的2倍。完整锚点区间内,任一影线越轨不得超过通道宽度的25%,越轨K线占比不得超过20%。默认 `scope=current` 仅保留终点距最新K线不超过8根的候选,并独立核对终点之后到最新K线的算术延伸:最大影线超出不得超过宽度的25%,最新收盘须在延伸边界内。`scope=historical` 用于历史结构回看,不核对或推断当前突破。返回最多3组(可设1至5组)的精确锚点、斜率、宽度、越轨数量、越轨比例和最大偏离;同终点/同平行点的重复,或共用平行拐点且斜率与宽度均在10%内的近似几何候选会合并。拒绝原因按规则计数。交易日空档仍按K线序号计算。这些阈值只是保守几何筛选,不是专业质量评分;末端未确认拐点不纳入,也不保证一定有候选。零候选仅表示没有三锚点组合同时满足全部规则,不证明近期缺少拐点或真实走势没有通道;输出另列近期已确认高低拐点数量及这项解释限制。当前模式的区间外延伸仅是数学核对,不是“已确认突破”信号。候选需由宿主检查类型、时间和数据质量;仅分析时不绘图,用户直接要求绘图时可选最近合格候选;其 `result_ref` 仍为 `analysis_only`。
|
|
41
|
+
|
|
42
|
+
`range_candidates` 是 M3 的只读平台区间原型,区别于按用户给定边界绘图的 `range_box`。默认分别检查截至最新已加载K线的20、30、40、60根窗口;按窗口内最高影线和最低影线确定边界,要求上下边界各至少两次相隔3根以上的触碰,并限制区间宽度、整个窗口和最近10根的收盘价位移及拟合漂移。返回最多3组候选、精确时间/价位、触碰次数与时间、逐窗口拒绝原因;边界和终点完全相同的不同窗口会合并为一组,并报告去重数量。用户可指定1至8个互异的16至120根窗口及最多1至5组候选。没有符合规则的窗口应返回零候选,不强行画平台;这不是突破信号或已确认的专业平台识别,宿主检查结果后,用户直接要求绘图时可选最近合格候选。`result_ref` 仅供分析,不能作为普通 `edit.result_ref` 传给 `prepare_chart_edit`。
|
|
43
|
+
|
|
44
|
+
上述分析结果 `result_usage=analysis_only`,不能作为普通 `edit.result_ref` 绘图。所有自动候选统一使用受保护的专用入口:`candidate_result_ref`、从1开始的 `candidate_number`、`user_confirmed_candidate:true` 必须同时传入 `prepare_chart_edit` 的 `edit`。水平线、趋势线、斐波那契、ABC、通道、平台框和价格形态都由插件从引用中读取锚点或边界,不让宿主抄坐标。手动选点仍使用直接参数或普通函数结果;修改自动选中的水平价位会转为手动输入。用户直接要求绘图或明确选择候选时,宿主可设置 `user_confirmed_candidate:true` 并完成绘图;仅分析或要求先提案时保持只读。九转没有候选序号,可通过受保护的 `result_ref` 创建或更新单组 `nine_count` 快照。函数本身不会因返回候选而自动落图,也不声称候选是收益预测、缠论分型或波浪拐点。
|
|
45
|
+
|
|
46
|
+
## M4.1 条件序列线
|
|
47
|
+
|
|
48
|
+
`price_series_line` 的 `value_source` 为 `{ "kind": "open"|"high"|"low"|"close" }` 或 `{ "kind": "sma"|"ema", "period": 2..200 }`。可选 `condition` 使用同一价格单位:`left` 为价格来源,`op` 为 `gt/gte/lt/lte`,`right` 为 `{ "source": 价格来源 }` 或 `{ "constant": 正价格 }`。不接受宿主传来的行情数组、任意代码或公式文本。均线暖机与条件不成立处存为 `null`,渲染时断线;零命中只返回空结果摘要,不能创建空图形。
|
|
49
|
+
|
|
50
|
+
`conditional_annotation` 复用上述价格来源与条件,但条件必填。`annotation_kind` 选择 `text_annotation`、`number_annotation` 或 `icon_annotation`,并且只传对应的 `annotation_text`、`annotation_value` 或 `annotation_icon`。首版固定选择**最近一次命中的已加载 K 线**,在其 `anchor_source` 价格处生成一枚标注;不是把所有命中 K 线批量标记。函数只返回摘要和结果引用,创建/重算位置及内容使用对应标注 `kind` 的 `prepare_chart_edit(result_ref)`,续改和删除沿用已有对象 ID。零命中不能落图;也不将条件本身解释为买卖信号。2026-10-21 本地网页验证了创建、同ID改字改色与删除,但最新K线长标签在右缘裁切;源码已补文字位置约束,待下一次网页验证。
|
|
51
|
+
|
|
52
|
+
`price_band` 的上下边界分别使用 `upper_source` / `lower_source`,取值范围同序列线;可选同一种价格条件。每根K线必须是上边界严格高于下边界,否则明确拒绝,不自动交换边界。均线暖机与条件不成立处上下值同时为 `null`,相邻有效点之间填充多边形,跨空档不连接;零命中不能落图。BBANDS 的 TA 引用走同一类对象,但携带中轨,作为一组上/中/下三线修改或删除;普通两边界价带不能伪造中轨。完整序列仅保存在有大小上限的本地结果记录中,宿主只传结果引用。最多4000根、4个 AI 价带;每个价带是一个稳定 ID,创建、重算来源/条件、改样式、删除及反馈沿用受保护流程。2026-10-21 本地网页验证了普通填充可见、同ID改色与删除;BBANDS 三线视觉另行核验。反馈可确认指标注册,不证明实际像素可见。`show_fill` 不是价带参数,不能套用平台框字段。
|
|
53
|
+
|
|
54
|
+
`price_sticks` 是 STICKLINE 的受控首版:两端分别取 `start_source` / `end_source` 的 OHLC 或 SMA(close, 2–200),可使用同一种价格条件。每根命中K线独立画一条垂直柱线,不跨空档连接;相等端点为零长度,跳过而不是伪造可见对象。两端可交叉,不自动交换。最多4000根、4组对象;完整序列留在受限本地结果记录,宿主只传结果引用。创建、重新计算、改颜色、删除走受保护链路;源码和自动测试已接通,且已加载到 56790 本地实例,网页像素验收仍待下一轮,不等同于完整通达信 STICKLINE 语义。
|
|
55
|
+
|
|
56
|
+
计算只返回摘要与 `result_ref`,完整序列保存在上限256 KiB的本地结果记录;最多4000根当前加载K线、8个 AI 条件序列线对象。创建、修改来源/条件、改颜色和删除均走当前图的准备→应用→状态核对,整条线只有一个稳定 ID。序列只画在价格主图;切换周期后不同周期的对象保留但隐藏,返回原周期还要核对 K线身份,不能拿旧结果给新周期套图。前端复用已有 KLineCharts 指标渲染,不重置行情、原指标或视口。反馈可确认指标注册,但不是像素可见性证明。
|
|
57
|
+
|
|
58
|
+
斐波那契默认比例为 0、0.236、0.382、0.5、0.618、0.786、1;可指定1至16个递增、不重复的0至1比例。0位是终点 B,1位是起点 A;允许上涨及下跌,拒绝等价锚点。不支持扩展位或对数计算;不根据屏幕像素插值算价格。0.618线默认加粗。
|
|
59
|
+
|
|
60
|
+
所有时间必须精确落在已加载 K 线上;沿用日期 / 带时区 ISO / Unix 秒字符串规则。起点早于终点,不吸附交易日、不补取行情、不自动选最近高低点。首轮重点仍为日线 / 周线,分钟沿用来源与权限限制。
|
|
61
|
+
|
|
62
|
+
## 接口与数据流
|
|
63
|
+
|
|
64
|
+
1. `list_chart_functions` 返回已实现函数、版本、参数和限制;不是整个金融公式语言。
|
|
65
|
+
2. `get_chart_context` 获取本对话当前视图、观察版本及对齐的命名来源。
|
|
66
|
+
3. `run_chart_function(context_ref, expected_revision, chart_session, symbol, function, parameters)` 只计算,返回 `result_ref`、规范化参数、简短结果及 `calculation_ms`。不创建候选快照或修改图表。
|
|
67
|
+
4. `result_usage=guarded_drawing` 的结果调用原有 `prepare_chart_edit`,在 `edit` 内指定 `result_ref`、对应 `kind` 与 `action`。更新还需已有 `object_id`;可以附加名称 / 颜色,以及适用的中线 / 填充开关。引用与直接锚点、边界、比例互斥。`pivot_candidates`、`regression_channel` 没有落图入口;通道/平台候选只在用户明确选定后使用上述专用候选入口。
|
|
68
|
+
5. 继续 `apply_chart_edit` 和 `get_chart_edit_status`。计算结果、准备候选、排队、应用确认是不同状态;对象 API 反馈不等于像素核验。
|
|
69
|
+
|
|
70
|
+
函数名与绘图类型的对应关系:fibonacci_retracement → fibonacci_retracement,parallel_channel → channel,range_box → rectangle。
|
|
71
|
+
|
|
72
|
+
labeled_polyline → labeled_polyline。调用参数 points 中每个点都有明确 label、time 和 price,标签在组内唯一、时间严格递增,不自动排序 / 吸附 / 选择点位。标签为1至16个字符,既可用 A/B/C、1/2/3/4/5,也可用其他名称,不限制为预置波浪模板。
|
|
73
|
+
|
|
74
|
+
## 独立文字与数值标注
|
|
75
|
+
|
|
76
|
+
通过原有 `prepare_chart_edit` 创建 `text_annotation`、`number_annotation` 或 `icon_annotation`,都需提供明确的 `anchor_time` 和 `anchor_price`。文字 / 数值分别使用 `annotation_text` / `annotation_value`;图标使用 `annotation_icon`,当前支持 `arrow_up`、`arrow_down`、`diamond`。锚点必须精确对应当前加载的 K 线;数字按输入数值显示。更新 / 删除必须使用当前观察中的对象 ID。标注与其他 AI 图形共用同一来源对齐、周期保护、应用确认、对象反馈和刷新恢复流程;不自动选择锚点或计算数字。
|
|
77
|
+
|
|
78
|
+
创建文字标注示例:
|
|
79
|
+
|
|
80
|
+
```json
|
|
81
|
+
{"kind":"text_annotation","action":"create","anchor_time":"2026-10-16","anchor_price":34,"annotation_text":"前高","name":"前高说明","color":"warning"}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
创建数值标注时改用 `kind:"number_annotation"` 和 `annotation_value:0.618`;后续按当前观察读取的 `object_id` 修改或删除。
|
|
85
|
+
|
|
86
|
+
创建菱形图标时使用 `kind:"icon_annotation"` 和 `annotation_icon:"diamond"`;标注名称会显示在图标旁。图标通过线段图元渲染,不依赖系统 emoji 字体。
|
|
87
|
+
|
|
88
|
+
斐波那契所有水平位、价位和比例标签归一个稳定对象 ID 管理;创建、锚点 / 比例更新、颜色 / 名称更新、整体删除均经过同一受保护链路。样式更新和删除不需要重新计算;几何更新建议重新运行函数,直接传引用,不让宿主重新手算价位。
|
|
89
|
+
|
|
90
|
+
## 带标签折线:只改一个点
|
|
91
|
+
|
|
92
|
+
创建时运行 labeled_polyline 函数,沿用 result_ref → prepare / apply / status;每组的折线和标签归一个稳定 ID 管理。数字标签也是文字,不包含独立数值格式化或自动波浪解释。
|
|
93
|
+
|
|
94
|
+
修改 C 点时,只向 prepare_chart_edit 的 edit 传入以下字段,不重新运行创建函数,不让宿主抄写 A/B 等邻点:
|
|
95
|
+
|
|
96
|
+
```json
|
|
97
|
+
{
|
|
98
|
+
"kind": "labeled_polyline",
|
|
99
|
+
"action": "update",
|
|
100
|
+
"object_id": "从当前观察读到的完整对象ID",
|
|
101
|
+
"point_label": "C",
|
|
102
|
+
"point_time": "2026-10-19",
|
|
103
|
+
"point_price": 31
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
可以只修改时间或价格,也可同时修改;点名必须明确存在。插件从原对象保留其他点,校验修改后的顺序和全部锚点,失败不改变原图。change.point_changes 给出本次候选中实际变化的点;这仍是候选差异,不代替应用回执。
|
|
108
|
+
|
|
109
|
+
整组名称 / 颜色更新直接使用同一 object_id 及 name / color;删除只传 kind、action、object_id,不逐条删标签或线段。点名是组内身份,不是全局图形 ID;若多组都含 C 或组名重名,宿主须先澄清,不能按最近对象猜测。
|
|
110
|
+
|
|
111
|
+
本步不支持更新时整组传 points 或创建 result_ref 重写已有对象,也不支持增删单点 / 重命名单点 / 调换顺序;这些需求另行实现。当前最多30组,每组2至8点;后续缠论长序列接入需单独设计容量,不直接套用该上限。
|
|
112
|
+
|
|
113
|
+
## 结果复用与边界
|
|
114
|
+
|
|
115
|
+
- 相同视图、相同行情和规范化参数产生同一引用。修改其他对象或视口不改变价格计算;准备 / 应用仍核对新鲜观察版本与完整当前状态。
|
|
116
|
+
- 引用绑定运行宿主、上下文、对话、视图、标的、周期、分钟粒度、复权、来源元数据及完整已加载 OHLCV 指纹。数据改变须重算,不能跨图借用。
|
|
117
|
+
- 本阶段最多4000根加载数据、每视图64个函数结果、30组斐波那契对象;不自动删除旧结果。结果文件只保存绑定指纹、函数版本、参数和紧凑结果,不复制行情或凭据。
|
|
118
|
+
- 函数引用不提供跨进程或重挂载复用保证;重挂载后先读新上下文并重算。已确认图形仍沿用现有同宿主检查点恢复协议,不扩大恢复承诺。
|
|
119
|
+
- 创建参数不全时澄清;用户明确授权宿主提出锚点时可由宿主分析。要求先提案时,确认前不得准备或应用。
|
|
120
|
+
- 不开放鼠标拖拽、撤销、手动对象 AI 修改、自动平台 / 波浪识别或缠论。当前通道候选只做规则型几何提议,不等于自动识别质量已验收。没有“专业判断正确”或收益保证。
|
|
121
|
+
|
|
122
|
+
## 验收与后续
|
|
123
|
+
|
|
124
|
+
### 第一期「专业绘图」界面
|
|
125
|
+
|
|
126
|
+
复用盘点:KLineCharts 9.6 的基础覆盖物与交互适合旧「画线」入口,但直接创建它们不会自动获得当前 AI 对象的来源绑定、稳定 ID、版本保护和应用反馈。因此第一期不把内置覆盖物另开为第二条落图通道。水平/趋势线、通道、平台框、斐波那契和 ABC 均复用仓库已有 AI 覆盖物及受保护对象;SMA/EMA 复用序列线,BOLL 复用三轨价带。KLineChart Pro 只参考工具选择、参数面板的组织方式,不复制代码或整包接入;extension 需要 10+,与当前 9.6 不兼容;HQChart 仅供函数分类参考,不引入公式解释器。若未来复制任一外部实现,先核对其许可证和附加条款。
|
|
127
|
+
|
|
128
|
+
图表工具栏新增「专业绘图」:SMA、EMA、BOLL、水平线、趋势线、平行通道、平台框、斐波那契、ABC 标注。201 个 TA-Lib 函数仍可通过宿主只读查询和计算,但界面仅开放这三类可绘制指标;既有 MA/BOLL 指标开关与新建的受保护绘图对象彼此独立。
|
|
129
|
+
|
|
130
|
+
用户在界面选 K 线锚点、改价与参数后,先得到未保存提案;再次确认才执行相同的计算/结果引用、准备、应用、状态核对流程。工作台只展示当前标的和周期的精选 AI 对象,可按完整 ID 改名、改色、改参数或删除;Agent 创建的同类对象也会进入清单,反向亦然。ABC 是指定 A/B/C 点的折线标注,不是自动识浪,修改时一次只更新一个有名点。来源、观察版本或图表切换后,提案失效;不通过旧引用重新落图。
|
|
131
|
+
|
|
132
|
+
旧「画线」手绘对象照常显示和保存,本期不自动迁移成可由 Agent 修改的对象。界面应用状态只证明网页接受了对象,不证明像素或选点质量;仍需集中网页验收。拖拽、撤销、缠论和自动形态判断不在本期。
|
|
133
|
+
|
|
134
|
+
自动化分别检查独立公式价位、重复结果一致性、函数引用绘图、稳定 ID 续改 / 删除、单点修改不改变邻点、来源隔离、错误拒绝、跨语言应用回执、对象反馈、恢复检查点及渲染模板。真实网页自然语言验收另行记录,不用隔离测试冒充网页通过。
|
|
135
|
+
|
|
136
|
+
耗时字段包含读取 / 校验 / 保存结果,不能当作纯算法耗时。token 与端到端提速尚无对照数据,不宣称已节省。本文描述 v0.5.0 发布候选;新指标计算以 TA-Lib 口径为准,旧快照保持原状。自动分析算法的专业质量仍另行验收。
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Natural-language drawing examples
|
|
2
|
+
|
|
3
|
+
[Back to overview](../README.en.md)
|
|
4
|
+
|
|
5
|
+
These historical captures demonstrate chat-driven drawing; the current UI differs.
|
|
6
|
+
|
|
7
|
+
Live natural-language example: DeepSeek screened the watchlist for an ABC **candidate** in Wolong Electric, checked the four 0/A/B/C anchors against candles, and used the plugin to draw them. This unedited full-page capture shows the conversation, application result, and visible chart together. The host interprets the pattern; a rules-based candidate is not a verified Elliott-wave conclusion.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
In the same chart, a follow-up natural-language request adds a descending channel. DeepSeek checks its three-anchor geometry and price deviations; the plugin applies it as a separate object while preserving the ABC labels. The lines are visible, but their trading relevance still requires independent judgment.
|
|
12
|
+
|
|
13
|
+

|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# 自然语言绘图实例
|
|
2
|
+
|
|
3
|
+
[返回首页](../README.md)
|
|
4
|
+
|
|
5
|
+
以下为历史版本实测,界面外观与当前候选版有差异。
|
|
6
|
+
|
|
7
|
+
自然语言实测:DeepSeek 从自选中筛出卧龙电驱的 ABC **候选**,逐根核对 0、A、B、C 四个锚点,再通过插件落图。同一张完整网页截图展示了对话、应用回执和右侧画布;画布上的折线与标签已实际可见。形态判断由宿主完成,规则筛查不等于专业波浪结论。
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
继续用自然语言在同一张图上添加下降通道:DeepSeek 核对三点几何与区间内的价格偏离,插件将通道作为独立对象落图;原有 ABC 标注仍保留。图上的线条可见,但通道是否具有交易参考价值仍需自行判断。
|
|
12
|
+
|
|
13
|
+

|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# 嵌入式图表:把 K 线挂进宿主面板
|
|
2
|
+
|
|
3
|
+
侧栏是图表的一种宿主,不是唯一一种。本页记录把同一个图表挂进**有界面板**(画布卡片、模拟盘侧栏、任意自建面板)时,宿主与插件之间的契约;没有这些选项时,插件的行为与侧栏挂载完全一致。
|
|
4
|
+
|
|
5
|
+
## 宿主侧入口
|
|
6
|
+
|
|
7
|
+
`src/client/KlineContent.tsx` 的 `StandaloneKlineApp` 接受一组可选宿主属性(类型定义在 `src/client/embed.ts`):
|
|
8
|
+
|
|
9
|
+
| 属性 | 取值 | 作用 |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| `embedded` | `boolean`,默认 `false` | 有界面板模式:对话框与提示锚在面板内,布局按面板高度排版,不再使用整页定位 |
|
|
12
|
+
| `chrome` | `'full' \| 'chart' \| 'chart-search'`,默认 `'full'` | 保留的工作区外壳:完整工作台、只留图表页、图表页加标的搜索 |
|
|
13
|
+
| `theme` | `'dark' \| 'light'`,省略即跟随宿主文档 | 固定图表的调色板;挂载后改变也只重绘,不重挂 |
|
|
14
|
+
| `symbol` / `name` | 标的代码与名称,可选 | 外部选择联动:面板选中的标的推给图表,同一标的不会重开 |
|
|
15
|
+
| `onIdentity` | `(symbol, name) => void` | 图表内的搜索或切换回调给宿主,宿主据此更新自己的单据 |
|
|
16
|
+
|
|
17
|
+
`embedded` 与 `chrome` 落到宿主元素的两个数据集属性(`data-embedded`、`data-chrome`),视图侧只按这两个属性选样式;侧栏挂载不带属性,因此内嵌样式不可能误命中侧栏。
|
|
18
|
+
|
|
19
|
+
标的比较保留市场身份,仅归一化同一市场的别名(如 `SH` / `XSHG`)。不带后缀的代码按图表输入规则解析;存在歧义时,宿主应传完整代码,例如 `000001.XSHG`(上证指数)与 `000001.XSHE`(平安银行)。不同代码的前缀相同不会被视为同一标的。
|
|
20
|
+
|
|
21
|
+
挂载后移除 `theme` 属性会解除固定配色,恢复宿主主题路径,无需重新挂载图表。
|
|
22
|
+
|
|
23
|
+
## 视图侧的桥
|
|
24
|
+
|
|
25
|
+
内嵌挂载时,视图在 `window.__DSH_KLINE_EMBED__` 上装一组宿主可调用的方法:
|
|
26
|
+
|
|
27
|
+
- `ready`:首帧就绪的 promise;
|
|
28
|
+
- `openSymbol(symbol, name?)`:打开一个标的;
|
|
29
|
+
- `currentSymbol()`:当前图上的标的;
|
|
30
|
+
- `setTheme('host' | 'dark' | 'light')`:`host` 表示重新跟随宿主文档。
|
|
31
|
+
|
|
32
|
+
桥只在 `embedded` 时安装,侧栏视图的原有对外表面不变。要求宿主等 `ready` 之后再调用,避免与首帧竞争。
|
|
33
|
+
|
|
34
|
+
## 宿主需要保证的三件事
|
|
35
|
+
|
|
36
|
+
1. **有界**:面板自身要有确定高度。内嵌模式按 `document.documentElement.clientHeight` 取高度,宿主不提供高度时图表会退化成内容高度。
|
|
37
|
+
2. **调色板**:面板背景与文字颜色由宿主决定,需要一致时显式传 `theme`,不要依赖宿主 DOM 的 CSS 变量被继承进来。
|
|
38
|
+
3. **不弹全屏对话框**:内嵌模式下对话框是面板内的绝对定位元素;宿主不要给面板 `overflow: visible`,否则对话框会盖到面板外。
|
|
39
|
+
|
|
40
|
+
## 边界
|
|
41
|
+
|
|
42
|
+
- 本节描述的是**同一插件内的挂载契约**,不是跨包的 ESM 入口:客户端的交付形态是 DSH 的浏览器模块(`window.__ModuleLoader__`),第三方面板插件通过宿主槽位获得图表,而不是 `import` 本包。
|
|
43
|
+
- 图表的数据源、指标、绘图与对话共享链路与侧栏挂载共用同一套代码;内嵌不引入第二条取数路径。
|