@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,284 @@
|
|
|
1
|
+
/** Object-API observations, not pixel verification or a trading conclusion. */
|
|
2
|
+
import { drawingInventory } from "./chart-observation.js";
|
|
3
|
+
import { fibonacciOverlayPoints } from "./fibonacci.js";
|
|
4
|
+
import { polylineOverlayPoints } from "./labeled-polyline.js";
|
|
5
|
+
import { chartAnnotationText } from "./chart-annotations.js";
|
|
6
|
+
export const FEEDBACK_KINDS = ['horizontal_line', 'trend_line', 'channel', 'rectangle', 'fibonacci_retracement', 'labeled_polyline', 'text_annotation', 'number_annotation', 'icon_annotation', 'series_line', 'price_band', 'price_sticks', 'price_pattern', 'nine_count'];
|
|
7
|
+
export const FEEDBACK_STATUSES = ['created', 'hidden', 'removed', 'failed', 'unknown'];
|
|
8
|
+
export const FEEDBACK_REASONS = ['overlay_registered', 'period_mismatch', 'anchors_not_loaded', 'outside_time_viewport',
|
|
9
|
+
'invalid_geometry', 'overlay_missing', 'overlay_mismatch', 'overlay_api_unavailable', 'overlay_lookup_failed', 'snapshot_outside_loaded_history',
|
|
10
|
+
'overlay_removed', 'overlay_still_present', 'context_not_ready', 'application_rolled_back', 'rollback_unknown', 'creation_failed',
|
|
11
|
+
'indicator_registered', 'indicator_removed', 'indicator_missing', 'indicator_still_present', 'indicator_api_unavailable'];
|
|
12
|
+
const PHASES = ['current', 'applied', 'rolled_back', 'rollback_unknown'];
|
|
13
|
+
const STATUS_REASONS = { created: ['overlay_registered', 'outside_time_viewport', 'indicator_registered'], hidden: ['period_mismatch', 'anchors_not_loaded', 'snapshot_outside_loaded_history'],
|
|
14
|
+
removed: ['overlay_removed', 'indicator_removed'], failed: ['invalid_geometry', 'overlay_missing', 'overlay_mismatch', 'overlay_still_present', 'creation_failed', 'indicator_missing', 'indicator_still_present'],
|
|
15
|
+
unknown: ['overlay_api_unavailable', 'overlay_lookup_failed', 'overlay_mismatch', 'context_not_ready', 'application_rolled_back', 'rollback_unknown', 'indicator_api_unavailable'] };
|
|
16
|
+
function empty(status) { return { schema_version: 1, report_status: status, evidence_scope: 'chart_object_api', render_verified: false, pixel_verified: false, items: [] }; }
|
|
17
|
+
function timeRangeOverlap(points, first, last) {
|
|
18
|
+
if (!first || !last || !points.length)
|
|
19
|
+
return null;
|
|
20
|
+
const times = points.map(point => point.timestamp / 1000);
|
|
21
|
+
return Math.max(...times) >= first && Math.min(...times) <= last;
|
|
22
|
+
}
|
|
23
|
+
export function sanitizeDrawingFeedback(raw, allowed) {
|
|
24
|
+
if (raw == null)
|
|
25
|
+
return empty('not_reported');
|
|
26
|
+
if (['not_reported', 'invalid'].includes(raw.report_status))
|
|
27
|
+
return empty(raw.report_status);
|
|
28
|
+
try {
|
|
29
|
+
if (raw.schema_version !== 1 || !PHASES.includes(raw.phase) || !Array.isArray(raw.items) || raw.items.length > 360)
|
|
30
|
+
throw Error('invalid_report');
|
|
31
|
+
const objects = new Map(allowed.map(d => [d.id, d.kind])), seen = new Set();
|
|
32
|
+
const items = raw.items.map((item) => {
|
|
33
|
+
if (typeof item.id !== 'string' || !item.id || item.id.length > 256 || objects.get(item.id) !== item.kind || seen.has(item.id)
|
|
34
|
+
|| !FEEDBACK_KINDS.includes(item.kind) || !FEEDBACK_STATUSES.includes(item.status) || !FEEDBACK_REASONS.includes(item.reason)
|
|
35
|
+
|| !STATUS_REASONS[item.status].includes(item.reason)
|
|
36
|
+
|| !['unknown', 'outside_time_viewport', 'not_applicable'].includes(item.visibility)
|
|
37
|
+
|| (item.time_range_overlap != null && typeof item.time_range_overlap !== 'boolean'))
|
|
38
|
+
throw Error('invalid_item');
|
|
39
|
+
if ((item.reason === 'outside_time_viewport') !== (item.visibility === 'outside_time_viewport'))
|
|
40
|
+
throw Error('invalid_visibility');
|
|
41
|
+
seen.add(item.id);
|
|
42
|
+
return { id: item.id, kind: item.kind, status: item.status, reason: item.reason, visibility: item.visibility,
|
|
43
|
+
outside_loaded_price_envelope: item.outside_loaded_price_envelope === true,
|
|
44
|
+
time_range_overlap: typeof item.time_range_overlap === 'boolean' ? item.time_range_overlap : null };
|
|
45
|
+
});
|
|
46
|
+
if (seen.size !== objects.size)
|
|
47
|
+
throw Error('incomplete_report');
|
|
48
|
+
const evidence_scope = ['chart_object_api', 'indicator_registry', 'mixed_chart_api_and_indicator_registry'].includes(raw.evidence_scope)
|
|
49
|
+
? raw.evidence_scope : 'chart_object_api';
|
|
50
|
+
return { ...empty('reported'), phase: raw.phase, evidence_scope, items };
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return empty('invalid');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function expectedOverlay(command, rows, interval, times) {
|
|
57
|
+
const types = { DRAW_HORIZONTAL_LINE: ['ftvHorizontalLine', 'agentHorizontalLines'], DRAW_AGENT_TREND_LINE: ['ftvTrendLine', 'agentTrendLines'],
|
|
58
|
+
DRAW_AGENT_CHANNEL: ['ftvAgentChannel', 'agentChannels'], DRAW_AGENT_RECTANGLE: ['ftvAgentRectangle', 'agentRectangles'],
|
|
59
|
+
DRAW_AGENT_FIBONACCI: ['ftvAgentFibonacci', 'agentFibonacci'], DRAW_AGENT_POLYLINE: ['ftvAgentPolyline', 'agentPolylines'],
|
|
60
|
+
DRAW_AGENT_ANNOTATION: ['ftvAgentAnnotation', 'agentAnnotations'] };
|
|
61
|
+
const point = (p) => ({ timestamp: Number(p?.time) * 1000, value: Number(p?.price) });
|
|
62
|
+
const type = command.type;
|
|
63
|
+
if (['DRAW_AGENT_CHANNEL', 'DRAW_AGENT_RECTANGLE', 'DRAW_AGENT_FIBONACCI', 'DRAW_AGENT_POLYLINE', 'DRAW_AGENT_ANNOTATION'].includes(type) && command.interval !== interval)
|
|
64
|
+
return { skip: 'period_mismatch' };
|
|
65
|
+
let points, sourceTimes = [];
|
|
66
|
+
if (type === 'DRAW_HORIZONTAL_LINE')
|
|
67
|
+
points = [{ timestamp: rows.at(-1)?.time * 1000, value: Number(command.price) }];
|
|
68
|
+
else if (type === 'DRAW_AGENT_ANNOTATION') {
|
|
69
|
+
try {
|
|
70
|
+
const clean = { ...command };
|
|
71
|
+
points = [{ timestamp: clean.time * 1000, value: clean.price }];
|
|
72
|
+
sourceTimes = [clean.time];
|
|
73
|
+
chartAnnotationText(clean);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return { failure: 'invalid_geometry' };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else if (type === 'DRAW_AGENT_POLYLINE') {
|
|
80
|
+
try {
|
|
81
|
+
points = polylineOverlayPoints(command);
|
|
82
|
+
sourceTimes = command.points.map((p) => p.time);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return { failure: 'invalid_geometry' };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (type === 'DRAW_AGENT_FIBONACCI') {
|
|
89
|
+
sourceTimes = [command.from?.time, command.to?.time];
|
|
90
|
+
try {
|
|
91
|
+
points = fibonacciOverlayPoints(command);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return { failure: 'invalid_geometry' };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (type === 'DRAW_AGENT_RECTANGLE') {
|
|
98
|
+
sourceTimes = [command.start_time, command.end_time];
|
|
99
|
+
points = [{ timestamp: command.start_time * 1000, value: command.upper_price }, { timestamp: command.end_time * 1000, value: command.lower_price }];
|
|
100
|
+
if (!(command.start_time < command.end_time && command.lower_price > 0 && command.lower_price < command.upper_price))
|
|
101
|
+
return { failure: 'invalid_geometry' };
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
sourceTimes = [command.from?.time, command.to?.time, ...(type === 'DRAW_AGENT_CHANNEL' ? [command.parallel?.time] : [])];
|
|
105
|
+
if (!(command.from?.time < command.to?.time))
|
|
106
|
+
return { failure: 'invalid_geometry' };
|
|
107
|
+
points = type === 'DRAW_AGENT_CHANNEL' ? [command.upper?.from, command.upper?.to, command.lower?.from, command.lower?.to].map(point)
|
|
108
|
+
: [command.from, command.to].map(point);
|
|
109
|
+
if (type === 'DRAW_AGENT_CHANNEL' && (command.geometry_basis !== 'candle_index_linear_price'
|
|
110
|
+
|| !command.parallel || command.parallel.time < command.from.time || command.parallel.time > command.to.time))
|
|
111
|
+
return { failure: 'invalid_geometry' };
|
|
112
|
+
}
|
|
113
|
+
if (points.some(p => !Number.isFinite(p.timestamp) || !Number.isFinite(p.value) || p.value <= 0))
|
|
114
|
+
return { failure: 'invalid_geometry' };
|
|
115
|
+
if (sourceTimes.some(time => !times.has(time)))
|
|
116
|
+
return { skip: 'anchors_not_loaded' };
|
|
117
|
+
return { name: types[type][0], group: types[type][1], points };
|
|
118
|
+
}
|
|
119
|
+
export function collectDrawingFeedback(payload, context, chart, previous = [], phase = 'current') {
|
|
120
|
+
const commands = payload?.chartCommands ?? [], inventory = drawingInventory(payload ?? {});
|
|
121
|
+
const after = new Set(inventory.map(d => d.id)), before = drawingInventory({ chartCommands: previous, interval: payload?.interval });
|
|
122
|
+
const deleted = before.filter(d => !after.has(d.id)), rows = context?.candles ?? [];
|
|
123
|
+
const times = new Set(rows.map((r) => r.time));
|
|
124
|
+
const ready = context?.state === 'ready' && rows.length > 0;
|
|
125
|
+
let canRead = false;
|
|
126
|
+
try {
|
|
127
|
+
canRead = typeof chart?.getOverlayById === 'function';
|
|
128
|
+
}
|
|
129
|
+
catch { }
|
|
130
|
+
const lookup = (id) => { try {
|
|
131
|
+
return { value: chart.getOverlayById(id) };
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return { error: true };
|
|
135
|
+
} };
|
|
136
|
+
const viewport = context?.visible_range;
|
|
137
|
+
const first = viewport?.available && rows[viewport.index_from]?.time;
|
|
138
|
+
const last = viewport?.available && viewport.index_to_exclusive > viewport.index_from && rows[viewport.index_to_exclusive - 1]?.time;
|
|
139
|
+
const low = rows.length ? Math.min(...rows.map((r) => r.low)) : NaN, high = rows.length ? Math.max(...rows.map((r) => r.high)) : NaN;
|
|
140
|
+
const items = inventory.map((drawing) => {
|
|
141
|
+
const cmd = commands.find((c) => c.id === drawing.id), item = { id: drawing.id, kind: drawing.kind, status: 'unknown',
|
|
142
|
+
reason: 'context_not_ready', visibility: 'unknown', outside_loaded_price_envelope: false, time_range_overlap: null };
|
|
143
|
+
if (!ready)
|
|
144
|
+
return item;
|
|
145
|
+
if (phase === 'rolled_back' || phase === 'rollback_unknown') {
|
|
146
|
+
item.reason = phase === 'rolled_back' ? 'application_rolled_back' : 'rollback_unknown';
|
|
147
|
+
return item;
|
|
148
|
+
}
|
|
149
|
+
if (['price_pattern', 'nine_count'].includes(drawing.kind)) {
|
|
150
|
+
if (cmd.interval !== (context.ui_interval || context.interval)) {
|
|
151
|
+
item.status = 'hidden';
|
|
152
|
+
item.reason = 'period_mismatch';
|
|
153
|
+
item.visibility = 'not_applicable';
|
|
154
|
+
return item;
|
|
155
|
+
}
|
|
156
|
+
const loaded = drawing.kind === 'nine_count' ? (cmd.marks ?? []).every((m) => times.has(m.time))
|
|
157
|
+
: (cmd.pattern?.anchors ?? []).every((a) => times.has(a.time))
|
|
158
|
+
&& (cmd.pattern?.type !== 'platform' || (times.has(cmd.pattern?.box?.start_time) && times.has(cmd.pattern?.box?.end_time)));
|
|
159
|
+
if (!loaded) {
|
|
160
|
+
item.status = 'hidden';
|
|
161
|
+
item.reason = 'snapshot_outside_loaded_history';
|
|
162
|
+
item.visibility = 'not_applicable';
|
|
163
|
+
return item;
|
|
164
|
+
}
|
|
165
|
+
const registry = chart?.__dshPatternGroupRegistry;
|
|
166
|
+
if (!(registry instanceof Map)) {
|
|
167
|
+
item.reason = 'overlay_api_unavailable';
|
|
168
|
+
return item;
|
|
169
|
+
}
|
|
170
|
+
item.status = registry.get(drawing.id) === cmd.geometry_hash ? 'created' : 'failed';
|
|
171
|
+
item.reason = item.status === 'created' ? 'overlay_registered' : 'overlay_missing';
|
|
172
|
+
return item;
|
|
173
|
+
}
|
|
174
|
+
if (['series_line', 'price_band', 'price_sticks'].includes(drawing.kind)) {
|
|
175
|
+
if (cmd.interval !== (context.ui_interval || context.interval)) {
|
|
176
|
+
item.status = 'hidden';
|
|
177
|
+
item.reason = 'period_mismatch';
|
|
178
|
+
item.visibility = 'not_applicable';
|
|
179
|
+
return item;
|
|
180
|
+
}
|
|
181
|
+
const registry = drawing.kind === 'series_line' ? chart?.__dshSeriesLineRegistry
|
|
182
|
+
: drawing.kind === 'price_band' ? chart?.__dshPriceBandRegistry : chart?.__dshPriceSticksRegistry;
|
|
183
|
+
if (!(registry instanceof Map)) {
|
|
184
|
+
item.reason = 'indicator_api_unavailable';
|
|
185
|
+
return item;
|
|
186
|
+
}
|
|
187
|
+
item.status = registry.get(drawing.id) === (drawing.kind === 'series_line' ? cmd.series_hash
|
|
188
|
+
: drawing.kind === 'price_band' ? cmd.band_hash : cmd.sticks_hash) ? 'created' : 'failed';
|
|
189
|
+
item.reason = item.status === 'created' ? 'indicator_registered' : 'indicator_missing';
|
|
190
|
+
return item;
|
|
191
|
+
}
|
|
192
|
+
const expected = expectedOverlay(cmd, rows, context.ui_interval || context.interval, times);
|
|
193
|
+
const prices = expected.points?.map((p) => p.value) ?? [];
|
|
194
|
+
item.outside_loaded_price_envelope = prices.some((p) => p < low || p > high);
|
|
195
|
+
if (!canRead) {
|
|
196
|
+
item.reason = 'overlay_api_unavailable';
|
|
197
|
+
return item;
|
|
198
|
+
}
|
|
199
|
+
const observed = lookup(drawing.id);
|
|
200
|
+
if (observed.error) {
|
|
201
|
+
item.reason = 'overlay_lookup_failed';
|
|
202
|
+
return item;
|
|
203
|
+
}
|
|
204
|
+
if (expected.skip) {
|
|
205
|
+
item.status = observed.value ? 'unknown' : 'hidden';
|
|
206
|
+
item.reason = observed.value ? 'overlay_mismatch' : expected.skip;
|
|
207
|
+
item.visibility = 'not_applicable';
|
|
208
|
+
return item;
|
|
209
|
+
}
|
|
210
|
+
if (expected.failure) {
|
|
211
|
+
item.status = 'failed';
|
|
212
|
+
item.reason = expected.failure;
|
|
213
|
+
return item;
|
|
214
|
+
}
|
|
215
|
+
item.time_range_overlap = drawing.kind === 'horizontal_line' ? (first && last ? true : null)
|
|
216
|
+
: timeRangeOverlap(expected.points, first, last);
|
|
217
|
+
if (!observed.value) {
|
|
218
|
+
item.status = 'failed';
|
|
219
|
+
item.reason = 'overlay_missing';
|
|
220
|
+
return item;
|
|
221
|
+
}
|
|
222
|
+
const overlay = observed.value;
|
|
223
|
+
let same = false;
|
|
224
|
+
try {
|
|
225
|
+
const actual = overlay.points;
|
|
226
|
+
same = overlay.id === drawing.id && overlay.name === expected.name && overlay.groupId === expected.group && Array.isArray(actual)
|
|
227
|
+
&& actual.length === expected.points.length && actual.every((p, i) => p.timestamp === expected.points[i].timestamp
|
|
228
|
+
&& Number.isFinite(p.value) && Math.abs(p.value - expected.points[i].value) <= Math.max(1, Math.abs(p.value)) * 1e-10)
|
|
229
|
+
&& overlay.extendData?.label === (cmd.label || '') && overlay.extendData?.colorName === (cmd.color || 'info')
|
|
230
|
+
&& (drawing.kind !== 'channel' || overlay.extendData?.showMidline === (cmd.show_midline !== false))
|
|
231
|
+
&& (drawing.kind !== 'rectangle' || overlay.extendData?.showFill === (cmd.show_fill !== false))
|
|
232
|
+
&& (drawing.kind !== 'fibonacci_retracement' || JSON.stringify(overlay.extendData?.levels) === JSON.stringify(cmd.levels))
|
|
233
|
+
&& (drawing.kind !== 'labeled_polyline' || JSON.stringify(overlay.extendData?.pointLabels) === JSON.stringify(cmd.points.map((p) => p.label)))
|
|
234
|
+
&& (!['text_annotation', 'number_annotation', 'icon_annotation'].includes(drawing.kind)
|
|
235
|
+
|| overlay.extendData?.annotationKind === cmd.kind && (cmd.kind === 'icon_annotation'
|
|
236
|
+
? overlay.extendData?.annotationIcon === cmd.icon : overlay.extendData?.text === chartAnnotationText(cmd)));
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
item.reason = 'overlay_lookup_failed';
|
|
240
|
+
return item;
|
|
241
|
+
}
|
|
242
|
+
item.status = same ? 'created' : 'failed';
|
|
243
|
+
item.reason = same ? 'overlay_registered' : 'overlay_mismatch';
|
|
244
|
+
if (same && item.time_range_overlap === false) {
|
|
245
|
+
item.visibility = 'outside_time_viewport';
|
|
246
|
+
item.reason = 'outside_time_viewport';
|
|
247
|
+
}
|
|
248
|
+
return item;
|
|
249
|
+
});
|
|
250
|
+
for (const drawing of deleted) {
|
|
251
|
+
if (phase === 'rolled_back' || phase === 'rollback_unknown') {
|
|
252
|
+
items.push({ id: drawing.id, kind: drawing.kind, status: 'unknown', reason: phase === 'rolled_back' ? 'application_rolled_back' : 'rollback_unknown',
|
|
253
|
+
visibility: 'not_applicable', outside_loaded_price_envelope: false });
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (!ready) {
|
|
257
|
+
items.push({ id: drawing.id, kind: drawing.kind, status: 'unknown', reason: 'context_not_ready',
|
|
258
|
+
visibility: 'not_applicable', outside_loaded_price_envelope: false });
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (['price_pattern', 'nine_count'].includes(drawing.kind)) {
|
|
262
|
+
const registry = chart?.__dshPatternGroupRegistry;
|
|
263
|
+
items.push({ id: drawing.id, kind: drawing.kind, status: !(registry instanceof Map) ? 'unknown' : registry.has(drawing.id) ? 'failed' : 'removed',
|
|
264
|
+
reason: !(registry instanceof Map) ? 'overlay_api_unavailable' : registry.has(drawing.id) ? 'overlay_still_present' : 'overlay_removed',
|
|
265
|
+
visibility: 'not_applicable', outside_loaded_price_envelope: false });
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (['series_line', 'price_band', 'price_sticks'].includes(drawing.kind)) {
|
|
269
|
+
const registry = drawing.kind === 'series_line' ? chart?.__dshSeriesLineRegistry
|
|
270
|
+
: drawing.kind === 'price_band' ? chart?.__dshPriceBandRegistry : chart?.__dshPriceSticksRegistry;
|
|
271
|
+
items.push({ id: drawing.id, kind: drawing.kind, status: !(registry instanceof Map) ? 'unknown' : registry.has(drawing.id) ? 'failed' : 'removed',
|
|
272
|
+
reason: !(registry instanceof Map) ? 'indicator_api_unavailable' : registry.has(drawing.id) ? 'indicator_still_present' : 'indicator_removed',
|
|
273
|
+
visibility: 'not_applicable', outside_loaded_price_envelope: false });
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const observed = canRead ? lookup(drawing.id) : { error: true };
|
|
277
|
+
items.push({ id: drawing.id, kind: drawing.kind, status: observed.error ? 'unknown' : observed.value ? 'failed' : 'removed',
|
|
278
|
+
reason: observed.error ? (canRead ? 'overlay_lookup_failed' : 'overlay_api_unavailable') : observed.value ? 'overlay_still_present' : 'overlay_removed', visibility: 'not_applicable',
|
|
279
|
+
outside_loaded_price_envelope: false });
|
|
280
|
+
}
|
|
281
|
+
const hasSeries = items.some((item) => ['series_line', 'price_band', 'price_sticks'].includes(item.kind)), hasOverlay = items.some((item) => !['series_line', 'price_band', 'price_sticks'].includes(item.kind));
|
|
282
|
+
return { schema_version: 1, phase, evidence_scope: hasSeries ? hasOverlay ? 'mixed_chart_api_and_indicator_registry' : 'indicator_registry' : 'chart_object_api',
|
|
283
|
+
render_verified: false, pixel_verified: false, items };
|
|
284
|
+
}
|
package/lib/fibonacci.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Validate price-domain geometry and produce exact overlay points, never pixels. */
|
|
2
|
+
export function fibonacciOverlayPoints(c) {
|
|
3
|
+
const validPoint = (p) => Number.isSafeInteger(p?.time) && p.time > 0 && typeof p.price === 'number' && Number.isFinite(p.price) && p.price > 0;
|
|
4
|
+
if (c?.geometry_basis !== 'linear_price_retracement' || !validPoint(c.from) || !validPoint(c.to)
|
|
5
|
+
|| c.from.time >= c.to.time || c.from.price === c.to.price || !Array.isArray(c.ratios) || !c.ratios.length || c.ratios.length > 16
|
|
6
|
+
|| !Array.isArray(c.levels) || c.levels.length !== c.ratios.length)
|
|
7
|
+
throw Error('invalid_fibonacci_geometry');
|
|
8
|
+
const points = [c.from, c.to].map(p => ({ timestamp: p.time * 1000, value: p.price }));
|
|
9
|
+
c.ratios.forEach((r, i) => {
|
|
10
|
+
const level = c.levels[i], expected = c.to.price - r * (c.to.price - c.from.price);
|
|
11
|
+
if (typeof r !== 'number' || !Number.isFinite(r) || r < 0 || r > 1 || i > 0 && r <= c.ratios[i - 1]
|
|
12
|
+
|| level?.ratio !== r || typeof level.price !== 'number' || !Number.isFinite(level.price) || level.price <= 0
|
|
13
|
+
|| Math.abs(level.price - expected) > Math.max(1, Math.abs(expected)) * 1e-12)
|
|
14
|
+
throw Error('invalid_fibonacci_geometry');
|
|
15
|
+
points.push({ timestamp: c.from.time * 1000, value: level.price }, { timestamp: c.to.time * 1000, value: level.price });
|
|
16
|
+
});
|
|
17
|
+
return points;
|
|
18
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge the DSH FTShare settings credential into the MCP server's process.
|
|
3
|
+
*
|
|
4
|
+
* The Key and every FTShare request live in different processes: the DSH host
|
|
5
|
+
* owns the saved credential through the `ftshareSettings` service, while the
|
|
6
|
+
* Python MCP server owns the requests and can only read a credential from its
|
|
7
|
+
* environment or a file. This module closes that seam with the smallest
|
|
8
|
+
* contract both sides already share — one `0600` JSON file inside the runtime
|
|
9
|
+
* directory, pointed at by the launcher, re-read before each request, so a Key
|
|
10
|
+
* change applies without restarting the MCP server.
|
|
11
|
+
*
|
|
12
|
+
* Everything here is optional by construction: a host without the
|
|
13
|
+
* `ftshareSettings` service never starts the bridge, no file is written, and
|
|
14
|
+
* the MCP server keeps resolving credentials exactly as it did before.
|
|
15
|
+
*/
|
|
16
|
+
import { chmod, lstat, mkdir, mkdtemp, readdir, rename, rm, writeFile } from 'node:fs/promises';
|
|
17
|
+
import { basename, dirname, join } from 'node:path';
|
|
18
|
+
/** Environment variable naming the bridge file for the Python MCP server. */
|
|
19
|
+
export const SETTINGS_CREDENTIAL_ENV = 'DSH_KLINE_SETTINGS_KEY_FILE';
|
|
20
|
+
/** Directory, relative to the runtime directory, owned by the host bridge. */
|
|
21
|
+
export const CREDENTIAL_DIRECTORY = 'credentials';
|
|
22
|
+
const CREDENTIAL_FILE_PATTERN = /^ftshare-(\d+)\.json$/u;
|
|
23
|
+
const MAX_KEY_LENGTH = 512;
|
|
24
|
+
/**
|
|
25
|
+
* Path of the credential file published by the DSH host.
|
|
26
|
+
*
|
|
27
|
+
* The launcher mirrors this rule in `scripts/run-dsh-kline.mjs`; keeping the
|
|
28
|
+
* owner pid in the name means a file left behind by an earlier host can never
|
|
29
|
+
* be read by a later one.
|
|
30
|
+
*/
|
|
31
|
+
export function settingsCredentialFile(runtimeDirectory, hostPid) {
|
|
32
|
+
return join(runtimeDirectory, CREDENTIAL_DIRECTORY, `ftshare-${hostPid}.json`);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Follow the settings credential until the returned bridge is disposed.
|
|
36
|
+
*
|
|
37
|
+
* The current Key is published immediately and re-published on every
|
|
38
|
+
* invalidation; clearing the Key removes the file. Failures are reported to
|
|
39
|
+
* `warn` and otherwise ignored: a credential convenience must never take the
|
|
40
|
+
* chart down.
|
|
41
|
+
*/
|
|
42
|
+
export function startFtshareSettingsBridge(options) {
|
|
43
|
+
const hostPid = options.hostPid ?? process.pid;
|
|
44
|
+
if (!Number.isSafeInteger(hostPid) || hostPid <= 0)
|
|
45
|
+
throw new Error('invalid host process id');
|
|
46
|
+
const file = settingsCredentialFile(options.runtimeDirectory, hostPid);
|
|
47
|
+
const directory = dirname(file);
|
|
48
|
+
let pending = Promise.resolve();
|
|
49
|
+
let disposed = false;
|
|
50
|
+
const sync = async () => {
|
|
51
|
+
const key = usableKey(await options.settings.apiKey.resolve());
|
|
52
|
+
if (disposed)
|
|
53
|
+
return;
|
|
54
|
+
if (key)
|
|
55
|
+
await writeCredentialFile(file, key);
|
|
56
|
+
else
|
|
57
|
+
await withdrawCredentialFile(file);
|
|
58
|
+
};
|
|
59
|
+
const schedule = () => {
|
|
60
|
+
pending = pending.then(sync, sync).catch(async () => {
|
|
61
|
+
// Failed resolution/rotation must not leave a previously published key
|
|
62
|
+
// active. Never include provider exception text: it may contain a key.
|
|
63
|
+
await withdrawCredentialFile(file).catch(() => { });
|
|
64
|
+
try {
|
|
65
|
+
options.warn?.('FTShare settings credential publication failed');
|
|
66
|
+
}
|
|
67
|
+
catch { }
|
|
68
|
+
});
|
|
69
|
+
return pending;
|
|
70
|
+
};
|
|
71
|
+
const unsubscribe = options.settings.apiKey.subscribe(() => { void schedule(); });
|
|
72
|
+
void pruneCredentialDirectory(directory, hostPid).catch(() => { });
|
|
73
|
+
void schedule();
|
|
74
|
+
return {
|
|
75
|
+
async dispose() {
|
|
76
|
+
disposed = true;
|
|
77
|
+
unsubscribe();
|
|
78
|
+
await pending.catch(() => { });
|
|
79
|
+
await withdrawCredentialFile(file).catch(() => { });
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** Reject anything the Python reader would discard, so both sides agree. */
|
|
84
|
+
function usableKey(value) {
|
|
85
|
+
const key = typeof value === 'string' ? value.trim() : '';
|
|
86
|
+
if (!key || key.length > MAX_KEY_LENGTH)
|
|
87
|
+
return '';
|
|
88
|
+
for (const character of key) {
|
|
89
|
+
const code = character.codePointAt(0) ?? 0;
|
|
90
|
+
if (code < 32 || code === 127)
|
|
91
|
+
return '';
|
|
92
|
+
}
|
|
93
|
+
return key;
|
|
94
|
+
}
|
|
95
|
+
async function writeCredentialFile(file, key) {
|
|
96
|
+
const directory = dirname(file);
|
|
97
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
98
|
+
const info = await lstat(directory);
|
|
99
|
+
if (!info.isDirectory() || info.isSymbolicLink())
|
|
100
|
+
throw new Error('unsafe credential directory');
|
|
101
|
+
await restrict(directory, 0o700);
|
|
102
|
+
// Exclusive random directory prevents following a pre-created temporary
|
|
103
|
+
// symlink and keeps concurrent publishers from sharing a staging file.
|
|
104
|
+
const staging = await mkdtemp(join(directory, `.${basename(file)}.`));
|
|
105
|
+
const temporary = join(staging, 'credential.json');
|
|
106
|
+
try {
|
|
107
|
+
await restrict(staging, 0o700);
|
|
108
|
+
await writeFile(temporary, JSON.stringify({ version: 1, api_key: key }), { mode: 0o600, flag: 'wx' });
|
|
109
|
+
await restrict(temporary, 0o600);
|
|
110
|
+
await rename(temporary, file);
|
|
111
|
+
await restrict(file, 0o600);
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
await rm(staging, { recursive: true, force: true }).catch(() => { });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** Drop files owned by hosts that are gone; a stale Key must not linger. */
|
|
118
|
+
async function pruneCredentialDirectory(directory, keepPid) {
|
|
119
|
+
const info = await lstat(directory);
|
|
120
|
+
if (!info.isDirectory() || info.isSymbolicLink())
|
|
121
|
+
return;
|
|
122
|
+
for (const name of await readdir(directory)) {
|
|
123
|
+
const match = CREDENTIAL_FILE_PATTERN.exec(name);
|
|
124
|
+
if (!match)
|
|
125
|
+
continue;
|
|
126
|
+
const pid = Number(match[1]);
|
|
127
|
+
if (pid === keepPid || !Number.isSafeInteger(pid) || pid <= 0 || isAlive(pid))
|
|
128
|
+
continue;
|
|
129
|
+
await rm(join(directory, name), { force: true });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async function withdrawCredentialFile(file) {
|
|
133
|
+
try {
|
|
134
|
+
const info = await lstat(dirname(file));
|
|
135
|
+
if (!info.isDirectory() || info.isSymbolicLink())
|
|
136
|
+
throw new Error('unsafe credential directory');
|
|
137
|
+
await rm(file, { force: true });
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
if (error.code !== 'ENOENT')
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function isAlive(pid) {
|
|
145
|
+
try {
|
|
146
|
+
process.kill(pid, 0);
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
// A live process owned by someone else is still a live process.
|
|
151
|
+
return error.code === 'EPERM';
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async function restrict(path, mode) {
|
|
155
|
+
// Windows has no POSIX mode bits; the Python reader only enforces them there
|
|
156
|
+
// as well, so a platform that cannot apply them is not an error.
|
|
157
|
+
if (process.platform !== 'win32')
|
|
158
|
+
await chmod(path, mode);
|
|
159
|
+
}
|