Portierung der beiden fehlenden Grundbausteine aus PolytraderSharp (voller Ausbau). Core S-0 (Datenfundament fuer Analyse/Forensik): - core_decision_journal + core_order_events (+ ReasonCode/Decision/OrderEvent-Enums), IDecisionJournal/IOrderEventLog mit fehlertoleranten EF-Impls (Handel bricht nie). - SignalId-Durchreichung TradeSignal -> ExecutionService -> core_trade_history; ExecutionService schreibt an jeder Verzweigung Journal/Order-Events. - JSONL-Log-Sink (LogJson + Dual-Sink), pure Analytik: RealizedPnlEngine (FIFO), TradeAnalytics, DossierBuilder. Migration AddAnalysisFoundation. Accounting-Modul (acc_): unabhaengiger IBKR-Kontoauszug (Activity Flex Query) hinter Interfaces mit Offline-Null-Stubs -> append-only Ledger + Periodenabrechnung/BWA + FX (USD/EUR) + CSV/PDF (PDFsharp/MigraDoc). Steuerschicht bewusst offen (Platzhalter-Tab). Kein Handel. Migration InitialAccounting. Supervisor-Modul (sup_): read-only OpenRouter-Agent (Function-Calling-Loop) + read-only Tool-Registry (8 Tools) + Profile + Dossier-Browser + Counterfactual-Job (Stub) + Tagesbericht/MCP-Light (opt-in). Migration InitialSupervisor. Verdrahtung: Program.cs (beide Module + Icons), slnx/App/Tests-Referenzen, provision-db.ps1, AppSettings-Sektionen, docs/konzepte, README. Tests: 79 -> 117 gruen (FIFO/KPIs/Dossier/JSONL, Classifier/Engine/FX/Idempotenz, OpenRouter/Registry/Agent/MCP, STA-Konstruktion beider neuen Fenster). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
86 lines
2.5 KiB
C#
86 lines
2.5 KiB
C#
using FluentAssertions;
|
|
using IBKRTrader.Core.Analytics;
|
|
using IBKRTrader.Core.Persistence.Entities;
|
|
|
|
namespace IBKRTrader.Tests.Analytics;
|
|
|
|
[Trait("cat", "unit")]
|
|
public class RealizedPnlEngineTests
|
|
{
|
|
private static CoreTrade Fill(string action, decimal qty, decimal price, int minute, string symbol = "AAPL") =>
|
|
new()
|
|
{
|
|
Module = "CT", Symbol = symbol, Action = action,
|
|
Quantity = qty, Price = price, TotalValue = qty * price,
|
|
TradedAt = new DateTime(2026, 1, 1, 0, minute, 0, DateTimeKind.Utc)
|
|
};
|
|
|
|
[Fact]
|
|
public void BuyThenSellAll_RealizesFullPnl()
|
|
{
|
|
var fills = new[] { Fill("BUY", 10, 100m, 0), Fill("SELL", 10, 130m, 1) };
|
|
|
|
var realized = RealizedPnlEngine.Match(fills);
|
|
|
|
realized.Should().HaveCount(1);
|
|
realized[0].RealizedPnl.Should().Be(300m); // (130-100)*10
|
|
}
|
|
|
|
[Fact]
|
|
public void Sell_MatchesOldestLotsFirst_Fifo()
|
|
{
|
|
var fills = new[]
|
|
{
|
|
Fill("BUY", 10, 100m, 0),
|
|
Fill("BUY", 10, 120m, 1),
|
|
Fill("SELL", 15, 130m, 2) // 10 gegen 100er-Lot, 5 gegen 120er-Lot
|
|
};
|
|
|
|
var realized = RealizedPnlEngine.Match(fills);
|
|
|
|
realized.Should().HaveCount(2);
|
|
realized[0].RealizedPnl.Should().Be((130m - 100m) * 10m); // 300
|
|
realized[1].RealizedPnl.Should().Be((130m - 120m) * 5m); // 50
|
|
RealizedPnlEngine.TotalRealized(fills).Should().Be(350m);
|
|
}
|
|
|
|
[Fact]
|
|
public void PartialSell_LeavesRemainderOpen()
|
|
{
|
|
var fills = new[] { Fill("BUY", 10, 100m, 0), Fill("SELL", 4, 130m, 1) };
|
|
|
|
var realized = RealizedPnlEngine.Match(fills);
|
|
|
|
realized.Should().HaveCount(1);
|
|
realized[0].Quantity.Should().Be(4m);
|
|
realized[0].RealizedPnl.Should().Be(120m);
|
|
}
|
|
|
|
[Fact]
|
|
public void SellExceedingHoldings_IgnoresSurplus_NoShort()
|
|
{
|
|
var fills = new[] { Fill("BUY", 5, 100m, 0), Fill("SELL", 8, 130m, 1) };
|
|
|
|
var realized = RealizedPnlEngine.Match(fills);
|
|
|
|
realized.Should().HaveCount(1);
|
|
realized[0].Quantity.Should().Be(5m); // nur die gehaltenen 5 realisiert
|
|
}
|
|
|
|
[Fact]
|
|
public void SeparatesBySymbol()
|
|
{
|
|
var fills = new[]
|
|
{
|
|
Fill("BUY", 10, 100m, 0, "AAPL"),
|
|
Fill("BUY", 10, 50m, 1, "MSFT"),
|
|
Fill("SELL", 10, 130m, 2, "AAPL")
|
|
};
|
|
|
|
var realized = RealizedPnlEngine.Match(fills);
|
|
|
|
realized.Should().HaveCount(1);
|
|
realized[0].Symbol.Should().Be("AAPL");
|
|
}
|
|
}
|