mirror of
https://github.com/db9979/wibosim.git
synced 2026-12-06 13:34:09 +01:00
- Depot: Knopf Absichern/Stop ändern je Position (Long/Short, Haupt-, Battle-, Fondsdepot), 5/10/15 %, optional Trailing, Klartext mit Auslösekurs und Ergebnis ab Einstand - Orderticket: aufklappbarer Positionsgrößen-Rechner (Risiko in % oder €, Stop-Abstand aus Auslöser), Gebühren und Kaufkraft/Short-Kapazität berücksichtigt - Kurschart: eigene Käufe/Verkäufe als Dreiecke, aktive Stops/Limits/Take-Profit als Linien mit Tooltip, abschaltbar, Textalternative; neue Leseaktion myTrades - Kursalarme: seit Kauf (±X % zum Einstand, einmalig) und Tagesbewegung (einmal je Tag), Meldung über die zentrale Hinweis-Prüfung Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
97 lines
5.2 KiB
JavaScript
97 lines
5.2 KiB
JavaScript
// Eigene Trades und Orders im Kurschart (reine Funktionen, ohne DOM): Käufe/Verkäufe aus dem Orderjournal den Balken
|
||
// des Charts zuordnen, aktive Stops/Limits/Take-Profit als Linien beschreiben, Textfassung für Bildschirmleser.
|
||
|
||
const ACC = { battle: 'Battle', fund: 'Fonds' }; // kurz: der Tooltip muss auch am Handy passen
|
||
const fmt = (x) => (Number.isFinite(x) ? x.toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, '.') : '–');
|
||
const fint = (x) => String(Math.round(x)).replace(/\B(?=(\d{3})+(?!\d))/g, '.');
|
||
|
||
/** Index des Balkens, in den ein Zeitpunkt fällt (-1: außerhalb) */
|
||
export function barIndexAt(bars, ts, { minute = false } = {}) {
|
||
const n = bars.length;
|
||
if (!n || !Number.isFinite(ts)) return -1;
|
||
if (minute) {
|
||
// Minutenbalken: [t_i, t_{i+1}); der letzte Balken reicht höchstens eine typische Balkenbreite weiter
|
||
if (ts < bars[0].t) return -1;
|
||
let lo = 0, hi = n - 1;
|
||
while (lo < hi) { const mid = (lo + hi + 1) >> 1; if (bars[mid].t <= ts) lo = mid; else hi = mid - 1; }
|
||
if (lo === n - 1) {
|
||
const gap = n > 1 ? bars[n - 1].t - bars[n - 2].t : 60_000;
|
||
if (ts >= bars[n - 1].t + Math.max(gap, 1) * 2 && !bars[n - 1].live) return -1;
|
||
}
|
||
return lo;
|
||
}
|
||
// Tagesbalken: ts ist der Zeitpunkt des Handelsschlusses (bei zusammengefassten Balken tsTo) → erster Balken mit Schluss ≥ ts
|
||
const end = (b) => b.tsTo ?? b.ts;
|
||
let lo = 0, hi = n - 1, hit = -1;
|
||
while (lo <= hi) { const mid = (lo + hi) >> 1; if (end(bars[mid]) >= ts) { hit = mid; hi = mid - 1; } else lo = mid + 1; }
|
||
if (hit === -1) return bars[n - 1].live ? n - 1 : -1; // nach dem letzten Schluss: laufender Tag
|
||
if (hit === 0) {
|
||
const w = n > 1 ? end(bars[1]) - end(bars[0]) : Infinity;
|
||
if (ts < end(bars[0]) - w) return -1; // deutlich vor dem ersten Balken
|
||
}
|
||
return hit;
|
||
}
|
||
|
||
/**
|
||
* Trades den Balken zuordnen und je Balken zusammenfassen.
|
||
* @param {{ts, side, qty, price, acc?}[]} trades aufsteigend @param {object[]} bars
|
||
* @returns {{i: number, buy: {qty, px, n}|null, sell: {qty, px, n}|null, tip: string[]}[]}
|
||
*/
|
||
export function tradeMarks(trades, bars, { minute = false } = {}) {
|
||
const by = new Map();
|
||
for (const t of trades ?? []) {
|
||
const i = barIndexAt(bars, t.ts, { minute });
|
||
if (i < 0) continue;
|
||
let m = by.get(i);
|
||
if (!m) { m = { i, buy: null, sell: null, tip: [] }; by.set(i, m); }
|
||
const k = t.side === 'buy' ? 'buy' : 'sell';
|
||
const cur = m[k] ?? { qty: 0, px: 0, val: 0, n: 0 };
|
||
cur.n++; cur.qty += t.qty; cur.val += t.qty * t.price; cur.px = cur.val / cur.qty;
|
||
m[k] = cur;
|
||
m.tip.push(`Dein ${k === 'buy' ? 'Kauf' : 'Verkauf'}: ${fint(t.qty)} × ${fmt(t.price)} €${ACC[t.acc] ? ` (${ACC[t.acc]})` : ''}`);
|
||
}
|
||
for (const m of by.values()) if (m.tip.length > 4) m.tip = [...m.tip.slice(0, 3), `… und ${m.tip.length - 3} weitere`];
|
||
return [...by.values()].sort((a, b) => a.i - b.i);
|
||
}
|
||
|
||
const KIND_OF = { stop: 'stop', stoplimit: 'stop', trailing: 'stop', takeprofit: 'tp', limit: 'limit' };
|
||
const LABEL = { stop: 'Stop-Loss', stoplimit: 'Stop-Limit', trailing: 'Trailing-Stop', takeprofit: 'Take-Profit', limit: 'Limit' };
|
||
|
||
/**
|
||
* Aktive Orders eines Werts als Linien.
|
||
* @param {{acc: string, p: {orders?: any[], cond?: any[]}|null}[]} books Depots (main/battle/fund)
|
||
* @returns {{p: number, kind: 'stop'|'tp'|'limit', side: string, label: string, tip: string}[]}
|
||
*/
|
||
export function orderLevels(books, symbol) {
|
||
const out = [];
|
||
for (const { acc, p } of books ?? []) {
|
||
if (!p) continue;
|
||
const where = ACC[acc] ? ` · ${ACC[acc]}` : '';
|
||
for (const o of p.orders ?? []) {
|
||
if (o.symbol !== symbol || !(o.price > 0)) continue;
|
||
const left = (o.qty ?? 0) - (o.filled ?? 0);
|
||
out.push({ p: o.price, kind: 'limit', side: o.side, label: `Limit ${o.side === 'buy' ? 'Kauf' : 'Verkauf'}`, tip: `Limit-${o.side === 'buy' ? 'Kauf' : 'Verkauf'} ${fint(left)} St. ${fmt(o.price)} €${where}` });
|
||
}
|
||
for (const c of p.cond ?? []) {
|
||
const k = KIND_OF[c.type];
|
||
if (c.symbol !== symbol || !k || !(c.trigger > 0)) continue;
|
||
const lab = c.label ?? LABEL[c.type] ?? c.type;
|
||
out.push({ p: c.trigger, kind: k, side: c.side, label: LABEL[c.type] ?? lab, tip: `${lab} ${fint(c.qty)} St. ab ${fmt(c.trigger)} €${c.trailPct ? ` (${fmt(c.trailPct * 100).replace(/,00$/, '')} %)` : ''}${where}` });
|
||
}
|
||
}
|
||
return out.sort((a, b) => b.p - a.p);
|
||
}
|
||
|
||
/** Schlüssel, an dem man erkennt, ob sich die Linien geändert haben (spart Neuberechnen je Takt) */
|
||
export const levelsKey = (levels) => levels.map((l) => `${l.kind}${l.side}${l.p}`).join('|');
|
||
|
||
/** Textfassung (Bildschirmleser): „Deine Trades im Zeitraum: 2 Käufe, 1 Verkauf. Aktive Orders: Stop-Loss 12,30 €.“ */
|
||
export function overlayText(marks, levels) {
|
||
const nb = marks.reduce((a, m) => a + (m.buy?.n ?? 0), 0);
|
||
const ns = marks.reduce((a, m) => a + (m.sell?.n ?? 0), 0);
|
||
const parts = [];
|
||
if (nb || ns) parts.push(`Deine Trades im Zeitraum: ${[nb ? `${nb} ${nb === 1 ? 'Kauf' : 'Käufe'}` : '', ns ? `${ns} ${ns === 1 ? 'Verkauf' : 'Verkäufe'}` : ''].filter(Boolean).join(', ')}.`);
|
||
if (levels.length) parts.push(`Aktive Orders: ${levels.map((l) => `${l.label} ${fmt(l.p)} Euro`).join(', ')}.`);
|
||
return parts.join(' ');
|
||
}
|