mirror of
https://github.com/db9979/wibosim.git
synced 2026-12-06 13:34:09 +01:00
- Abend- und Wochenendhandel über einen Market Maker (Spread, Größenlimit, Abendkurs als Eröffnungstendenz) - Hauptversammlungen mit Abstimmungen über Dividende, Rückkauf, Kapitalerhöhung und Vergütung - Prognose-Tippspiel für den nächsten Handelstag mit Rangliste - Plan für den nächsten Handelstag, Watchlist, Kursalarme, Wochenrückblick - Analysen mit Likes, Auswertung und Analysten-Rangliste - Parlamentsabstimmungen enden abends, Wahlkampf am Wochenende, TV-Duell - Clan-Wochenziel, Krypto-Wochenende, Börsengänge bevorzugt Do/Fr - Fixes: Aktien-Links, Speicherung von Trailing-Stops und Dividendenanpassungen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
187 lines
10 KiB
JavaScript
187 lines
10 KiB
JavaScript
// Feierabend: alles, was nach Börsenschluss und am Wochenende Spaß macht und auf den nächsten Handelstag vorbereitet.
|
||
// – Watchlist und Kursalarme (auch für Abendkurse und WiboCoin)
|
||
// – Plan für den nächsten Handelstag: gesammelte Auktionsorders, Alarme, Termine der eigenen Werte
|
||
// – Wochenrückblick nach dem letzten Handelstag der Woche (Rendite, beste/schlechteste Trades, Vergleich mit
|
||
// Index und Rivalen)
|
||
// – Clan-Wochenziel „Feierabend“: Tipps, HV-Stimmen und Abendtrades der Mitglieder zählen gemeinsam
|
||
// – Krypto-Wochenende: am Wochenende ist bei WiboCoin mehr los
|
||
import { uid, fmt, rnd } from './util.js';
|
||
|
||
const MAX_ALERTS = 20, MAX_WATCH = 30;
|
||
const WEEKEND_CRYPTO = [
|
||
{ title: 'Wochenend-Rallye: Kleinanleger kaufen WiboCoin, während die Börse schläft', size: 0.2 },
|
||
{ title: 'Sonntagsflaute am Kryptomarkt: WiboCoin rutscht in dünnem Handel ab', size: -0.18 },
|
||
{ title: 'Gerücht am Wochenende: Großer Online-Händler prüft Zahlungen mit WiboCoin', size: 0.24 },
|
||
{ title: 'Wochenend-Panne: Wallet-Anbieter stundenlang offline – WiboCoin unter Druck', size: -0.22 },
|
||
];
|
||
const CLAN_GOAL = { tip: 2, agm: 3, evening: 1 }; // Punkte je Aktivität
|
||
|
||
export class AfterHours {
|
||
constructor(g, saved) {
|
||
this.g = g;
|
||
this.state = { clanWeek: null, clan: {}, lastWeekly: null, weekStartIndex: null, weekKey: null, ...(saved ?? {}) };
|
||
}
|
||
get cfg() { return this.g.cfg; }
|
||
snapshot() { return this.state; }
|
||
|
||
// ================================================================ Watchlist & Kursalarme
|
||
toggleWatch(p, symbol) {
|
||
if (!this.g.stocks.has(symbol)) throw new Error('Unbekanntes Wertpapier');
|
||
p.watch ??= [];
|
||
if (p.watch.includes(symbol)) p.watch = p.watch.filter((x) => x !== symbol);
|
||
else { if (p.watch.length >= MAX_WATCH) throw new Error(`Höchstens ${MAX_WATCH} Werte auf der Watchlist`); p.watch.push(symbol); }
|
||
this.g.markDirty(p);
|
||
return { watch: p.watch };
|
||
}
|
||
|
||
addAlert(p, { symbol, price } = {}) {
|
||
const s = this.g.stocks.get(String(symbol ?? ''));
|
||
if (!s) throw new Error('Unbekanntes Wertpapier');
|
||
price = Math.round(Number(price) * 100) / 100;
|
||
if (!(price > 0)) throw new Error('Ungültiger Kurs');
|
||
if (Math.abs(price / s.last - 1) < 0.001) throw new Error('Der Alarmkurs liegt praktisch beim aktuellen Kurs');
|
||
if (Math.abs(price / s.last - 1) > 0.5) throw new Error('Alarmkurs höchstens 50 % vom aktuellen Kurs entfernt');
|
||
p.alerts ??= [];
|
||
if (p.alerts.length >= MAX_ALERTS) throw new Error(`Höchstens ${MAX_ALERTS} Kursalarme`);
|
||
const a = { id: uid(3), symbol: s.symbol, price, dir: price > s.last ? 'above' : 'below', ts: this.g.clock.now() };
|
||
p.alerts.push(a);
|
||
this.g.markDirty(p);
|
||
return { alerts: p.alerts };
|
||
}
|
||
|
||
delAlert(p, id) {
|
||
p.alerts = (p.alerts ?? []).filter((a) => a.id !== String(id ?? ''));
|
||
this.g.markDirty(p);
|
||
return { alerts: p.alerts };
|
||
}
|
||
|
||
#checkAlerts() {
|
||
const g = this.g;
|
||
for (const p of g.players.values()) {
|
||
if (!p.alerts?.length) continue;
|
||
const keep = [];
|
||
for (const a of p.alerts) {
|
||
const s = g.stocks.get(a.symbol);
|
||
if (!s) continue;
|
||
const hit = a.dir === 'above' ? s.last >= a.price : s.last <= a.price;
|
||
if (!hit) { keep.push(a); continue; }
|
||
const where = g.afterHours() && !s.allDay ? ' (Abendkurs)' : '';
|
||
g.notify(p.id, { kind: 'alert', title: `Kursalarm ${s.symbol}: ${fmt(s.last)} €${where}`, text: `${s.name} ist ${a.dir === 'above' ? 'über' : 'unter'} ${fmt(a.price)} € ${a.dir === 'above' ? 'gestiegen' : 'gefallen'}.`, action: { symbol: s.symbol } });
|
||
g.sendTo(p.id, { type: 'alert', symbol: s.symbol, price: s.last, target: a.price, dir: a.dir });
|
||
}
|
||
if (keep.length !== p.alerts.length) { p.alerts = keep; g.markDirty(p); }
|
||
}
|
||
}
|
||
|
||
// ================================================================ Plan für den nächsten Handelstag
|
||
plan(p) {
|
||
const g = this.g;
|
||
const orders = g.ex.ordersOf(p.id).map((o) => {
|
||
const s = g.stocks.get(o.symbol);
|
||
return { symbol: o.symbol, side: o.side, qty: o.qty - (o.filled ?? 0), type: o.type, price: o.price ?? o.limit ?? null, trigger: o.kind === 'cond' ? g.ex.condTrigger(o) : null, kind: o.kind, last: s?.last ?? null };
|
||
});
|
||
const held = new Set(Object.entries(p.positions).filter(([, x]) => x.qty !== 0).map(([k]) => k));
|
||
const watch = (p.watch ?? []).map((sym) => {
|
||
const s = g.stocks.get(sym);
|
||
return s ? { symbol: sym, name: s.name, last: s.last, close: s.close ?? s.prevClose, change: (s.last / s.prevClose - 1) * 100, eveChange: g.afterHours() && s.close ? (s.last / s.close - 1) * 100 : null, held: held.has(sym) } : null;
|
||
}).filter(Boolean);
|
||
const mine = new Set([...held, ...(p.watch ?? [])]);
|
||
const events = g.calendar().items.filter((i) => (i.symbol && mine.has(i.symbol)) || ['cb', 'macro', 'election'].includes(i.kind)).filter((i) => i.ts >= g.clock.now() - 3_600_000).slice(0, 12);
|
||
return { orders, alerts: p.alerts ?? [], watch, events, recap: p.weekRecap ?? null, clan: this.clanView(p) };
|
||
}
|
||
|
||
// ================================================================ Wochenrückblick
|
||
/** nach jedem Handelsschluss: letzter Handelstag der Woche? */
|
||
onClose() {
|
||
const g = this.g, c = g.clock;
|
||
const st = g.phaseInfo ?? c.state();
|
||
const thisWeek = c.localDay(g.clock.now() - 60_000).week, nextWeek = c.localDay(st.nextPreOpen + 60_000).week;
|
||
if (c.turbo ? g.day % 5 !== 4 : thisWeek === nextWeek) return;
|
||
if (this.state.lastWeekly === `${g.season.n}|${thisWeek}`) return;
|
||
this.state.lastWeekly = `${g.season.n}|${thisWeek}`;
|
||
this.#weekly();
|
||
}
|
||
|
||
/** Wochenbeginn: Indexstand merken */
|
||
onNewDay() {
|
||
const wk = this.g.clock.localDay().week;
|
||
if (this.state.weekKey !== wk) { this.state.weekKey = wk; this.state.weekStartIndex = this.g.index().value; }
|
||
}
|
||
|
||
#weekly() {
|
||
const g = this.g;
|
||
const idx = g.index().value;
|
||
const idxWeek = this.state.weekStartIndex ? (idx / this.state.weekStartIndex - 1) * 100 : null;
|
||
const ret = (p) => (g.equity(p) / Math.max(1, p.weekStartEquity ?? g.cfg.startCash) - 1) * 100;
|
||
const rivals = [...g.players.values()].filter((p) => p.rival?.active).map((p) => ({ name: p.name, pct: ret(p) })).sort((a, b) => b.pct - a.pct);
|
||
const humans = [...g.players.values()].filter((p) => !p.rival && !p.banned && (p.trades ?? 0) > 0);
|
||
const ranked = humans.map((p) => ({ p, pct: ret(p) })).sort((a, b) => b.pct - a.pct);
|
||
const since = g.clock.now() - (g.clock.turbo ? 5 * g.cfg.dayMs : 7 * 86_400_000);
|
||
for (const [i, { p, pct }] of ranked.entries()) {
|
||
let fills = [];
|
||
try { fills = g.store.fillsSince(p.id, since).filter((f) => f.realized && ['trade', 'auction', 'evening'].includes(f.kind)); } catch { /* egal */ }
|
||
const by = [...fills].sort((a, b) => b.realized - a.realized);
|
||
const best = by[0]?.realized > 0 ? { symbol: by[0].symbol, realized: by[0].realized } : null;
|
||
const worst = by.at(-1)?.realized < 0 ? { symbol: by.at(-1).symbol, realized: by.at(-1).realized } : null;
|
||
const beat = rivals.filter((r) => pct > r.pct).length;
|
||
const recap = {
|
||
week: this.state.lastWeekly, ts: g.clock.now(), pct, index: idxWeek, rank: i + 1, of: ranked.length, trades: fills.length,
|
||
best, worst, rivalsBeaten: beat, rivals: rivals.length, topRival: rivals[0] ?? null,
|
||
};
|
||
p.weekRecap = recap;
|
||
g.markDirty(p);
|
||
const vsIdx = idxWeek === null ? '' : pct >= idxWeek ? ` – ${fmt(pct - idxWeek)} Prozentpunkte besser als der WIBO-10` : ` – der WIBO-10 war ${fmt(idxWeek - pct)} Prozentpunkte besser`;
|
||
g.notify(p.id, {
|
||
kind: 'weekly', title: `Dein Wochenrückblick: ${pct >= 0 ? '+' : ''}${fmt(pct)} %`,
|
||
text: `Platz ${i + 1} von ${ranked.length} in dieser Woche${vsIdx}.${best ? ` Bester Trade: ${best.symbol} +${fmt(best.realized)} €.` : ''}${worst ? ` Schlechtester: ${worst.symbol} ${fmt(worst.realized)} €.` : ''}${rivals.length ? ` Du hast ${beat} von ${rivals.length} Rivalen geschlagen.` : ''}`,
|
||
action: { view: 'termine' },
|
||
});
|
||
}
|
||
}
|
||
|
||
// ================================================================ Clan-Wochenziel „Feierabend“
|
||
#clanKey() { return `${this.g.season.n}|${this.g.clock.localDay().week}`; }
|
||
/** Aktivität nach Börsenschluss (aus progress.event) */
|
||
clanEvent(p, kind) {
|
||
const pts = CLAN_GOAL[kind];
|
||
if (!pts || p.rival) return;
|
||
const c = this.g.clubs?.clubOf(p.id);
|
||
if (!c) return;
|
||
const key = this.#clanKey();
|
||
if (this.state.clanWeek !== key) { this.state.clanWeek = key; this.state.clan = {}; }
|
||
const st = (this.state.clan[c.id] ??= { pts: 0, by: {}, done: false });
|
||
st.pts += pts;
|
||
st.by[p.id] = (st.by[p.id] ?? 0) + pts;
|
||
this.g.metaDirty = true;
|
||
const goal = this.#goal(c);
|
||
if (!st.done && st.pts >= goal) {
|
||
st.done = true;
|
||
for (const m of c.members) {
|
||
const mp = this.g.players.get(m.id);
|
||
if (!mp) continue;
|
||
this.g.prog.addXp(mp, 60);
|
||
this.g.notify(m.id, { kind: 'clan', title: `Clan-Wochenziel erreicht: ${c.name}`, text: 'Gemeinsam nach Börsenschluss aktiv – jedes Mitglied erhält 60 XP.' });
|
||
}
|
||
}
|
||
}
|
||
#goal(c) { return 10 + 6 * Math.max(1, c.members.length); }
|
||
clanView(p) {
|
||
const c = this.g.clubs?.clubOf(p.id);
|
||
if (!c) return null;
|
||
const st = this.state.clanWeek === this.#clanKey() ? this.state.clan[c.id] : null;
|
||
return { name: c.name, pts: st?.pts ?? 0, goal: this.#goal(c), done: !!st?.done, mine: st?.by?.[p.id] ?? 0, rules: 'Tipp 2 Punkte · HV-Stimme 3 Punkte · Abendtrade 1 Punkt' };
|
||
}
|
||
|
||
// ================================================================ Takt
|
||
tick(dtMs) {
|
||
const g = this.g;
|
||
if (g.tickCount % 5 === 0) this.#checkAlerts();
|
||
// Krypto-Wochenende: im Mittel etwa alle 8 Stunden ein Ereignis
|
||
if (g.afterHours() === 'weekend' && this.cfg.weekendCrypto && Math.random() < dtMs / (8 * 3_600_000)) {
|
||
g.assets.cryptoEvent(WEEKEND_CRYPTO[Math.floor(rnd(0, WEEKEND_CRYPTO.length))]);
|
||
}
|
||
// Wahlkampf am Wochenende: etwa alle 6 Stunden eine Kundgebung/Umfrage
|
||
if (g.afterHours() === 'weekend' && Math.random() < dtMs / (6 * 3_600_000)) g.gov.weekendCampaign();
|
||
}
|
||
}
|