mirror of
https://github.com/db9979/wibosim.git
synced 2026-12-06 13:34:09 +01:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
371 lines
22 KiB
JavaScript
371 lines
22 KiB
JavaScript
// Weitere Anlageklassen: Staatsanleihen mit konstanter Laufzeit (Zinskurve), Rohstoffe und US-Dollar.
|
||
// Alle Instrumente werden über dasselbe Orderbuch gehandelt wie Aktien (Market Maker, Auktionen, Leerverkauf).
|
||
// Ihr „innerer Wert“ folgt der Makrolage: Anleihen der Zinskurve, Öl der Konjunktur und Angebotsschocks,
|
||
// Erdgas der Heizsaison und dem Füllstand der Gasspeicher, Gold Realzins und Angst, der Dollar der Zinsdifferenz.
|
||
// Umgekehrt wirken Öl, Gas und Dollar auf Inflation, Wachstum und Unternehmensgewinne; der Risikoaufschlag aus dem
|
||
// Staatshaushalt verteuert Kredite.
|
||
import { INFL_TARGET, TREND_GDP } from './world.js';
|
||
import { gauss, clamp, rnd, fmt } from './util.js';
|
||
|
||
export const ASSET_CLASSES = { stock: 'Aktien', bond: 'Anleihen', commodity: 'Rohstoffe', fx: 'Devisen', crypto: 'Krypto' };
|
||
|
||
// price: Startkurs (Anleihen werden beim Start aus der Zinskurve bewertet) · shares: Größe für Markttiefe/Leihe
|
||
// pullDays/misSd: wie eng der Kurs dem inneren Wert folgt · vol: Basisvolatilität p. a.
|
||
export const INSTRUMENTS = [
|
||
{ symbol: 'WIB2', kind: 'bond', name: 'Wiborien-Anleihe 2 J.', maturity: 2, coupon: 2.75, price: 100, shares: 6e8, vol: 0.018, pullDays: 0.3, misSd: 0.002,
|
||
desc: 'Staatsanleihe der Republik Wiborien mit konstanter Restlaufzeit von 2 Jahren. Kurs in € je 100 € Nennwert, Kupon 2,75 % p. a. (vierteljährlich).' },
|
||
{ symbol: 'WIB10', kind: 'bond', name: 'Wiborien-Anleihe 10 J.', maturity: 10, coupon: 3.0, price: 100, shares: 6e8, vol: 0.065, pullDays: 0.3, misSd: 0.004,
|
||
desc: 'Staatsanleihe der Republik Wiborien mit konstanter Restlaufzeit von 10 Jahren. Kurs in € je 100 € Nennwert, Kupon 3,0 % p. a. (vierteljährlich).' },
|
||
{ symbol: 'OEL', kind: 'commodity', name: 'Rohöl (1 Barrel)', price: 72.4, shares: 4e8, vol: 0.34, pullDays: 2, misSd: 0.04,
|
||
desc: 'Rohöl-Kontrakt, Preis in € je Barrel. Folgt der Weltkonjunktur und Förderentscheidungen des (fiktiven) Petrolia-Kartells. Teures Öl treibt die Inflation und belastet Auto, Industrie und Handel.' },
|
||
{ symbol: 'GAS', kind: 'commodity', name: 'Erdgas (1 MWh)', price: 35.0, shares: 3e8, vol: 0.6, pullDays: 2, misSd: 0.05,
|
||
desc: 'Erdgas-Kontrakt, Preis in € je Megawattstunde. Im Winter teurer als im Sommer, dazu entscheidet der Füllstand der Gasspeicher: Sind sie vor oder im Winter knapp gefüllt, steigt der Preis. Teures Gas treibt die Inflation, belastet Haushalte, Industrie und Handel – Versorger profitieren.' },
|
||
{ symbol: 'GOLD', kind: 'commodity', name: 'Gold (10 g)', price: 742.0, shares: 6e7, vol: 0.16, pullDays: 3, misSd: 0.03,
|
||
desc: 'Gold, Preis in € je 10 Gramm. Sicherer Hafen: steigt bei Angst, fallenden Realzinsen und Zweifeln an den Staatsfinanzen.' },
|
||
{ symbol: 'USD', kind: 'fx', name: 'US-Dollar (100 $)', price: 92.5, shares: 5e8, vol: 0.08, pullDays: 3, misSd: 0.015,
|
||
desc: 'Preis von 100 US-Dollar in Euro. Steigt, wenn der Euro schwächer wird – etwa bei sinkender Zinsdifferenz. Ein teurer Dollar hilft Exporteuren und erhöht die Inflation.' },
|
||
// Krypto: rund um die Uhr handelbar (auch nachts und am Wochenende), keine Auktionen, keine Leerverkäufe, kein innerer Wert
|
||
// im klassischen Sinn – der Kurs folgt Stimmung, Hype, Nachrichten und der Zinslage. corridorMult: breitere Preiskorridore.
|
||
{ symbol: 'WIBC', kind: 'crypto', name: 'WiboCoin', price: 418.0, shares: 2.1e7, vol: 0.85, pullDays: 1.5, misSd: 0.1, corridorMult: 2.5, noShort: true, allDay: true,
|
||
desc: 'Fiktive Kryptowährung mit begrenzter Menge (21 Mio. Coins). Rund um die Uhr handelbar, auch wenn die Börse geschlossen ist. Keine Zinsen, keine Dividende, keine Leerverkäufe – der Kurs lebt von Stimmung, Hype und Nachrichten und schwankt sehr stark.' },
|
||
];
|
||
const BASE = Object.fromEntries(INSTRUMENTS.map((i) => [i.symbol, i.price]));
|
||
const pick = (a) => a[Math.floor(Math.random() * a.length)];
|
||
// fiktive Krypto-Ereignisse (size: log. Hype-Schub)
|
||
const CRYPTO_EVENTS = [
|
||
{ title: 'Zahlungsdienst Fintaxo akzeptiert WiboCoin – Kurs springt', size: 0.25, sectors: ['banks'] },
|
||
{ title: 'Großanleger „Nordlicht Capital“ steigt mit Millionenbetrag bei WiboCoin ein', size: 0.3 },
|
||
{ title: 'Krypto-Börse „CoinHafen“ meldet Hackerangriff – WiboCoin unter Druck', size: -0.35 },
|
||
{ title: 'Finanzaufsicht prüft strengere Regeln für Kryptowährungen', size: -0.2 },
|
||
{ title: 'WiboCoin-Halbierung: Neue Coins werden ab sofort nur noch halb so schnell ausgegeben', size: 0.18 },
|
||
{ title: 'Influencer-Hype: WiboCoin trendet in sozialen Netzwerken', size: 0.22 },
|
||
{ title: 'Großer WiboCoin-Fonds muss Anteile zurücknehmen – Notverkäufe', size: -0.28 },
|
||
{ title: 'Zentralbank warnt vor Spekulation mit Kryptowährungen', size: -0.12 },
|
||
];
|
||
// fiktive Ereignisse am Gasmarkt (supply: log. Angebotsschock, + = knapper/teurer · gap: Änderung des Speicherfüllstands
|
||
// gegenüber dem Üblichen · winter: nur in der Heizsaison)
|
||
const GAS_EVENTS = [
|
||
{ title: 'Kältewelle über Wiborien: Gasverbrauch auf Rekordniveau, die Speicher leeren sich schneller', supply: 0.08, gap: -0.06, winter: true },
|
||
{ title: 'Ungewöhnlich milder Winter: Gasspeicher bleiben voller als erwartet', supply: -0.07, gap: 0.05, winter: true },
|
||
{ title: 'Wartung an der Nordmeer-Leitung verlängert – weniger Erdgas für Wiborien', supply: 0.1, gap: -0.04 },
|
||
{ title: 'Neues Flüssiggas-Terminal in Nordhafen nimmt den Betrieb auf', supply: -0.08, gap: 0.03 },
|
||
{ title: 'Lieferland Karvanien droht mit gekürzten Gaslieferungen', supply: 0.1, gap: -0.02 },
|
||
{ title: 'Gaslieferverträge mit Karvanien verlängert – Versorgung für zwei Jahre gesichert', supply: -0.1, gap: 0.02 },
|
||
];
|
||
export const FOREIGN_RATE = 3.5; // Zinsniveau im Ausland (langfristiger Mittelwert)
|
||
const CURVE_POINTS = [0.25, 1, 2, 3, 5, 7, 10, 20, 30];
|
||
|
||
// ---------------------------------------------------------------- Erdgas: Heizsaison und Gasspeicher
|
||
// Das Spieljahr hat Jahreszeiten. In Echtzeit (auch Hybrid) folgt es dem Kalender des Servers in der Spiel-Zeitzone.
|
||
// Im Turbo-Modus läuft es über die Handelstage (daysPerYear je Jahr); Spieltag 1 liegt dann Anfang April. Die Speicher
|
||
// sind nach dem Winter fast leer, werden über den Sommer gefüllt und im Winter wieder geleert.
|
||
const GAS_PHASE0 = 0.25;
|
||
export const MONTHS = ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'];
|
||
const TAU = 2 * Math.PI;
|
||
/** Stelle im Spieljahr (0 = 1. Januar … 1) für einen Handelstag */
|
||
export function yearPhase(day, daysPerYear) {
|
||
const y = Math.max(1, daysPerYear);
|
||
return ((((day - 1) % y) + y) % y / y + GAS_PHASE0) % 1;
|
||
}
|
||
/** Stelle im Kalenderjahr für einen Zeitpunkt (Datum in der Zeitzone tz); der Monat ergibt sich exakt als floor(phase × 12) */
|
||
export function calendarPhase(ts, tz) {
|
||
const p = Object.fromEntries(new Intl.DateTimeFormat('en-CA', { timeZone: tz, year: 'numeric', month: 'numeric', day: 'numeric' })
|
||
.formatToParts(new Date(ts)).filter((x) => x.type !== 'literal').map((x) => [x.type, Number(x.value)]));
|
||
const days = new Date(Date.UTC(p.year, p.month, 0)).getUTCDate();
|
||
return (p.month - 1 + (p.day - 0.5) / days) / 12;
|
||
}
|
||
/** Saisonaufschlag auf den Gaspreis (log.): Höchststand Mitte Januar (≈ +15 %), Tiefststand Mitte Juli (≈ −13 %) */
|
||
export function gasSeason(phase) { return 0.14 * Math.cos(TAU * (phase - 0.04)); }
|
||
/** üblicher Füllstand der Gasspeicher (0…1): voll Mitte Oktober (≈ 90 %), fast leer Mitte April (≈ 34 %) */
|
||
export function storageNorm(phase) { return 0.62 + 0.28 * Math.cos(TAU * (phase - 0.79)); }
|
||
/** Wie stark der Füllstand den Preis bewegt: vor und im Winter voll, im Hochsommer kaum */
|
||
export function winterWeight(phase) { return 0.5 + 0.5 * Math.cos(TAU * (phase - 0.97)); }
|
||
const STORE_K = 1.2; // log. Preisaufschlag je fehlendem Füllstand (1 = 100 %) bei vollem Wintergewicht
|
||
const GAP_SD = 0.05; // typische Abweichung des Füllstands vom Üblichen
|
||
const r3 = (x) => Math.round(x * 1000) / 1000;
|
||
|
||
/** Startzustand je Instrument (wird mit dem Spielstand gespeichert) */
|
||
export function initAsset(s, day) {
|
||
if (s.kind === 'bond') return { couponStart: day };
|
||
if (s.kind === 'crypto') return { drift: 0, hype: 0 };
|
||
return { drift: 0, supply: 0 };
|
||
}
|
||
|
||
/** Rendite (in %) für eine Laufzeit T in Jahren – Nelson-Siegel-artig zwischen Leitzins und langfristigem Niveau */
|
||
export function yieldAt(T, ctx) {
|
||
const k = 1.5;
|
||
const f = (1 - Math.exp(-T / k)) / (T / k);
|
||
const long = ctx.longNeutral;
|
||
return long + (ctx.rateEff - long) * f + 0.15 * Math.sqrt(T) + ctx.risk * Math.min(1, T / 5) + 0.12 * ctx.sentiment;
|
||
}
|
||
|
||
/** Sauberer Anleihekurs je 100 Nennwert bei vierteljährlichem Kupon */
|
||
export function bondPrice(coupon, T, y) {
|
||
const n = Math.round(T * 4), c = coupon / 4, r = y / 400;
|
||
let pv = 0;
|
||
for (let i = 1; i <= n; i++) pv += c / (1 + r) ** i;
|
||
return pv + 100 / (1 + r) ** n;
|
||
}
|
||
|
||
export class Assets {
|
||
constructor(g) { this.g = g; this.foreignRate = FOREIGN_RATE; }
|
||
get cfg() { return this.g.cfg; }
|
||
list() { return [...this.g.stocks.values()].filter((s) => s.kind !== 'stock'); }
|
||
get(sym) { return this.g.stocks.get(sym); }
|
||
|
||
/** Makrogrößen, die für alle Bewertungen gelten */
|
||
curveCtx(base) {
|
||
const m = this.g.macro;
|
||
const pub = m.published ?? m;
|
||
const infExp = 0.6 * pub.inflation + 0.4 * INFL_TARGET;
|
||
return {
|
||
rateEff: base.rateEff, risk: base.risk ?? 0, sentiment: m.sentiment,
|
||
longNeutral: 0.6 + infExp, inflation: pub.inflation,
|
||
};
|
||
}
|
||
|
||
/** Innerer Wert eines Nicht-Aktien-Instruments */
|
||
value(s, ctx) {
|
||
const c = this.curveCtx(ctx);
|
||
const a = s.asset ?? {};
|
||
const m = this.g.macro;
|
||
if (s.kind === 'bond') {
|
||
const y = yieldAt(s.maturity, c);
|
||
const accrued = (s.coupon / 4) * clamp((this.g.day - (a.couponStart ?? this.g.day)) / ctx.quarterDays, 0, 1);
|
||
return bondPrice(s.coupon, s.maturity, y) + accrued;
|
||
}
|
||
const realRate = c.rateEff - c.inflation;
|
||
let ln = Math.log(BASE[s.symbol]) + (a.drift ?? 0);
|
||
if (s.symbol === 'OEL') ln += 0.1 * (m.gdp - TREND_GDP) + 0.06 * m.sentiment + (a.supply ?? 0);
|
||
else if (s.symbol === 'GAS') {
|
||
// Heizsaison und knappe Speicher vor/im Winter; schwach an Konjunktur und (Ölpreisbindung alter Lieferverträge) an Öl gekoppelt
|
||
const o = this.get('OEL');
|
||
ln += this.gasTerm() + 0.05 * (m.gdp - TREND_GDP) + 0.2 * (o?.fv ? clamp(Math.log(o.fv / BASE.OEL), -1, 1) : 0) + (a.supply ?? 0);
|
||
}
|
||
else if (s.symbol === 'GOLD') ln += -0.08 * (realRate - 0.5) - 0.05 * m.sentiment + 0.1 * c.risk;
|
||
else if (s.symbol === 'USD') ln += 0.04 * (this.foreignRate - c.rateEff - (FOREIGN_RATE - 2.5)) + 0.05 * c.risk;
|
||
else if (s.kind === 'crypto') ln += (a.hype ?? 0) + 0.3 * m.sentiment - 0.06 * (c.rateEff - 2.5) + 0.15 * c.risk;
|
||
return Math.exp(ln);
|
||
}
|
||
|
||
/** Außenwirtschaftliche Größen für Makro und Unternehmensgewinne (log. Abweichung vom Startniveau, geglättet über den inneren Wert) */
|
||
ext() {
|
||
const o = this.get('OEL'), u = this.get('USD'), gs = this.get('GAS');
|
||
// Gas: nur die Abweichung vom saisonüblichen Preis wirkt (ein normaler Winter ist eingeplant); die Gaspreisbremse
|
||
// halbiert, was von teurem Gas bei Verbrauchern und in der Inflation ankommt
|
||
const gas = gs?.fv ? clamp(Math.log(gs.fv / BASE.GAS) - gasSeason(this.gasPhase()), -1, 1) : 0;
|
||
return {
|
||
oil: o?.fv ? clamp(Math.log(o.fv / BASE.OEL), -1, 1) : 0,
|
||
gas, gasCpi: gas > 0 && this.lawActive('gasbrake') ? gas * 0.5 : gas,
|
||
usd: u?.fv ? clamp(Math.log(u.fv / BASE.USD), -0.5, 0.5) : 0,
|
||
risk: this.g.gov?.riskPremium() ?? 0,
|
||
};
|
||
}
|
||
|
||
/** Langsame Zufallsbewegungen und abklingende Angebotsschocks (dt in Handelstagen) */
|
||
step(dtDays) {
|
||
for (const s of this.list()) this.stepOne(s, dtDays);
|
||
const k = Math.LN2 / 120;
|
||
this.foreignRate += k * (FOREIGN_RATE - this.foreignRate) * dtDays + 0.25 * Math.sqrt(dtDays / 21) * gauss() * 0.3;
|
||
this.foreignRate = clamp(this.foreignRate, 0, 8);
|
||
}
|
||
|
||
/** Zufallsbewegung eines einzelnen Instruments */
|
||
stepOne(s, dtDays) {
|
||
const ou = (x, hl, sd) => {
|
||
const k = Math.LN2 / hl;
|
||
return x * Math.exp(-k * dtDays) + sd * Math.sqrt(2 * k * dtDays) * gauss();
|
||
};
|
||
{
|
||
if (s.kind === 'bond') return;
|
||
const a = (s.asset ??= initAsset(s, this.g.day));
|
||
const sd = { OEL: 0.12, GAS: 0.16, GOLD: 0.08, USD: 0.04 }[s.symbol] ?? (s.kind === 'crypto' ? 0.35 : 0.05);
|
||
a.drift = ou(a.drift ?? 0, s.kind === 'crypto' ? 40 : 60, sd);
|
||
if (a.supply) a.supply *= Math.exp((-Math.LN2 * dtDays) / 30);
|
||
if (a.hype) a.hype *= Math.exp((-Math.LN2 * dtDays) / 6); // Hype verfliegt (Halbwertszeit ~6 Handelstage)
|
||
}
|
||
}
|
||
|
||
/** Vorbörse: Kupons, Förderentscheidungen */
|
||
newDay() {
|
||
this.g.causes?.set('Rohstoffe & Devisen: Marktlage');
|
||
const g = this.g, cfg = this.cfg;
|
||
for (const s of this.list()) {
|
||
if (s.kind !== 'bond') continue;
|
||
s.asset ??= initAsset(s, g.day);
|
||
if (g.day - s.asset.couponStart >= cfg.quarterDays) {
|
||
s.asset.couponStart = g.day;
|
||
g.ex.exDividend(s, Math.round((s.coupon / 4) * 100) / 100, 'Kupon');
|
||
}
|
||
}
|
||
// Erdgas: Heizsaison und Speicher rücken einen Tag weiter; zu jedem Konjunkturtermin ein Speicherbericht
|
||
g.causes?.set('Erdgas: Heizsaison & Gasspeicher');
|
||
g.econ.revalue(() => this.stepGas());
|
||
if (g.day % cfg.macroEveryDays === 0) this.gasReport();
|
||
// Gasmarkt: etwa drei größere Ereignisse je Spieljahr
|
||
if (Math.random() < 3 / cfg.daysPerYear) this.gasEvent();
|
||
g.causes?.set('Rohstoffe & Devisen: Marktlage');
|
||
// Ölkartell: im Mittel etwa zwei Entscheidungen je Spieljahr
|
||
if (Math.random() < 2 / cfg.daysPerYear) this.oilShock(rnd(0.08, 0.22) * (Math.random() < 0.55 ? 1 : -1));
|
||
// Krypto: etwa sechs große Ereignisse je Spieljahr
|
||
if (Math.random() < 6 / cfg.daysPerYear) this.cryptoEvent();
|
||
}
|
||
|
||
/** Krypto-Ereignis (fiktiv): Hype-Schub nach oben oder unten */
|
||
cryptoEvent(ev = null) {
|
||
const g = this.g, s = this.get('WIBC');
|
||
if (!s) return null;
|
||
ev ??= pick(CRYPTO_EVENTS);
|
||
g.causes?.set(`Krypto: ${ev.title}`);
|
||
this.hype(s, ev.size * rnd(0.7, 1.3));
|
||
g.addNews({ id: `crypto-${Date.now()}-${Math.floor(Math.random() * 1e4)}`, title: ev.title, source: 'Kryptomarkt', real: false, sentiment: 0, sectors: ev.sectors ?? [], symbol: 'WIBC', affected: ['WIBC'], ts: Date.now() }, false);
|
||
g.log('macro', ev.title);
|
||
return ev;
|
||
}
|
||
/** Hype verschieben (log. Größe) – Nachrichten, Ereignisse */
|
||
hype(s, size) {
|
||
this.g.econ.revalue(() => { s.asset ??= initAsset(s, this.g.day); s.asset.hype = clamp((s.asset.hype ?? 0) + size, -1.5, 1.5); });
|
||
this.g.econ.bumpVol(s, 1 + Math.min(3, Math.abs(size) * 8));
|
||
}
|
||
|
||
/** Angebotsschock beim Öl (log. Größe, + = knapper/teurer) */
|
||
oilShock(size, reason) {
|
||
const g = this.g, s = this.get('OEL');
|
||
if (!s) return;
|
||
g.econ.revalue(() => { s.asset.supply = (s.asset.supply ?? 0) + size; });
|
||
g.econ.bumpVol(s, 2.5);
|
||
const txt = reason ?? (size > 0
|
||
? `Petrolia-Kartell kürzt die Fördermenge – Ölpreis springt (Angebot −${Math.round(size * 20)} %)`
|
||
: `Petrolia-Kartell weitet die Förderung aus – Ölpreis gibt nach`);
|
||
g.addNews({ id: `oil-${Date.now()}`, title: txt, source: 'Rohstoffmärkte', real: false, sentiment: 0, sectors: ['energy'], symbol: 'OEL', affected: ['OEL'], ts: Date.now() }, false);
|
||
g.log('macro', txt);
|
||
g.econ.cascade('energy', size > 0 ? -size * 0.25 : -size * 0.15, 'Energiepreise');
|
||
}
|
||
|
||
// ---------------------------------------------------------------- Erdgas
|
||
/** Gilt ein Gesetz (nach Inkrafttreten)? */
|
||
lawActive(key) {
|
||
const r = this.g.pol?.inForce?.[key];
|
||
return !!r && this.g.day >= (r.day ?? 0);
|
||
}
|
||
/** Zustand des Gasmarkts (im Makrozustand gespeichert; ältere Spielstände bekommen ihn beim ersten Zugriff) */
|
||
gasState() { return (this.g.macro.gas ??= { gap: 0, day: this.g.day, history: [] }); }
|
||
/** Stelle im Spieljahr, für die der Gaspreis zuletzt bewertet wurde */
|
||
gasPhase() {
|
||
const clock = this.g.clock;
|
||
return clock && !clock.turbo ? calendarPhase(clock.now(), this.cfg.timezone || 'Europe/Berlin') : yearPhase(this.gasState().day, this.cfg.daysPerYear);
|
||
}
|
||
/** Füllstand der Gasspeicher (0…1) */
|
||
storage() { return clamp(storageNorm(this.gasPhase()) + this.gasState().gap, 0.02, 1); }
|
||
/** Saison- und Speicheranteil des Gaspreises (log.) */
|
||
gasTerm() {
|
||
const ph = this.gasPhase();
|
||
return gasSeason(ph) - STORE_K * (this.storage() - storageNorm(ph)) * winterWeight(ph);
|
||
}
|
||
|
||
/** Ein Handelstag weiter: Jahreszeit fortschreiben; die Abweichung vom üblichen Füllstand schwankt und baut sich ab */
|
||
stepGas() {
|
||
const g = this.g, st = this.gasState(), Y = this.cfg.daysPerYear;
|
||
const dt = Math.min(Y, Math.max(0, g.day - st.day));
|
||
if (!dt) return;
|
||
// Speicherpflicht: Betreiber lagern vor dem Winter etwas mehr ein, Lücken werden schneller geschlossen
|
||
const duty = this.lawActive('gasstore');
|
||
const target = duty ? 0.04 : 0;
|
||
const k = Math.LN2 / (Y * (duty ? 0.12 : 0.25));
|
||
const e = Math.exp(-k * dt);
|
||
st.gap = clamp(target + (st.gap - target) * e + GAP_SD * Math.sqrt(1 - e * e) * gauss(), -0.3, 0.2);
|
||
st.day = g.day;
|
||
st.history.push({ day: g.day, fill: r3(this.storage()), norm: r3(storageNorm(this.gasPhase())) });
|
||
if (st.history.length > 120) st.history.splice(0, st.history.length - 120);
|
||
}
|
||
|
||
/** Lage am Gasmarkt für Anzeige und Hilfe */
|
||
gasInfo(hist = 60) {
|
||
const st = this.gasState(), ph = this.gasPhase(), norm = storageNorm(ph), fill = this.storage(), w = winterWeight(ph);
|
||
return {
|
||
storage: fill, norm, gap: fill - norm, month: MONTHS[Math.floor(ph * 12) % 12], heating: ph >= 0.75 || ph < 0.25,
|
||
filling: storageNorm((ph + 0.01) % 1) > norm, winterWeight: w,
|
||
season: Math.exp(gasSeason(ph)) - 1, storagePremium: Math.exp(-STORE_K * (fill - norm) * w) - 1,
|
||
duty: this.lawActive('gasstore'), brake: this.lawActive('gasbrake'), history: hist ? st.history.slice(-hist) : [],
|
||
};
|
||
}
|
||
|
||
/** Speicherbericht der (fiktiven) Netzagentur – reine Information, der Füllstand ist bereits eingepreist */
|
||
gasReport() {
|
||
const i = this.gasInfo(), p = (x) => Math.round(x * 100);
|
||
const txt = `Gasspeicher-Bericht: Füllstand ${p(i.storage)} % (üblich im ${i.month}: ${p(i.norm)} %) – die Speicher werden ${i.filling ? 'gefüllt' : 'geleert'}`;
|
||
this.g.addNews({ id: `gasrep-${Date.now()}`, title: txt, source: 'Netzagentur Wiborien', real: false, sentiment: 0, sectors: [], symbol: 'GAS', affected: ['GAS'], ts: Date.now() }, false);
|
||
}
|
||
|
||
/** Ereignis am Gasmarkt (fiktiv): Angebotsschock und veränderter Speicherfüllstand */
|
||
gasEvent(ev = null) {
|
||
if (!this.get('GAS')) return null;
|
||
const heating = this.gasInfo().heating;
|
||
ev ??= pick(GAS_EVENTS.filter((e) => !e.winter || heating));
|
||
this.g.causes?.set(`Erdgas: ${ev.title}`);
|
||
this.gasShock(ev.supply * rnd(0.7, 1.3), ev.title, ev.gap ?? 0);
|
||
return ev;
|
||
}
|
||
|
||
/** Angebotsschock beim Gas (log. Größe, + = knapper/teurer); gap verschiebt den Speicherfüllstand; news = false: ohne eigene Meldung */
|
||
gasShock(size, reason, gap = 0, news = true) {
|
||
const g = this.g, s = this.get('GAS');
|
||
if (!s) return;
|
||
g.econ.revalue(() => {
|
||
s.asset.supply = (s.asset.supply ?? 0) + size;
|
||
// mit Speicherpflicht fallen Lücken kleiner aus
|
||
if (gap) { const st = this.gasState(); st.gap = clamp(st.gap + gap * (gap < 0 && this.lawActive('gasstore') ? 0.6 : 1), -0.3, 0.2); }
|
||
});
|
||
g.econ.bumpVol(s, 2);
|
||
const txt = reason ?? (size > 0 ? 'Gasmarkt: Lieferengpässe – Gaspreis steigt kräftig' : 'Gasmarkt: reichlich Angebot – Gaspreis gibt nach');
|
||
if (!news) return;
|
||
g.addNews({ id: `gas-${Date.now()}-${Math.floor(Math.random() * 1e4)}`, title: txt, source: 'Rohstoffmärkte', real: false, sentiment: 0, sectors: ['energy'], symbol: 'GAS', affected: ['GAS'], ts: Date.now() }, false);
|
||
g.log('macro', txt);
|
||
}
|
||
|
||
/** Zinskurve für die Anzeige */
|
||
curve() {
|
||
const c = this.curveCtx(this.g.econ.ctx());
|
||
const points = CURVE_POINTS.map((T) => ({ T, y: T === 0.25 ? this.g.macro.rate : yieldAt(T, c) }));
|
||
const y2 = yieldAt(2, c), y10 = yieldAt(10, c);
|
||
return {
|
||
points, y2, y10, spread: y10 - y2, inverted: y10 < y2, foreignRate: this.foreignRate,
|
||
riskPremium: c.risk, longNeutral: c.longNeutral,
|
||
};
|
||
}
|
||
|
||
/** Kennzahlen je Instrument für Ticker/Detailansicht */
|
||
info(s) {
|
||
if (s.kind === 'bond') {
|
||
const c = this.curveCtx(this.g.econ.ctx());
|
||
const accrued = (s.coupon / 4) * clamp((this.g.day - (s.asset?.couponStart ?? this.g.day)) / this.cfg.quarterDays, 0, 1);
|
||
// Rendite aus dem Marktkurs (Newton), nicht aus dem inneren Wert
|
||
const clean = s.last - accrued;
|
||
let y = yieldAt(s.maturity, c);
|
||
for (let i = 0; i < 20; i++) {
|
||
const p = bondPrice(s.coupon, s.maturity, y), d = (bondPrice(s.coupon, s.maturity, y + 0.01) - p) / 0.01;
|
||
if (!d) break;
|
||
y -= (p - clean) / d;
|
||
}
|
||
const dur = (bondPrice(s.coupon, s.maturity, y - 0.01) - bondPrice(s.coupon, s.maturity, y + 0.01)) / (0.02 * clean) * 100;
|
||
return {
|
||
kind: 'bond', maturity: s.maturity, coupon: s.coupon, yield: y, accrued, cleanPrice: clean, duration: dur,
|
||
nextCouponIn: Math.max(0, this.cfg.quarterDays - (this.g.day - (s.asset?.couponStart ?? this.g.day))),
|
||
};
|
||
}
|
||
return { kind: s.kind, base: BASE[s.symbol], changeFromStart: (s.last / BASE[s.symbol] - 1) * 100 };
|
||
}
|
||
|
||
/** Startkurse der Anleihen aus der Zinskurve und des Gases aus Saison und Speicher – nur beim allerersten Start
|
||
* bzw. wenn ein älterer Spielstand den Wert noch nicht kennt */
|
||
seed() {
|
||
for (const s of this.list()) {
|
||
if ((s.kind !== 'bond' && s.symbol !== 'GAS') || s.seeded) continue;
|
||
const v = Math.round(this.value(s, this.g.econ.ctx()) * 100) / 100;
|
||
Object.assign(s, { fair: v, last: v, prevClose: v, close: v, open: v, high: v, low: v, refAuction: v, seeded: true });
|
||
}
|
||
}
|
||
|
||
describeMove(s) { return `${s.name}: ${fmt(s.last)} €`; }
|
||
}
|