// Vorher-Nachher-Vergleich für die Umsatz-/Margenableitung (src/margins.js): Simulationen mit festem Zufallsstartwert, // deren Gewinne, Erwartungen, Dividenden und Kurse sich durch die neue Ableitung NICHT ändern dürfen. // Genutzt von src/prt2margin.test.js; die Vergleichswerte in test/fixtures/prt2-before.json wurden mit genau diesen // Funktionen auf dem Stand vor der Änderung (0.63.0) erzeugt: // node -e "import('./test/prt2sim.js').then(async (m) => console.log(JSON.stringify(await m.beforeAfter(''))))" import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { pathToFileURL } from 'node:url'; export function mulberry32(a) { return function () { a |= 0; a = (a + 0x6d2b79f5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } const r6 = (x) => Math.round(x * 1e6) / 1e6; const imp = (dir, f) => import(pathToFileURL(path.join(dir, 'src', f)).href); /** * Nur Unternehmenszahlen: alle Startfirmen, `years` Spieljahre zu je vier Quartalen, wechselnde Konjunktur (Boom, * Rezession, Ölpreis-, Gas- und Dollarschwankungen). Liefert je Firma die Gewinne, Erwartungen, Dividenden und den * Fundamentalwert zu jedem Quartal (dazu Umsätze, die sich ändern dürfen). */ export async function fundSim(dir, { seed = 4242, years = 4, quarterDays = 21 } = {}) { const F = await imp(dir, 'fundamentals.js'); const { COMPANIES, SECTORS } = await imp(dir, 'world.js'); const rnd0 = Math.random; Math.random = mulberry32(seed); try { const stocks = COMPANIES.map((c, i) => { const s = { ...c, last: c.price }; s.fund = F.initFundamentals(s, i, 0); return s; }); const out = {}; for (const s of stocks) out[s.symbol] = { sector: s.sector, shares: s.shares, eps: [], est: [], div: [], fv: [], revenue: [], init: s.fund.quarters.map((q) => q.eps) }; const days = years * 4 * quarterDays; for (let day = 1; day <= days; day++) { const ph = (2 * Math.PI * day) / (quarterDays * 4 * 1.7); const macro = { gdp: 1.2 + 2.2 * Math.sin(ph), inflation: 2 + 1.5 * Math.cos(ph * 0.7), sectorMood: Object.fromEntries(Object.keys(SECTORS).map((k, j) => [k, 0.3 * Math.sin(ph + j)])), regime: Math.sin(ph) < -0.8 ? 'recession' : 'normal', ext: { oil: 0.4 * Math.sin(ph * 1.3), gas: 0.3 * Math.cos(ph * 0.9), usd: 0.1 * Math.sin(ph * 0.5) }, }; const ctx = { rateEff: 2.5 + Math.sin(ph), day, quarterDays, oil: macro.ext.oil, gas: macro.ext.gas, usd: macro.ext.usd }; for (const s of stocks) { for (const ev of F.dailyFundamentals(s, day, macro, quarterDays, 0.15)) { const o = out[s.symbol]; if (ev.kind === 'earnings') { o.eps.push(ev.quarter.eps); o.est.push(r6(ev.quarter.est)); o.div.push(ev.dividend); o.revenue.push(ev.quarter.revenue); o.fv.push(r6(F.fundamentalValue(s, ctx))); s.last = F.fundamentalValue(s, ctx); F.updateAnalysts(s, s.last); } } } } for (const s of stocks) out[s.symbol].consensus = s.fund.consensus; return out; } finally { Math.random = rnd0; } } /** * Ganzes Spiel (Orderbuch, Bots, Zentralbank, Gesetze …) im Zeitraffer mit künstlicher Uhr: Schlusskurse je Tag, * Gewinne und Umsätze je Quartal. Nur in einem eigenen Prozess aufrufen (Math.random und Date.now werden ersetzt). */ export async function gameSim(dir, { seed = 11, days = 16 } = {}) { Math.random = mulberry32(seed); process.env.TIME_MODE = 'turbo'; process.env.NODE_ENV = 'test'; process.env.DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'prt2-sim-')); const clock = { t: Date.UTC(2026, 0, 5, 9) }; Date.now = () => clock.t; const { settings } = await imp(dir, 'settings.js'); settings.news.enabled = false; settings.news.simulatedEveryMin = 0; Object.assign(settings.game, { timeMode: 'turbo', dayMs: 30_000, tickMs: 1000, candleMs: 2000, volAuctionSec: 3 }); const { Store } = await imp(dir, 'store.js'); const { Game } = await imp(dir, 'game.js'); const store = new Store(process.env.DATA_DIR); store.backup = () => null; const g = new Game({ store, now: () => clock.t }); const start = g.day; const closes = {}; let lastDay = g.day; while (g.day - start < days) { clock.t += 1000; g.tick(); g.ex.pendingTrades.length = 0; g.drainChanged?.(); if (g.day !== lastDay) { lastDay = g.day; for (const s of g.stocks.values()) if (s.kind === 'stock') (closes[s.symbol] ??= []).push(r6(s.last)); } } const res = {}; for (const s of g.stocks.values()) { if (s.kind !== 'stock') continue; res[s.symbol] = { sector: s.sector, shares: s.shares, closes: closes[s.symbol] ?? [], eps: s.fund.quarters.map((q) => q.eps), revenue: s.fund.quarters.map((q) => q.revenue), consensus: s.fund.consensus, lastDividend: s.fund.lastDividend, fv: r6(s.fv) }; } try { store.close(); } catch { /* egal */ } fs.rmSync(process.env.DATA_DIR, { recursive: true, force: true }); return res; }