@dsh-ft/dsh-fft-paper-trading 0.7.0 → 0.7.2
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/lib/client.js +9 -1
- package/package.json +2 -5
- package/src/client/index.js +8 -0
- package/node_modules/@ftshare-lab/dsh-kline/CHANGELOG.md +0 -564
- package/node_modules/@ftshare-lab/dsh-kline/LICENSE +0 -21
- package/node_modules/@ftshare-lab/dsh-kline/PROVENANCE.md +0 -13
- package/node_modules/@ftshare-lab/dsh-kline/README.en.md +0 -265
- package/node_modules/@ftshare-lab/dsh-kline/README.md +0 -281
- package/node_modules/@ftshare-lab/dsh-kline/chart_service.py +0 -686
- package/node_modules/@ftshare-lab/dsh-kline/config/basic-symbols.json +0 -570
- package/node_modules/@ftshare-lab/dsh-kline/config/dsh-kline.patch.yml +0 -4
- package/node_modules/@ftshare-lab/dsh-kline/config/dsh-tool-catalog.json +0 -3783
- package/node_modules/@ftshare-lab/dsh-kline/cordis.patch.yml +0 -33
- package/node_modules/@ftshare-lab/dsh-kline/core/__init__.py +0 -0
- package/node_modules/@ftshare-lab/dsh-kline/core/adjustment.py +0 -16
- package/node_modules/@ftshare-lab/dsh-kline/core/calc.py +0 -683
- package/node_modules/@ftshare-lab/dsh-kline/core/rows.py +0 -228
- package/node_modules/@ftshare-lab/dsh-kline/core/ta_engine.py +0 -99
- package/node_modules/@ftshare-lab/dsh-kline/lib/candle-sequence.js +0 -15
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-adjustment.js +0 -7
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-annotations.js +0 -33
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-applications.js +0 -171
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-context.js +0 -164
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-observation.js +0 -241
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-service-status.js +0 -82
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-source-status.js +0 -39
- package/node_modules/@ftshare-lab/dsh-kline/lib/chart-sources.js +0 -349
- package/node_modules/@ftshare-lab/dsh-kline/lib/client.js +0 -15445
- package/node_modules/@ftshare-lab/dsh-kline/lib/drawing-feedback.js +0 -252
- package/node_modules/@ftshare-lab/dsh-kline/lib/fibonacci.js +0 -18
- package/node_modules/@ftshare-lab/dsh-kline/lib/ftshare-settings-bridge.js +0 -159
- package/node_modules/@ftshare-lab/dsh-kline/lib/index.js +0 -279
- package/node_modules/@ftshare-lab/dsh-kline/lib/labeled-polyline.js +0 -17
- package/node_modules/@ftshare-lab/dsh-kline/lib/price-bands.js +0 -55
- package/node_modules/@ftshare-lab/dsh-kline/lib/price-series.js +0 -47
- package/node_modules/@ftshare-lab/dsh-kline/lib/price-sticks.js +0 -32
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/candle-sequence.d.ts +0 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-adjustment.d.ts +0 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-annotations.d.ts +0 -4
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-applications.d.ts +0 -26
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-context.d.ts +0 -14
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-observation.d.ts +0 -8
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-service-status.d.ts +0 -22
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-source-status.d.ts +0 -47
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/chart-sources.d.ts +0 -25
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/BetterSidebarTab.d.ts +0 -12
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/ClassicShell.d.ts +0 -4
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/KlineContent.d.ts +0 -37
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/PaperKlinePanel.d.ts +0 -15
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/canvas-panel.d.ts +0 -33
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/chart-apply.d.ts +0 -13
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/chart-context.d.ts +0 -23
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/chart-session-resolver.d.ts +0 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/client-context.d.ts +0 -14
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/conversation.d.ts +0 -39
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/default-tab.d.ts +0 -3
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/embed.d.ts +0 -37
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/generated-view.d.ts +0 -2
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/horizontal-lines.d.ts +0 -1
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/host-services.d.ts +0 -55
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/index.d.ts +0 -4
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/mode-preferences.d.ts +0 -13
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar-contract.d.ts +0 -52
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar-register.d.ts +0 -46
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar-services.d.ts +0 -45
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/native-sidebar.d.ts +0 -47
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/sidebar-integration.d.ts +0 -12
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/ui-onboarding.d.ts +0 -13
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/client/version.d.ts +0 -1
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/drawing-feedback.d.ts +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/fibonacci.d.ts +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/ftshare-settings-bridge.d.ts +0 -50
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/index.d.ts +0 -26
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/labeled-polyline.d.ts +0 -11
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/price-bands.d.ts +0 -3
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/price-series.d.ts +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/lib/types/price-sticks.d.ts +0 -3
- package/node_modules/@ftshare-lab/dsh-kline/package.json +0 -136
- package/node_modules/@ftshare-lab/dsh-kline/requirements.txt +0 -6
- package/node_modules/@ftshare-lab/dsh-kline/scripts/bootstrap.sh +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/scripts/dsh-bootstrap.mjs +0 -179
- package/node_modules/@ftshare-lab/dsh-kline/scripts/dsh-mcp-plugin.mjs +0 -27
- package/node_modules/@ftshare-lab/dsh-kline/scripts/run-dsh-kline.mjs +0 -655
- package/node_modules/@ftshare-lab/dsh-kline/scripts/run-dsh-kline.sh +0 -5
- package/node_modules/@ftshare-lab/dsh-kline/server.py +0 -1734
- package/node_modules/@ftshare-lab/dsh-kline/tools/__init__.py +0 -0
- package/node_modules/@ftshare-lab/dsh-kline/tools/annotations.py +0 -119
- package/node_modules/@ftshare-lab/dsh-kline/tools/baostock_source.py +0 -176
- package/node_modules/@ftshare-lab/dsh-kline/tools/calc.py +0 -39
- package/node_modules/@ftshare-lab/dsh-kline/tools/channel_candidates.py +0 -260
- package/node_modules/@ftshare-lab/dsh-kline/tools/channel_preview.py +0 -82
- package/node_modules/@ftshare-lab/dsh-kline/tools/channels.py +0 -104
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_applications.py +0 -204
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_context.py +0 -186
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_edits.py +0 -345
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_functions.py +0 -466
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_indicators.py +0 -63
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_measurements.py +0 -84
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_source_status.py +0 -32
- package/node_modules/@ftshare-lab/dsh-kline/tools/chart_sources.py +0 -31
- package/node_modules/@ftshare-lab/dsh-kline/tools/draw.py +0 -394
- package/node_modules/@ftshare-lab/dsh-kline/tools/drawing_feedback.py +0 -45
- package/node_modules/@ftshare-lab/dsh-kline/tools/drawing_interaction.py +0 -117
- package/node_modules/@ftshare-lab/dsh-kline/tools/drawing_verification.py +0 -141
- package/node_modules/@ftshare-lab/dsh-kline/tools/fetch.py +0 -4681
- package/node_modules/@ftshare-lab/dsh-kline/tools/fibonacci.py +0 -83
- package/node_modules/@ftshare-lab/dsh-kline/tools/foreign_daily.py +0 -185
- package/node_modules/@ftshare-lab/dsh-kline/tools/free_sources.py +0 -483
- package/node_modules/@ftshare-lab/dsh-kline/tools/lines.py +0 -91
- package/node_modules/@ftshare-lab/dsh-kline/tools/pivots.py +0 -60
- package/node_modules/@ftshare-lab/dsh-kline/tools/polylines.py +0 -125
- package/node_modules/@ftshare-lab/dsh-kline/tools/price_bands.py +0 -165
- package/node_modules/@ftshare-lab/dsh-kline/tools/price_series.py +0 -162
- package/node_modules/@ftshare-lab/dsh-kline/tools/price_sticks.py +0 -114
- package/node_modules/@ftshare-lab/dsh-kline/tools/range_candidates.py +0 -170
- package/node_modules/@ftshare-lab/dsh-kline/tools/rectangles.py +0 -76
- package/node_modules/@ftshare-lab/dsh-kline/tools/regression_channels.py +0 -87
- package/node_modules/@ftshare-lab/dsh-kline/tools/ta_functions.py +0 -228
- package/node_modules/@ftshare-lab/dsh-kline/tools/trend_lines.py +0 -106
- package/node_modules/@ftshare-lab/dsh-kline/tools/watchlist.py +0 -149
- package/node_modules/@ftshare-lab/dsh-kline/view/ft-logo.jpg +0 -0
- package/node_modules/@ftshare-lab/dsh-kline/view/vendor/klinecharts.min.js +0 -7
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
"""Explicit labeled time/price paths, not automatic wave recognition."""
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
from copy import deepcopy
|
|
5
|
-
import hashlib
|
|
6
|
-
import secrets
|
|
7
|
-
|
|
8
|
-
from pydantic import BaseModel, ConfigDict, Field
|
|
9
|
-
|
|
10
|
-
from tools.lines import LineError, validate_line_fields
|
|
11
|
-
from tools.trend_lines import resolve_anchor_time
|
|
12
|
-
|
|
13
|
-
POLYLINE_COMMAND = "DRAW_AGENT_POLYLINE"
|
|
14
|
-
MAX_POINTS = 8
|
|
15
|
-
MAX_POLYLINES = 30
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
class LabeledPoint(BaseModel):
|
|
19
|
-
model_config = ConfigDict(extra="forbid")
|
|
20
|
-
label: str = Field(min_length=1, max_length=16, description="点名,组内唯一,例如 A、B、C 或 1、2;不是自动识浪结果")
|
|
21
|
-
time: str = Field(min_length=1, description="已加载 K 线的日期、带时区 ISO 或 Unix 秒字符串")
|
|
22
|
-
price: float = Field(gt=0, allow_inf_nan=False, strict=True)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def normalize_point_label(label):
|
|
26
|
-
if (not isinstance(label, str) or not 1 <= len(label.strip()) <= 16
|
|
27
|
-
or any(ord(c) < 32 for c in label)):
|
|
28
|
-
raise LineError("invalid_point_label", "点名需为1至16个字符,不能空白或包含控制字符。")
|
|
29
|
-
return label.strip()
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def validate_stored_points(points):
|
|
33
|
-
"""Whitelist canonical object fields; never sort, truncate or repair a path."""
|
|
34
|
-
if not isinstance(points, list) or not 2 <= len(points) <= MAX_POINTS:
|
|
35
|
-
raise LineError("invalid_polyline_points", "折线需要2至8个明确的带标签锚点。")
|
|
36
|
-
cleaned = []
|
|
37
|
-
for point in points:
|
|
38
|
-
if not isinstance(point, dict) or type(point.get("time")) is not int or point["time"] <= 0:
|
|
39
|
-
raise LineError("invalid_polyline_points", "折线锚点时间格式无效。")
|
|
40
|
-
label = normalize_point_label(point.get("label"))
|
|
41
|
-
price, _, _ = validate_line_fields(price=point.get("price"))
|
|
42
|
-
if price is None: raise LineError("invalid_price", "每个折线锚点都需要明确价格。")
|
|
43
|
-
cleaned.append({"label": label, "time": point["time"], "price": price})
|
|
44
|
-
if len({p["label"] for p in cleaned}) != len(cleaned):
|
|
45
|
-
raise LineError("duplicate_point_label", "同组点名必须唯一,不能按重复标签猜点。")
|
|
46
|
-
if any(a["time"] >= b["time"] for a, b in zip(cleaned, cleaned[1:])):
|
|
47
|
-
raise LineError("invalid_anchor_order", "折线锚点时间必须递增;不自动排序或交换点位。")
|
|
48
|
-
return cleaned
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def resolve_points(payload, points):
|
|
52
|
-
if not isinstance(points, list) or not 2 <= len(points) <= MAX_POINTS:
|
|
53
|
-
raise LineError("invalid_polyline_points", "折线需要2至8个明确的带标签锚点。")
|
|
54
|
-
resolved = []
|
|
55
|
-
for raw in points:
|
|
56
|
-
point = raw.model_dump() if isinstance(raw, LabeledPoint) else raw
|
|
57
|
-
if not isinstance(point, dict) or set(point) != {"label", "time", "price"}:
|
|
58
|
-
raise LineError("invalid_polyline_points", "每个锚点只包含 label、time、price。")
|
|
59
|
-
resolved.append({**point, "time": resolve_anchor_time(payload, point["time"])})
|
|
60
|
-
return validate_stored_points(resolved)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def edit_polyline(payload, action, *, polyline_id=None, points=None, point_label=None,
|
|
64
|
-
point_time=None, point_price=None, name=None, color=None, request_id=None):
|
|
65
|
-
result = deepcopy(payload)
|
|
66
|
-
commands = result.get("chartCommands", [])
|
|
67
|
-
matches = [c for c in commands if c.get("type") == POLYLINE_COMMAND and c.get("id") == polyline_id]
|
|
68
|
-
if len(matches) > 1:
|
|
69
|
-
raise LineError("ambiguous_drawing_id", "同一对象 ID 不唯一,不能安全修改。")
|
|
70
|
-
previous = matches[0] if matches else None
|
|
71
|
-
if action in {"update", "delete"} and previous is None:
|
|
72
|
-
raise LineError("polyline_not_found", "当前快照不存在该 AI 标注折线,请先读回完整对象 ID。")
|
|
73
|
-
if action == "delete":
|
|
74
|
-
if any(v is not None for v in (points, point_label, point_time, point_price, name, color)):
|
|
75
|
-
raise LineError("invalid_edit_fields", "删除整组只需对象 ID。")
|
|
76
|
-
commands.remove(previous)
|
|
77
|
-
return result, deepcopy(previous)
|
|
78
|
-
if action not in {"create", "update"}:
|
|
79
|
-
raise LineError("invalid_action", "不支持的标注折线操作。")
|
|
80
|
-
_, name, color = validate_line_fields(name=name, color=color, request_id=request_id)
|
|
81
|
-
if action == "create":
|
|
82
|
-
if any(v is not None for v in (point_label, point_time, point_price)):
|
|
83
|
-
raise LineError("invalid_edit_fields", "创建需完整 points,单点修改字段只用于更新。")
|
|
84
|
-
path = resolve_points(payload, points)
|
|
85
|
-
else:
|
|
86
|
-
# Updating one point must not require the LLM to copy/reconstruct its
|
|
87
|
-
# neighbours. Full replacement/point insertion/removal are out of scope.
|
|
88
|
-
if points is not None:
|
|
89
|
-
raise LineError("polyline_replacement_not_supported", "续改请用 point_label 及 point_time / point_price,仅改目标点;不接受整组重写。")
|
|
90
|
-
path = validate_stored_points(previous["points"])
|
|
91
|
-
if point_label is not None:
|
|
92
|
-
label = normalize_point_label(point_label)
|
|
93
|
-
if point_time is None and point_price is None:
|
|
94
|
-
raise LineError("empty_point_update", "请明确目标点的新时间或价格,不能猜移动幅度。")
|
|
95
|
-
target = next((p for p in path if p["label"] == label), None)
|
|
96
|
-
if target is None:
|
|
97
|
-
raise LineError("point_not_found", "当前组内不存在该点名;请核对点名,不按位置或近似名称猜点。")
|
|
98
|
-
if point_time is not None: target["time"] = resolve_anchor_time(payload, point_time)
|
|
99
|
-
if point_price is not None: target["price"] = validate_line_fields(price=point_price)[0]
|
|
100
|
-
elif point_time is not None or point_price is not None:
|
|
101
|
-
raise LineError("point_label_required", "修改单点必须明确组内 point_label。")
|
|
102
|
-
elif name is None and color is None:
|
|
103
|
-
raise LineError("empty_update", "请指定点名及新时间 / 价格,或整组名称 / 颜色。")
|
|
104
|
-
path = validate_stored_points(path)
|
|
105
|
-
for point in path: resolve_anchor_time(payload, str(point["time"]))
|
|
106
|
-
obj = {"type": POLYLINE_COMMAND, "id": previous["id"] if previous else "poly_" + secrets.token_hex(12),
|
|
107
|
-
"interval": payload.get("interval"), "points": path,
|
|
108
|
-
"label": name if name is not None else previous["label"] if previous else "",
|
|
109
|
-
"color": color if color is not None else previous["color"] if previous else "info"}
|
|
110
|
-
if previous and previous.get("create_request_id"): obj["create_request_id"] = previous["create_request_id"]
|
|
111
|
-
if action == "create":
|
|
112
|
-
if request_id:
|
|
113
|
-
obj["create_request_id"] = request_id
|
|
114
|
-
obj["id"] = "poly_" + hashlib.sha256(request_id.encode()).hexdigest()[:24]
|
|
115
|
-
retry = next((c for c in commands if c.get("type") == POLYLINE_COMMAND and c.get("create_request_id") == request_id), None)
|
|
116
|
-
if retry:
|
|
117
|
-
if any(retry.get(k) != obj[k] for k in ("points", "label", "color", "interval")):
|
|
118
|
-
raise LineError("request_conflict", "同一 request_id 的标注折线参数不同。")
|
|
119
|
-
return result, deepcopy(retry)
|
|
120
|
-
if sum(c.get("type") == POLYLINE_COMMAND for c in commands) >= MAX_POLYLINES:
|
|
121
|
-
raise LineError("too_many_polylines", "当前图表最多保存30组 AI 标注折线。")
|
|
122
|
-
commands.append(obj)
|
|
123
|
-
else:
|
|
124
|
-
commands[commands.index(previous)] = obj
|
|
125
|
-
return result, deepcopy(obj)
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
"""Bounded, candle-aligned fill between two deterministic price sources."""
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
from copy import deepcopy
|
|
5
|
-
import hashlib
|
|
6
|
-
import json
|
|
7
|
-
import math
|
|
8
|
-
import secrets
|
|
9
|
-
from typing import Literal
|
|
10
|
-
|
|
11
|
-
from pydantic import BaseModel, ConfigDict, Field
|
|
12
|
-
|
|
13
|
-
from tools.lines import LineError, validate_line_fields
|
|
14
|
-
from tools.price_series import PriceCondition, PriceSource, calculate_price_series, candle_sequence_hash, MAX_SERIES_BARS
|
|
15
|
-
|
|
16
|
-
BAND_COMMAND = "DRAW_AGENT_PRICE_BAND"
|
|
17
|
-
MAX_PRICE_BANDS = 4
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
class BollBandSource(BaseModel):
|
|
21
|
-
"""Metadata for one selected TA-Lib BBANDS group, not host-supplied prices."""
|
|
22
|
-
model_config = ConfigDict(extra="forbid")
|
|
23
|
-
kind: Literal["bbands"]
|
|
24
|
-
output: Literal["upperband", "lowerband"]
|
|
25
|
-
period: int = Field(strict=True, ge=2, le=200)
|
|
26
|
-
deviation_up: float = Field(gt=0, le=5, allow_inf_nan=False)
|
|
27
|
-
deviation_down: float = Field(gt=0, le=5, allow_inf_nan=False)
|
|
28
|
-
matype: Literal[0] = 0
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def _band_source(value, output):
|
|
32
|
-
if isinstance(value, dict) and value.get("kind") == "bbands":
|
|
33
|
-
source = BollBandSource.model_validate(value).model_dump()
|
|
34
|
-
if source["output"] != output:
|
|
35
|
-
raise LineError("invalid_boll_source", "BOLL 上下轨来源不匹配。")
|
|
36
|
-
return source
|
|
37
|
-
return PriceSource.model_validate(value).model_dump(exclude_none=True)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def _middle_values(values, lower, upper):
|
|
41
|
-
if not isinstance(values, list) or len(values) != len(lower):
|
|
42
|
-
raise LineError("invalid_boll_middle", "BOLL 中轨长度与当前K线不一致。")
|
|
43
|
-
for middle, low, high in zip(values, lower, upper):
|
|
44
|
-
if low is None:
|
|
45
|
-
if middle is None: continue
|
|
46
|
-
raise LineError("invalid_boll_middle", "BOLL 中轨空值须与上下轨同步。")
|
|
47
|
-
if (type(middle) not in (int, float) or not math.isfinite(middle)
|
|
48
|
-
or not low < middle < high):
|
|
49
|
-
raise LineError("invalid_boll_middle", "BOLL 中轨必须在有效上下轨之间,空值须同步。")
|
|
50
|
-
return list(values)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def calculate_price_band(rows, upper_source: PriceSource, lower_source: PriceSource,
|
|
54
|
-
condition: PriceCondition | None = None):
|
|
55
|
-
upper = calculate_price_series(rows, upper_source, condition)["values"]
|
|
56
|
-
lower = calculate_price_series(rows, lower_source, condition)["values"]
|
|
57
|
-
upper_values, lower_values = [], []
|
|
58
|
-
for high, low in zip(upper, lower):
|
|
59
|
-
if high is None or low is None:
|
|
60
|
-
upper_values.append(None); lower_values.append(None)
|
|
61
|
-
elif high <= low:
|
|
62
|
-
raise LineError("invalid_price_band_order", "条件命中的上下边界必须严格保持上方高于下方;未交换或忽略交叉K线。")
|
|
63
|
-
else:
|
|
64
|
-
upper_values.append(high); lower_values.append(low)
|
|
65
|
-
matches = sum(value is not None for value in upper_values)
|
|
66
|
-
segments = sum(value is not None and (index == 0 or upper_values[index - 1] is None)
|
|
67
|
-
for index, value in enumerate(upper_values))
|
|
68
|
-
return {"upper_values": upper_values, "lower_values": lower_values,
|
|
69
|
-
"summary": {"loaded_bars": len(rows), "matched_bars": matches,
|
|
70
|
-
"segment_count": segments, "empty": matches == 0,
|
|
71
|
-
"warmup_or_condition_gaps": len(rows) - matches, "drawing_created": False}}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def validate_band_values(upper_values, lower_values, count):
|
|
75
|
-
if (type(count) is not int or not 1 <= count <= MAX_SERIES_BARS
|
|
76
|
-
or not isinstance(upper_values, list) or not isinstance(lower_values, list)
|
|
77
|
-
or len(upper_values) != count or len(lower_values) != count):
|
|
78
|
-
raise LineError("invalid_price_band_values", "带状序列长度与当前K线不一致。")
|
|
79
|
-
for high, low in zip(upper_values, lower_values):
|
|
80
|
-
if high is None and low is None: continue
|
|
81
|
-
if (type(high) not in (int, float) or type(low) not in (int, float)
|
|
82
|
-
or not math.isfinite(high) or not math.isfinite(low) or low <= 0 or high <= low):
|
|
83
|
-
raise LineError("invalid_price_band_values", "带状边界无效或空值未成对出现。")
|
|
84
|
-
if not any(value is not None for value in upper_values):
|
|
85
|
-
raise LineError("empty_price_band", "条件没有命中可填充K线,未创建空带。")
|
|
86
|
-
return list(upper_values), list(lower_values)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
def edit_price_band(payload, action, *, band_id=None, upper_source=None, lower_source=None,
|
|
90
|
-
condition=None, upper_values=None, lower_values=None, middle_values=None, name=None, color=None,
|
|
91
|
-
request_id=None):
|
|
92
|
-
result = deepcopy(payload)
|
|
93
|
-
commands = result.get("chartCommands", [])
|
|
94
|
-
rows = next((c.get("rows", []) for c in commands if c.get("type") == "SET_CANDLES"), [])
|
|
95
|
-
matches = [c for c in commands if c.get("type") == BAND_COMMAND and c.get("id") == band_id]
|
|
96
|
-
if len(matches) > 1: raise LineError("ambiguous_drawing_id", "同一带状对象 ID 不唯一。")
|
|
97
|
-
previous = matches[0] if matches else None
|
|
98
|
-
if action in {"update", "delete"} and previous is None:
|
|
99
|
-
raise LineError("price_band_not_found", "当前图不存在该 AI 带状对象。")
|
|
100
|
-
if action == "delete":
|
|
101
|
-
if any(value is not None for value in (upper_source, lower_source, condition, upper_values,
|
|
102
|
-
lower_values, middle_values, name, color)):
|
|
103
|
-
raise LineError("invalid_edit_fields", "删除带状对象只需对象 ID。")
|
|
104
|
-
commands.remove(previous)
|
|
105
|
-
return result, deepcopy(previous)
|
|
106
|
-
if action not in {"create", "update"}: raise LineError("invalid_action", "不支持的带状对象操作。")
|
|
107
|
-
_, name, color = validate_line_fields(name=name, color=color, request_id=request_id)
|
|
108
|
-
supplied = [upper_source, lower_source, upper_values, lower_values]
|
|
109
|
-
if action == "create" and any(value is None for value in supplied):
|
|
110
|
-
raise LineError("band_result_required", "创建带状对象必须使用完整函数结果引用。")
|
|
111
|
-
if any(value is None for value in supplied) and any(value is not None for value in supplied):
|
|
112
|
-
raise LineError("band_result_required", "修改边界或条件时须重新运行函数并使用完整结果引用。")
|
|
113
|
-
if all(value is None for value in supplied) and middle_values is not None:
|
|
114
|
-
raise LineError("band_result_required", "修改 BOLL 中轨必须重新运行函数并使用完整结果引用。")
|
|
115
|
-
if action == "update" and all(value is None for value in supplied) and name is None and color is None:
|
|
116
|
-
raise LineError("empty_update", "请指定新函数结果、名称或颜色。")
|
|
117
|
-
if all(value is not None for value in supplied):
|
|
118
|
-
upper_values, lower_values = validate_band_values(upper_values, lower_values, len(rows))
|
|
119
|
-
upper_source = _band_source(upper_source, "upperband")
|
|
120
|
-
lower_source = _band_source(lower_source, "lowerband")
|
|
121
|
-
is_boll = upper_source["kind"] == "bbands"
|
|
122
|
-
if is_boll != (lower_source["kind"] == "bbands"):
|
|
123
|
-
raise LineError("invalid_boll_source", "BOLL 上下轨必须来自同一组计算。")
|
|
124
|
-
if is_boll:
|
|
125
|
-
if (condition is not None or {k: v for k, v in upper_source.items() if k != "output"}
|
|
126
|
-
!= {k: v for k, v in lower_source.items() if k != "output"}):
|
|
127
|
-
raise LineError("invalid_boll_source", "BOLL 上中下轨必须使用同一函数参数且不可附加条件。")
|
|
128
|
-
middle_values = _middle_values(middle_values, lower_values, upper_values)
|
|
129
|
-
elif middle_values is not None:
|
|
130
|
-
raise LineError("invalid_boll_middle", "普通价带不能附带 BOLL 中轨。")
|
|
131
|
-
condition = PriceCondition.model_validate(condition).model_dump(exclude_none=True) if condition is not None else None
|
|
132
|
-
else:
|
|
133
|
-
upper_values, lower_values = validate_band_values(previous["upper_values"], previous["lower_values"], len(rows))
|
|
134
|
-
upper_source, lower_source, condition = previous["upper_source"], previous["lower_source"], previous.get("condition")
|
|
135
|
-
middle_values = previous.get("middle_values")
|
|
136
|
-
obj = {"type": BAND_COMMAND, "id": previous["id"] if previous else "band_" + secrets.token_hex(12),
|
|
137
|
-
"interval": payload.get("interval"), "bar_count": len(rows),
|
|
138
|
-
"row_hash": candle_sequence_hash(rows),
|
|
139
|
-
"upper_source": upper_source, "lower_source": lower_source, "condition": condition,
|
|
140
|
-
"upper_values": upper_values, "lower_values": lower_values,
|
|
141
|
-
"label": name if name is not None else previous.get("label", "") if previous else "",
|
|
142
|
-
"color": color if color is not None else previous.get("color", "info") if previous else "info"}
|
|
143
|
-
if middle_values is not None:
|
|
144
|
-
obj["middle_values"] = middle_values
|
|
145
|
-
obj["band_hash"] = hashlib.sha256(json.dumps({"upper_source": upper_source, "lower_source": lower_source,
|
|
146
|
-
"condition": condition, "upper_values": upper_values, "lower_values": lower_values,
|
|
147
|
-
**({"middle_values": middle_values} if middle_values is not None else {})},
|
|
148
|
-
ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()).hexdigest()
|
|
149
|
-
if previous and previous.get("create_request_id"): obj["create_request_id"] = previous["create_request_id"]
|
|
150
|
-
if action == "create":
|
|
151
|
-
if request_id:
|
|
152
|
-
obj["create_request_id"] = request_id
|
|
153
|
-
obj["id"] = "band_" + hashlib.sha256(request_id.encode()).hexdigest()[:24]
|
|
154
|
-
retry = next((c for c in commands if c.get("type") == BAND_COMMAND and c.get("create_request_id") == request_id), None)
|
|
155
|
-
if retry:
|
|
156
|
-
if any(retry.get(key) != obj.get(key) for key in ("interval", "bar_count", "upper_source", "lower_source",
|
|
157
|
-
"condition", "upper_values", "lower_values", "middle_values", "label", "color")):
|
|
158
|
-
raise LineError("request_conflict", "同一 request_id 的带状参数不同。")
|
|
159
|
-
return result, deepcopy(retry)
|
|
160
|
-
if sum(c.get("type") == BAND_COMMAND for c in commands) >= MAX_PRICE_BANDS:
|
|
161
|
-
raise LineError("too_many_price_bands", "当前图最多保存4个 AI 带状对象。")
|
|
162
|
-
commands.append(obj)
|
|
163
|
-
else:
|
|
164
|
-
commands[commands.index(previous)] = obj
|
|
165
|
-
return result, deepcopy(obj)
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
"""Deterministic price-series lines on the currently loaded candles only."""
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
from copy import deepcopy
|
|
5
|
-
import hashlib
|
|
6
|
-
import json
|
|
7
|
-
import math
|
|
8
|
-
import secrets
|
|
9
|
-
import struct
|
|
10
|
-
from typing import Literal
|
|
11
|
-
|
|
12
|
-
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
|
13
|
-
|
|
14
|
-
from core.calc import series_ma
|
|
15
|
-
from core.ta_engine import calculate
|
|
16
|
-
from tools.lines import LineError, validate_line_fields
|
|
17
|
-
|
|
18
|
-
SERIES_COMMAND = "DRAW_AGENT_SERIES_LINE"
|
|
19
|
-
MAX_SERIES_LINES = 8
|
|
20
|
-
MAX_SERIES_BARS = 4000
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def candle_sequence_hash(rows):
|
|
24
|
-
"""Stable browser-compatible identity for candle-index-aligned drawings."""
|
|
25
|
-
value = 0xcbf29ce484222325
|
|
26
|
-
for row in rows:
|
|
27
|
-
for key in ("time", "open", "high", "low", "close", "volume"):
|
|
28
|
-
for byte in struct.pack(">d", float(row[key])):
|
|
29
|
-
value = ((value ^ byte) * 0x100000001b3) & 0xffffffffffffffff
|
|
30
|
-
return f"{value:016x}"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class PriceSource(BaseModel):
|
|
34
|
-
model_config = ConfigDict(extra="forbid")
|
|
35
|
-
kind: Literal["open", "high", "low", "close", "sma", "ema"]
|
|
36
|
-
period: int | None = Field(default=None, strict=True, ge=2, le=200)
|
|
37
|
-
|
|
38
|
-
@model_validator(mode="after")
|
|
39
|
-
def valid_period(self):
|
|
40
|
-
if (self.kind in {"sma", "ema"}) != (self.period is not None):
|
|
41
|
-
raise ValueError("period is required only for sma or ema")
|
|
42
|
-
return self
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
class PriceOperand(BaseModel):
|
|
46
|
-
model_config = ConfigDict(extra="forbid")
|
|
47
|
-
source: PriceSource | None = None
|
|
48
|
-
constant: float | None = Field(default=None, strict=True, gt=0, allow_inf_nan=False)
|
|
49
|
-
|
|
50
|
-
@model_validator(mode="after")
|
|
51
|
-
def one_value(self):
|
|
52
|
-
if (self.source is None) == (self.constant is None):
|
|
53
|
-
raise ValueError("exactly one of source or constant is required")
|
|
54
|
-
return self
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
class PriceCondition(BaseModel):
|
|
58
|
-
model_config = ConfigDict(extra="forbid")
|
|
59
|
-
left: PriceSource
|
|
60
|
-
op: Literal["gt", "gte", "lt", "lte"]
|
|
61
|
-
right: PriceOperand
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def _source_values(rows, source: PriceSource):
|
|
65
|
-
if source.kind not in {"sma", "ema"}:
|
|
66
|
-
return [row[source.kind] for row in rows]
|
|
67
|
-
if source.kind == "sma":
|
|
68
|
-
ma = series_ma(rows, [source.period])[f"ma{source.period}"]
|
|
69
|
-
return [ma.get(row["time"]) for row in rows]
|
|
70
|
-
return calculate("EMA", rows, parameters={"timeperiod": source.period})[1]["real"]
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def calculate_price_series(rows, source: PriceSource, condition: PriceCondition | None = None):
|
|
74
|
-
if not isinstance(rows, list) or not 1 <= len(rows) <= MAX_SERIES_BARS:
|
|
75
|
-
raise LineError("invalid_price_series_history", "仅支持当前图1至4000根已加载K线,不补取行情。")
|
|
76
|
-
values = _source_values(rows, source)
|
|
77
|
-
left = _source_values(rows, condition.left) if condition else None
|
|
78
|
-
right = (_source_values(rows, condition.right.source) if condition and condition.right.source else
|
|
79
|
-
[condition.right.constant] * len(rows) if condition else None)
|
|
80
|
-
operations = {"gt": lambda a, b: a > b, "gte": lambda a, b: a >= b,
|
|
81
|
-
"lt": lambda a, b: a < b, "lte": lambda a, b: a <= b}
|
|
82
|
-
output = []
|
|
83
|
-
for index, value in enumerate(values):
|
|
84
|
-
allowed = (condition is None or left[index] is not None and right[index] is not None
|
|
85
|
-
and operations[condition.op](left[index], right[index]))
|
|
86
|
-
output.append(float(value) if value is not None and allowed else None)
|
|
87
|
-
matches = sum(value is not None for value in output)
|
|
88
|
-
segments = sum(value is not None and (index == 0 or output[index - 1] is None)
|
|
89
|
-
for index, value in enumerate(output))
|
|
90
|
-
return {"values": output, "summary": {"loaded_bars": len(rows), "matched_bars": matches,
|
|
91
|
-
"segment_count": segments, "empty": matches == 0,
|
|
92
|
-
"warmup_or_condition_gaps": len(rows) - matches, "drawing_created": False}}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
def validate_series_values(values, count):
|
|
96
|
-
if (not isinstance(values, list) or len(values) != count or not 1 <= count <= MAX_SERIES_BARS
|
|
97
|
-
or any(value is not None and (type(value) not in (int, float) or not math.isfinite(value) or value <= 0)
|
|
98
|
-
for value in values)):
|
|
99
|
-
raise LineError("invalid_price_series_values", "序列长度、价格或当前K线对齐关系无效。")
|
|
100
|
-
if not any(value is not None for value in values):
|
|
101
|
-
raise LineError("empty_price_series", "条件没有命中可绘制K线,未创建空图形。")
|
|
102
|
-
return list(values)
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
def edit_price_series(payload, action, *, series_id=None, source=None, condition=None, values=None,
|
|
106
|
-
name=None, color=None, request_id=None):
|
|
107
|
-
result = deepcopy(payload)
|
|
108
|
-
commands = result.get("chartCommands", [])
|
|
109
|
-
rows = next((c["rows"] for c in commands if c.get("type") == "SET_CANDLES"), [])
|
|
110
|
-
matches = [c for c in commands if c.get("type") == SERIES_COMMAND and c.get("id") == series_id]
|
|
111
|
-
if len(matches) > 1:
|
|
112
|
-
raise LineError("ambiguous_drawing_id", "同一序列对象 ID 不唯一,不能安全修改。")
|
|
113
|
-
previous = matches[0] if matches else None
|
|
114
|
-
if action in {"update", "delete"} and previous is None:
|
|
115
|
-
raise LineError("series_line_not_found", "当前快照不存在该 AI 序列线,请先读回对象 ID。")
|
|
116
|
-
if action == "delete":
|
|
117
|
-
if any(item is not None for item in (source, condition, values, name, color)):
|
|
118
|
-
raise LineError("invalid_edit_fields", "删除序列线只需对象 ID。")
|
|
119
|
-
commands.remove(previous)
|
|
120
|
-
return result, deepcopy(previous)
|
|
121
|
-
if action not in {"create", "update"}:
|
|
122
|
-
raise LineError("invalid_action", "不支持的序列线操作。")
|
|
123
|
-
_, name, color = validate_line_fields(name=name, color=color, request_id=request_id)
|
|
124
|
-
if action == "create" and (source is None or values is None):
|
|
125
|
-
raise LineError("series_result_required", "创建序列线必须使用函数结果引用。")
|
|
126
|
-
if (source is None) != (values is None):
|
|
127
|
-
raise LineError("series_result_required", "修改计算条件时须重新运行函数并传完整结果引用。")
|
|
128
|
-
if action == "update" and source is None and name is None and color is None:
|
|
129
|
-
raise LineError("empty_update", "请指定新函数结果、名称或颜色。")
|
|
130
|
-
if values is not None:
|
|
131
|
-
clean = validate_series_values(values, len(rows))
|
|
132
|
-
normalized_source = PriceSource.model_validate(source).model_dump(exclude_none=True)
|
|
133
|
-
normalized_condition = (PriceCondition.model_validate(condition).model_dump(exclude_none=True)
|
|
134
|
-
if condition is not None else None)
|
|
135
|
-
else:
|
|
136
|
-
clean = validate_series_values(previous["values"], len(rows))
|
|
137
|
-
normalized_source, normalized_condition = previous["source"], previous.get("condition")
|
|
138
|
-
obj = {"type": SERIES_COMMAND, "id": previous["id"] if previous else "series_" + secrets.token_hex(12),
|
|
139
|
-
"interval": payload.get("interval"), "bar_count": len(rows),
|
|
140
|
-
"row_hash": candle_sequence_hash(rows),
|
|
141
|
-
"source": normalized_source, "condition": normalized_condition, "values": clean,
|
|
142
|
-
"label": name if name is not None else previous.get("label", "") if previous else "",
|
|
143
|
-
"color": color if color is not None else previous.get("color", "info") if previous else "info"}
|
|
144
|
-
obj["series_hash"] = hashlib.sha256(json.dumps({"source": normalized_source, "condition": normalized_condition,
|
|
145
|
-
"values": clean}, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()).hexdigest()
|
|
146
|
-
if previous and previous.get("create_request_id"):
|
|
147
|
-
obj["create_request_id"] = previous["create_request_id"]
|
|
148
|
-
if action == "create":
|
|
149
|
-
if request_id:
|
|
150
|
-
obj["create_request_id"] = request_id
|
|
151
|
-
obj["id"] = "series_" + hashlib.sha256(request_id.encode()).hexdigest()[:24]
|
|
152
|
-
retry = next((c for c in commands if c.get("type") == SERIES_COMMAND and c.get("create_request_id") == request_id), None)
|
|
153
|
-
if retry:
|
|
154
|
-
if any(retry.get(key) != obj[key] for key in ("bar_count", "source", "condition", "values", "label", "color", "interval")):
|
|
155
|
-
raise LineError("request_conflict", "同一 request_id 的序列线参数不同。")
|
|
156
|
-
return result, deepcopy(retry)
|
|
157
|
-
if sum(c.get("type") == SERIES_COMMAND for c in commands) >= MAX_SERIES_LINES:
|
|
158
|
-
raise LineError("too_many_series_lines", "当前图表最多保存8条 AI 序列线。")
|
|
159
|
-
commands.append(obj)
|
|
160
|
-
else:
|
|
161
|
-
commands[commands.index(previous)] = obj
|
|
162
|
-
return result, deepcopy(obj)
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
"""Deterministic, bounded per-candle sticks on the current price pane."""
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
from copy import deepcopy
|
|
5
|
-
import hashlib
|
|
6
|
-
import json
|
|
7
|
-
import math
|
|
8
|
-
import secrets
|
|
9
|
-
|
|
10
|
-
from tools.lines import LineError, validate_line_fields
|
|
11
|
-
from tools.price_series import (PriceCondition, PriceSource, MAX_SERIES_BARS,
|
|
12
|
-
calculate_price_series, candle_sequence_hash)
|
|
13
|
-
|
|
14
|
-
STICKS_COMMAND = "DRAW_AGENT_PRICE_STICKS"
|
|
15
|
-
MAX_PRICE_STICKS = 4
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def calculate_price_sticks(rows, first_source: PriceSource, second_source: PriceSource,
|
|
19
|
-
condition: PriceCondition | None = None):
|
|
20
|
-
first = calculate_price_series(rows, first_source, condition)["values"]
|
|
21
|
-
second = calculate_price_series(rows, second_source, condition)["values"]
|
|
22
|
-
start, end = [], []
|
|
23
|
-
equal_endpoints = 0
|
|
24
|
-
for one, two in zip(first, second):
|
|
25
|
-
if one is None or two is None or one == two:
|
|
26
|
-
start.append(None)
|
|
27
|
-
end.append(None)
|
|
28
|
-
if one is not None and two is not None: equal_endpoints += 1
|
|
29
|
-
else:
|
|
30
|
-
start.append(one)
|
|
31
|
-
end.append(two)
|
|
32
|
-
matches = sum(value is not None for value in start)
|
|
33
|
-
return {"start_values": start, "end_values": end,
|
|
34
|
-
"summary": {"loaded_bars": len(rows), "matched_bars": matches,
|
|
35
|
-
"empty": matches == 0, "warmup_or_condition_gaps": len(rows) - matches,
|
|
36
|
-
"equal_endpoints_skipped": equal_endpoints, "drawing_created": False}}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def validate_stick_values(start_values, end_values, count):
|
|
40
|
-
if (type(count) is not int or not 1 <= count <= MAX_SERIES_BARS
|
|
41
|
-
or not isinstance(start_values, list) or not isinstance(end_values, list)
|
|
42
|
-
or len(start_values) != count or len(end_values) != count):
|
|
43
|
-
raise LineError("invalid_price_stick_values", "柱线序列长度与当前K线不一致。")
|
|
44
|
-
for start, end in zip(start_values, end_values):
|
|
45
|
-
if start is None and end is None: continue
|
|
46
|
-
if (type(start) not in (int, float) or type(end) not in (int, float)
|
|
47
|
-
or not math.isfinite(start) or not math.isfinite(end)
|
|
48
|
-
or start <= 0 or end <= 0 or start == end):
|
|
49
|
-
raise LineError("invalid_price_stick_values", "柱线端点无效、等价或空值未成对出现。")
|
|
50
|
-
if not any(value is not None for value in start_values):
|
|
51
|
-
raise LineError("empty_price_sticks", "条件没有命中可绘制柱线,未创建空对象。")
|
|
52
|
-
return list(start_values), list(end_values)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def edit_price_sticks(payload, action, *, sticks_id=None, start_source=None, end_source=None,
|
|
56
|
-
condition=None, start_values=None, end_values=None, name=None, color=None,
|
|
57
|
-
request_id=None):
|
|
58
|
-
result = deepcopy(payload)
|
|
59
|
-
commands = result.get("chartCommands", [])
|
|
60
|
-
rows = next((c.get("rows", []) for c in commands if c.get("type") == "SET_CANDLES"), [])
|
|
61
|
-
matches = [c for c in commands if c.get("type") == STICKS_COMMAND and c.get("id") == sticks_id]
|
|
62
|
-
if len(matches) > 1: raise LineError("ambiguous_drawing_id", "同一柱线对象 ID 不唯一。")
|
|
63
|
-
previous = matches[0] if matches else None
|
|
64
|
-
if action in {"update", "delete"} and previous is None:
|
|
65
|
-
raise LineError("price_sticks_not_found", "当前图不存在该 AI 柱线对象。")
|
|
66
|
-
if action == "delete":
|
|
67
|
-
if any(value is not None for value in (start_source, end_source, condition, start_values,
|
|
68
|
-
end_values, name, color)):
|
|
69
|
-
raise LineError("invalid_edit_fields", "删除柱线对象只需对象 ID。")
|
|
70
|
-
commands.remove(previous)
|
|
71
|
-
return result, deepcopy(previous)
|
|
72
|
-
if action not in {"create", "update"}: raise LineError("invalid_action", "不支持的柱线对象操作。")
|
|
73
|
-
_, name, color = validate_line_fields(name=name, color=color, request_id=request_id)
|
|
74
|
-
supplied = [start_source, end_source, start_values, end_values]
|
|
75
|
-
if action == "create" and any(value is None for value in supplied):
|
|
76
|
-
raise LineError("sticks_result_required", "创建柱线对象必须使用完整函数结果引用。")
|
|
77
|
-
if any(value is None for value in supplied) and any(value is not None for value in supplied):
|
|
78
|
-
raise LineError("sticks_result_required", "修改端点或条件时须重新运行函数并使用完整结果引用。")
|
|
79
|
-
if action == "update" and all(value is None for value in supplied) and name is None and color is None:
|
|
80
|
-
raise LineError("empty_update", "请指定新函数结果、名称或颜色。")
|
|
81
|
-
if all(value is not None for value in supplied):
|
|
82
|
-
start_values, end_values = validate_stick_values(start_values, end_values, len(rows))
|
|
83
|
-
start_source = PriceSource.model_validate(start_source).model_dump(exclude_none=True)
|
|
84
|
-
end_source = PriceSource.model_validate(end_source).model_dump(exclude_none=True)
|
|
85
|
-
condition = PriceCondition.model_validate(condition).model_dump(exclude_none=True) if condition is not None else None
|
|
86
|
-
else:
|
|
87
|
-
start_values, end_values = validate_stick_values(previous["start_values"], previous["end_values"], len(rows))
|
|
88
|
-
start_source, end_source, condition = previous["start_source"], previous["end_source"], previous.get("condition")
|
|
89
|
-
obj = {"type": STICKS_COMMAND, "id": previous["id"] if previous else "sticks_" + secrets.token_hex(12),
|
|
90
|
-
"interval": payload.get("interval"), "bar_count": len(rows), "row_hash": candle_sequence_hash(rows),
|
|
91
|
-
"start_source": start_source, "end_source": end_source, "condition": condition,
|
|
92
|
-
"start_values": start_values, "end_values": end_values,
|
|
93
|
-
"label": name if name is not None else previous.get("label", "") if previous else "",
|
|
94
|
-
"color": color if color is not None else previous.get("color", "info") if previous else "info"}
|
|
95
|
-
obj["sticks_hash"] = hashlib.sha256(json.dumps({"start_source": start_source, "end_source": end_source,
|
|
96
|
-
"condition": condition, "start_values": start_values, "end_values": end_values},
|
|
97
|
-
ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()).hexdigest()
|
|
98
|
-
if previous and previous.get("create_request_id"): obj["create_request_id"] = previous["create_request_id"]
|
|
99
|
-
if action == "create":
|
|
100
|
-
if request_id:
|
|
101
|
-
obj["create_request_id"] = request_id
|
|
102
|
-
obj["id"] = "sticks_" + hashlib.sha256(request_id.encode()).hexdigest()[:24]
|
|
103
|
-
retry = next((c for c in commands if c.get("type") == STICKS_COMMAND and c.get("create_request_id") == request_id), None)
|
|
104
|
-
if retry:
|
|
105
|
-
if any(retry.get(key) != obj[key] for key in ("interval", "bar_count", "start_source", "end_source",
|
|
106
|
-
"condition", "start_values", "end_values", "label", "color")):
|
|
107
|
-
raise LineError("request_conflict", "同一 request_id 的柱线参数不同。")
|
|
108
|
-
return result, deepcopy(retry)
|
|
109
|
-
if sum(c.get("type") == STICKS_COMMAND for c in commands) >= MAX_PRICE_STICKS:
|
|
110
|
-
raise LineError("too_many_price_sticks", "当前图最多保存4组 AI 柱线。")
|
|
111
|
-
commands.append(obj)
|
|
112
|
-
else:
|
|
113
|
-
commands[commands.index(previous)] = obj
|
|
114
|
-
return result, deepcopy(obj)
|