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
59 lines
3.9 KiB
JavaScript
59 lines
3.9 KiB
JavaScript
// Positionsgrößen-Rechner (reine Rechnung, ohne DOM): Wie viele Stück darf ich kaufen bzw. leerverkaufen, damit ich
|
||
// beim Auslösen meines Stops höchstens den gewählten Betrag verliere? Gebühren für Ein- und Ausstieg (Taker + fest)
|
||
// werden grob eingerechnet; Kaufkraft bzw. Short-Kapazität und Leerverkaufsverbote begrenzen die Stückzahl.
|
||
|
||
/**
|
||
* @param {object} o
|
||
* @param {number} o.price erwarteter Einstiegskurs je Stück
|
||
* @param {number} o.stopPct Abstand des Stops vom Einstieg in Prozent
|
||
* @param {number} [o.riskEur] höchstens zu verlierender Betrag in €
|
||
* @param {number} [o.riskPct] alternativ: Prozent vom Depotwert
|
||
* @param {number} [o.equity] Depotwert (für riskPct)
|
||
* @param {'buy'|'sell'} [o.side] Kauf (Long) oder Verkauf (Short)
|
||
* @param {number} [o.available] Kaufkraft (Kauf)
|
||
* @param {number} [o.shortCapacity] zusätzliches Short-Volumen in € (Verkauf)
|
||
* @param {number} [o.pos] aktueller Bestand (±)
|
||
* @param {boolean} [o.noShort] Leerverkauf nicht möglich
|
||
* @param {{taker: number, fixed: number}} [o.fees]
|
||
* @param {number} [o.maxQty] Höchstmenge je Order (z. B. Abendhandel)
|
||
* @returns {{ok: boolean, why?: string, qty: number, raw: number, risk: number, riskPerShare: number, notional: number, fee: number,
|
||
* capped: null|'kaufkraft'|'short'|'noShort'|'maxQty', stopPrice: number, budget: number}}
|
||
*/
|
||
export function positionSize(o) {
|
||
const fees = o.fees ?? { taker: 0.0005, fixed: 0.9 };
|
||
const price = Number(o.price), stopPct = Number(o.stopPct);
|
||
const side = o.side === 'sell' ? 'sell' : 'buy';
|
||
const empty = (why) => ({ ok: false, why, qty: 0, raw: 0, risk: 0, riskPerShare: 0, notional: 0, fee: 0, capped: null, stopPrice: NaN, budget: 0 });
|
||
if (!(price > 0)) return empty('Kein Kurs');
|
||
if (!(stopPct > 0 && stopPct < 100)) return empty('Stop-Abstand fehlt');
|
||
const budget = o.riskEur > 0 ? Number(o.riskEur) : o.riskPct > 0 && o.equity > 0 ? (o.equity * o.riskPct) / 100 : 0;
|
||
if (!(budget > 0)) return empty('Risiko fehlt');
|
||
const stopPrice = side === 'buy' ? price * (1 - stopPct / 100) : price * (1 + stopPct / 100);
|
||
// Verlust je Stück: Kursabstand + Taker-Gebühr beim Ein- und beim Ausstieg; die festen Gebühren zweimal vorab abziehen
|
||
const riskPerShare = Math.abs(price - stopPrice) + (price + stopPrice) * (fees.taker ?? 0);
|
||
const left = budget - 2 * (fees.fixed ?? 0);
|
||
if (left <= 0) return { ...empty('Risiko kleiner als die Gebühren'), budget, stopPrice };
|
||
const raw = Math.floor(left / riskPerShare + 1e-9);
|
||
let qty = raw, capped = null;
|
||
const unit = price * (1 + (fees.taker ?? 0));
|
||
if (side === 'buy') {
|
||
const cap = Math.floor(Math.max(0, (Number(o.available ?? Infinity) - (fees.fixed ?? 0))) / unit + 1e-9);
|
||
if (qty > cap) { qty = cap; capped = 'kaufkraft'; }
|
||
} else {
|
||
const held = Math.max(0, o.pos ?? 0);
|
||
const cap = o.noShort ? held : held + Math.floor(Math.max(0, Number(o.shortCapacity ?? Infinity)) / price + 1e-9);
|
||
if (qty > cap) { qty = cap; capped = o.noShort ? 'noShort' : 'short'; }
|
||
}
|
||
if (o.maxQty > 0 && qty > o.maxQty) { qty = Math.floor(o.maxQty); capped = 'maxQty'; }
|
||
qty = Math.max(0, qty);
|
||
const notional = qty * price;
|
||
const fee = qty ? notional * (fees.taker ?? 0) + (fees.fixed ?? 0) : 0;
|
||
const risk = qty ? qty * riskPerShare + 2 * (fees.fixed ?? 0) : 0;
|
||
return { ok: qty > 0, why: qty > 0 ? undefined : raw > 0 ? capText(capped) : 'Risiko zu klein für ein Stück', qty, raw, risk, riskPerShare, notional, fee, capped, stopPrice, budget };
|
||
}
|
||
|
||
/** Kurze Begründung, warum weniger Stück vorgeschlagen werden als das Risiko erlauben würde */
|
||
export function capText(capped) {
|
||
return { kaufkraft: 'begrenzt durch deine Kaufkraft', short: 'begrenzt durch deine Short-Kapazität (Sicherheitsleistung)', noShort: 'begrenzt auf deinen Bestand – Leerverkauf nicht möglich', maxQty: 'begrenzt durch die Höchstmenge je Order' }[capped] ?? '';
|
||
}
|