Files
IBKRTrader/tests/IBKRTrader.Tests/Trading/RiskServiceOptionTests.cs
T
RichardandClaude Opus 5 084cd7ef77
Build & Test / build (ubuntu-latest) (push) Canceled after 0s
Build & Test / build (windows-latest) (push) Canceled after 0s
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>
2026-09-04 09:35:38 +02:00

213 lines
7.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using FluentAssertions;
using IBKRTrader.Core.Capital;
using IBKRTrader.Core.Trading;
namespace IBKRTrader.Tests.Trading;
/// <summary>
/// Sell-to-open und Deckungsprüfung im Core (#300).
///
/// <para>Bis hierher lehnte <c>EvaluateSell</c> jeden Verkauf ohne Bestand grundsätzlich ab – und
/// genau das ist die Kernoperation des OptionsWheels. Die Prüfung sitzt bewusst im Core: ein
/// Modul, das sich selbst prüft, prüft sich irgendwann nicht mehr.</para>
/// </summary>
[Trait("cat", "unit")]
public class RiskServiceOptionTests
{
private readonly RiskService _risk = new();
private static readonly DateOnly Expiry = new(2026, 9, 18);
private static readonly RiskParameters Params = new(5m, 20m, 5m);
private static OptionSpec Put(decimal strike = 150m) => new("AAPL", Expiry, strike, OptionRight.Put);
private static OptionSpec Call(decimal strike = 150m) => new("AAPL", Expiry, strike, OptionRight.Call);
private static TradeSignal Sell(OptionSpec spec, int? qty = 1) => new()
{
Symbol = "AAPL", Side = TradeSide.Sell, SourceModule = "OW",
Option = spec, SuggestedQuantity = qty
};
private static TradeSignal Buy(OptionSpec spec, int? qty = null) => new()
{
Symbol = "AAPL", Side = TradeSide.Buy, SourceModule = "OW",
Option = spec, SuggestedQuantity = qty
};
/// <summary>Reichlich Konto, reichlich Spielraum – damit nur die geprüfte Regel greift.</summary>
private static RiskContext Ctx(
decimal price = 2.50m, int held = 0, int shares = 0, int boundShares = 0,
decimal cash = 1_000_000m, decimal bookHeadroom = 1_000_000m,
decimal symbolExposure = 0m, decimal symbolLimit = 0m, bool halted = false) => new()
{
Price = price,
NetLiquidation = 1_000_000m,
ExistingQuantity = held,
Coverage = new CoverageCalculator.Coverage(shares, boundShares, 0m),
AvailableCashForReservation = cash,
BookHeadroom = bookHeadroom,
SymbolExposure = symbolExposure,
SymbolLimit = symbolLimit,
BuyingHalted = halted
};
// ─── Cash-Secured Put ────────────────────────────────────────────────────────
[Fact]
public void SellingAPutWithoutHolding_IsApproved_WhenCashCoversTheObligation()
{
var d = _risk.Evaluate(Sell(Put()), Ctx(cash: 15_000m), Params);
d.Approved.Should().BeTrue();
d.Quantity.Should().Be(1);
d.ClosingQuantity.Should().Be(0); // eroeffnet, schliesst nichts
}
[Fact]
public void SellingAPut_IsRefused_WhenCashIsShort()
{
var d = _risk.Evaluate(Sell(Put()), Ctx(cash: 14_999m), Params);
d.Approved.Should().BeFalse();
d.Breach!.Kind.Should().Be(LimitKind.Coverage);
d.Reason.Should().Contain("Deckung");
}
[Fact]
public void SellingThreePuts_WithCashForTwo_IsRefused_AndSaysTwoWouldWork()
{
var d = _risk.Evaluate(Sell(Put(), qty: 3), Ctx(cash: 32_000m), Params);
d.Approved.Should().BeFalse();
d.Breach!.MaxQuantity.Should().Be(2);
}
// ─── Covered Call ────────────────────────────────────────────────────────────
[Fact]
public void SellingACall_IsApproved_WithOneHundredFreeShares()
{
var d = _risk.Evaluate(Sell(Call()), Ctx(shares: 100), Params);
d.Approved.Should().BeTrue();
d.Quantity.Should().Be(1);
}
[Fact]
public void SellingASecondCallAgainstTheSameShares_IsRefused()
{
var d = _risk.Evaluate(Sell(Call()), Ctx(shares: 100, boundShares: 100), Params);
d.Approved.Should().BeFalse();
d.Breach!.Kind.Should().Be(LimitKind.Coverage);
}
[Fact]
public void CashDoesNotCoverACall_OnlySharesDo()
{
// Ein Call wird mit Stuecken gedeckt, nicht mit Geld. Eine Million Bargeld traegt keinen
// einzigen geschriebenen Call.
var d = _risk.Evaluate(Sell(Call()), Ctx(shares: 0, cash: 1_000_000m), Params);
d.Approved.Should().BeFalse();
}
// ─── Schließen ist nie limitiert ─────────────────────────────────────────────
[Fact]
public void BuyingToCloseAWrittenContract_IsAllowed_EvenWhenBuyingIsHalted()
{
// Ein Rueckkauf baut eine Verpflichtung ab. Der Kaufstopp soll Neuengagements verhindern,
// nicht deren Aufloesung - sonst sperrt die Sicherung den Ausgang.
var d = _risk.Evaluate(Buy(Put()), Ctx(held: -2, halted: true, cash: 0m, bookHeadroom: 0m), Params);
d.Approved.Should().BeTrue();
d.Quantity.Should().Be(2);
d.ClosingQuantity.Should().Be(2);
}
[Fact]
public void BuyingToClose_IsCappedAtTheWrittenQuantity()
{
var d = _risk.Evaluate(Buy(Put(), qty: 5), Ctx(held: -2), Params);
d.Quantity.Should().Be(2);
}
[Fact]
public void SellingToCloseABoughtContract_IsAllowed_WithoutAnyCoverage()
{
var d = _risk.Evaluate(Sell(Put(), qty: 1), Ctx(held: 3, cash: 0m), Params);
d.Approved.Should().BeTrue();
d.Quantity.Should().Be(1);
d.ClosingQuantity.Should().Be(1);
}
// ─── Grenzen des eröffnenden Teils ───────────────────────────────────────────
[Fact]
public void OpeningIsRefused_WhenTheBookHasNoHeadroomLeft()
{
var d = _risk.Evaluate(Sell(Put()), Ctx(cash: 1_000_000m, bookHeadroom: 10_000m), Params);
d.Approved.Should().BeFalse();
d.Breach!.Kind.Should().Be(LimitKind.BookCap);
}
[Fact]
public void OpeningIsRefused_WhenItWouldBreachTheSymbolConcentration()
{
var d = _risk.Evaluate(Sell(Put()),
Ctx(cash: 1_000_000m, symbolExposure: 40_000m, symbolLimit: 50_000m), Params);
d.Approved.Should().BeFalse();
d.Breach!.Kind.Should().Be(LimitKind.SymbolConcentration);
}
[Fact]
public void BuyingAnOption_SizesByContractCost_NotByShareCost()
{
// Kurs 2,50 je Kontrakt = 250 je Stueck. Bei 5 % von 1.000.000 = 50.000 Budget waeren das
// 200 Kontrakte - ohne den Multiplikator kaeme das Hundertfache heraus.
var d = _risk.Evaluate(Buy(Put()), Ctx(price: 2.50m), Params);
d.Approved.Should().BeTrue();
d.Quantity.Should().Be(200);
}
[Fact]
public void BuyingIsRefused_WhileTheWatchdogHoldsTheLine()
{
var d = _risk.Evaluate(Buy(Put()), Ctx(halted: true), Params);
d.Approved.Should().BeFalse();
d.Breach!.Kind.Should().Be(LimitKind.BuyingHalted);
}
// ─── Der Aktienpfad bleibt, wie er war ───────────────────────────────────────
[Fact]
public void SellingStockWithoutHolding_IsStillRefused()
{
var signal = new TradeSignal { Symbol = "AAPL", Side = TradeSide.Sell, SourceModule = "CT" };
var d = _risk.Evaluate(signal, Ctx(price: 100m, cash: 1_000_000m), Params);
d.Approved.Should().BeFalse();
d.Reason.Should().Contain("Keine Position");
}
[Fact]
public void SellingStockYouHold_StillClosesTheWholePosition()
{
var signal = new TradeSignal { Symbol = "AAPL", Side = TradeSide.Sell, SourceModule = "CT" };
var d = _risk.Evaluate(signal, Ctx(price: 100m, held: 7), Params);
d.Approved.Should().BeTrue();
d.Quantity.Should().Be(7);
d.ClosingQuantity.Should().Be(7);
}
}