Options-Fundament und Kapitalmodell im Core; Modul OptionsWheel
Das OptionsWheel setzt auf Dingen auf, die es im Core nicht gab. Diese Sitzung baut sie. #298 Optionen als Instrument. InstrumentKind/OptionSpec und ein druckbarer Instrumentenschluessel ("AAPL|20260918|C|150"); TradeSignal und OrderRequest tragen ein optionales OptionSpec, ohne das sich der Aktienpfad unveraendert verhaelt. Position.Notional rechnet ab jetzt mit der Kontraktgroesse - vorher war jede Option um Faktor 100 zu klein bewertet und damit jedes Risikolimit wirkungslos, lautlos und in die gefaehrliche Richtung. Daneben neu BoundCapital: ein Short Put bindet Strike x Kontraktgroesse, nicht die vereinnahmte Praemie. #300 sell-to-open. Jeder Auftrag zerfaellt in einen schliessenden und einen eroeffnenden Teil; der schliessende wird von keinem Limit verhindert - auch nicht vom Kaufstopp, denn ein Rueckkauf baut eine Verpflichtung ab. Deckungspruefung im Core, nicht im Modul: Short Call nur mit freien Aktien, Short Put nur mit reservierbarem Bargeld. #302-#305 Kapital- und Buchmodell. Buecher mit reservierten Namen und Eigentumsregeln, Pool mit Caps und Headroom, Margin-Sperre in vier Schichten, core_reservation mit Lebenszyklus, Frist, Idempotenz ueber (SignalId, ContractKey) und Quarantaene beim Start. #309/#310 Modul OptionsWheel: ow_-Schema mit fuenf Tabellen, Zustandsautomat, StrikeSelector, RollDecider, PremiumMath - vollstaendig ohne Broker testbar. Der Handelsbetrieb bleibt gesperrt, solange #299 und #294 fehlen; das Modul sagt das ueber GetActivationBlocker, statt still nichts zu tun. #293 --verify-order am Daemon: eine Order durch die vollstaendige Kette (Buch, Risiko, Bindung, Order, Fill, Buchung, Abschluss). Ohne --confirm ein Trockenlauf; Abbruch ausserhalb von Paper-Modus UND Paper-Port. Drei Widersprueche in den Konzepten sind dabei entschieden worden: Schicht B heisst ab jetzt "kein UNGEDECKTER Short" (der Grund der Regel war der Kredit, nicht der Short), die Hausreserve wird einmal abgezogen statt zweimal, und der Positionsschluessel ist eine Spalte statt eines Sechser-Verbunds (MySQL verlangt NOT NULL fuer Schluesselspalten). Begruendungen stehen in docs/ROADMAP.md. Enthaelt ausserdem die noch nicht committete Arbeit aus der vorigen Sitzung: die Fehlerbehebungen aus Epic X (WorkerBase-Intervall, BackupWorker, --check gegen die Datenbank, CapitolTrades-429, SIGTERM) samt DatabaseEndpoint, MysqlDumpTool und der Roadmap-Umstellung. Sie lag im Arbeitsbaum und liess sich nicht abtrennen, weil beide Staende dieselben Dateien beruehren. 213 -> 369 Tests, Build ohne Warnung. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -267,34 +267,45 @@ internal sealed class IbkrConnection : DefaultEWrapper, IDisposable
|
||||
/// Löst ein Symbol in einen vollständigen Kontrakt auf (inklusive ConId, damit Kurse und
|
||||
/// Orders dasselbe Instrument treffen). Ergebnisse werden für die Laufzeit zwischengespeichert.
|
||||
/// </summary>
|
||||
public async Task<Contract?> ResolveContractAsync(string symbol, TimeSpan timeout, CancellationToken ct)
|
||||
/// <summary>
|
||||
/// Löst ein Instrument gegen TWS in einen vollständigen Kontrakt auf – Aktie wie Option.
|
||||
///
|
||||
/// <para>Der Zwischenspeicher liegt auf dem Instrumentenschlüssel, nicht auf dem Ticker: sonst
|
||||
/// überschrieben sich alle Optionen desselben Basiswerts gegenseitig und ein Auftrag auf den
|
||||
/// 150er-Call träfe den zuletzt aufgelösten Kontrakt.</para>
|
||||
/// </summary>
|
||||
public async Task<Contract?> ResolveContractAsync(InstrumentRef instrument, TimeSpan timeout, CancellationToken ct)
|
||||
{
|
||||
if (_contractCache.TryGetValue(symbol, out var cached)) return cached;
|
||||
var key = instrument.Key;
|
||||
if (_contractCache.TryGetValue(key, out var cached)) return cached;
|
||||
|
||||
var id = NextRequestId();
|
||||
var slot = new ContractSlot();
|
||||
_contracts[id] = slot;
|
||||
try
|
||||
{
|
||||
_socket.reqContractDetails(id, IbkrMapping.Stock(symbol));
|
||||
var query = instrument.Option is { } option
|
||||
? IbkrMapping.Option(option)
|
||||
: IbkrMapping.Stock(instrument.Symbol);
|
||||
_socket.reqContractDetails(id, query);
|
||||
|
||||
if (!await WaitAsync(slot.Done.Task, timeout, ct).ConfigureAwait(false))
|
||||
{
|
||||
_logger.Warn(LogModule, $"Zeitüberschreitung bei der Kontraktsuche für {symbol}.");
|
||||
_logger.Warn(LogModule, $"Zeitüberschreitung bei der Kontraktsuche für {key}.");
|
||||
return null;
|
||||
}
|
||||
if (slot.Error is not null)
|
||||
{
|
||||
_logger.Warn(LogModule, $"Kontrakt {symbol} nicht auflösbar: {slot.Error}");
|
||||
_logger.Warn(LogModule, $"Kontrakt {key} nicht auflösbar: {slot.Error}");
|
||||
return null;
|
||||
}
|
||||
if (slot.First is null)
|
||||
{
|
||||
_logger.Warn(LogModule, $"Kein Kontrakt für {symbol} gefunden.");
|
||||
_logger.Warn(LogModule, $"Kein Kontrakt für {key} gefunden.");
|
||||
return null;
|
||||
}
|
||||
|
||||
_contractCache[symbol] = slot.First;
|
||||
_contractCache[key] = slot.First;
|
||||
return slot.First;
|
||||
}
|
||||
finally
|
||||
@@ -335,7 +346,11 @@ internal sealed class IbkrConnection : DefaultEWrapper, IDisposable
|
||||
_accounts[id] = slot;
|
||||
try
|
||||
{
|
||||
_socket.reqAccountSummary(id, "All", "NetLiquidation,AvailableFunds");
|
||||
// $LEDGER liefert zusaetzlich die Bargeldbestaende je Waehrung. Sie werden fuer die
|
||||
// Waehrungsdeckung gebraucht (Kapitalmodell §4 Schicht C): ein Kauf in X darf nur aus
|
||||
// Cash in X finanziert werden, sonst entsteht ein Sollsaldo in X - ein Margin-Kredit,
|
||||
// auch wenn das Konto in Summe positiv aussieht.
|
||||
_socket.reqAccountSummary(id, "All", "NetLiquidation,AvailableFunds,TotalCashValue,$LEDGER");
|
||||
|
||||
if (!await WaitAsync(slot.Done.Task, timeout, ct).ConfigureAwait(false))
|
||||
{
|
||||
@@ -350,7 +365,9 @@ internal sealed class IbkrConnection : DefaultEWrapper, IDisposable
|
||||
|
||||
return new AccountState(
|
||||
ReadDecimal(slot.Values, "NetLiquidation"),
|
||||
ReadDecimal(slot.Values, "AvailableFunds"));
|
||||
ReadDecimal(slot.Values, "AvailableFunds"),
|
||||
ReadDecimal(slot.Values, "TotalCashValue"),
|
||||
slot.Cash.Count == 0 ? null : slot.Cash);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -523,7 +540,20 @@ internal sealed class IbkrConnection : DefaultEWrapper, IDisposable
|
||||
|
||||
public override void accountSummary(int reqId, string account, string tag, string value, string currency)
|
||||
{
|
||||
if (_accounts.TryGetValue(reqId, out var slot)) slot.Values[tag] = value;
|
||||
if (!_accounts.TryGetValue(reqId, out var slot)) return;
|
||||
|
||||
slot.Values[tag] = value;
|
||||
|
||||
// Aus dem Ledger interessiert genau ein Wert je Waehrung: der Bargeldbestand. "BASE" ist
|
||||
// die Zusammenfassung in Kontobasiswaehrung und keine eigene Waehrung - wuerde sie mit
|
||||
// aufgenommen, zaehlte dasselbe Geld zweimal.
|
||||
if (tag.Equals("TotalCashBalance", StringComparison.OrdinalIgnoreCase) &&
|
||||
!string.IsNullOrWhiteSpace(currency) &&
|
||||
!currency.Equals("BASE", StringComparison.OrdinalIgnoreCase) &&
|
||||
decimal.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var cash))
|
||||
{
|
||||
slot.Cash[currency.ToUpperInvariant()] = cash;
|
||||
}
|
||||
}
|
||||
|
||||
public override void accountSummaryEnd(int reqId)
|
||||
@@ -722,7 +752,9 @@ internal sealed class IbkrConnection : DefaultEWrapper, IDisposable
|
||||
|
||||
private sealed class AccountSlot : Slot
|
||||
{
|
||||
public readonly Dictionary<string, string> Values = new(StringComparer.OrdinalIgnoreCase);
|
||||
public readonly Dictionary<string, string> Values = new(StringComparer.OrdinalIgnoreCase);
|
||||
/// <summary>Bargeld je Waehrung aus dem $LEDGER-Abschnitt.</summary>
|
||||
public readonly Dictionary<string, decimal> Cash = new(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private sealed class ContractSlot : Slot
|
||||
|
||||
Reference in New Issue
Block a user